Esempio n. 1
0
 /**
  * @test
  * @functional
  */
 public function testGetVersions()
 {
     $list = Script::find(null, null, null, 10);
     $this->assertInternalType('array', $list->getArrayCopy());
     $this->assertInternalType('integer', count($list));
     $this->assertInternalType('integer', $list->count());
     try {
         /* @var $script Script */
         $script = Script::findOne([]);
     } catch (\Exception $e) {
         $this->markTestSkipped($e->getMessage());
     }
     if (!$script) {
         $this->markTestSkipped("There are no scripts so it can not proceed");
     }
     $versions = $script->getVersions()->getArrayCopy();
     $this->assertInternalType('array', $versions);
 }
Esempio n. 2
0
 /**
  * @test
  *
  * @throws \Scalr\Exception\ModelException
  */
 public function testComplex()
 {
     $user = $this->getUser();
     $environment = $this->getEnvironment();
     $fictionController = new ApiController();
     /* @var $script Script */
     $script = static::createEntity(new Script(), ['name' => "{$this->uuid}-script", 'description' => "{$this->uuid}-description", 'envId' => $environment->id, 'createdById' => $user->getId()]);
     //post script version
     $data = ['body' => '#!cmd'];
     $response = $this->postVersion($script->id, $data);
     $this->assertEquals(201, $response->status, $this->printResponseError($response));
     $versionNumber = $response->getBody()->data->version;
     /* @var $version ScriptVersion */
     $version = ScriptVersion::findPk($script->id, $versionNumber);
     $this->assertNotEmpty($version);
     $this->assertObjectEqualsEntity($data, $version);
     //post script version already existing
     $data = ['version' => $version->version, 'body' => '#!/bin/sh'];
     $response = $this->postVersion($script->id, $data);
     $this->assertEquals(201, $response->status, $this->printResponseError($response));
     $versionNumber = $response->getBody()->data->version;
     $this->assertNotEquals($data['version'], $versionNumber);
     //post script with properties that not existing
     $data = ['foo' => 'bar'];
     $response = $this->postVersion($script->id, $data);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //post version to scalr-scoped script
     /* @var $scalrScript Script */
     $scalrScript = static::createEntity(new Script(), ['name' => "{$this->uuid}-script-scalr-scoped", 'description' => "{$this->uuid}-description-scalr-scoped", 'createdById' => $user->getId()]);
     $data = ['body' => '#!/bin/sh'];
     $response = $this->postVersion($scalrScript->id, $data);
     $this->assertErrorMessageContains($response, 403, ErrorMessage::ERR_PERMISSION_VIOLATION);
     //test script fetch
     $response = $this->getVersion($script->id, $version->version);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     $this->assertObjectEqualsEntity($response->getBody()->data, $version);
     //test fetch script that doe not exists
     $response = $this->getVersion($script->id, $script->getLatestVersion()->version + 1);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     //modify script version
     $data = ['body' => '#!/bin/bash'];
     $response = $this->modifyVersion($script->id, $version->version, $data);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     $this->assertObjectEqualsEntity($response->getBody()->data, ScriptVersion::findPk($script->id, $version->version));
     //modify property that does not exists
     $data = ['foo' => 'bar'];
     $response = $this->modifyVersion($script->id, $version->version, $data);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //modify properties that not alterable
     $scriptVersionAdapter = new ScriptVersionAdapter($fictionController);
     $adapterRules = $scriptVersionAdapter->getRules();
     $publicProperties = $adapterRules[BaseAdapter::RULE_TYPE_TO_DATA];
     $alterableProperties = $adapterRules[ApiEntityAdapter::RULE_TYPE_ALTERABLE];
     $nonAlterableProperties = array_diff(array_values($publicProperties), $alterableProperties);
     foreach ($nonAlterableProperties as $property) {
         if (in_array($property, ['id', 'version'])) {
             continue;
         }
         $data = [$property => 'foo'];
         $response = $this->modifyVersion($script->id, $version->version, $data);
         $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     }
     //modify script that does not exists
     $data = ['body' => '#!powershell'];
     $response = $this->modifyVersion($script->id, $script->getLatestVersion()->version + 1, $data);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     //modify Scalr-scoped script version
     /* @var $scalrVersion ScriptVersion */
     $scalrVersion = static::createEntity(new ScriptVersion(), ['scriptId' => $scalrScript->id, 'version' => 2, 'content' => '#!/bin/sh']);
     //modify Scalr-scoped script version
     $data = ['body' => '#!cmd'];
     $response = $this->modifyVersion($scalrScript->id, $scalrVersion->version, $data);
     $this->assertErrorMessageContains($response, 403, ErrorMessage::ERR_PERMISSION_VIOLATION, 'Insufficient permissions');
     /* @var $version ScriptVersion */
     $version = static::createEntity(new ScriptVersion(), ['scriptId' => $script->id, 'version' => $script->getLatestVersion()->version + 1, 'content' => '#!foobar']);
     //test have access to all listed scripts versions
     $versions = $this->listVersions($script->id);
     foreach ($versions as $version) {
         $this->assertTrue(ScriptVersion::findPk($script->id, $version->version)->hasAccessPermissions($user));
     }
     $listUri = static::getUserApiUrl("/scripts/{$script->id}/script-versions/");
     //test list script versions filters
     $filterable = $scriptVersionAdapter->getRules()[ApiEntityAdapter::RULE_TYPE_FILTERABLE];
     /* @var $version ScriptVersion */
     foreach ($versions as $version) {
         foreach ($filterable as $property) {
             $filterValue = $version->{$property};
             $listResult = $this->listVersions($script->id, [$property => $filterValue]);
             if (!static::isRecursivelyEmpty($filterValue)) {
                 foreach ($listResult as $filtered) {
                     $this->assertEquals($filterValue, $filtered->{$property}, "Property '{$property}' mismatch");
                 }
             }
         }
         $response = $this->getVersion($script->id, $version->version);
         $this->assertEquals(200, $response->status, $this->printResponseError($response));
         $dbScriptVersions = ScriptVersion::findPk($script->id, $version->version);
         $this->assertObjectEqualsEntity($response->getBody()->data, $dbScriptVersions, $scriptVersionAdapter);
     }
     //test invalid filters
     $response = $this->request($listUri, Request::METHOD_GET, ['foo' => 'bar']);
     $this->assertErrorMessageContains($response, 400, ErrorMessage::ERR_INVALID_STRUCTURE);
     //delete script version
     /* @var $version ScriptVersion */
     $version = static::createEntity(new ScriptVersion(), ['scriptId' => $script->id, 'version' => $script->getLatestVersion()->version + 1, 'content' => '#!/bin/sh foobar']);
     $response = $this->deleteVersion($script->id, $version->version);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     //delete scalr-scoped script version
     $response = $this->deleteVersion($scalrVersion->scriptId, $scalrVersion->version);
     $this->assertErrorMessageContains($response, 403, ErrorMessage::ERR_PERMISSION_VIOLATION);
     //delete script version that does not exists
     $response = $this->deleteVersion($script->id, $script->getLatestVersion()->version + 1);
     $this->assertErrorMessageContains($response, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     $scripts = Script::find([['$or' => [['accountId' => $user->accountId], ['accountId' => null]]], ['$or' => [['envId' => $environment->id], ['envId' => null]]]]);
     foreach ($scripts as $script) {
         //test have access to all listed scripts versions
         $versions = $this->listVersions($script->id);
         foreach ($versions as $version) {
             $version = ScriptVersion::findPk($script->id, $version->version);
             $this->assertTrue($version->hasAccessPermissions($user));
         }
         if ($version->getScope() !== ScopeInterface::SCOPE_ENVIRONMENT) {
             $response = $this->postVersion($version->scriptId, ['body' => '#!/bin/sh' . $this->getTestName()]);
             $this->assertErrorMessageContains($response, 403, ErrorMessage::ERR_PERMISSION_VIOLATION);
         }
     }
 }
 public function ScriptsList()
 {
     $this->restrictAccess(Acl::RESOURCE_ADMINISTRATION_SCRIPTS);
     $response = $this->CreateInitialResponse();
     $response->ScriptSet = new stdClass();
     $response->ScriptSet->Item = array();
     foreach (Script::find(array('$or' => array(array('accountId' => NULL), array('accountId' => $this->user->getAccountId())))) as $script) {
         /* @var $script Script */
         $itm = new stdClass();
         $itm->{"ID"} = $script->id;
         $itm->{"Name"} = $script->name;
         $itm->{"Description"} = $script->description;
         $itm->{"LatestRevision"} = $script->getLatestVersion()->version;
         $response->ScriptSet->Item[] = $itm;
     }
     return $response;
 }
Esempio n. 4
0
 /**
  * @param string $scriptId
  * @param string $query
  * @param string $scope
  * @param JsonData $sort
  * @param int $start
  * @param int $limit
  */
 public function xListAction($scriptId = null, $query = null, $scope = null, JsonData $sort, $start = 0, $limit = 20)
 {
     $this->request->restrictAccess('SCRIPTS');
     $criteria = [];
     if ($this->user->isScalrAdmin()) {
         $criteria[] = ['accountId' => NULL];
     } else {
         if ($scope == ScopeInterface::SCOPE_SCALR) {
             $criteria[] = ['accountId' => NULL];
         } else {
             if ($scope == ScopeInterface::SCOPE_ACCOUNT) {
                 $criteria[] = ['accountId' => $this->user->getAccountId()];
                 $criteria[] = ['envId' => NULL];
             } else {
                 if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) {
                     $criteria[] = ['accountId' => $this->user->getAccountId()];
                     $criteria[] = ['envId' => $this->getEnvironmentId(true)];
                 } else {
                     $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]];
                     if ($this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) {
                         $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['envId' => NULL]]];
                     } else {
                         $criteria[] = ['envId' => $this->getEnvironmentId(true)];
                     }
                 }
             }
         }
     }
     if ($query) {
         $querySql = '%' . $query . '%';
         $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['name' => ['$like' => $querySql]], ['description' => ['$like' => $querySql]]]];
     }
     if ($scriptId) {
         $criteria[] = ['id' => $scriptId];
     }
     $result = Script::find($criteria, null, Utils::convertOrder($sort, ['name' => true], ['id', 'name', 'description', 'isSync', 'dtCreated', 'dtChanged']), $limit, $start, true);
     $data = [];
     foreach ($result as $script) {
         /* @var $script Script */
         $data[] = $this->getScript($script);
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Esempio n. 5
0
 public function ScriptsList()
 {
     $this->restrictAccess(Acl::RESOURCE_SCRIPTS_ENVIRONMENT);
     $response = $this->CreateInitialResponse();
     $response->ScriptSet = new stdClass();
     $response->ScriptSet->Item = array();
     foreach (Script::find(['$or' => [['accountId' => null], ['accountId' => $this->user->getAccountId()]]]) as $script) {
         /* @var $script Script */
         $itm = new stdClass();
         $itm->ID = $script->id;
         $itm->Name = $script->name;
         $itm->Description = $script->description;
         $itm->LatestRevision = $script->getLatestVersion()->version;
         $response->ScriptSet->Item[] = $itm;
     }
     return $response;
 }
Esempio n. 6
0
 /**
  * @param string $query
  * @param string $origin
  * @param JsonData $sort
  * @param int $start
  * @param int $limit
  */
 public function xListAction($query = null, $origin = null, JsonData $sort, $start = 0, $limit = 20)
 {
     $this->request->restrictAccess(Acl::RESOURCE_ADMINISTRATION_SCRIPTS);
     $criteria = [];
     if ($this->user->isScalrAdmin()) {
         $criteria[] = ['accountId' => NULL];
     } else {
         if ($origin == 'Shared') {
             $criteria[] = ['accountId' => NULL];
         } else {
             if ($origin == 'Custom') {
                 $criteria[] = ['accountId' => $this->user->getAccountId()];
                 $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId()], ['envId' => NULL]]];
             } else {
                 $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]];
                 $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId()], ['envId' => NULL]]];
             }
         }
     }
     if ($query) {
         $querySql = '%' . $query . '%';
         $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['name' => ['$like' => $querySql]], ['description' => ['$like' => $querySql]]]];
     }
     $result = Script::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['name' => 'ASC'], ['id', 'name', 'description', 'isSync', 'dtCreated', 'dtChanged']), $limit, $start, true);
     $data = [];
     foreach ($result as $script) {
         /* @var Script $script */
         $s = get_object_vars($script);
         $s['dtCreated'] = Scalr_Util_DateTime::convertTz($script->dtCreated);
         $s['dtChanged'] = Scalr_Util_DateTime::convertTz($script->dtChanged);
         $s['version'] = $script->getLatestVersion()->version;
         $data[] = $s;
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Esempio n. 7
0
 /**
  * Gets role scripts
  *
  * @param    array        $criteria     optional The search criteria.
  * @param    array        $order        optional The results order looks like [[property1 => true|false], ...]
  * @param    int          $limit        optional The records limit
  * @param    int          $offset       optional The offset
  * @param    bool         $countRecords optional True to calculate total number of the records without limit
  *
  * @return Script[]
  */
 public function getScripts(array $criteria = null, array $order = null, $limit = null, $offset = null, $countRecords = null)
 {
     return Script::find(array_merge(['roleId' => $this->id], $criteria), $order, $limit, $offset, $countRecords);
 }
Esempio n. 8
0
 public function xUpdateAgentAction()
 {
     $this->request->restrictAccess(Acl::RESOURCE_FARMS_SERVERS);
     $this->request->defineParams(array('serverId'));
     /* @var Entity\Script $scr */
     $scr = Entity\Script::find(array(array('id' => 2102), array('accountId' => NULL)));
     if (!$scr) {
         throw new Exception("Automatical scalarizr update doesn't supported by this scalr version");
     }
     $dbServer = DBServer::LoadByID($this->getParam('serverId'));
     $this->user->getPermissions()->validate($dbServer);
     $scriptSettings = array('version' => $scr->getLatestVersion()->version, 'scriptid' => 2102, 'timeout' => 300, 'issync' => 0, 'params' => serialize(array()), 'type' => Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_SCALR);
     $message = new Scalr_Messaging_Msg_ExecScript("Manual");
     $message->setServerMetaData($dbServer);
     $script = Scalr_Scripting_Manager::prepareScript($scriptSettings, $dbServer);
     $itm = new stdClass();
     // Script
     $itm->asynchronous = $script['issync'] == 1 ? '0' : '1';
     $itm->timeout = $script['timeout'];
     if ($script['body']) {
         $itm->name = $script['name'];
         $itm->body = $script['body'];
     } else {
         $itm->path = $script['path'];
     }
     $itm->executionId = $script['execution_id'];
     $message->scripts = array($itm);
     $dbServer->SendMessage($message);
     $this->response->success('Scalarizr update successfully initiated. Please wait a few minutes and then refresh the page');
 }