From 0a901e394cef06bd41b4c3d4590a3c45b85b425f Mon Sep 17 00:00:00 2001 From: Alexandru Coman Date: Tue, 14 Feb 2017 15:15:48 +0200 Subject: [PATCH] Improve the structure of IPConfiguration model --- hnv/client.py | 41 +++++++++++++++++-- .../fake/response/ip_configurations.json | 23 +++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/hnv/client.py b/hnv/client.py index e671701..5d168ec 100644 --- a/hnv/client.py +++ b/hnv/client.py @@ -332,7 +332,8 @@ class Resource(model.Model): # we will force the regexp to match the entire resource # reference. regexp = "^{regexp}$".format(regexp=regexp) - self._regexp[model_cls] = re.compile(regexp) + self._regexp[model_cls] = re.compile(regexp, + flags=re.IGNORECASE) def get_resource(self): """Return the associated resource.""" @@ -634,7 +635,7 @@ class IPConfiguration(_BaseHNVModel): """Indicates the allocation method (Static or Dynamic).""" public_ip_address = model.Field( - name="public_ip_address", key="publicIpAddress", + name="public_ip_address", key="publicIPAddress", is_required=False) """Indicates the public IP address of the IP Configuration.""" @@ -648,6 +649,40 @@ class IPConfiguration(_BaseHNVModel): """Indicates a reference to the subnet resource that the IP Configuration is connected to.""" + @classmethod + def from_raw_data(cls, raw_data): + """Create a new model using raw API response.""" + properties = raw_data["properties"] + + address_pools = [] + for content in properties.get("loadBalancerBackendAddressPools", []): + resource = Resource.from_raw_data(content) + address_pools.append(resource) + properties["loadBalancerBackendAddressPools"] = address_pools + + nat_rules = [] + for content in properties.get("loadBalancerInboundNatRules", None): + resource = Resource.from_raw_data(content) + nat_rules.append(resource) + properties["loadBalancerInboundNatRules"] = nat_rules + + raw_content = properties.get("publicIPAddress", None) + if raw_content is not None: + resource = Resource.from_raw_data(raw_content) + properties["publicIPAddress"] = resource + + raw_content = properties.get("serviceInsertion", None) + if raw_content is not None: + resource = Resource.from_raw_data(raw_content) + properties["serviceInsertion"] = resource + + raw_content = properties.get("subnet", None) + if raw_content is not None: + resource = Resource.from_raw_data(raw_content) + properties["subnet"] = resource + + return super(IPConfiguration, cls).from_raw_data(raw_data) + class DNSSettings(model.Model): @@ -1844,7 +1879,7 @@ class PublicIPAddresses(_BaseHNVModel): can be used to communicate with the virtual network from outside it. """ - _endpoint = "/networking/v1/publicIpAddresses/{resource_id}" + _endpoint = "/networking/v1/publicIPAddresses/{resource_id}" ip_address = model.Field(name="ip_address", key="ipAddress", is_required=False, is_read_only=False) diff --git a/hnv/tests/fake/response/ip_configurations.json b/hnv/tests/fake/response/ip_configurations.json index 909a394..3f3300d 100644 --- a/hnv/tests/fake/response/ip_configurations.json +++ b/hnv/tests/fake/response/ip_configurations.json @@ -18,6 +18,29 @@ "loadBalancerBackendAddressPools": [], "loadBalancerInboundNatRules": [] } + }, + { + "resourceRef": "/networkInterfaces/ec3ac77e-64be-4bc1-a2e3- 7cd6170a4752/ipConfigurations/cbcab016-6c87-4a32-8158-08e0db71635a", + "resourceId": "cbcab016-6c87-4a32-8158-08e0db71635a", + "etag": "W/\"5e2e060a-2103-4022-87ee-bf1667bd18eb\"", + "instanceId": "83283a7e-4885-468a-9a2a-c7c568efd290", + "properties": { + "provisioningState": "Succeeded", + "privateIPAddress": "13.168.101.21", + "privateIPAllocationMethod": "Static", + "subnet": { + "resourceRef": "/virtualNetworks/740f3670-de42-4345-aaa7-6bb8d423c5df/subnets/da459373- 42ee-43d3-b094-6e2176406e4a" + }, + "accessControlList": { + "resourceRef": "/accessControlLists/4561e835-128c-44cd-b55f-98bca0d34aba" + }, + "loadBalancerBackendAddressPools": [ + { + "resourceRef": "/loadBalancers/2ea43ab6-cb92-4ad3-854f- bc62092cf4b0/backendAddressPools/1cd5d838-b574-4bcb-b6ac-9db3fc5e5f4d" + } + ], + "loadBalancerInboundNatRules": [] + } } ], "nextLink": ""