Skip to content

Commit 12c5c32

Browse files
committed
Add first batch of thests
1 parent 4b7fa9a commit 12c5c32

7 files changed

Lines changed: 460 additions & 35 deletions

File tree

hcloud/networks/client.py

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def delete(self):
4444
# type: () -> BoundAction
4545
"""Deletes a network.
4646
47-
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
47+
:return: boolean
4848
"""
4949
return self._client.delete(self)
5050

@@ -245,12 +245,12 @@ def delete(self, network):
245245
"""Deletes a network.
246246
247247
:param network: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`
248-
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
248+
:return: boolean
249249
"""
250-
response = self._client.request(
250+
self._client.request(
251251
url="/networks/{network_id}".format(network_id=network.id), method="DELETE"
252252
)
253-
return BoundAction(self._client.actions, response["action"])
253+
return True
254254

255255
def get_actions_list(
256256
self, network, status=None, sort=None, page=None, per_page=None
@@ -307,28 +307,97 @@ def get_actions(self, network, status=None, sort=None):
307307

308308
def add_subnet(self, network, subnet):
309309
# type: (Union[Network, BoundNetwork], NetworkSubnet) -> List[BoundAction]
310-
# TODO
311-
pass
310+
"""Adds a subnet entry to a network.
311+
312+
:param network: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`
313+
:param subnet: :class:`NetworkSubnet <hcloud.networks.domain.NetworkSubnet>`
314+
The NetworkSubnet you want to add to the Network
315+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
316+
"""
317+
data = {
318+
"type": subnet.type,
319+
"ip_range": subnet.ip_range,
320+
"network_zone": subnet.network_zone,
321+
}
312322

313-
def remove_subnet(self, network, subnet):
323+
response = self._client.request(
324+
url="/networks/{network_id}/actions/add_subnet".format(network_id=network.id),
325+
method="POST", json=data)
326+
return BoundAction(self._client.actions, response['action'])
327+
328+
def delete_subnet(self, network, subnet):
314329
# type: (Union[Network, BoundNetwork], NetworkSubnet) -> List[BoundAction]
315-
# TODO
316-
pass
330+
"""Removes a subnet entry from a network
331+
332+
:param network: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`
333+
:param subnet: :class:`NetworkSubnet <hcloud.networks.domain.NetworkSubnet>`
334+
The NetworkSubnet you want to remove from the Network
335+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
336+
"""
337+
data = {
338+
"ip_range": subnet.ip_range,
339+
}
340+
341+
response = self._client.request(
342+
url="/networks/{network_id}/actions/delete_subnet".format(network_id=network.id),
343+
method="POST", json=data)
344+
return BoundAction(self._client.actions, response['action'])
317345

318346
def add_route(self, network, route):
319347
# type: (Union[Network, BoundNetwork], NetworkRoute) -> List[BoundAction]
320-
# TODO
321-
pass
348+
"""Adds a route entry to a network.
349+
350+
:param network: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`
351+
:param route: :class:`NetworkRoute <hcloud.networks.domain.NetworkRoute>`
352+
The NetworkRoute you want to add to the Network
353+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
354+
"""
355+
data = {
356+
"destination": route.destination,
357+
"gateway": route.gateway,
358+
}
322359

323-
def remove_route(self, network, route):
360+
response = self._client.request(
361+
url="/networks/{network_id}/actions/add_route".format(network_id=network.id),
362+
method="POST", json=data)
363+
return BoundAction(self._client.actions, response['action'])
364+
365+
def delete_route(self, network, route):
324366
# type: (Union[Network, BoundNetwork], NetworkRoute) -> List[BoundAction]
325-
# TODO
326-
pass
367+
"""Removes a route entry to a network.
368+
369+
:param network: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`
370+
:param route: :class:`NetworkRoute <hcloud.networks.domain.NetworkRoute>`
371+
The NetworkRoute you want to remove from the Network
372+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
373+
"""
374+
data = {
375+
"destination": route.destination,
376+
"gateway": route.gateway,
377+
}
378+
379+
response = self._client.request(
380+
url="/networks/{network_id}/actions/delete_route".format(network_id=network.id),
381+
method="POST", json=data)
382+
return BoundAction(self._client.actions, response['action'])
327383

328384
def change_ip_range(self, network, ip_range):
329385
# type: (Union[Network, BoundNetwork], str) -> List[BoundAction]
330-
# TODO
331-
pass
386+
"""Changes the IP range of a network.
387+
388+
:param network: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>` or :class:`Network <hcloud.networks.domain.Network>`
389+
:param ip_range: str
390+
The new prefix for the whole network.
391+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
392+
"""
393+
data = {
394+
"ip_range": ip_range,
395+
}
396+
397+
response = self._client.request(
398+
url="/networks/{network_id}/actions/change_ip_range".format(network_id=network.id),
399+
method="POST", json=data)
400+
return BoundAction(self._client.actions, response['action'])
332401

333402
def change_protection(self, network, delete=None):
334403
# type: (Union[Network, BoundNetwork], Optional[bool]) -> BoundAction

hcloud/networks/domain.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from hcloud.core.domain import BaseDomain
3+
from hcloud.helpers.descriptors import ISODateTime
34

45

56
class Network(BaseDomain):
@@ -31,22 +32,27 @@ class Network(BaseDomain):
3132
"routes",
3233
"servers",
3334
"protection",
34-
"labels",
35+
"labels"
3536
)
3637

38+
created = ISODateTime()
39+
supported_fields = ("created",)
40+
3741
def __init__(
38-
self,
39-
id=None,
40-
name=None,
41-
ip_range=None,
42-
subnets=None,
43-
routes=None,
44-
servers=None,
45-
protection=None,
46-
labels=None,
42+
self,
43+
id=None,
44+
name=None,
45+
created=None,
46+
ip_range=None,
47+
subnets=None,
48+
routes=None,
49+
servers=None,
50+
protection=None,
51+
labels=None,
4752
):
4853
self.id = id
4954
self.name = name
55+
self.created = created
5056
self.ip_range = ip_range
5157
self.subnets = subnets
5258
self.routes = routes
@@ -56,12 +62,13 @@ def __init__(
5662

5763

5864
class NetworkSubnet(BaseDomain):
59-
__slots__ = ("type", "ip_range", "network_zone")
65+
__slots__ = ("type", "ip_range", "network_zone", "gateway")
6066

61-
def __init__(self, type=None, ip_range=None, network_zone=None):
67+
def __init__(self, type=None, ip_range=None, network_zone=None, gateway=None):
6268
self.type = type
6369
self.ip_range = ip_range
6470
self.network_zone = network_zone
71+
self.gateway = gateway
6572

6673

6774
class NetworkRoute(BaseDomain):
@@ -83,6 +90,10 @@ class CreateNetworkResponse(BaseDomain):
8390

8491
__slots__ = ("network", "action")
8592

86-
def __init__(self, network, action): # type: BoundNetwork # type: BoundAction
93+
def __init__(
94+
self,
95+
network, # type: BoundNetwork
96+
action # type: BoundAction
97+
):
8798
self.network = network
8899
self.action = action

tests/unit/actions/test_domain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
class TestAction(object):
88

99
def test_started_finished_is_datetime(self):
10-
server = Action(id=1, started="2016-01-30T23:50+00:00", finished="2016-03-30T23:50+00:00")
11-
assert server.started == datetime.datetime(2016, 1, 30, 23, 50, tzinfo=tzoffset(None, 0))
12-
assert server.finished == datetime.datetime(2016, 3, 30, 23, 50, tzinfo=tzoffset(None, 0))
10+
action = Action(id=1, started="2016-01-30T23:50+00:00", finished="2016-03-30T23:50+00:00")
11+
assert action.started == datetime.datetime(2016, 1, 30, 23, 50, tzinfo=tzoffset(None, 0))
12+
assert action.finished == datetime.datetime(2016, 3, 30, 23, 50, tzinfo=tzoffset(None, 0))

0 commit comments

Comments
 (0)