Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Total Downloads](https://poser.pugx.org/lkdevelopment/hetzner-cloud-php-sdk/downloads)](https://packagist.org/packages/lkdevelopment/hetzner-cloud-php-sdk)
[![Actions Status](https://github.com/lkdevelopment/hetzner-cloud-php-sdk/workflows/CI/badge.svg)](https://github.com/lkdevelopment/hetzner-cloud-php-sdk/actions)
# Hetzner Cloud PHP SDK
A PHP SDK for the Hetzner Cloud API: https://docs.hetzner.cloud/
A PHP SDK for both [Hetzner Cloud API](https://docs.hetzner.cloud/reference/cloud) and [Hetzner API](https://docs.hetzner.cloud/reference/hetzner)
## Installation

You can install the package via composer:
Expand Down
24 changes: 24 additions & 0 deletions examples/storage_boxes/create_a_storage_box.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use LKDev\HetznerCloud\Models\StorageBoxes\StorageBoxAccessSettings;

require_once __DIR__.'/../bootstrap.php';

$serverType = $hetznerClient->serverTypes()->get(1);
$location = $hetznerClient->locations()->getByName('fsn1');
$type = $hetznerClient->storageBoxTypes()->get('bx11');

$response = $hetznerClient->storageBoxes()->create(
name: 'my-storage-box',
location: $location->name,
storageBoxType: $type->name,
password: '{my s3cr3t p@ssword}',
labels: ['type' => 'untest'],
accessSettings: new StorageBoxAccessSettings(reachable_externally:true),
);

$response->getResponsePart('action')->waitUntilCompleted();
$box = $response->getResponsePart('storage_box')->reload();

echo "Name: {$box->name}".PHP_EOL;
echo "ID: {$box->id}".PHP_EOL;
27 changes: 27 additions & 0 deletions examples/storage_boxes/create_a_storage_box_subaccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use LKDev\HetznerCloud\Models\StorageBoxes\StorageBoxAccessSettings;

require_once __DIR__.'/../bootstrap.php';

$box = $hetznerClient->storageBoxes()->get('some-existing-box');

$response = $box->createSubaccount(
'my_home_dir',
'MySecret!1234',
'some_name',
new StorageBoxAccessSettings(
reachable_externally: true,
ssh_enabled: true,
),
'Description',
[
'some' => 'label',
],
);
$response->getResponsePart('action')->waitUntilCompleted();
$account = $response->getResponsePart('subaccount')->reload();

echo "Name: {$account->name}".PHP_EOL;
echo "ID: {$account->id}".PHP_EOL;
echo "HomeDir: {$account->home_directory}".PHP_EOL;
7 changes: 7 additions & 0 deletions examples/storage_boxes/get_all_storage_box_types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require_once __DIR__.'/../bootstrap.php';

foreach ($hetznerClient->storageBoxTypes()->all() as $type) {
echo "Name: {$type->name} - ID: {$type->id}".PHP_EOL;
}
54 changes: 54 additions & 0 deletions src/HetznerAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use LKDev\HetznerCloud\Models\Servers\Servers;
use LKDev\HetznerCloud\Models\Servers\Types\ServerTypes;
use LKDev\HetznerCloud\Models\SSHKeys\SSHKeys;
use LKDev\HetznerCloud\Models\StorageBoxes\StorageBoxActions;
use LKDev\HetznerCloud\Models\StorageBoxes\StorageBoxes;
use LKDev\HetznerCloud\Models\StorageBoxTypes\StorageBoxTypes;
use LKDev\HetznerCloud\Models\Volumes\Volumes;
use LKDev\HetznerCloud\Models\Zones\Zones;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -63,6 +66,11 @@ class HetznerAPIClient
*/
protected GuzzleClient $httpClient;

/**
* @var \LKDev\HetznerCloud\Clients\GuzzleClient|null
*/
protected ?GuzzleClient $apiHetznerComClient = null;

/**
* @param string $apiToken
* @param string $baseUrl
Expand Down Expand Up @@ -141,6 +149,28 @@ public function setHttpClient(GuzzleClient $client): self
return $this;
}

/**
* @return GuzzleClient
*/
public function getApiHetznerComClient(): GuzzleClient
{
if ($this->apiHetznerComClient === null) {
$this->apiHetznerComClient = new GuzzleClient($this, ['base_uri' => 'https://api.hetzner.com/v1/']);
}

return $this->apiHetznerComClient;
}

/**
* @return $this
*/
public function setApiHetznerComClient(GuzzleClient $client): self
{
$this->apiHetznerComClient = $client;

return $this;
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
*
Expand Down Expand Up @@ -333,6 +363,30 @@ public function zones()
return new Zones($this->httpClient);
}

/**
* @return StorageBoxes
*/
public function storageBoxes(): StorageBoxes
{
return new StorageBoxes($this->getApiHetznerComClient());
}

/**
* @return StorageBoxTypes
*/
public function storageBoxTypes(): StorageBoxTypes
{
return new StorageBoxTypes($this->getApiHetznerComClient());
}

/**
* @return StorageBoxActions
*/
public function storageBoxActions(): StorageBoxActions
{
return new StorageBoxActions($this->getApiHetznerComClient());
}

/**
* @return GuzzleClient
*/
Expand Down
98 changes: 98 additions & 0 deletions src/Models/StorageBoxTypes/StorageBoxType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace LKDev\HetznerCloud\Models\StorageBoxTypes;

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

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

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

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

/**
* @var int|null
*/
public ?int $snapshot_limit;

/**
* @var int|null
*/
public ?int $automatic_snapshot_limit;

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

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

/**
* @var StorageBoxTypePrice[]
*/
public ?array $prices;

/**
* @var object|null
*/
public ?object $deprecation;

/**
* @param GuzzleClient|null $httpClient
*/
public function __construct(?GuzzleClient $httpClient = null)
{
$storageClient = $httpClient ?? (HetznerAPIClient::$instance ? HetznerAPIClient::$instance->getApiHetznerComClient() : null);
parent::__construct($storageClient);
}

/**
* @param $data
* @return $this
*/
public function setAdditionalData($data)
{
$this->id = $data->id;
$this->name = $data->name;
$this->description = $data->description;
$this->snapshot_limit = $data->snapshot_limit ?? null;
$this->automatic_snapshot_limit = $data->automatic_snapshot_limit ?? null;
$this->subaccounts_limit = $data->subaccounts_limit;
$this->size = $data->size;
$this->prices = isset($data->prices) ? array_map(function ($price) {
return StorageBoxTypePrice::parse($price);
}, $data->prices) : null;
$this->deprecation = $data->deprecation ?? null;

return $this;
}

/**
* @param $input
* @return static|null
*/
public static function parse($input)
{
if ($input == null) {
return;
}

return (new self())->setAdditionalData($input);
}
}
60 changes: 60 additions & 0 deletions src/Models/StorageBoxTypes/StorageBoxTypePrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace LKDev\HetznerCloud\Models\StorageBoxTypes;

use LKDev\HetznerCloud\Models\Prices\Price;

class StorageBoxTypePrice
{
/**
* @var string
*/
public string $location;

/**
* @var Price
*/
public ?Price $price_hourly;

/**
* @var Price
*/
public ?Price $price_monthly;

/**
* @var Price|null
*/
public ?Price $setup_fee;

/**
* @param string $location
* @param Price|null $priceHourly
* @param Price|null $priceMonthly
* @param Price|null $setupFee
*/
public function __construct(string $location, ?Price $priceHourly, ?Price $priceMonthly, ?Price $setupFee = null)
{
$this->location = $location;
$this->price_hourly = $priceHourly;
$this->price_monthly = $priceMonthly;
$this->setup_fee = $setupFee;
}

/**
* @param $input
* @return self|null
*/
public static function parse($input): ?self
{
if ($input == null) {
return null;
}

return new self(
$input->location ?? '',
Price::parse($input->price_hourly ?? null),
Price::parse($input->price_monthly ?? null),
Price::parse($input->setup_fee ?? null)
);
}
}
Loading
Loading