1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Api; |
13
|
|
|
|
14
|
|
|
use DigitalOceanV2\Entity\Key as KeyEntity; |
15
|
|
|
use DigitalOceanV2\Exception\HttpException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Antoine Corcy <[email protected]> |
19
|
|
|
* @author Graham Campbell <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Key extends AbstractApi |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @return KeyEntity[] |
25
|
|
|
*/ |
26
|
|
|
public function getAll() |
27
|
|
|
{ |
28
|
|
|
$keys = $this->adapter->get(sprintf('%s/account/keys?per_page=%d', $this->endpoint, 200)); |
29
|
|
|
|
30
|
|
|
$keys = json_decode($keys); |
31
|
|
|
|
32
|
|
|
$this->extractMeta($keys); |
33
|
|
|
|
34
|
|
|
return array_map(function ($key) { |
35
|
|
|
return new KeyEntity($key); |
36
|
|
|
}, $keys->ssh_keys); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param int $id |
41
|
|
|
* |
42
|
|
|
* @return KeyEntity |
43
|
|
|
*/ |
44
|
|
|
public function getById($id) |
45
|
|
|
{ |
46
|
|
|
$key = $this->adapter->get(sprintf('%s/account/keys/%d', $this->endpoint, $id)); |
47
|
|
|
|
48
|
|
|
$key = json_decode($key); |
49
|
|
|
|
50
|
|
|
return new KeyEntity($key->ssh_key); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $fingerprint |
55
|
|
|
* |
56
|
|
|
* @return KeyEntity |
57
|
|
|
*/ |
58
|
|
|
public function getByFingerprint($fingerprint) |
59
|
|
|
{ |
60
|
|
|
$key = $this->adapter->get(sprintf('%s/account/keys/%s', $this->endpoint, $fingerprint)); |
61
|
|
|
|
62
|
|
|
$key = json_decode($key); |
63
|
|
|
|
64
|
|
|
return new KeyEntity($key->ssh_key); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @param string $publicKey |
70
|
|
|
* |
71
|
|
|
* @throws HttpException |
72
|
|
|
* |
73
|
|
|
* @return KeyEntity |
74
|
|
|
*/ |
75
|
|
|
public function create($name, $publicKey) |
76
|
|
|
{ |
77
|
|
|
$key = $this->adapter->post(sprintf('%s/account/keys', $this->endpoint), ['name' => $name, 'public_key' => $publicKey]); |
78
|
|
|
|
79
|
|
|
$key = json_decode($key); |
80
|
|
|
|
81
|
|
|
return new KeyEntity($key->ssh_key); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $id |
86
|
|
|
* @param string $name |
87
|
|
|
* |
88
|
|
|
* @throws HttpException |
89
|
|
|
* |
90
|
|
|
* @return KeyEntity |
91
|
|
|
*/ |
92
|
|
|
public function update($id, $name) |
93
|
|
|
{ |
94
|
|
|
$key = $this->adapter->put(sprintf('%s/account/keys/%s', $this->endpoint, $id), ['name' => $name]); |
95
|
|
|
|
96
|
|
|
$key = json_decode($key); |
97
|
|
|
|
98
|
|
|
return new KeyEntity($key->ssh_key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $id |
103
|
|
|
* |
104
|
|
|
* @throws HttpException |
105
|
|
|
*/ |
106
|
|
|
public function delete($id) |
107
|
|
|
{ |
108
|
|
|
$this->adapter->delete(sprintf('%s/account/keys/%s', $this->endpoint, $id)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|