Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions linode_api4/objects/nodebalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ def load_ssl_data(self, cert_file, key_file):
self.ssl_key = f.read()


class NodeBalancerVPCConfig(DerivedBase):
"""
The VPC configuration for this NodeBalancer.

API documentation: https://techdocs.akamai.com/linode-api/reference/get-node-balancer-vpc-config
"""

api_endpoint = "/nodebalancers/{nodebalancer_id}/vpcs/{id}"
derived_url_path = "vpcs"
parent_id_name = "nodebalancer_id"

properties = {
"id": Property(identifier=True),
"nodebalancer_id": Property(identifier=True),
"ipv4_range": Property(mutable=True),
"ipv4_range_auto_assign": Property(mutable=True),
"subnet_id": Property(mutable=True),
"vpc_id": Property(mutable=True),
"purpose": Property(mutable=True),
Comment thread
dawiddzhafarov marked this conversation as resolved.
Outdated
}


class NodeBalancer(Base):
"""
A single NodeBalancer you can access.
Expand All @@ -253,6 +275,9 @@ class NodeBalancer(Base):
"tags": Property(mutable=True, unordered=True),
"client_udp_sess_throttle": Property(mutable=True),
"locks": Property(unordered=True),
"type": Property(),
"frontend_address_type": Property(),
"frontend_vpc_subnet_id": Property(),
}

# create derived objects
Expand Down Expand Up @@ -356,3 +381,77 @@ def firewalls(self):
Firewall(self._client, firewall["id"])
for firewall in result["data"]
]

def vpcs(self):
"""
View VPC information for VPCs associated with this NodeBalancer.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-node-balancer-vpcs

:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
:rtype: List[NodeBalancerVPCConfig]
"""
result = self._client.get(
"{}/vpcs".format(NodeBalancer.api_endpoint), model=self
)

return [
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
for vpc in result["data"]
]

def vpc(self, id):
"""
View VPC information for a VPC associated with this NodeBalancer.

API Documentation: https://www.linode.com/docs/api/nodebalancers/#nodebalancer-vpcs-view
Comment thread
dawiddzhafarov marked this conversation as resolved.
Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


:param id: The ID of the NodeBalancer VPC Config to view.
:type id: int

:returns: A NodeBalancerVPCConfig of the Linode NodeBalancer.
:rtype: NodeBalancerVPCConfig
"""
result = self._client.get(
"{}/vpcs/{}".format(NodeBalancer.api_endpoint, id), model=self
Comment thread
dawiddzhafarov marked this conversation as resolved.
Outdated
)

return NodeBalancerVPCConfig(
self._client, result["id"], self.id, json=result
)

def backend_vpcs(self):
"""
View VPC information for backend VPCs associated with this NodeBalancer.

API Documentation: TODO

:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
:rtype: List[NodeBalancerVPCConfig]
"""
Comment thread
dawiddzhafarov marked this conversation as resolved.
result = self._client.get(
"{}/backend_vpcs".format(NodeBalancer.api_endpoint), model=self
)

return [
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
for vpc in result["data"]
]

def frontend_vpcs(self):
"""
View VPC information for frontend VPCs associated with this NodeBalancer.

API Documentation: TODO

