From bb3fb29fad7de9a9ba8caad6265dbf3f5c0eee7f Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Sun, 20 Jan 2019 10:52:08 +0200 Subject: [PATCH] Add apis to get tier0 uplink cidrs and not just ips Change-Id: I6583338b196de84c6d8db122609c6031a7750329 --- vmware_nsxlib/v3/policy/core_resources.py | 27 ++++++++++++++++++----- vmware_nsxlib/v3/resources.py | 26 +++++++++++++++++----- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 248318cc..b1c2c6b2 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -1039,9 +1039,9 @@ class NsxPolicyTier0Api(NsxPolicyResourceBase): return self.nsx_api.router.get_tier0_router_tz( nsx_router_uuid) - def get_uplink_ips(self, tier0_id, tenant=constants.POLICY_INFRA_TENANT): - """Return a link of all uplink ips of this tier0 router""" - uplink_ips = [] + def _get_uplink_subnets(self, tier0_id, + tenant=constants.POLICY_INFRA_TENANT): + subnets = [] services = self.get_locale_services(tier0_id, tenant=tenant) for srv in services: # get the interfaces of this service @@ -1053,10 +1053,27 @@ class NsxPolicyTier0Api(NsxPolicyResourceBase): t0interface_def).get('results', []) for interface in interfaces: if interface.get('type') == 'EXTERNAL': - for subnet in interface.get('subnets', []): - uplink_ips.extend(subnet.get('ip_addresses', [])) + subnets.extend(interface.get('subnets', [])) + return subnets + + def get_uplink_ips(self, tier0_id, tenant=constants.POLICY_INFRA_TENANT): + """Return a link of all uplink ips of this tier0 router""" + subnets = self._get_uplink_subnets(tier0_id, tenant=tenant) + uplink_ips = [] + for subnet in subnets: + uplink_ips.extend(subnet.get('ip_addresses', [])) return uplink_ips + def get_uplink_cidrs(self, tier0_id, tenant=constants.POLICY_INFRA_TENANT): + """Return a link of all uplink cidrs of this tier0 router""" + subnets = self._get_uplink_subnets(tier0_id, tenant=tenant) + cidrs = [] + for subnet in subnets: + for ip_address in subnet.get('ip_addresses'): + cidrs.append('%s/%s' % (ip_address, + subnet.get('prefix_len'))) + return cidrs + class NsxPolicyTier1NatRuleApi(NsxPolicyResourceBase): DEFAULT_NAT_ID = 'USER' diff --git a/vmware_nsxlib/v3/resources.py b/vmware_nsxlib/v3/resources.py index 1048d1b0..6f979d0c 100644 --- a/vmware_nsxlib/v3/resources.py +++ b/vmware_nsxlib/v3/resources.py @@ -357,13 +357,29 @@ class LogicalRouterPort(utils.NsxLibApiBase): if port['resource_type'] == nsx_constants.LROUTERPORT_UPLINK: return port - def get_tier0_uplink_ips(self, logical_router_id): + def get_tier0_uplink_subnets(self, logical_router_id): port = self.get_tier0_uplink_port(logical_router_id) - ips = [] if port: - for subnet in port.get('subnets', []): - for ip_address in subnet.get('ip_addresses'): - ips.append(ip_address) + return port.get('subnets', []) + return [] + + def get_tier0_uplink_cidrs(self, logical_router_id): + # return a list of tier0 uplink ip/prefix addresses + subnets = self.get_tier0_uplink_subnets(logical_router_id) + cidrs = [] + for subnet in subnets: + for ip_address in subnet.get('ip_addresses'): + cidrs.append('%s/%s' % (ip_address, + subnet.get('prefix_length'))) + return cidrs + + def get_tier0_uplink_ips(self, logical_router_id): + # return a list of tier0 uplink ip addresses + subnets = self.get_tier0_uplink_subnets(logical_router_id) + ips = [] + for subnet in subnets: + for ip_address in subnet.get('ip_addresses'): + ips.append(ip_address) return ips