Skip to content

Commit bedda1f

Browse files
Implement endpoints & fields related to VPCs and non-interface networking
1 parent cf04ca6 commit bedda1f

17 files changed

+373
-36
lines changed

linode_api4/groups/networking.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
from typing import Any, Dict, Optional, Union
2+
13
from linode_api4.errors import UnexpectedResponseError
24
from linode_api4.groups import Group
35
from linode_api4.objects import (
46
VLAN,
57
Base,
68
Firewall,
9+
FirewallCreateDevicesOptions,
10+
FirewallSettings,
711
Instance,
812
IPAddress,
913
IPv6Pool,
1014
IPv6Range,
1115
NetworkTransferPrice,
1216
Region,
1317
)
18+
from linode_api4.objects.base import _flatten_request_body_recursive
19+
from linode_api4.util import drop_null_keys
1420

1521

1622
class NetworkingGroup(Group):
@@ -33,7 +39,15 @@ def firewalls(self, *filters):
3339
"""
3440
return self.client._get_and_filter(Firewall, *filters)
3541

36-
def firewall_create(self, label, rules, **kwargs):
42+
def firewall_create(
43+
self,
44+
label: str,
45+
rules: Dict[str, Any],
46+
devices: Optional[
47+
Union[FirewallCreateDevicesOptions, Dict[str, Any]]
48+
] = None,
49+
**kwargs,
50+
):
3751
"""
3852
Creates a new Firewall, either in the given Region or
3953
attached to the given Instance.
@@ -44,6 +58,8 @@ def firewall_create(self, label, rules, **kwargs):
4458
:type label: str
4559
:param rules: The rules to apply to the new Firewall. For more information on Firewall rules, see our `Firewalls Documentation`_.
4660
:type rules: dict
61+
:param devices: Represents devices to create created alongside a Linode Firewall.
62+
:type devices: Optional[Union[FirewallCreateDevicesOptions, Dict[str, Any]]]
4763
4864
:returns: The new Firewall.
4965
:rtype: Firewall
@@ -81,10 +97,14 @@ def firewall_create(self, label, rules, **kwargs):
8197
params = {
8298
"label": label,
8399
"rules": rules,
100+
"devices": devices,
84101
}
85102
params.update(kwargs)
86103

87-
result = self.client.post("/networking/firewalls", data=params)
104+
result = self.client.post(
105+
"/networking/firewalls",
106+
data=drop_null_keys(_flatten_request_body_recursive(params)),
107+
)
88108

89109
if not "id" in result:
90110
raise UnexpectedResponseError(
@@ -94,6 +114,25 @@ def firewall_create(self, label, rules, **kwargs):
94114
f = Firewall(self.client, result["id"], result)
95115
return f
96116

117+
def firewall_settings(self) -> FirewallSettings:
118+
"""
119+
Returns an object representing the Linode Firewall settings for the current user.
120+
121+
API Documentation: Not yet available.
122+
123+
:returns: An object representing the Linode Firewall settings for the current user.
124+
:rtype: FirewallSettings
125+
"""
126+
result = self.client.get("/networking/firewalls/settings")
127+
128+
if "default_firewall_ids" not in result:
129+
raise UnexpectedResponseError(
130+
"Unexpected response when getting firewall settings!",
131+
json=result,
132+
)
133+
134+
return FirewallSettings(self.client, None, result)
135+
97136
def ips(self, *filters):
98137
"""
99138
Returns a list of IP addresses on this account, excluding private addresses.

linode_api4/objects/networking.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from dataclasses import dataclass
2-
from typing import Optional
1+
from dataclasses import dataclass, field
2+
from typing import List, Optional
33

44
from linode_api4.common import Price, RegionPrice
55
from linode_api4.errors import UnexpectedResponseError
@@ -87,6 +87,7 @@ class IPAddress(Base):
8787
"public": Property(),
8888
"rdns": Property(mutable=True),
8989
"linode_id": Property(),
90+
"interface_id": Property(),
9091
"region": Property(slug_relationship=Region),
9192
"vpc_nat_1_1": Property(json_object=InstanceIPNAT1To1),
9293
}
@@ -99,6 +100,8 @@ def linode(self):
99100
self._set("_linode", Instance(self._client, self.linode_id))
100101
return self._linode
101102

103+
# TODO (Enhanced Interfaces): Add `interface` property method
104+
102105
def to(self, linode):
103106
"""
104107
This is a helper method for ip-assign, and should not be used outside
@@ -176,6 +179,47 @@ class VLAN(Base):
176179
}
177180

178181