:returns: A List of NodeBalancerVPCConfig of the Linode NodeBalancer.
:rtype: List[NodeBalancerVPCConfig]
"""
Comment thread
dawiddzhafarov marked this conversation as resolved.
result = self._client.get(
"{}/frontend_vpcs".format(NodeBalancer.api_endpoint), model=self
)

return [
NodeBalancerVPCConfig(self._client, vpc["id"], self.id, json=vpc)
for vpc in result["data"]
]
12 changes: 9 additions & 3 deletions test/fixtures/nodebalancers.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"label": "balancer123456",
"client_conn_throttle": 0,
"tags": ["something"],
"locks": ["cannot_delete_with_subresources"]
"locks": ["cannot_delete_with_subresources"],
"type": "common",
"frontend_address_type": "vpc",
"frontend_vpc_subnet_id": 5555
},
{
"created": "2018-01-01T00:01:01",
Expand All @@ -24,10 +27,13 @@
"label": "balancer123457",
"client_conn_throttle": 0,
"tags": [],
"locks": []
"locks": [],
"type": "premium_40gb",
"frontend_address_type": "vpc",
"frontend_vpc_subnet_id": 6666
}
],
"results": 2,
"page": 1,
"pages": 1
}
}
5 changes: 4 additions & 1 deletion test/fixtures/nodebalancers_123456.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
],
"locks": [
"cannot_delete_with_subresources"
]
],
"type": "common",
"frontend_address_type": "vpc",
"frontend_vpc_subnet_id": 5555
}
16 changes: 16 additions & 0 deletions test/fixtures/nodebalancers_12345_backend__vpcs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [
{
"id": 101,
"nodebalancer_id": 12345,
"subnet_id": 6666,
"vpc_id": 222,
"ipv4_range": "10.200.1.0/24",
"ipv4_range_auto_assign": false,
"purpose": "backend"
}
],
"page": 1,
"pages": 1,
"results": 1
}
16 changes: 16 additions & 0 deletions test/fixtures/nodebalancers_12345_frontend__vpcs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [
{
"id": 99,
"nodebalancer_id": 12345,
"subnet_id": 5555,
"vpc_id": 111,
"ipv4_range": "10.100.5.0/24",
"ipv4_range_auto_assign": false,
"purpose": "frontend"
}
],
"page": 1,
"pages": 1,
"results": 1
}
25 changes: 25 additions & 0 deletions test/fixtures/nodebalancers_12345_vpcs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"data": [
{
"id": 99,
"nodebalancer_id": 12345,
"subnet_id": 5555,
"vpc_id": 111,
"ipv4_range": "10.100.5.0/24",
"ipv4_range_auto_assign": false,
"purpose": "frontend"
},
{
"id": 100,
"nodebalancer_id": 12345,
"subnet_id": 5556,
"vpc_id": 112,
"ipv4_range": "10.100.6.0/24",
"ipv4_range_auto_assign": false,
"purpose": "backend"
}
],
"page": 1,
"pages": 1,
"results": 1
Comment thread
dawiddzhafarov marked this conversation as resolved.
Outdated
}
9 changes: 9 additions & 0 deletions test/fixtures/nodebalancers_12345_vpcs_99.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 99,
"nodebalancer_id": 12345,
"subnet_id": 5555,
"vpc_id": 111,
"ipv4_range": "10.100.5.0/24",
"ipv4_range_auto_assign": false,
"purpose": "frontend"
}
112 changes: 112 additions & 0 deletions test/unit/objects/nodebalancers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
NodeBalancer,
NodeBalancerConfig,
NodeBalancerNode,
NodeBalancerVPCConfig,
)


Expand Down Expand Up @@ -266,3 +267,114 @@ def test_statistics(self):
"linode.com - balancer12345 (12345) - day (5 min avg)",
)
self.assertEqual(m.call_url, statistics_url)

def test_list_nodebalancers(self):
"""
Test that you can list all NodeBalancers.
"""
nbs = self.client.nodebalancers()

self.assertEqual(len(nbs), 2)

self.assertEqual(nbs[0].id, 123456)
self.assertEqual(nbs[0].label, "balancer123456")
self.assertEqual(nbs[0].type, "common")
self.assertEqual(nbs[0].frontend_address_type, "vpc")
self.assertEqual(nbs[0].frontend_vpc_subnet_id, 5555)

self.assertEqual(nbs[1].id, 123457)
self.assertEqual(nbs[1].label, "balancer123457")
self.assertEqual(nbs[1].type, "premium_40gb")
self.assertEqual(nbs[1].frontend_address_type, "vpc")
self.assertEqual(nbs[1].frontend_vpc_subnet_id, 6666)

def test_get_nodebalancer(self):
"""
Test that you can get a single NodeBalancer by ID.
"""
nb = NodeBalancer(self.client, 123456)

self.assertEqual(nb.id, 123456)
self.assertEqual(nb.label, "balancer123456")
self.assertEqual(nb.type, "common")
self.assertEqual(nb.frontend_address_type, "vpc")
self.assertEqual(nb.frontend_vpc_subnet_id, 5555)

def test_vpcs(self):
"""
Test that you can list VPC configurations for a NodeBalancer.
"""
vpcs_url = "/nodebalancers/12345/vpcs"
with self.mock_get(vpcs_url) as m:
nb = NodeBalancer(self.client, 12345)
result = nb.vpcs()

self.assertEqual(m.call_url, vpcs_url)
self.assertEqual(len(result), 2)

self.assertIsInstance(result[0], NodeBalancerVPCConfig)
self.assertEqual(result[0].id, 99)
self.assertEqual(result[0].subnet_id, 5555)
self.assertEqual(result[0].vpc_id, 111)
self.assertEqual(result[0].ipv4_range, "10.100.5.0/24")
self.assertEqual(result[0].purpose, "frontend")

self.assertIsInstance(result[1], NodeBalancerVPCConfig)
self.assertEqual(result[1].id, 100)
self.assertEqual(result[1].subnet_id, 5556)
self.assertEqual(result[1].vpc_id, 112)
self.assertEqual(result[1].ipv4_range, "10.100.6.0/24")
self.assertEqual(result[1].purpose, "backend")

def test_vpc(self):
"""
Test that you can get a single VPC configuration for a NodeBalancer.
"""
vpc_url = "/nodebalancers/12345/vpcs/99"
with self.mock_get(vpc_url) as m:
nb = NodeBalancer(self.client, 12345)
result = nb.vpc(99)

self.assertEqual(m.call_url, vpc_url)
self.assertIsInstance(result, NodeBalancerVPCConfig)
self.assertEqual(result.id, 99)
self.assertEqual(result.subnet_id, 5555)
self.assertEqual(result.vpc_id, 111)
self.assertEqual(result.ipv4_range, "10.100.5.0/24")
self.assertEqual(result.purpose, "frontend")

def test_backend_vpcs(self):
"""
Test that you can list backend VPC configurations for a NodeBalancer.
"""
backend_vpcs_url = "/nodebalancers/12345/backend_vpcs"
with self.mock_get(backend_vpcs_url) as m:
nb = NodeBalancer(self.client, 12345)
result = nb.backend_vpcs()

self.assertEqual(m.call_url, backend_vpcs_url)
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
self.assertEqual(result[0].id, 101)
self.assertEqual(result[0].subnet_id, 6666)
self.assertEqual(result[0].vpc_id, 222)
self.assertEqual(result[0].ipv4_range, "10.200.1.0/24")
self.assertEqual(result[0].purpose, "backend")

def test_frontend_vpcs(self):
"""
Test that you can list frontend VPC configurations for a NodeBalancer.
"""
frontend_vpcs_url = "/nodebalancers/12345/frontend_vpcs"
with self.mock_get(frontend_vpcs_url) as m:
nb = NodeBalancer(self.client, 12345)
result = nb.frontend_vpcs()

self.assertEqual(m.call_url, frontend_vpcs_url)
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], NodeBalancerVPCConfig)
self.assertEqual(result[0].id, 99)
self.assertEqual(result[0].subnet_id, 5555)
self.assertEqual(result[0].vpc_id, 111)
self.assertEqual(result[0].ipv4_range, "10.100.5.0/24")
self.assertEqual(result[0].purpose, "frontend")