-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathCertificates.php
More file actions
185 lines (165 loc) · 4.98 KB
/
Certificates.php
File metadata and controls
185 lines (165 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Created by PhpStorm.
* User: lukaskammerling
* Date: 28.01.18
* Time: 21:00.
*/
namespace LKDev\HetznerCloud\Models\Certificates;
use LKDev\HetznerCloud\APIResponse;
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Contracts\Resources;
use LKDev\HetznerCloud\Models\Meta;
use LKDev\HetznerCloud\Models\Model;
use LKDev\HetznerCloud\RequestOpts;
use LKDev\HetznerCloud\Traits\GetFunctionTrait;
class Certificates extends Model implements Resources
{
use GetFunctionTrait;
/**
* @var array
*/
protected $certificates;
/**
* Creates a new SSH Key with the given name and certificate.
*
* @see https://docs.hetzner.cloud/#certificates-create-a-certificate
*
* @param string $name
* @param string $certificate
* @param string $privateKey
* @param array $labels
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function create(
string $name,
?string $certificate = null,
?string $privateKey = null,
array $labels = [],
string $type = 'uploaded'
): ?Certificate {
$parameters = [
'name' => $name,
'type' => $type,
];
if ($certificate !== null) {
$parameters['certificate'] = $certificate;
}
if ($privateKey !== null) {
$parameters['private_key'] = $privateKey;
}
if (! empty($labels)) {
$parameters['labels'] = $labels;
}
$response = $this->httpClient->post('certificates', [
'json' => $parameters,
]);
if (! HetznerAPIClient::hasError($response)) {
return Certificate::parse(json_decode((string) $response->getBody())->certificate);
}
return null;
}
/**
* Returns all certificate objects.
*
* @see https://docs.hetzner.cloud/#certificates-get-all-certificates
*
* @param RequestOpts $requestOpts
* @return array
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function all(?RequestOpts $requestOpts = null): array
{
if ($requestOpts == null) {
$requestOpts = new RequestOpts();
}
return $this->_all($requestOpts);
}
/**
* Returns all certificate objects.
*
* @see https://docs.hetzner.cloud/#certificates-get-all-certificates
*
* @param RequestOpts $requestOpts
* @return APIResponse
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function list(?RequestOpts $requestOpts = null): ?APIResponse
{
if ($requestOpts == null) {
$requestOpts = new RequestOpts();
}
$response = $this->httpClient->get('certificates'.$requestOpts->buildQuery());
if (! HetznerAPIClient::hasError($response)) {
$resp = json_decode((string) $response->getBody());
return APIResponse::create([
'meta' => Meta::parse($resp->meta),
$this->_getKeys()['many'] => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']},
], $response->getHeaders());
}
return null;
}
/**
* @param $input
* @return $this
*/
public function setAdditionalData($input)
{
$this->certificates = array_map(function ($certificate) {
return Certificate::parse($certificate);
}, $input);
return $this;
}
/**
* @param $input
* @return $this|static
*/
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
/**
* Returns a specific certificate object.
*
* @see https://docs.hetzner.cloud/#certificates-get-a-certificate
*
* @param int $id
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function getById(int $id)
{
$response = $this->httpClient->get('certificates/'.$id);
if (! HetznerAPIClient::hasError($response)) {
return Certificate::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']});
}
return null;
}
/**
* Returns a specific certificate object.
*
* @see https://docs.hetzner.cloud/#certificates-get-a-certificate
*
* @param string $name
* @return \LKDev\HetznerCloud\Models\Certificates\Certificate
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function getByName(string $name): ?Certificate
{
$certificates = $this->list(new CertificateRequestOpts($name));
return (count($certificates->{$this->_getKeys()['many']}) > 0) ? $certificates->{$this->_getKeys()['many']}[0] : null;
}
/**
* @return array
*/
public function _getKeys(): array
{
return ['one' => 'certificate', 'many' => 'certificates'];
}
}