From 2172dee816262510c29d216661ecad473ca8826c Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Tue, 12 Jan 2016 05:11:22 -0800 Subject: [PATCH] NSX|V3: ensure that port update allows traffic Ensure that when a port is update that the VIF attachment is also updated. This allows traffic to be sent to and from the instance. Change-Id: Id031bdd3e5a37d3b5b9da2fb7ec34290adf7cabe Closes-bug: #1533198 --- vmware_nsx/nsxlib/v3/resources.py | 52 +++++++++++++++++++---------- vmware_nsx/plugins/nsx_v3/plugin.py | 22 ++++++++++-- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/vmware_nsx/nsxlib/v3/resources.py b/vmware_nsx/nsxlib/v3/resources.py index 73ad063d07..b6f512b152 100644 --- a/vmware_nsx/nsxlib/v3/resources.py +++ b/vmware_nsx/nsxlib/v3/resources.py @@ -174,7 +174,8 @@ class LogicalPort(AbstractRESTResource): self, display_name=None, admin_state=True, tags=None, address_bindings=None, - switch_profile_ids=None): + switch_profile_ids=None, + attachment=None): tags = tags or [] address_bindings = address_bindings or [] switch_profile_ids = switch_profile_ids or [] @@ -211,15 +212,13 @@ class LogicalPort(AbstractRESTResource): }) body['switching_profile_ids'] = profiles + if attachment: + body['attachment'] = attachment + return body - def create(self, lswitch_id, vif_uuid, tags=None, - attachment_type=nsx_constants.ATTACHMENT_VIF, - admin_state=True, name=None, address_bindings=None, - parent_name=None, parent_tag=None, - switch_profile_ids=None): - tags = tags or [] - + def _prepare_attachment(self, vif_uuid, parent_name, parent_tag, + address_bindings, attachment_type): # NOTE(arosen): if a parent_name is specified we need to use the # CIF's attachment. key_values = None @@ -233,21 +232,33 @@ class LogicalPort(AbstractRESTResource): # NOTE(arosen): The above api body structure might change # in the future - body = {'logical_switch_id': lswitch_id} if attachment_type and vif_uuid: - body['attachment'] = {'attachment_type': attachment_type, - 'id': vif_uuid} + attachment = {'attachment_type': attachment_type, + 'id': vif_uuid} if key_values: - body['attachment']['context'] = {'key_values': key_values} - body['attachment']['context']['resource_type'] = \ + attachment['context'] = {'key_values': key_values} + attachment['context']['resource_type'] = \ nsx_constants.CIF_RESOURCE_TYPE + return attachment + def create(self, lswitch_id, vif_uuid, tags=None, + attachment_type=nsx_constants.ATTACHMENT_VIF, + admin_state=True, name=None, address_bindings=None, + parent_name=None, parent_tag=None, + switch_profile_ids=None): + tags = tags or [] + + body = {'logical_switch_id': lswitch_id} + attachment = self._prepare_attachment(vif_uuid, parent_name, + parent_tag, address_bindings, + attachment_type) body.update(self._build_body_attrs( display_name=name, admin_state=admin_state, tags=tags, address_bindings=address_bindings, - switch_profile_ids=switch_profile_ids)) + switch_profile_ids=switch_profile_ids, + attachment=attachment)) return self._client.create(body=body) def delete(self, lport_id): @@ -256,18 +267,25 @@ class LogicalPort(AbstractRESTResource): @utils.retry_upon_exception_nsxv3( nsx_exc.StaleRevision, max_attempts=cfg.CONF.nsx_v3.retries) - def update(self, lport_id, name=None, admin_state=None, + def update(self, lport_id, vif_uuid, + name=None, admin_state=None, address_bindings=None, switch_profile_ids=None, - resources=None): + resources=None, + attachment_type=nsx_constants.ATTACHMENT_VIF, + parent_name=None, parent_tag=None): lport = self.get(lport_id) tags = lport.get('tags', []) if resources: tags = utils.update_v3_tags(tags, resources) + attachment = self._prepare_attachment(vif_uuid, parent_name, + parent_tag, address_bindings, + attachment_type) lport.update(self._build_body_attrs( display_name=name, admin_state=admin_state, tags=tags, address_bindings=address_bindings, - switch_profile_ids=switch_profile_ids)) + switch_profile_ids=switch_profile_ids, + attachment=attachment)) # If revision_id of the payload that we send is older than what NSX has # then we will get a 412: Precondition Failed. In that case we need to diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index 9e403785f9..a0d7d0318e 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -852,12 +852,30 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, resources = [{'resource_type': resource_type, 'tag': updated_device_id}] + vif_uuid = updated_port['id'] + parent_name, tag = self._get_data_from_binding_profile( + context, updated_port) + attachment_type = nsx_constants.ATTACHMENT_VIF + if (not updated_device_owner or + updated_device_owner in (l3_db.DEVICE_OWNER_ROUTER_INTF, + nsx_constants.BRIDGE_ENDPOINT)): + attachment_type = None + vif_uuid = None + + if updated_device_owner.startswith(const.DEVICE_OWNER_COMPUTE_PREFIX): + name = 'instance-port_%s' % updated_port['id'] + else: + name = updated_port.get('name') + self._port_client.update( - lport_id, name=updated_port.get('name'), + lport_id, vif_uuid, name=name, + attachment_type=attachment_type, admin_state=updated_port.get('admin_state_up'), address_bindings=address_bindings, switch_profile_ids=switch_profile_ids, - resources=resources) + resources=resources, + parent_name=parent_name, + parent_tag=tag) security.update_lport_with_security_groups( context, lport_id,