Skip to content

Commit

Permalink
When listing OSs, you can now see licences
Browse files Browse the repository at this point in the history
This is in conjunction with bringing the possibility to upgrade/downgrade licences for cPanel or Plesk using the API.
  • Loading branch information
samihsoylu committed Jan 29, 2021
1 parent 764955f commit d898c2c
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

6.6.0
-----
* Added VPS license resource

6.5.0
-----
* Added isLocked property to HA-IP
Expand Down
88 changes: 88 additions & 0 deletions src/Entity/Vps/License.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Transip\Api\Library\Entity\Vps;

use Transip\Api\Library\Entity\AbstractEntity;

class License extends AbstractEntity
{
public const TYPE_ADDON = 'addon';
public const TYPE_OPERATING_SYSTEM = 'operating-system';

/** @var int */
protected $id;

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

/** @var int */
protected $price;

/** @var int */
protected $recurringPrice;

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

/** @var int */
protected $quantity;

/** @var int */
protected $maxQuantity;

/** @var LicenseKey[] */
protected $keys;

public function __construct(array $valueArray = [])
{
parent::__construct($valueArray);

$licenseKeysArray = $valueArray['keys'] ?? [];

$licenseKeys = [];
foreach ($licenseKeysArray as $licenseKey) {
$licenseKeys[] = new LicenseKey($licenseKey);
}
$this->keys = $licenseKeys;
}

public function getId(): int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function getPrice(): int
{
return $this->price;
}

public function getRecurringPrice(): int
{
return $this->recurringPrice;
}

public function getType(): string
{
return $this->type;
}

public function getQuantity(): int
{
return $this->quantity;
}

public function getMaxQuantity(): int
{
return $this->maxQuantity;
}

public function getKeys(): array
{
return $this->keys;
}
}
24 changes: 24 additions & 0 deletions src/Entity/Vps/LicenseKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Transip\Api\Library\Entity\Vps;

use Transip\Api\Library\Entity\AbstractEntity;

class LicenseKey extends AbstractEntity
{
/** @var string */
protected $name;

/** @var string */
protected $key;

public function getName(): string
{
return $this->name;
}

public function getKey(): string
{
return $this->key;
}
}
48 changes: 48 additions & 0 deletions src/Entity/Vps/LicenseProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Transip\Api\Library\Entity\Vps;

use Transip\Api\Library\Entity\AbstractEntity;

class LicenseProduct extends AbstractEntity
{
/** @var string */
protected $name;

/** @var int */
protected $price;

/** @var int */
protected $recurringPrice;

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

/** @var int */
protected $maxQuantity;

public function getName(): string
{
return $this->name;
}

public function getPrice(): int
{
return $this->price;
}

public function getRecurringPrice(): int
{
return $this->recurringPrice;
}

public function getType(): string
{
return $this->type;
}

public function getMaxQuantity(): int
{
return $this->maxQuantity;
}
}
41 changes: 41 additions & 0 deletions src/Entity/Vps/Licenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Transip\Api\Library\Entity\Vps;

use Transip\Api\Library\Entity\AbstractEntity;

class Licenses extends AbstractEntity
{
/** @var License[] */
protected $active = [];

/** @var License[] */
protected $cancellable = [];

/** @var LicenseProduct[] */
protected $available = [];

/**
* @return License[]
*/
public function getActive(): array
{
return $this->active;
}

/**
* @return License[]
*/
public function getCancellable(): array
{
return $this->cancellable;
}

/**
* @return LicenseProduct[]
*/
public function getAvailable(): array
{
return $this->available;
}
}
29 changes: 26 additions & 3 deletions src/Entity/Vps/OperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class OperatingSystem extends AbstractEntity
{
public const INSTALL_FLAVOUR_INSTALLER = 'installer';
public const INSTALL_FLAVOUR_INSTALLER = 'installer';
public const INSTALL_FLAVOUR_PREINSTALLABLE = 'preinstallable';
public const INSTALL_FLAVOUR_CLOUDINIT = 'cloudinit';
public const INSTALL_FLAVOUR_CLOUDINIT = 'cloudinit';

/**
* @var string $name
Expand All @@ -35,6 +35,21 @@ class OperatingSystem extends AbstractEntity
*/
protected $installFlavours = [];

/**
* @var LicenseProduct[]
*/
protected $licenses = [];

public function __construct(array $valueArray = [])
{
parent::__construct($valueArray);

$licenses = $valueArray['licenses'] ?? [];
foreach ($licenses as $license) {
$this->licenses[] = new LicenseProduct($license);
}
}

public function getName(): string
{
return $this->name;
Expand All @@ -46,7 +61,7 @@ public function getDescription(): string
}

/**
* @deprecated
* @deprecated use getInstallFlavours() instead
* @return bool
*/
public function isPreinstallableImage(): bool
Expand All @@ -68,4 +83,12 @@ public function getInstallFlavours(): array
{
return $this->installFlavours;
}

/**
* @return LicenseProduct[]
*/
public function getLicenses(): array
{
return $this->licenses;
}
}
59 changes: 59 additions & 0 deletions src/Repository/Vps/LicenseRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Transip\Api\Library\Repository\Vps;

