Skip to content

Commit

Permalink
Implement Resources Images & Image & the Server Method createImage
Browse files Browse the repository at this point in the history
  • Loading branch information
LKaemmerling committed Jan 29, 2018
1 parent 41cfe0d commit e9ab838
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 8 deletions.
167 changes: 166 additions & 1 deletion src/Models/Images/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,171 @@

namespace LKDev\HetznerCloud\Models\Images;

class Image
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Model;
use LKDev\HetznerCloud\Models\Servers\Server;

/**
*
*/
class Image extends Model
{
/**
* @var int
*/
public $id;

/**
* @var string
*/
public $type;

/**
* @var string
*/
public $status;

/**
* @var string
*/
public $name;

/**
* @var string
*/
public $description;

/**
* @var float
*/
public $imageSize;

/**
* @var integer
*/
public $diskSize;

/**
* @var string
*/
public $created;

/**
* @var \LKDev\HetznerCloud\Models\Servers\Server
*/
public $createdFrom;

/**
* @var int
*/
public $boundTo;

/**
* @var string
*/
public $osFlavor;

/**
* @var string
*/
public $osVersion;

/**
* @var bool
*/
public $rapidDeploy;

/**
* Image constructor.
*
* @param int $id
* @param string $type
* @param string $status
* @param string $name
* @param string $description
* @param float $imageSize
* @param int $diskSize
* @param string $created
* @param \LKDev\HetznerCloud\Models\Servers\Server $createdFrom
* @param int $boundTo
* @param string $osFlavor
* @param string $osVersion
* @param bool $rapidDeploy
*/
public function __construct(
int $id,
string $type,
string $status,
string $name,
string $description,
float $imageSize,
int $diskSize,
string $created,
Server $createdFrom,
int $boundTo,
string $osFlavor,
string $osVersion,
bool $rapidDeploy
) {
$this->id = $id;
$this->type = $type;
$this->status = $status;
$this->name = $name;
$this->description = $description;
$this->imageSize = $imageSize;
$this->diskSize = $diskSize;
$this->created = $created;
$this->createdFrom = $createdFrom;
$this->boundTo = $boundTo;
$this->osFlavor = $osFlavor;
$this->osVersion = $osVersion;
$this->rapidDeploy = $rapidDeploy;
parent::__construct();
}

/**
* Updates the Image. You may change the description or convert a Backup image to a Snapshot Image. Only images of type snapshot and backup can be updated.
*
* @see https://docs.hetzner.cloud/#resources-images-put
* @param string $description
* @param string $type
* @return \LKDev\HetznerCloud\Models\Images\Image
* @throws \LKDev\HetznerCloud\APIException
*/
public function update(string $description, string $type): Image
{
$response = $this->httpClient->put('images/'.$this->id, [
'json' => [
'description' => $description,
'type' => $type,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return self::parse(json_decode((string) $response->getBody())->image);
}
}

/**
* Deletes an Image. Only images of type snapshot and backup can be deleted.
*
* @see https://docs.hetzner.cloud/#resources-images-delete
* @return bool
* @throws \LKDev\HetznerCloud\APIException
*/
public function delete(): bool
{
$response = $this->httpClient->delete('images/'.$this->id);
if (! HetznerAPIClient::hasError($response)) {
return true;
}
}

/**
* @param object $input
* @return \LKDev\HetznerCloud\Models\Images\Image|static
*/
public static function parse(object $input)
{
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, Server::parse($input->created_from), $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy);
}
}
72 changes: 72 additions & 0 deletions src/Models/Images/Images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Created by PhpStorm.
* User: lukaskammerling
* Date: 28.01.18
* Time: 21:01
*/

namespace LKDev\HetznerCloud\Models\Images;

use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Model;

class Images extends Model
{ /**
* @var array
*/
public $images;

/**
* Returns all image objects.
*
* @see https://docs.hetzner.cloud/#resources-images-get
* @return array
* @throws \LKDev\HetznerCloud\APIException
*/
public function all(): array
{
$response = $this->httpClient->get('images');
if (! HetznerAPIClient::hasError($response)) {
return self::parse(json_decode((string) $response->getBody()))->images;
}
}

/**
* Returns a specific image object.
*
* @see https://docs.hetzner.cloud/#resources-images-get-1
* @param int $imageId
* @return \LKDev\HetznerCloud\Models\Images\Image
* @throws \LKDev\HetznerCloud\APIException
*/
public function get(int $imageId): Image
{
$response = $this->httpClient->get('images/'.$imageId);
if (! HetznerAPIClient::hasError($response)) {
return Image::parse(json_decode((string) $response->getBody())->image);
}
}

/**
* @param object $input
* @return $this
*/
public function setAdditionalData(object $input)
{
$this->images = collect($input->images)->map(function ($image, $key) {
return Image::parse($image);
})->toArray();

return $this;
}

/**
* @param object $input
* @return $this|static
*/
public static function parse(object $input)
{
return (new self())->setAdditionalData($input);
}
}
18 changes: 11 additions & 7 deletions src/Models/Servers/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function powerOn(): Action
*/
public function softReboot(): Action
{
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/reboot'));
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/reboot'));
if (! HetznerAPIClient::hasError($response)) {
return Action::parse(json_decode((string) $response->getBody()->action));
}
Expand Down Expand Up @@ -250,7 +250,7 @@ public function enableRescue($type = 'linux64', $ssh_keys = []): Action
*/
public function disableRescue(): Action
{
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/disable_rescue'));
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/disable_rescue'));
if (! HetznerAPIClient::hasError($response)) {
return Action::parse(json_decode((string) $response->getBody()->action));
}
Expand All @@ -263,16 +263,20 @@ public function disableRescue(): Action
* @param string $description
* @param string $type
* @return \LKDev\HetznerCloud\Models\Images\Image
* @throws \LKDev\HetznerCloud\APIException
*/
public function createImage(string $description = '', string $type = 'snapshot'): Image
{
// ToDo
$this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/create_image'), [

$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/create_image'), [
'form_params' => [
'description' => $description,
'type' => $type,
],
]);
if (! HetznerAPIClient::hasError($response)) {
return Image::parse(json_decode((string) $response->getBody()->image));
}
}

/**
Expand Down Expand Up @@ -360,9 +364,9 @@ public function disableBackups(): Action
* @return \LKDev\HetznerCloud\Models\Actions\Action
* @throws \LKDev\HetznerCloud\APIException
*/
public function attachISO(ISO $iso):Action
public function attachISO(ISO $iso): Action
{
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/attach_iso'), [
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/attach_iso'), [
'form_params' => [
'iso' => $iso->id,
],
Expand All @@ -381,7 +385,7 @@ public function attachISO(ISO $iso):Action
*/
public function detachISO(): Action
{
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/detach_iso'));
$response = $this->httpClient->post($this->replaceServerIdInUri('servers/{id}/actions/detach_iso'));
if (! HetznerAPIClient::hasError($response)) {
return Action::parse(json_decode((string) $response->getBody()->action));
}
Expand Down

0 comments on commit e9ab838

Please sign in to comment.