182+
@dataclass
183+
class FirewallCreateDevicesOptions(JSONObject):
184+
"""
185+
Represents devices to create created alongside a Linode Firewall.
186+
"""
187+
188+
linodes: List[int] = field(default_factory=list)
189+
nodebalancers: List[int] = field(default_factory=list)
190+
interfaces: List[int] = field(default_factory=list)
191+
192+
193+
@dataclass
194+
class FirewallSettingsDefaultFirewallIDs(JSONObject):
195+
"""
196+
Contains the IDs of Linode Firewalls that should be used by default
197+
when creating various interface types.
198+
"""
199+
200+
vpc_interface: Optional[int] = None
201+
public_interface: Optional[int] = None
202+
linode: Optional[int] = None
203+
nodebalancer: Optional[int] = None
204+
205+
206+
class FirewallSettings(Base):
207+
"""
208+
Represents the Firewall settings for the current user.
209+
210+
API Documentation: Not yet available.
211+
"""
212+
213+
api_endpoint = "/networking/firewalls/settings"
214+
215+
properties = {
216+
"default_firewall_ids": Property(
217+
json_object=FirewallSettingsDefaultFirewallIDs,
218+
mutable=True,
219+
),
220+
}
221+
222+
179223
class FirewallDevice(DerivedBase):
180224
"""
181225
An object representing the assignment between a Linode Firewall and another Linode resource.

linode_api4/objects/vpc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
@dataclass
1212
class VPCSubnetLinodeInterface(JSONObject):
1313
id: int = 0
14+
config_id: Optional[int] = None
1415
active: bool = False
1516

1617

test/fixtures/networking_firewalls_123_devices.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@
1010
},
1111
"id": 123,
1212
"updated": "2018-01-02T00:01:01"
13+
},
14+
{
15+
"created": "2018-01-01T00:01:01",
16+
"entity": {
17+
"id": 123,
18+
"label": null,
19+
"type": "interface",
20+
"url": "/v4/linode/instances/123/interfaces/123"
21+
},
22+
"id": 456,
23+
"updated": "2018-01-02T00:01:01"
1324
}
1425
],
1526
"page": 1,
1627
"pages": 1,
17-
"results": 1
28+
"results": 2
1829
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"created": "2018-01-01T00:01:01",
3+
"entity": {
4+
"id": 123,
5+
"label": null,
6+
"type": "interface",
7+
"url": "/v4/linode/instances/123/interfaces/123"
8+
},
9+
"id": 456,
10+
"updated": "2018-01-02T00:01:01"
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"default_firewall_ids": {
3+
"vpc_interface": 123,
4+
"public_interface": 456,
5+
"linode": 789,
6+
"nodebalancer": 321
7+
}
8+
}

test/fixtures/networking_ips_127.0.0.1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"address": "127.0.0.1",
33
"gateway": "127.0.0.1",
44
"linode_id": 123,
5+
"interface_id": 456,
56
"prefix": 24,
67
"public": true,
78
"rdns": "test.example.org",

test/fixtures/regions.json

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"capabilities": [
77
"Linodes",
88
"NodeBalancers",
9-
"Block Storage"
9+
"Block Storage",
10+
"Linode Interfaces"
1011
],
1112
"status": "ok",
1213
"resolvers": {
@@ -26,7 +27,8 @@
2627
"capabilities": [
2728
"Linodes",
2829
"NodeBalancers",
29-
"Block Storage"
30+
"Block Storage",
31+
"Linode Interfaces"
3032
],
3133
"status": "ok",
3234
"resolvers": {
@@ -46,7 +48,8 @@
4648
"capabilities": [
4749
"Linodes",
4850
"NodeBalancers",
49-
"Block Storage"
51+
"Block Storage",
52+
"Linode Interfaces"
5053
],
5154
"status": "ok",
5255
"resolvers": {
@@ -62,7 +65,8 @@
6265
"capabilities": [
6366
"Linodes",
6467
"NodeBalancers",
65-
"Block Storage"
68+
"Block Storage",
69+
"Linode Interfaces"
6670
],
6771
"status": "ok",
6872
"resolvers": {
@@ -82,7 +86,8 @@
8286
"capabilities": [
8387
"Linodes",
8488
"NodeBalancers",
85-
"Block Storage"
89+
"Block Storage",
90+
"Linode Interfaces"
8691
],
8792
"status": "ok",
8893
"resolvers": {
@@ -102,7 +107,8 @@
102107
"capabilities": [
103108
"Linodes",
104109
"NodeBalancers",
105-
"Block Storage"
110+
"Block Storage",
111+
"Linode Interfaces"
106112
],
107113
"status": "ok",
108114
"resolvers": {
@@ -123,7 +129,8 @@
123129
"Linodes",
124130
"NodeBalancers",
125131
"Block Storage",
126-
"Object Storage"
132+
"Object Storage",
133+
"Linode Interfaces"
127134
],
128135
"status": "ok",
129136
"resolvers": {
@@ -143,7 +150,8 @@
143150
"capabilities": [
144151
"Linodes",
145152
"NodeBalancers",
146-
"Block Storage"
153+
"Block Storage",
154+
"Linode Interfaces"
147155
],
148156
"status": "ok",
149157
"resolvers": {
@@ -164,7 +172,8 @@
164172
"Linodes",
165173
"NodeBalancers",
166174
"Block Storage",
167-
"Object Storage"
175+
"Object Storage",
176+
"Linode Interfaces"
168177
],
169178
"status": "ok",
170179
"resolvers": {
@@ -185,7 +194,8 @@
185194
"Linodes",
186195
"NodeBalancers",
187196
"Block Storage",
188-
"Object Storage"
197+
"Object Storage",
198+
"Linode Interfaces"
189199
],
190200
"status": "ok",
191201
"resolvers": {
@@ -205,7 +215,8 @@
205215
"capabilities": [
206216
"Linodes",
207217
"NodeBalancers",
208-
"Block Storage"
218+
"Block Storage",
219+
"Linode Interfaces"
209220
],
210221
"status": "ok",
211222
"resolvers": {

test/fixtures/vpcs_123456_subnets.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"interfaces": [
1111
{
1212
"id": 678,
13-
"active": true
13+
"active": true,
14+
"config_id": null
1415
},
1516
{
1617
"id": 543,
17-
"active": false
18+
"active": false,
19+
"config_id": null
1820
}
1921
]
2022
}

test/fixtures/vpcs_123456_subnets_789.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"interfaces": [
99
{
1010
"id": 678,
11-
"active": true
11+
"active": true,
12+
"config_id": null
1213
},
1314
{
1415
"id": 543,
15-
"active": false
16+
"active": false,
17+
"config_id": null
1618
}
1719
]
1820
}

0 commit comments

Comments
 (0)