use Transip\Api\Library\Entity\Vps\License;
use Transip\Api\Library\Entity\Vps\LicenseProduct;
use Transip\Api\Library\Entity\Vps\Licenses;
use Transip\Api\Library\Repository\ApiRepository;
use Transip\Api\Library\Repository\VpsRepository;

class LicenseRepository extends ApiRepository
{
public const RESOURCE_NAME = 'licenses';

protected function getRepositoryResourceNames(): array
{
return [VpsRepository::RESOURCE_NAME, self::RESOURCE_NAME];
}

public function getByVpsName(string $vpsName): Licenses
{
$response = $this->httpClient->get($this->getResourceUrl($vpsName));
$licencesArray = $this->getParameterFromResponse($response, 'licenses');

$struct = [];
foreach ($licencesArray as $licenseType => $licenses) {
foreach ($licenses as $license) {
if ($licenseType === 'available') {
$struct[$licenseType][] = new LicenseProduct($license);
continue;
}

$struct[$licenseType][] = new License($license);
}
}

return new Licenses($struct);
}

public function order(string $vpsName, string $licenseName, int $quantity): void
{
$parameters = [
'licenseName' => $licenseName,
'quantity' => $quantity,
];

$this->httpClient->post($this->getResourceUrl($vpsName), $parameters);
}

public function update(string $vpsName, int $licenseId, string $newLicenseName): void
{
$this->httpClient->put($this->getResourceUrl($vpsName, $licenseId), ['newLicenseName' => $newLicenseName]);
}

public function cancel(string $vpsName, int $licenseId): void
{
$this->httpClient->delete($this->getResourceUrl($vpsName, $licenseId));
}
}
9 changes: 8 additions & 1 deletion src/TransipAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use Transip\Api\Library\Repository\TrafficRepository;
use Transip\Api\Library\Repository\Vps\AddonRepository;
use Transip\Api\Library\Repository\Vps\BackupRepository as VpsBackupRepository;
use Transip\Api\Library\Repository\Vps\License\KeyRepository;
use Transip\Api\Library\Repository\Vps\LicenseRepository;
use Transip\Api\Library\Repository\Vps\MonitoringContactRepository;
use Transip\Api\Library\Repository\Vps\FirewallRepository as VpsFirewallRepository;
use Transip\Api\Library\Repository\Vps\IpAddressRepository as VpsIpAddressRepository;
Expand All @@ -57,7 +59,7 @@
class TransipAPI
{
public const TRANSIP_API_ENDPOINT = "https://api.transip.nl/v6";
public const TRANSIP_API_LIBRARY_VERSION = "6.5.0";
public const TRANSIP_API_LIBRARY_VERSION = "6.6.0";
public const TRANSIP_API_DEMO_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImN3MiFSbDU2eDNoUnkjelM4YmdOIn0.eyJpc3MiOiJhcGkudHJhbnNpcC5ubCIsImF1ZCI6ImFwaS50cmFuc2lwLm5sIiwianRpIjoiY3cyIVJsNTZ4M2hSeSN6UzhiZ04iLCJpYXQiOjE1ODIyMDE1NTAsIm5iZiI6MTU4MjIwMTU1MCwiZXhwIjoyMTE4NzQ1NTUwLCJjaWQiOiI2MDQ0OSIsInJvIjpmYWxzZSwiZ2siOmZhbHNlLCJrdiI6dHJ1ZX0.fYBWV4O5WPXxGuWG-vcrFWqmRHBm9yp0PHiYh_oAWxWxCaZX2Rf6WJfc13AxEeZ67-lY0TA2kSaOCp0PggBb_MGj73t4cH8gdwDJzANVxkiPL1Saqiw2NgZ3IHASJnisUWNnZp8HnrhLLe5ficvb1D9WOUOItmFC2ZgfGObNhlL2y-AMNLT4X7oNgrNTGm-mespo0jD_qH9dK5_evSzS3K8o03gu6p19jxfsnIh8TIVRvNdluYC2wo4qDl5EW5BEZ8OSuJ121ncOT1oRpzXB0cVZ9e5_UVAEr9X3f26_Eomg52-PjrgcRJ_jPIUYbrlo06KjjX2h0fzMr21ZE023Gw";

/**
Expand Down Expand Up @@ -271,6 +273,11 @@ public function vpsTCPMonitorContact(): MonitoringContactRepository
return new MonitoringContactRepository($this->httpClient);
}

public function vpsLicenses(): LicenseRepository
{
return new LicenseRepository($this->httpClient);
}

public function privateNetworks(): PrivateNetworkRepository
{
return new PrivateNetworkRepository($this->httpClient);
Expand Down

0 comments on commit d898c2c

Please sign in to comment.