From f601978eda4ca1b866e83914fecadba854701b51 Mon Sep 17 00:00:00 2001 From: dantingl Date: Wed, 8 Feb 2017 18:08:00 -0800 Subject: [PATCH] Update interface about NSX IPAM and CIF API change Change-Id: I224b8778cbb519ec9bc4ebebf9f1b3fbf4326b4d --- vmware_nsxlib/tests/unit/v3/test_constants.py | 8 ++- vmware_nsxlib/tests/unit/v3/test_resources.py | 66 ++++--------------- vmware_nsxlib/v3/__init__.py | 20 ++++-- vmware_nsxlib/v3/nsx_constants.py | 5 +- vmware_nsxlib/v3/resources.py | 44 +++++++------ 5 files changed, 58 insertions(+), 85 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/test_constants.py b/vmware_nsxlib/tests/unit/v3/test_constants.py index 40e7139a..447b329a 100644 --- a/vmware_nsxlib/tests/unit/v3/test_constants.py +++ b/vmware_nsxlib/tests/unit/v3/test_constants.py @@ -69,12 +69,14 @@ FAKE_CONTAINER_PORT = { "admin_state": "UP", "attachment": { "id": "9ca8d413-f7bf-4276-b4c9-62f42516bdb2", - "attachment_type": "CIF", + "attachment_type": "VIF", "context": { "vlan_tag": 122, "container_host_vif_id": "c6f817a0-4e36-421e-98a6-8a2faed880bc", - "key_values": [], - "resource_type": "CifAttachmentContext", + "resource_type": "VifAttachmentContext", + "app_id": "container-1", + "vif_type": "CHILD", + "allocate_addresses": "Both", } }, "switching_profile_ids": [ diff --git a/vmware_nsxlib/tests/unit/v3/test_resources.py b/vmware_nsxlib/tests/unit/v3/test_resources.py index 2f4f7941..aa89982a 100644 --- a/vmware_nsxlib/tests/unit/v3/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_resources.py @@ -319,21 +319,26 @@ class LogicalPortTestCase(nsxlib_testcase.NsxClientTestCase): fake_port['logical_switch_id'], fake_port['attachment']['id'], parent_vif_id=fake_container_host_vif_id, - parent_tag=fake_port_ctx['vlan_tag'], + traffic_tag=fake_port_ctx['vlan_tag'], address_bindings=pkt_classifiers, switch_profile_ids=switch_profile.build_switch_profile_ids( - mock.Mock(), *profile_dicts)) + mock.Mock(), *profile_dicts), + vif_type=fake_port_ctx['vif_type'], app_id=fake_port_ctx['app_id'], + allocate_addresses=fake_port_ctx['allocate_addresses']) resp_body = { 'logical_switch_id': fake_port['logical_switch_id'], 'switching_profile_ids': fake_port['switching_profile_ids'], 'attachment': { - 'attachment_type': 'CIF', + 'attachment_type': 'VIF', 'id': fake_port['attachment']['id'], 'context': { - 'vlan_tag': fake_port_ctx['vlan_tag'], - 'container_host_vif_id': fake_container_host_vif_id, - 'resource_type': 'CifAttachmentContext' + 'resource_type': 'VifAttachmentContext', + 'allocate_addresses': 'Both', + 'parent_vif_id': fake_container_host_vif_id, + 'traffic_tag': fake_port_ctx['vlan_tag'], + 'app_id': fake_port_ctx['app_id'], + 'vif_type': 'CHILD', } }, 'admin_state': 'UP', @@ -345,55 +350,6 @@ class LogicalPortTestCase(nsxlib_testcase.NsxClientTestCase): 'https://1.2.3.4/api/v1/logical-ports', data=jsonutils.dumps(resp_body, sort_keys=True)) - def test_create_logical_port_for_container_with_ip_pool_key_value(self): - """Test creating for container port returns correct response - - """ - fake_port = test_constants.FAKE_CONTAINER_PORT.copy() - fake_port_ctx = fake_port['attachment']['context'] - fake_container_host_vif_id = fake_port_ctx['container_host_vif_id'] - profile_dicts = [] - for profile_id in fake_port['switching_profile_ids']: - profile_dicts.append({'resource_type': profile_id['key'], - 'id': profile_id['value']}) - - key_values = [{'key': 'IP_POOL_ID', - 'value': test_constants.FAKE_IP_POOL_UUID}] - - mocked_resource = self._mocked_lport() - - switch_profile = resources.SwitchingProfile - mocked_resource.create( - fake_port['logical_switch_id'], - fake_port['attachment']['id'], - parent_vif_id=fake_container_host_vif_id, - parent_tag=[], - switch_profile_ids=switch_profile.build_switch_profile_ids( - mock.Mock(), *profile_dicts), - key_values=key_values) - - resp_body = { - 'logical_switch_id': fake_port['logical_switch_id'], - 'switching_profile_ids': fake_port['switching_profile_ids'], - 'attachment': { - 'attachment_type': 'CIF', - 'id': fake_port['attachment']['id'], - 'context': { - 'container_host_vif_id': fake_container_host_vif_id, - 'resource_type': 'CifAttachmentContext', - 'vlan_tag': [], - 'key_values': key_values - } - }, - 'admin_state': 'UP', - 'address_bindings': [] - } - - test_client.assert_json_call( - 'post', mocked_resource, - 'https://1.2.3.4/api/v1/logical-ports', - data=jsonutils.dumps(resp_body, sort_keys=True)) - def test_create_logical_port_admin_down(self): """Test creating port with admin_state down.""" fake_port = test_constants.FAKE_PORT diff --git a/vmware_nsxlib/v3/__init__.py b/vmware_nsxlib/v3/__init__.py index bf2220c4..a9537a0c 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -168,7 +168,8 @@ class NsxLibLogicalSwitch(utils.NsxLibApiBase): def create(self, display_name, transport_zone_id, tags, replication_mode=nsx_constants.MTEP, - admin_state=True, vlan_id=None): + admin_state=True, vlan_id=None, ip_pool_id=None, + mac_pool_id=None): # TODO(salv-orlando): Validate Replication mode and admin_state # NOTE: These checks might be moved to the API client library if one # that performs such checks in the client is available @@ -187,6 +188,12 @@ class NsxLibLogicalSwitch(utils.NsxLibApiBase): if vlan_id: body['vlan'] = vlan_id + if ip_pool_id: + body['ip_pool_id'] = ip_pool_id + + if mac_pool_id: + body['mac_pool_id'] = mac_pool_id + return self.client.create(resource, body) def delete(self, lswitch_id): @@ -495,17 +502,18 @@ class NsxLibIpBlockSubnet(utils.NsxLibApiBase): def create(self, ip_block_id, subnet_size): """Create a IP block subnet on the backend.""" - resource = 'pools/ip-blocks/%s/subnets' % ip_block_id - body = {'size': subnet_size} + resource = 'pools/ip-subnets' + body = {'size': subnet_size, + 'block_id': ip_block_id} return self.client.create(resource, body) - def delete(self, ip_block_id, subnet_id): + def delete(self, subnet_id): """Delete a IP block subnet on the backend.""" - resource = 'pools/ip-blocks/%s/subnets/%s' % (ip_block_id, subnet_id) + resource = 'pools/ip-subnets/%s' % subnet_id self.client.delete(resource) def list(self, ip_block_id): - resource = 'pools/ip-blocks/%s/subnets' % ip_block_id + resource = 'pools/ip-subnets?block_id=%s' % ip_block_id return self.client.get(resource) diff --git a/vmware_nsxlib/v3/nsx_constants.py b/vmware_nsxlib/v3/nsx_constants.py index 9db96493..8dbb249e 100644 --- a/vmware_nsxlib/v3/nsx_constants.py +++ b/vmware_nsxlib/v3/nsx_constants.py @@ -22,12 +22,13 @@ MTEP = "MTEP" # Port attachment types ATTACHMENT_VIF = "VIF" -ATTACHMENT_CIF = "CIF" ATTACHMENT_LR = "LOGICALROUTER" ATTACHMENT_DHCP = "DHCP_SERVICE" ATTACHMENT_MDPROXY = "METADATA_PROXY" -CIF_RESOURCE_TYPE = "CifAttachmentContext" +VIF_RESOURCE_TYPE = "VifAttachmentContext" + +ALLOCATE_ADDRESS_NONE = "None" # NSXv3 L2 Gateway constants BRIDGE_ENDPOINT = "BRIDGEENDPOINT" diff --git a/vmware_nsxlib/v3/resources.py b/vmware_nsxlib/v3/resources.py index da2dfcd7..34c0af5f 100644 --- a/vmware_nsxlib/v3/resources.py +++ b/vmware_nsxlib/v3/resources.py @@ -244,17 +244,20 @@ class LogicalPort(AbstractRESTResource): return body - def _prepare_attachment(self, vif_uuid, parent_vif_id, parent_tag, - address_bindings, attachment_type, key_values): + def _prepare_attachment(self, attachment_type, vif_uuid, + allocate_addresses, vif_type, + parent_vif_id, traffic_tag, app_id): if attachment_type and vif_uuid: attachment = {'attachment_type': attachment_type, 'id': vif_uuid} - if parent_vif_id: - context = {'vlan_tag': parent_tag, - 'container_host_vif_id': parent_vif_id, - 'resource_type': nsx_constants.CIF_RESOURCE_TYPE} - if key_values is not None: - context['key_values'] = key_values + if vif_type: + context = {'resource_type': nsx_constants.VIF_RESOURCE_TYPE, + 'allocate_addresses': allocate_addresses, + 'vif_type': vif_type} + if parent_vif_id: + context['parent_vif_id'] = parent_vif_id + context['traffic_tag'] = traffic_tag + context['app_id'] = app_id attachment['context'] = context return attachment elif attachment_type is None and vif_uuid is None: @@ -273,18 +276,18 @@ class LogicalPort(AbstractRESTResource): def create(self, lswitch_id, vif_uuid, tags=None, attachment_type=nsx_constants.ATTACHMENT_VIF, admin_state=True, name=None, address_bindings=None, - parent_vif_id=None, parent_tag=None, - switch_profile_ids=None, key_values=None): + parent_vif_id=None, traffic_tag=None, + switch_profile_ids=None, vif_type=None, app_id=None, + allocate_addresses=nsx_constants.ALLOCATE_ADDRESS_NONE): tags = tags or [] body = {'logical_switch_id': lswitch_id} # NOTE(arosen): If parent_vif_id is specified we need to use # CIF attachment type. - if parent_vif_id: - attachment_type = nsx_constants.ATTACHMENT_CIF - attachment = self._prepare_attachment(vif_uuid, parent_vif_id, - parent_tag, address_bindings, - attachment_type, key_values) + attachment = self._prepare_attachment(attachment_type, vif_uuid, + allocate_addresses, vif_type, + parent_vif_id, traffic_tag, + app_id) body.update(self._build_body_attrs( display_name=name, admin_state=admin_state, tags=tags, @@ -308,7 +311,9 @@ class LogicalPort(AbstractRESTResource): address_bindings=None, switch_profile_ids=None, tags_update=None, attachment_type=nsx_constants.ATTACHMENT_VIF, - parent_vif_id=None, parent_tag=None, key_values=None): + parent_vif_id=None, traffic_tag=None, + vif_type=None, app_id=None, + allocate_addresses=nsx_constants.ALLOCATE_ADDRESS_NONE): # Using internal method so we can access max_attempts in the decorator @utils.retry_upon_exception( exceptions.StaleRevision, @@ -323,9 +328,10 @@ class LogicalPort(AbstractRESTResource): if addr_bindings is None: addr_bindings = self._build_address_bindings( lport.get('address_bindings')) - attachment = self._prepare_attachment(vif_uuid, parent_vif_id, - parent_tag, addr_bindings, - attachment_type, key_values) + attachment = self._prepare_attachment(attachment_type, vif_uuid, + allocate_addresses, vif_type, + parent_vif_id, traffic_tag, + app_id) lport.update(self._build_body_attrs( display_name=name, admin_state=admin_state, tags=tags,