Skip to content

Commit 6d9a8bc

Browse files
Add support for NB frontend VPC IP and NB plan type (#660)
* Add support for NB frontend VPC IP and NB plan type * Fix test fixture and doc link * Fix NodeBalancerVPCConfig struct and address copilot suggestions * Fix doc links
1 parent bc5f735 commit 6d9a8bc

File tree

8 files changed

+298
-4
lines changed

8 files changed

+298
-4
lines changed

linode_api4/objects/nodebalancer.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,28 @@ def load_ssl_data(self, cert_file, key_file):
229229
self.ssl_key = f.read()
230230

231231

232+
class NodeBalancerVPCConfig(DerivedBase):
233+
"""
234+
The VPC configuration for this NodeBalancer.
235+
236+
API documentation: https://techdocs.akamai.com/linode-api/reference/get-node-balancer-vpc-config
237+
"""
238+
239+
api_endpoint = "/nodebalancers/{nodebalancer_id}/vpcs/{id}"
240+
derived_url_path = "vpcs"
241+
parent_id_name = "nodebalancer_id"
242+
243+
properties = {
244+
"id": Property(identifier=True),
245+
"nodebalancer_id": Property(identifier=True),
246+
"ipv4_range": Property(),
247+
"ipv6_range": Property(),
248+
"subnet_id": Property(),
249+
"vpc_id": Property(),
250+
"purpose": Property(),
251+
}
252+
253+
232254
class NodeBalancer(Base):
233255
"""
234256
A single NodeBalancer you can access.
@@ -253,6 +275,9 @@ class NodeBalancer(Base):
253275
"tags": Property(mutable=True, unordered=True),
254276
"client_udp_sess_throttle": Property(mutable=True),
255277
"locks": Property(unordered=True),
278+
"type": Property(),
279+
"frontend_address_type": Property(),
280+
"frontend_vpc_subnet_id": Property(),
256281
}
257282

258283
# create derived objects
@@ -356,3 +381,80 @@ def firewalls(self):
356381
Firewall(self._client, firewall["id"])
357382
for firewall in result["data"]
358383
]
384+
385+
def vpcs(self):
386+
"""
387+
View VPC information for VPCs associated with this NodeBalancer.
388+
389+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-node-balancer-vpcs
390+
391+
:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
392+
:rtype: List[NodeBalancerVPCConfig]
393+
"""
394+
result = self._client.get(
395+
"{}/vpcs".format(NodeBalancer.api_endpoint), model=self
396+
)
397+
398+
return [
399+
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
400+
for vpc in result["data"]
401+
]
402+
403+
def vpc(self, id):
404+
"""
405+
View VPC information for a VPC associated with this NodeBalancer.
406+
407+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-node-balancer-vpc-config
408+
409+
:param id: The ID of the NodeBalancer VPC Config to view.
410+
:type id: int
411+
412+
:returns: A NodeBalancerVPCConfig of the Linode NodeBalancer.
413+
:rtype: NodeBalancerVPCConfig
414+
"""
415+
result = self._client.get(
416+
"{}/vpcs/{}".format(
417+
NodeBalancer.api_endpoint, parse.quote(str(id))
418+
),
419+
model=self,
420+
)
421+
422+
return NodeBalancerVPCConfig(
423+
self._client, result["id"], self.id, json=result
424+
)
425+
426+
def backend_vpcs(self):
427+
"""
428+
View VPC information for backend VPCs associated with this NodeBalancer.
429+
430+
API Documentation: TODO
431+
432+
:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
433+
:rtype: List[NodeBalancerVPCConfig]
434+
"""
435+
result = self._client.get(
436+
"{}/backend_vpcs".format(NodeBalancer.api_endpoint), model=self
437+
)
438+
439+
return [
440+
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
441+
for vpc in result["data"]
442+
]
443+
444+
def frontend_vpcs(self):
445+
"""
446+
View VPC information for frontend VPCs associated with this NodeBalancer.
447+
448+
API Documentation: TODO
449+
450+
:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
451+
:rtype: List[NodeBalancerVPCConfig]
452+
"""
453+
result = self._client.get(
454+
"{}/frontend_vpcs".format(NodeBalancer.api_endpoint), model=self
455+
)
456+
457+
return [
458+
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
459+
for vpc in result["data"]
460+
]

test/fixtures/nodebalancers.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
"label": "balancer123456",
1212
"client_conn_throttle": 0,
1313
"tags": ["something"],
14-
"locks": ["cannot_delete_with_subresources"]
14+
"locks": ["cannot_delete_with_subresources"],
15+
"type": "common",
16+
"frontend_address_type": "vpc",
17+
"frontend_vpc_subnet_id": 5555
1518
},
1619
{
1720
"created": "2018-01-01T00:01:01",
@@ -24,10 +27,13 @@
2427
"label": "balancer123457",
2528
"client_conn_throttle": 0,
2629
"tags": [],
27-
"locks": []
30+
"locks": [],
31+
"type": "premium_40gb",
32+
"frontend_address_type": "vpc",
33+
"frontend_vpc_subnet_id": 6666
2834
}
2935
],
3036
"results": 2,
3137
"page": 1,
3238
"pages": 1
33-
}
39+
}

test/fixtures/nodebalancers_123456.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
],
1414
"locks": [
1515
"cannot_delete_with_subresources"
16-
]
16+
],
17+
"type": "common",
18+
"frontend_address_type": "vpc",
19+
"frontend_vpc_subnet_id": 5555
1720
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"data": [
3+
{
4+
"id": 101,
5+
"nodebalancer_id": 12345,
6+
"subnet_id": 6666,
7+
"vpc_id": 222,
8+
"ipv4_range": "10.200.1.0/24",
9+
"ipv6_range": "2001:db8:2::/64",
10+
"purpose": "backend"
11+
}
12+
],
13+
"page": 1,
14+
"pages": 1,
15+
"results": 1
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"data": [
3+
{
4+
"id": 99,
5+
"nodebalancer_id": 12345,
6+
"subnet_id": 5555,
7+
"vpc_id": 111,
8+
"ipv4_range": "10.100.5.0/24",
9+
"ipv6_range": "2001:db8::/64",
10+
"purpose": "frontend"
11+
}
12+
],
13+
"page": 1,
14+
"pages": 1,
15+
"results": 1
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"data": [
3+
{
4+
"id": 99,
5+
"nodebalancer_id": 12345,
6+
"subnet_id": 5555,
7+
"vpc_id": 111,
8+
"ipv4_range": "10.100.5.0/24",
9+
"ipv6_range": "2001:db8::/64",
10+
"purpose": "frontend"
11+
},
12+
{
13+
"id": 100,
14+
"nodebalancer_id": 12345,
15+
"subnet_id": 5556,
16+
"vpc_id": 112,
17+
"ipv4_range": "10.100.6.0/24",
18+
"ipv6_range": "2001:db8:1::/64",
19+
"purpose": "backend"
20+
}
21+
],
22+
"page": 1,
23+
"pages": 1,
24+
"results": 2
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 99,
3+
"nodebalancer_id": 12345,
4+
"subnet_id": 5555,
5+
"vpc_id": 111,
6+
"ipv4_range": "10.100.5.0/24",
7+
"ipv6_range": "2001:db8::/64",
8+
"purpose": "frontend"
9+
}

test/unit/objects/nodebalancers_test.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
NodeBalancer,
55
NodeBalancerConfig,
66
NodeBalancerNode,
7+
NodeBalancerVPCConfig,
78
)
89

910

@@ -266,3 +267,119 @@ def test_statistics(self):
266267
"linode.com - balancer12345 (12345) - day (5 min avg)",
267268
)
268269
self.assertEqual(m.call_url, statistics_url)
270+
271+
def test_list_nodebalancers(self):
272+
"""
273+
Test that you can list all NodeBalancers.
274+
"""
275+
nbs = self.client.nodebalancers()
276+
277+
self.assertEqual(len(nbs), 2)
278+
279+
self.assertEqual(nbs[0].id, 123456)
280+
self.assertEqual(nbs[0].label, "balancer123456")
281+
self.assertEqual(nbs[0].type, "common")
282+
self.assertEqual(nbs[0].frontend_address_type, "vpc")
283+
self.assertEqual(nbs[0].frontend_vpc_subnet_id, 5555)
284+
285+
self.assertEqual(nbs[1].id, 123457)
286+
self.assertEqual(nbs[1].label, "balancer123457")
287+
self.assertEqual(nbs[1].type, "premium_40gb")
288+
self.assertEqual(nbs[1].frontend_address_type, "vpc")
289+
self.assertEqual(nbs[1].frontend_vpc_subnet_id, 6666)
290+
291+
def test_get_nodebalancer(self):
292+
"""
293+
Test that you can get a single NodeBalancer by ID.
294+
"""
295+
nb = NodeBalancer(self.client, 123456)
296+
297+
self.assertEqual(nb.id, 123456)
298+
self.assertEqual(nb.label, "balancer123456")
299+
self.assertEqual(nb.type, "common")
300+
self.assertEqual(nb.frontend_address_type, "vpc")
301+
self.assertEqual(nb.frontend_vpc_subnet_id, 5555)
302+
303+
def test_vpcs(self):
304+
"""
305+
Test that you can list VPC configurations for a NodeBalancer.
306+
"""
307+
vpcs_url = "/nodebalancers/12345/vpcs"
308+
with self.mock_get(vpcs_url) as m:
309+
nb = NodeBalancer(self.client, 12345)
310+
result = nb.vpcs()
311+
312+
self.assertEqual(m.call_url, vpcs_url)
313+
self.assertEqual(len(result), 2)
314+
315+
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
316+
self.assertEqual(result[0].id, 99)
317+
self.assertEqual(result[0].subnet_id, 5555)
318+
self.assertEqual(result[0].vpc_id, 111)
319+
self.assertEqual(result[0].ipv4_range, "10.100.5.0/24")
320+
self.assertEqual(result[0].ipv6_range, "2001:db8::/64")
321+
self.assertEqual(result[0].purpose, "frontend")
322+
323+
self.assertIsInstance(result[1], NodeBalancerVPCConfig)
324+
self.assertEqual(result[1].id, 100)
325+
self.assertEqual(result[1].subnet_id, 5556)
326+
self.assertEqual(result[1].vpc_id, 112)
327+
self.assertEqual(result[1].ipv4_range, "10.100.6.0/24")
328+
self.assertEqual(result[1].ipv6_range, "2001:db8:1::/64")
329+
self.assertEqual(result[1].purpose, "backend")
330+
331+
def test_vpc(self):
332+
"""
333+
Test that you can get a single VPC configuration for a NodeBalancer.
334+
"""
335+
vpc_url = "/nodebalancers/12345/vpcs/99"
336+
with self.mock_get(vpc_url) as m:
337+
nb = NodeBalancer(self.client, 12345)
338+
result = nb.vpc(99)
339+
340+
self.assertEqual(m.call_url, vpc_url)
341+
self.assertIsInstance(result, NodeBalancerVPCConfig)
342+
self.assertEqual(result.id, 99)
343+
self.assertEqual(result.subnet_id, 5555)
344+
self.assertEqual(result.vpc_id, 111)
345+
self.assertEqual(result.ipv4_range, "10.100.5.0/24")
346+
self.assertEqual(result.ipv6_range, "2001:db8::/64")
347+
self.assertEqual(result.purpose, "frontend")
348+
349+
def test_backend_vpcs(self):
350+
"""
351+
Test that you can list backend VPC configurations for a NodeBalancer.
352+
"""
353+
backend_vpcs_url = "/nodebalancers/12345/backend_vpcs"
354+
with self.mock_get(backend_vpcs_url) as m:
355+
nb = NodeBalancer(self.client, 12345)
356+
result = nb.backend_vpcs()
357+
358+
self.assertEqual(m.call_url, backend_vpcs_url)
359+
self.assertEqual(len(result), 1)
360+
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
361+
self.assertEqual(result[0].id, 101)
362+
self.assertEqual(result[0].subnet_id, 6666)
363+
self.assertEqual(result[0].vpc_id, 222)
364+
self.assertEqual(result[0].ipv4_range, "10.200.1.0/24")
365+
self.assertEqual(result[0].ipv6_range, "2001:db8:2::/64")
366+
self.assertEqual(result[0].purpose, "backend")
367+
368+
def test_frontend_vpcs(self):
369+
"""
370+
Test that you can list frontend VPC configurations for a NodeBalancer.
371+
"""
372+
frontend_vpcs_url = "/nodebalancers/12345/frontend_vpcs"
373+
with self.mock_get(frontend_vpcs_url) as m:
374+
nb = NodeBalancer(self.client, 12345)
375+
result = nb.frontend_vpcs()
376+
377+
self.assertEqual(m.call_url, frontend_vpcs_url)
378+
self.assertEqual(len(result), 1)
379+
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
380+
self.assertEqual(result[0].id, 99)
381+
self.assertEqual(result[0].subnet_id, 5555)
382+
self.assertEqual(result[0].vpc_id, 111)
383+
self.assertEqual(result[0].ipv4_range, "10.100.5.0/24")
384+
self.assertEqual(result[0].ipv6_range, "2001:db8::/64")
385+
self.assertEqual(result[0].purpose, "frontend")

0 commit comments

Comments
 (0)