Scalr\Api\Service\User\V1beta0\Controller\FarmRoles::importServerAction PHP Method

importServerAction() public method

Import non-scalarizr server to the Farm Role
public importServerAction ( integer $farmRoleId ) : Scalr\Api\DataType\ResultEnvelope
$farmRoleId integer
return Scalr\Api\DataType\ResultEnvelope
    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));
    }