Skip to content

Commit d59ddd0

Browse files
authored
Add certificates support (#61)
Signed-off-by: Lukas Kämmerling <[email protected]>
1 parent f125672 commit d59ddd0

File tree

8 files changed

+505
-1
lines changed

8 files changed

+505
-1
lines changed

.github/workflows/php.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ jobs:
1717
php-version: ${{ matrix.php-versions }}
1818
- name: Install depdendencies
1919
run: |
20-
composer global require hirak/prestissimo
2120
composer install
2221
- name: Run phpunit
2322
run: php vendor/bin/phpunit

src/HetznerAPIClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GuzzleHttp\Client;
66
use LKDev\HetznerCloud\Clients\GuzzleClient;
77
use LKDev\HetznerCloud\Models\Actions\Actions;
8+
use LKDev\HetznerCloud\Models\Certificates\Certificates;
89
use LKDev\HetznerCloud\Models\Datacenters\Datacenters;
910
use LKDev\HetznerCloud\Models\FloatingIps\FloatingIps;
1011
use LKDev\HetznerCloud\Models\Images\Images;
@@ -17,6 +18,9 @@
1718
use LKDev\HetznerCloud\Models\Volumes\Volumes;
1819
use Psr\Http\Message\ResponseInterface;
1920

21+
/**
22+
* Class HetznerAPIClient.
23+
*/
2024
class HetznerAPIClient
2125
{
2226
/**
@@ -267,6 +271,14 @@ public function networks()
267271
return new Networks($this->httpClient);
268272
}
269273

274+
/**
275+
* @return Certificates
276+
*/
277+
public function certificates()
278+
{
279+
return new Certificates($this->httpClient);
280+
}
281+
270282
/**
271283
* @return GuzzleClient
272284
*/
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lukaskammerling
5+
* Date: 28.01.18
6+
* Time: 21:00.
7+
*/
8+
9+
namespace LKDev\HetznerCloud\Models\Certificates;
10+
11+
use LKDev\HetznerCloud\HetznerAPIClient;
12+
use LKDev\HetznerCloud\Models\Contracts\Resource;
13+
use LKDev\HetznerCloud\Models\Model;
14+
15+
class Certificate extends Model implements Resource
16+
{
17+
/**
18+
* @var int
19+
*/
20+
public $id;
21+
22+
/**
23+
* @var string
24+
*/
25+
public $name;
26+
27+
/**
28+
* @var string
29+
*/
30+
public $certificate;
31+
/**
32+
* @var string
33+
*/
34+
public $created;
35+
/**
36+
* @var string
37+
*/
38+
public $not_valid_before;
39+
/**
40+
* @var string
41+
*/
42+
public $not_valid_after;
43+
/**
44+
* @var array
45+
*/
46+
public $domain_names;
47+
/**
48+
* @var string
49+
*/
50+
public $fingerprint;
51+
/**
52+
* @var \stdClass
53+
*/
54+
public $used_by;
55+
/**
56+
* @var array
57+
*/
58+
public $labels;
59+
60+
/**
61+
* Certificate constructor.
62+
* @param int $id
63+
* @param string|null $name
64+
* @param string|null $certificate
65+
* @param string|null $created
66+
* @param string|null $not_valid_before
67+
* @param string|null $not_valid_after
68+
* @param array|null $domain_names
69+
* @param string|null $fingerprint
70+
* @param array|null $used_by
71+
* @param array|null $labels
72+
*/
73+
public function __construct(int $id, string $name = null, string $certificate = null, string $created = null, string $not_valid_before = null, string $not_valid_after = null, array $domain_names = null, string $fingerprint = null, $used_by = null, $labels = [])
74+
{
75+
$this->id = $id;
76+
$this->name = $name;
77+
$this->certificate = $certificate;
78+
$this->created = $created;
79+
$this->not_valid_before = $not_valid_before;
80+
$this->not_valid_after = $not_valid_after;
81+
$this->domain_names = $domain_names;
82+
$this->fingerprint = $fingerprint;
83+
$this->used_by = $used_by;
84+
$this->labels = $labels;
85+
86+
parent::__construct();
87+
}
88+
89+
/**
90+
* Update a ssh key.
91+
*
92+
* @see https://docs.hetzner.cloud/#resources-certificates-put
93+
* @param array $data
94+
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate|null
95+
* @throws \LKDev\HetznerCloud\APIException
96+
*/
97+
public function update(array $data): ?self
98+
{
99+
$response = $this->httpClient->put('certificates/'.$this->id, [
100+
'json' => $data,
101+
102+
]);
103+
if (! HetznerAPIClient::hasError($response)) {
104+
return self::parse(json_decode((string) $response->getBody())->certificate);
105+
}
106+
107+
return null;
108+
}
109+
110+
/**
111+
* Deletes a SSH key. It cannot be used anymore.
112+
*
113+
* @see https://docs.hetzner.cloud/#resources-certificates-delete
114+
* @return bool
115+
* @throws \LKDev\HetznerCloud\APIException
116+
*/
117+
public function delete(): bool
118+
{
119+
$response = $this->httpClient->delete('certificates/'.$this->id);
120+
if (! HetznerAPIClient::hasError($response)) {
121+
return true;
122+
}
123+
124+
return false;
125+
}
126+
127+
/**
128+
* @param $input
129+
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate|static
130+
*/
131+
public static function parse($input)
132+
{
133+
return new self($input->id, $input->name, $input->certificate, $input->created, $input->not_valid_before, $input->not_valid_after, $input->domain_names, $input->fingerprint, $input->used_by, $input->labels);
134+
}
135+
136+
/**
137+
* Reload the data of the SSH Key.
138+
*
139+
* @return Certificate
140+
* @throws \LKDev\HetznerCloud\APIException
141+
*/
142+
public function reload()
143+
{
144+
return HetznerAPIClient::$instance->certificates()->get($this->id);
145+
}
146+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lukaskammerling
5+
* Date: 2019-03-28
6+
* Time: 13:51.
7+
*/
8+
9+
namespace LKDev\HetznerCloud\Models\Certificates;
10+
11+
use LKDev\HetznerCloud\RequestOpts;
12+
13+
class CertificateRequestOpts extends RequestOpts
14+
{
15+
/**
16+
* @var string
17+
*/
18+
public $name;
19+
20+
/**
21+
* RequestOpts constructor.
22+
*
23+
* @param $name
24+
* @param $perPage
25+
* @param $page
26+
* @param $labelSelector
27+
*/
28+
public function __construct(string $name = null, int $perPage = null, int $page = null, string $labelSelector = null)
29+
{
30+
parent::__construct($perPage, $page, $labelSelector);
31+
$this->name = $name;
32+
}
33+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lukaskammerling
5+
* Date: 28.01.18
6+
* Time: 21:00.
7+
*/
8+
9+
namespace LKDev\HetznerCloud\Models\Certificates;
10+
11+
use LKDev\HetznerCloud\APIResponse;
12+
use LKDev\HetznerCloud\HetznerAPIClient;
13+
use LKDev\HetznerCloud\Models\Contracts\Resources;
14+
use LKDev\HetznerCloud\Models\Meta;
15+
use LKDev\HetznerCloud\Models\Model;
16+
use LKDev\HetznerCloud\RequestOpts;
17+
use LKDev\HetznerCloud\Traits\GetFunctionTrait;
18+
19+
class Certificates extends Model implements Resources
20+
{
21+
use GetFunctionTrait;
22+
23+
/**
24+
* @var array
25+
*/
26+
protected $certificates;
27+
28+
/**
29+
* Creates a new SSH Key with the given name and certificate.
30+
*
31+
* @see https://docs.hetzner.cloud/#certificates-create-a-certificate
32+
* @param string $name
33+
* @param string $certificate
34+
* @param string $privateKey
35+
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate
36+
* @throws \LKDev\HetznerCloud\APIException
37+
*/
38+
public function create(
39+
string $name,
40+
string $certificate,
41+
string $privateKey
42+
): ?Certificate {
43+
$response = $this->httpClient->post('certificates', [
44+
'json' => [
45+
'name' => $name,
46+
'certificate' => $certificate,
47+
'private_key' => $privateKey,
48+
],
49+
]);
50+
if (! HetznerAPIClient::hasError($response)) {
51+
return Certificate::parse(json_decode((string) $response->getBody())->certificate);
52+
}
53+
54+
return null;
55+
}
56+
57+
/**
58+
* Returns all certificate objects.
59+
*
60+
* @see https://docs.hetzner.cloud/#certificates-get-all-certificates
61+
* @param RequestOpts $requestOpts
62+
* @return array
63+
* @throws \LKDev\HetznerCloud\APIException
64+
*/
65+
public function all(RequestOpts $requestOpts = null): array
66+
{
67+
if ($requestOpts == null) {
68+
$requestOpts = new RequestOpts();
69+
}
70+
71+
return $this->_all($requestOpts);
72+
}
73+
74+
/**
75+
* Returns all certificate objects.
76+
*
77+
* @see https://docs.hetzner.cloud/#certificates-get-all-certificates
78+
* @param RequestOpts $requestOpts
79+
* @return APIResponse
80+
* @throws \LKDev\HetznerCloud\APIException
81+
*/
82+
public function list(RequestOpts $requestOpts = null): ?APIResponse
83+
{
84+
if ($requestOpts == null) {
85+
$requestOpts = new RequestOpts();
86+
}
87+
$response = $this->httpClient->get('certificates'.$requestOpts->buildQuery());
88+
if (! HetznerAPIClient::hasError($response)) {
89+
$resp = json_decode((string) $response->getBody());
90+
91+
return APIResponse::create([
92+
'meta' => Meta::parse($resp->meta),
93+
$this->_getKeys()['many'] => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']},
94+
], $response->getHeaders());
95+
}
96+
97+
return null;
98+
}
99+
100+
/**
101+
* @param $input
102+
* @return $this
103+
*/
104+
public function setAdditionalData($input)
105+
{
106+
$this->certificates = collect($input)->map(function ($certificate, $key) {
107+
return Certificate::parse($certificate);
108+
})->toArray();
109+
110+
return $this;
111+
}
112+
113+
/**
114+
* @param $input
115+
* @return $this|static
116+
*/
117+
public static function parse($input)
118+
{
119+
return (new self())->setAdditionalData($input);
120+
}
121+
122+
/**
123+
* Returns a specific certificate object.
124+
*
125+
* @see https://docs.hetzner.cloud/#certificates-get-a-certificate
126+
* @param int $id
127+
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate
128+
* @throws \LKDev\HetznerCloud\APIException
129+
*/
130+
public function getById(int $id)
131+
{
132+
$response = $this->httpClient->get('certificates/'.$id);
133+
if (! HetznerAPIClient::hasError($response)) {
134+
return Certificate::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']});
135+
}
136+
}
137+
138+
/**
139+
* Returns a specific certificate object.
140+
*
141+
* @see https://docs.hetzner.cloud/#certificates-get-a-certificate
142+
* @param string $name
143+
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate
144+
* @throws \LKDev\HetznerCloud\APIException
145+
*/
146+
public function getByName(string $name): ?Certificate
147+
{
148+
$certificates = $this->list(new CertificateRequestOpts($name));
149+
150+
return (count($certificates->{$this->_getKeys()['many']}) > 0) ? $certificates->{$this->_getKeys()['many']}[0] : null;
151+
}
152+
153+
/**
154+
* @return array
155+
*/
156+
public function _getKeys(): array
157+
{
158+
return ['one' => 'certificate', 'many' => 'certificates'];
159+
}
160+
}

0 commit comments

Comments
 (0)