-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path_ssh_keys.py
More file actions
109 lines (85 loc) · 2.8 KB
/
_ssh_keys.py
File metadata and controls
109 lines (85 loc) · 2.8 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
SSHKEYS_ENDPOINT = '/sshkeys'
class SSHKey:
"""An SSH key model class."""
def __init__(self, id: str, name: str, public_key: str) -> None:
"""Initialize a new SSH key object.
:param id: SSH key id
:type id: str
:param name: SSH key name
:type name: str
:param public_key: SSH key public key
:type public_key: str
"""
self._id = id
self._name = name
self._public_key = public_key
@property
def id(self) -> str:
"""Get the SSH key id.
:return: SSH key id
:rtype: str
"""
return self._id
@property
def name(self) -> str:
"""Get the SSH key name.
:return: SSH key name
:rtype: str
"""
return self._name
@property
def public_key(self) -> str:
"""Get the SSH key public key value.
:return: public SSH key
:rtype: str
"""
return self._public_key
class SSHKeysService:
"""A service for interacting with the SSH keys endpoint."""
def __init__(self, http_client) -> None:
self._http_client = http_client
def get(self) -> list[SSHKey]:
"""Get all of the client's SSH keys.
:return: list of SSH keys objects
:rtype: list[SSHKey]
"""
keys = self._http_client.get(SSHKEYS_ENDPOINT).json()
keys_object_list = [SSHKey(key['id'], key['name'], key['key']) for key in keys]
return keys_object_list
def get_by_id(self, id: str) -> SSHKey:
"""Get a specific SSH key by id.
:param id: SSH key id
:type id: str
:return: SSHKey object
:rtype: SSHKey
"""
key_dict = self._http_client.get(SSHKEYS_ENDPOINT + f'/{id}').json()[0]
key_object = SSHKey(key_dict['id'], key_dict['name'], key_dict['key'])
return key_object
def delete(self, id_list: list[str]) -> None:
"""Delete multiple SSH keys by id.
:param id_list: list of SSH keys ids
:type id_list: list[str]
"""
payload = {'keys': id_list}
self._http_client.delete(SSHKEYS_ENDPOINT, json=payload)
return
def delete_by_id(self, id: str) -> None:
"""Delete a single SSH key by id.
:param id: SSH key id
:type id: str
"""
self._http_client.delete(SSHKEYS_ENDPOINT + f'/{id}')
return
def create(self, name: str, key: str) -> SSHKey:
"""Create a new SSH key.
:param name: SSH key name
:type name: str
:param key: public SSH key value
:type key: str
:return: new SSH key object
:rtype: SSHKey
"""
payload = {'name': name, 'key': key}
id = self._http_client.post(SSHKEYS_ENDPOINT, json=payload).text
return SSHKey(id, name, key)