Since: 5.11.11 (23.02.2016)
Author: Andrii Penchuk (a.penchuk@scalr.com)
Inheritance: extends Scalr\Api\DataType\ApiEntityAdapter
Example #1
0
 /**
  * Import non-scalarizr server to the Farm Role
  *
  * @param int $farmRoleId
  * @return ResultEnvelope
  * @throws ApiErrorException
  */
 public function importServerAction($farmRoleId)
 {
     $this->checkPermissions(Acl::RESOURCE_DISCOVERY_SERVERS, Acl::PERM_DISCOVERY_SERVERS_IMPORT);
     /* @var  $farmRole FarmRole */
     $farmRole = $this->getFarmRole($farmRoleId, null, true);
     $this->farmController->checkPermissions($farmRole->getFarm(), ACL::PERM_FARMS_SERVERS);
     if (!$this->getEnvironment()->keychain($farmRole->platform)->isEnabled()) {
         throw new ApiErrorException(409, ErrorMessage::ERR_NOT_ENABLED_PLATFORM, sprintf("Platform '%s' is not enabled", $farmRole->platform));
     }
     $object = $this->request->getJsonBody();
     if (empty($object->cloudServerId)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property cloudServerId");
     }
     $cloudServerId = ServerAdapter::convertInputValue('string', $object->cloudServerId, 'cloudServerId');
     //TODO the loader of the list of the Tags should be moved into separate class/method
     $serverTags = [];
     if (!empty($object->tags)) {
         if (!is_array($object->tags)) {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Property tags must be array");
         }
         foreach ($object->tags as $tag) {
             if (!isset($tag->key)) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property tag.key");
             }
             $serverTags[ServerAdapter::convertInputValue('string', $tag->key, 'tag.key')] = isset($tag->value) ? ServerAdapter::convertInputValue('string', $tag->value, 'tag.value') : null;
         }
     }
     try {
         /* @var $server Server */
         $server = $farmRole->getServerImport($this->getUser())->import($cloudServerId, $serverTags);
     } catch (NotSupportedException $e) {
         throw new ApiNotImplementedErrorException(sprintf("Platform '%s' is not supported yet", $farmRole->platform));
     } catch (ValidationErrorException $e) {
         if (strpos($e->getMessage(), 'Instance was not found') !== false) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, $e->getMessage(), $e->getCode(), $e);
         } else {
             throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, $e->getMessage(), $e->getCode(), $e);
         }
     } catch (ServerImportException $e) {
         throw new ApiErrorException(409, ErrorMessage::ERR_CONFIGURATION_MISMATCH, $e->getMessage(), $e->getCode(), $e);
     } catch (Exception $e) {
         throw new ApiErrorException(503, ErrorMessage::ERR_SERVICE_UNAVAILABLE, $e->getMessage(), $e->getCode(), $e);
     }
     $this->response->setStatus(201);
     return $this->result($this->adapter('server')->toData($server));
 }
Example #2
0
 /**
  * Reboots instance
  *
  * @param string    $serverId   UUID of the server
  * @return ResultEnvelope
  * @throws ApiErrorException
  * @throws ApiNotImplementedErrorException
  */
 public function rebootAction($serverId)
 {
     $server = $this->getServer($serverId);
     $this->checkPermissions($server, Acl::PERM_FARMS_SERVERS);
     if (in_array($server->status, [Server::STATUS_TERMINATED, Server::STATUS_IMPORTING, Server::STATUS_PENDING_LAUNCH, Server::STATUS_PENDING_TERMINATE, Server::STATUS_TEMPORARY])) {
         throw new ApiErrorException(409, ErrorMessage::ERR_UNACCEPTABLE_STATE, sprintf("The Server can't be rebooted in %s state.", $server->status));
     }
     if ($server->platform == SERVER_PLATFORMS::AZURE) {
         throw new ApiNotImplementedErrorException('Reboot action has not been implemented for Azure cloud platform yet.');
     }
     $object = $this->request->getJsonBody();
     $hard = isset($object->hard) ? ServerAdapter::convertInputValue('boolean', $object->hard) : false;
     try {
         $server->reboot($hard);
     } catch (InstanceNotFound $e) {
         throw new ApiErrorException(409, ErrorMessage::ERR_OBJECT_NOT_FOUND_ON_CLOUD, "The Server that you are trying to use does not exist on the cloud.");
     } catch (Exception $e) {
         throw new ApiErrorException(409, ErrorMessage::ERR_UNACCEPTABLE_OBJECT_CONFIGURATION, $e->getMessage());
     }
     $this->response->setStatus(200);
     return $this->result($this->adapter('server')->toData($server));
 }