Skip to content

Commit cd7b2a3

Browse files
authored
Merge pull request #7 from roaldnefs/add-ssh-keys-service
Add SshKeyService to list, retrieve and delete SSH-keys
2 parents 236fd43 + 8fcc70f commit cd7b2a3

8 files changed

Lines changed: 156 additions & 8 deletions

File tree

requirements/tests.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mypy
22
pytest
3-
flake8
3+
flake8
4+
responses

tests/services/__init__.py

Whitespace-only changes.

tests/services/test_ssh_keys.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2020 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from typing import Type, List
21+
import responses # type: ignore
22+
23+
from transip import TransIP
24+
from transip.v6.objects import SshKey
25+
26+
27+
@responses.activate
28+
def test_ssh_keys_list(transip_minimal_client: Type[TransIP]) -> None:
29+
responses.add(
30+
responses.GET,
31+
"https://api.transip.nl/v6/ssh-keys",
32+
json={
33+
"sshKeys": [{
34+
"id": 123,
35+
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf2pxWX/yhUBDyk2LPhvRtI0LnVO8PyR5Zt6AHrnhtLGqK+8YG9EMlWbCCWrASR+Q1hFQG example",
36+
"description": "Jim key",
37+
"creationDate": "2020-12-01 15:25:01",
38+
"fingerprint": "bb:22:43:69:2b:0d:3e:16:58:91:27:8a:62:29:97:d1"
39+
}]
40+
},
41+
status=200,
42+
)
43+
44+
ssh_keys: List[Type[SshKey]] = transip_minimal_client.ssh_keys.list()
45+
ssh_key: Type[SshKey] = ssh_keys[0]
46+
assert len(ssh_keys) == 1
47+
assert ssh_key.get_id() == 123 # type: ignore
48+
49+
50+
@responses.activate
51+
def test_ssh_keys_get(transip_minimal_client: Type[TransIP]) -> None:
52+
responses.add(
53+
responses.GET,
54+
"https://api.transip.nl/v6/ssh-keys/123",
55+
json={
56+
"sshKey": {
57+
"id": 123,
58+
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf2pxWX/yhUBDyk2LPhvRtI0LnVO8PyR5Zt6AHrnhtLGqK+8YG9EMlWbCCWrASR+Q1hFQG example",
59+
"description": "Jim key",
60+
"creationDate": "2020-12-01 15:25:01",
61+
"fingerprint": "bb:22:43:69:2b:0d:3e:16:58:91:27:8a:62:29:97:d1"
62+
}
63+
},
64+
status=200,
65+
)
66+
67+
ssh_key_id: int = 123
68+
ssh_key: Type[SshKey] = transip_minimal_client.ssh_keys.get(ssh_key_id)
69+
assert ssh_key.get_id() == 123 # type: ignore
70+
71+
72+
@responses.activate
73+
def test_ssh_keys_delete(transip_minimal_client: Type[TransIP]) -> None:
74+
responses.add(
75+
responses.DELETE,
76+
"https://api.transip.nl/v6/ssh-keys/123",
77+
status=204,
78+
)
79+
80+
ssh_key_id: int = 123
81+
try:
82+
transip_minimal_client.ssh_keys.delete(ssh_key_id)
83+
except Exception as exc:
84+
assert False, f"'transip.TransIP.ssh_keys.delete' raised an exception {exc}"

transip/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(
6868
services.AvailabilityZoneService(self) # type: ignore
6969
)
7070
self.domains: Type[Any] = services.DomainService(self) # type: ignore
71+
self.ssh_keys: Type[Any] = services.SshKeyService(self) # type: ignore
7172
self.vpss: Type[Any] = services.VpsService(self) # type: ignore
7273

7374
@property
@@ -171,12 +172,14 @@ def request(
171172
method, path, data=data, json=json, params=params
172173
)
173174

174-
try:
175-
return response.json()
176-
except Exception:
177-
raise TransIPParsingError(
178-
message="Failed to parse the API response as JSON"
179-
)
175+
if response.text:
176+
try:
177+
return response.json()
178+
except Exception:
179+
raise TransIPParsingError(
180+
message="Failed to parse the API response as JSON"
181+
)
182+
return None
180183

181184
def get(
182185
self,
@@ -292,5 +295,5 @@ def delete(
292295
TransIPHTTPError: When the return code of the request is not 2xx
293296
"""
294297
return self.request(
295-
"GET", path, params=params
298+
"DELETE", path, params=params
296299
)

transip/v6/objects/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919

2020
from transip.v6.objects.availability_zone import AvailabilityZone # noqa: 401
2121
from transip.v6.objects.domain import Domain # noqa: 401
22+
from transip.v6.objects.ssh_key import SshKey # noqa: 401
2223
from transip.v6.objects.vps import Vps # noqa: 401

transip/v6/objects/ssh_key.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2020 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from transip.base import ApiObject
21+
22+
23+
class SshKey(ApiObject):
24+
25+
_id_attr: str = "id"

transip/v6/services/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919

2020
from transip.v6.services.availability_zone import AvailabilityZoneService # noqa: 401
2121
from transip.v6.services.domain import DomainService # noqa: 401
22+
from transip.v6.services.ssh_key import SshKeyService # noqa: 401
2223
from transip.v6.services.vps import VpsService # noqa: 401

transip/v6/services/ssh_key.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2020 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from typing import Optional, Type
21+
22+
from transip.base import ApiService, ApiObject
23+
from transip.mixins import GetMixin, DeleteMixin, ListMixin
24+
from transip.v6.objects.ssh_key import SshKey
25+
26+
27+
class SshKeyService(GetMixin, DeleteMixin, ListMixin, ApiService):
28+
29+
_path: str = "/ssh-keys"
30+
_obj_cls: Optional[Type[ApiObject]] = SshKey
31+
32+
_resp_list_attr: str = "sshKeys"
33+
_resp_get_attr: str = "sshKey"

0 commit comments

Comments
 (0)