/** * Gets specified orchestration rule * * @param int $ruleId Numeric identifier of the rule * @param bool $modify optional Modifying flag * * @return FarmRoleScript Returns the Script Entity on success * * @throws ApiErrorException */ public function getRule($ruleId, $modify = false) { $rule = FarmRoleScript::findPk($ruleId); if (!$rule) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested Rule either does not exist or is not owned by your environment."); } if (!$this->hasPermissions($rule, $modify)) { //Checks entity level write access permissions throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions"); } return $rule; }
/** * @test */ public function testComplex() { /* @var Script $script */ $script = static::generateScripts([['os' => 'linux']])[0]; /* @var ScriptVersion $version */ $version = static::generateVersions($script, [['content' => '#!/bin/sh']])[0]; $adapter = $this->getAdapter('OrchestrationRules\\FarmRoleScript'); /* @var User $user */ $user = $this->getUser(); $environment = $this->getEnvironment(); /* @var $farm Farm */ $farm = static::createEntity(new Farm(), ['changedById' => $user->getId(), 'name' => "{$this->uuid}-farm", 'description' => "{$this->uuid}-description", 'envId' => $environment->id, 'accountId' => $user->getAccountId(), 'createdById' => $user->getId()]); $farmRole = $this->createTestFarmRole($farm); static::createEntity(new FarmRoleScript(), ['farmRoleId' => $farmRole->id, 'scriptId' => $script->id, 'farmId' => $farm->id]); //test get endpoint $filterable = $adapter->getRules()[ApiEntityAdapter::RULE_TYPE_FILTERABLE]; $rules = $this->listRules($farmRole->id); foreach ($rules as $rule) { foreach ($filterable as $property) { $filterValue = $rule->{$property}; $listResult = $this->listRules($farmRole->id, [$property => $filterValue]); if (!static::isRecursivelyEmpty($filterValue)) { foreach ($listResult as $filtered) { $this->assertEquals($filterValue, $filtered->{$property}, "Property '{$property}' mismatch"); } } } $response = $this->getRule($farmRole->id, $rule->id); $this->assertEquals(200, $response->status, $this->printResponseError($response)); $dbRule = FarmRoleScript::findPk($rule->id); $this->assertObjectEqualsEntity($response->getBody()->data, $dbRule, $adapter); } $scalrFRScriptData = ['trigger' => ['triggerType' => FarmRoleScriptAdapter::TRIGGER_SINGLE_EVENT, 'event' => ['id' => 'HostInit']], 'target' => ['targetType' => FarmRoleScriptAdapter::TARGET_NAME_TRIGGERING_SERVER], 'action' => ['actionType' => FarmRoleScriptAdapter::ACTION_SCRIPT, 'scriptVersion' => ['script' => ['id' => $script->id], 'version' => $version->version]]]; $localFRScriptData = ['trigger' => ['triggerType' => FarmRoleScriptAdapter::TRIGGER_ALL_EVENTS], 'target' => ['targetType' => FarmRoleScriptAdapter::TARGET_NAME_NULL], 'action' => ['actionType' => FarmRoleScriptAdapter::ACTION_URI, 'path' => 'https://example.com']]; //post scalr rule $response = $this->postRule($farmRole->id, $scalrFRScriptData); $this->assertEquals(201, $response->status, $this->printResponseError($response)); $ruleId = $response->getBody()->data->id; /* @var $rule FarmRoleScript */ $rule = FarmRoleScript::findPk($ruleId); $this->assertNotEmpty($rule); $this->ruleToDelete($ruleId); $this->assertObjectEqualsEntity($scalrFRScriptData, $rule, $adapter); //post local rule $response = $this->postRule($farmRole->id, $localFRScriptData); $this->assertEquals(201, $response->status, $this->printResponseError($response)); $ruleId = $response->getBody()->data->id; /* @var $rule FarmRoleScript */ $rule = FarmRoleScript::findPk($ruleId); $this->assertNotEmpty($rule); $this->ruleToDelete($ruleId); $this->assertObjectEqualsEntity($localFRScriptData, $rule, $adapter); //post rule already existing $data = $scalrFRScriptData; $data['id'] = $ruleId; $response = $this->postRule($farmRole->id, $data); $this->assertEquals(201, $response->status, $this->printResponseError($response)); $ruleId = $response->getBody()->data->id; $this->ruleToDelete($ruleId); $this->assertNotEquals($data['id'], $ruleId); //post rule with script that does not exists $data = $scalrFRScriptData; $data['action']['scriptVersion']['script']['id'] = Script::findOne([], null, ['id' => true])->id + 1; $response = $this->postRule($farmRole->id, $data); $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND); //post rule with version that does not exists $data = $scalrFRScriptData; $data['action']['scriptVersion']['version'] = Script::findPk($data['action']['scriptVersion']['script']['id'])->getLatestVersion()->version + 1; $response = $this->postRule($farmRole->id, $data); $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND); //post rule with properties that not existing $data = $scalrFRScriptData; $data['foo'] = 'bar'; $response = $this->postRule($farmRole->id, $data); $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE); //post rule without required fields $data = $localFRScriptData; unset($data['action']); $response = $this->postRule($farmRole->id, $data); $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE); //post rule with invalid field $data = $localFRScriptData; $data['action'] = ''; $response = $this->postRule($farmRole->id, $data); $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE); //modify rule //TODO::ape add modify rule //fetch rule $response = $this->getRule($farmRole->id, $rule->id); $this->assertEquals(200, $response->status, $this->printResponseError($response)); $this->assertObjectEqualsEntity($response->getBody()->data, $rule, $adapter); //fetch rule that doe not exists $response = $this->getRule($farmRole->id, FarmRoleScript::findOne([], null, ['id' => false])->id + 1); $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND); //fetch rule with missmatch farm role id $response = $this->getRule(FarmRole::findOne([], null, ['id' => false])->id + 1, $rule->id); $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_VALUE); //test have access to all listed rules $rules = $this->listRules($farmRole->id); foreach ($rules as $rule) { $this->assertTrue(FarmRoleScript::findPk($rule->id)->hasAccessPermissions($user)); } //test invalid filters $url = self::getUserApiUrl("/farm-roles/{$farmRole->id}/orchestration-rules/"); $response = $this->request($url, Request::METHOD_GET, ['foo' => 'bar']); $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE); $response = $this->request($url, Request::METHOD_GET, ['scope' => 'foobar']); $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE); //delete script /* @var $rule FarmRoleScript */ $rule = static::createEntity(new FarmRoleScript(), ['farmRoleId' => $farmRole->id, 'scriptId' => $script->id, 'farmId' => $farm->id]); $response = $this->deleteRule($farmRole->id, $rule->id); $this->assertEquals(200, $response->status, $this->printResponseError($response)); //delete script that does not exists $response = $this->deleteRule($farmRole->id, FarmRoleScript::findOne([], null, ['id' => false])->id + 1); $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND); }