/**
* @param string $imageId
* @param string $platform
* @param string $osId
* @param string $name
* @param string $cloudLocation
* @param string $architecture
* @param int $size
* @param string $ec2Type
* @param bool $ec2Hvm
* @param JsonData $software
* @throws Scalr_Exception_Core
*/
public function xSaveAction($imageId, $platform, $osId, $name, $cloudLocation = '', $architecture = '', $size = null, $ec2Type = null, $ec2Hvm = null, JsonData $software = null)
{
$this->restrictAccess('IMAGES', 'MANAGE');
if ($platform == SERVER_PLATFORMS::GCE || $platform == SERVER_PLATFORMS::AZURE) {
$cloudLocation = '';
}
if ($accountId = $this->user->getAccountId()) {
if ($envId = $this->getEnvironmentId(true)) {
if (Image::findOne([['id' => $imageId], ['envId' => $envId], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) {
throw new Scalr_Exception_Core('This Image has already been registered in the Environment Scope.');
}
}
if (Image::findOne([['id' => $imageId], ['accountId' => $this->user->getAccountId()], ['envId' => null], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) {
throw new Scalr_Exception_Core('This Image has already been registered in the Account Scope.');
}
}
if (Image::findOne([['id' => $imageId], ['accountId' => null], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) {
$this->response->failure('This Image has already been registered in the Scalr Scope.');
return;
}
if (!Role::isValidName($name)) {
$this->response->failure('Name should start and end with letter or number and contain only letters, numbers and dashes.');
return;
}
$image = new Image();
$image->accountId = $this->user->getAccountId() ?: null;
$image->envId = $this->getEnvironmentId(true);
$image->id = $imageId;
$image->platform = $platform;
$image->cloudLocation = $cloudLocation;
$image->architecture = 'x86_64';
if ($this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) {
if ($image->checkImage() === false) {
$this->response->failure("This Image does not exist, or isn't usable by your account");
return;
}
} else {
$image->architecture = $architecture;
$image->size = $size;
if ($platform == SERVER_PLATFORMS::EC2) {
if ($ec2Type == 'ebs' || $ec2Type == 'instance-store') {
$image->type = $ec2Type;
if ($ec2Hvm) {
$image->type = $image->type . '-hvm';
}
}
}
}
$image->name = $name;
$image->source = Image::SOURCE_MANUAL;
$image->osId = $osId;
$image->createdById = $this->user->getId();
$image->createdByEmail = $this->user->getEmail();
$image->status = Image::STATUS_ACTIVE;
$image->save();
$props = [];
foreach ($software as $value) {
$props[$value] = null;
}
$image->setSoftware($props);
$this->response->data(['hash' => $image->hash]);
$this->response->success('Image has been added');
}