Skip to content

Commit d2bddc7

Browse files
Add support for NB frontend VPC IP and NB plan type
1 parent 362abdc commit d2bddc7

8 files changed

Lines changed: 290 additions & 4 deletions

linode_api4/objects/nodebalancer.py

Lines changed: 99 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(mutable=True),
247+
"ipv4_range_auto_assign": Property(mutable=True),
248+
"subnet_id": Property(mutable=True),
249+
"vpc_id": Property(mutable=True),
250+
"purpose": Property(mutable=True),
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,77 @@ 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://www.linode.com/docs/api/nodebalancers/#nodebalancer-vpcs-view
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(NodeBalancer.api_endpoint, id), model=self
417+
)
418+
419+
return NodeBalancerVPCConfig(
420+
self._client, result["id"], self.id, json=result
421+
)
422+
423+
def backend_vpcs(self):
424+
"""
425+
View VPC information for backend VPCs associated with this NodeBalancer.
426+
427+
API Documentation: TODO
428+
429+
:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
430+
:rtype: List[NodeBalancerVPCConfig]
431+
"""
432+
result = self._client.get(
433+
"{}/backend_vpcs".format(NodeBalancer.api_endpoint), model=self
434+
)
435+
436+
return [
437+
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
438+
for vpc in result["data"]
439+
]
440+
441+
def frontend_vpcs(self):
442+
"""
443+
View VPC information for frontend VPCs associated with this NodeBalancer.
444+
445+
API Documentation: TODO
446+
447+
:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
448+
:rtype: List[NodeBalancerVPCConfig]
449+
"""
450+
result = self._client.get(
451+
"{}/frontend_vpcs".format(NodeBalancer.api_endpoint), model=self
452+
)
453+
454+
return [
455+
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
456+
for vpc in result["data"]
457+
]

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+
"ipv4_range_auto_assign": false,
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+
"ipv4_range_auto_assign": false,
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+
"ipv4_range_auto_assign": false,
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+
"ipv4_range_auto_assign": false,
19+
"purpose": "backend"
20+
}
21+
],
22+
"page": 1,
23+
"pages": 1,
24+
"results": 1
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+
"ipv4_range_auto_assign": false,
8+
"purpose": "frontend"
9+
}

test/unit/objects/nodebalancers_test.py

Lines changed: 112 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,114 @@ 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].purpose, "frontend")
321+
322+
self.assertIsInstance(result[1], NodeBalancerVPCConfig)
323+
self.assertEqual(result[1].id, 100)
324+
self.assertEqual(result[1].subnet_id, 5556)
325+
self.assertEqual(result[1].vpc_id, 112)
326+
self.assertEqual(result[1].ipv4_range, "10.100.6.0/24")
327+
self.assertEqual(result[1].purpose, "backend")
328+
329+
def test_vpc(self):
330+
"""
331+
Test that you can get a single VPC configuration for a NodeBalancer.
332+
"""
333+
vpc_url = "/nodebalancers/12345/vpcs/99"
334+
with self.mock_get(vpc_url) as m:
335+
nb = NodeBalancer(self.client, 12345)
336+
result = nb.vpc(99)
337+
338+
self.assertEqual(m.call_url, vpc_url)
339+
self.assertIsInstance(result, NodeBalancerVPCConfig)
340+
self.assertEqual(result.id, 99)
341+
self.assertEqual(result.subnet_id, 5555)
342+
self.assertEqual(result.vpc_id, 111)
343+
self.assertEqual(result.ipv4_range, "10.100.5.0/24")
344+
self.assertEqual(result.purpose, "frontend")
345+
346+
def test_backend_vpcs(self):
347+
"""
348+
Test that you can list backend VPC configurations for a NodeBalancer.
349+
"""
350+
backend_vpcs_url = "/nodebalancers/12345/backend_vpcs"
351+
with self.mock_get(backend_vpcs_url) as m:
352+
nb = NodeBalancer(self.client, 12345)
353+
result = nb.backend_vpcs()
354+
355+
self.assertEqual(m.call_url, backend_vpcs_url)
356+
self.assertEqual(len(result), 1)
357+
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
358+
self.assertEqual(result[0].id, 101)
359+
self.assertEqual(result[0].subnet_id, 6666)
360+
self.assertEqual(result[0].vpc_id, 222)
361+
self.assertEqual(result[0].ipv4_range, "10.200.1.0/24")
362+
self.assertEqual(result[0].purpose, "backend")
363+
364+
def test_frontend_vpcs(self):
365+
"""
366+
Test that you can list frontend VPC configurations for a NodeBalancer.
367+
"""
368+
frontend_vpcs_url = "/nodebalancers/12345/frontend_vpcs"
369+
with self.mock_get(frontend_vpcs_url) as m:
370+
nb = NodeBalancer(self.client, 12345)
371+
result = nb.frontend_vpcs()
372+
373+
self.assertEqual(m.call_url, frontend_vpcs_url)
374+
self.assertEqual(len(result), 1)
375+
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
376+
self.assertEqual(result[0].id, 99)
377+
self.assertEqual(result[0].subnet_id, 5555)
378+
self.assertEqual(result[0].vpc_id, 111)
379+
self.assertEqual(result[0].ipv4_range, "10.100.5.0/24")
380+
self.assertEqual(result[0].purpose, "frontend")

0 commit comments

Comments
 (0)