public function registerAction()
{
$this->checkPermissions(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE);
$object = $this->request->getJsonBody();
$imageAdapter = $this->adapter('image');
//Pre validates the request object
$imageAdapter->validateObject($object, Request::METHOD_POST);
if (isset($object->scope) && $object->scope !== $this->getScope()) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid scope");
}
//Read only property. It is needed before toEntity() call to set envId and accountId properties properly
$object->scope = $this->getScope();
/* @var $image Entity\Image */
//Converts object into Role entity
$image = $imageAdapter->toEntity($object);
$image->hash = null;
$image->source = Entity\Image::SOURCE_MANUAL;
$image->status = Entity\Image::STATUS_ACTIVE;
$imageAdapter->validateEntity($image);
if ($this->getScope() == ScopeInterface::SCOPE_ENVIRONMENT && !$image->getEnvironment()->isPlatformEnabled($image->platform)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Platform '%s' is not enabled.", $image->platform));
}
if ($image->checkImage() === false) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unable to find the requested image on the cloud, or it is not usable by your account");
}
if (!empty($object->name)) {
$image->name = $object->name;
}
//Saves entity
$image->save();
//Responds with 201 Created status
$this->response->setStatus(201);
return $this->result($imageAdapter->toData($image));
}