Skip to content

Commit 9af076b

Browse files
authored
Added create method to SSHKeys
1 parent fd58477 commit 9af076b

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Models/SSHKeys/SSHKeys.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ class SSHKeys extends Model
2121
*/
2222
public $sshKeys;
2323

24+
/**
25+
* Creates a new SSH Key with the given name and public_key.
26+
*
27+
* @see https://docs.hetzner.cloud/#resources-ssh-keys-post
28+
* @param string $name
29+
* @param string $publicKey
30+
* @return \LKDev\HetznerCloud\Models\SSHKeys\SSHKey
31+
* @throws \LKDev\HetznerCloud\APIException
32+
*/
33+
public function create(
34+
string $name,
35+
string $publicKey
36+
): SSHKey {
37+
$response = $this->httpClient->post('ssh_keys', [
38+
'json' => [
39+
'name' => $name,
40+
'public_key' => $publicKey,
41+
],
42+
]);
43+
if (! HetznerAPIClient::hasError($response)) {
44+
return SSHKey::parse(json_decode((string) $response->getBody())->ssh_key);
45+
}
46+
}
47+
2448
/**
2549
* Returns all ssh key objects.
2650
*
@@ -69,4 +93,4 @@ public static function parse( $input)
6993
{
7094
return (new self())->setAdditionalData($input);
7195
}
72-
}
96+
}

0 commit comments

Comments
 (0)