/** * Finds out the Image that corresponds to the Role by the specified criteria * * @param $platform string The cloud platform * @param $cloudLocation string The cloud location * @return RoleImage Returns the RoleImage object. * It throws an exception when the RoleImage does not exist * @throws \Exception */ public function getImage($platform, $cloudLocation) { if (in_array($platform, [\SERVER_PLATFORMS::GCE, \SERVER_PLATFORMS::AZURE])) { $cloudLocation = ''; } $image = RoleImage::findOne([['roleId' => $this->id], ['platform' => $platform], ['cloudLocation' => $cloudLocation]]); if (!$image) { throw new ImageNotFoundException(sprintf("No valid role image found for roleId: %d, platform: %s, cloudLocation: %s", $this->id, $platform, $cloudLocation)); } return $image; }
/** * Associates a new Image with the Role * * @param int $roleId The Identifier of the role * @return \Scalr\Api\DataType\ResultEnvelope * @throws ApiErrorException */ public function registerImageAction($roleId) { $this->checkScopedPermissions('ROLES', 'MANAGE'); //Gets role checking Environment scope $role = $this->getRole($roleId, true); $object = $this->request->getJsonBody(); $objectImageId = static::getBareId($object, 'image'); $objectRoleId = static::getBareId($object, 'role'); if (empty($objectImageId)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Invalid body"); } if (!preg_match('/' . ApiApplication::REGEXP_UUID . '/', $objectImageId)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid image identifier"); } if (!empty($objectRoleId) && $roleId != $objectRoleId) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid identifier of the role"); } if (is_object($object->image)) { $imageAdapter = $this->adapter('image'); //Pre validates the request object $imageAdapter->validateObject($object->image); } $criteria = $this->getScopeCriteria(); $criteria[] = ['hash' => $objectImageId]; /* @var $image Entity\Image */ $image = Entity\Image::findOne($criteria); if (empty($image)) { throw new ApiErrorException(404, ErrorMessage::ERR_INVALID_VALUE, "The Image either does not exist or isn't in scope for the current Environment."); } $roleImage = Entity\RoleImage::findOne([['roleId' => $roleId], ['platform' => $image->platform], ['cloudLocation' => $image->cloudLocation]]); if (!empty($roleImage)) { throw new ApiErrorException(400, ErrorMessage::ERR_BAD_REQUEST, sprintf("Image with cloud location %s has already been registered", $image->cloudLocation)); } $this->setImage($role, $image->platform, $image->cloudLocation, $image->id, $this->getUser()->id, $this->getUser()->email); $this->response->setStatus(201); return $this->result(['image' => ['id' => $image->hash], 'role' => ['id' => $role->id]]); }