NSX|V3: add tag for instance id if possible
When a port is created, for example via nova, the port will contain the device id. In this case lets add a tag that will help identify the instance. Closes-bug: #1530629 Change-Id: I75bd24d4cb3a42e0d4fad00fc9bec05c08b2ccbf
This commit is contained in:
		@@ -116,19 +116,23 @@ def build_v3_api_version_tag():
 | 
			
		||||
             'tag': version.version_info.release_string()}]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _validate_resource_type_length(resource_type):
 | 
			
		||||
    # Add in a validation to ensure that we catch this at build time
 | 
			
		||||
    if len(resource_type) > MAX_RESOURCE_TYPE_LEN:
 | 
			
		||||
        raise exceptions.InvalidInput(
 | 
			
		||||
            error_message=(_('Resource type cannot exceed %(max_len)s '
 | 
			
		||||
                             'characters: %(resource_type)s') %
 | 
			
		||||
                           {'max_len': MAX_RESOURCE_TYPE_LEN,
 | 
			
		||||
                            'resource_type': resource_type}))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def build_v3_tags_payload(resource, resource_type, project_name):
 | 
			
		||||
    """
 | 
			
		||||
    Construct the tags payload that will be pushed to NSX-v3
 | 
			
		||||
    Add <resource_type>:<resource-id>, os-project-id:<tenant-id>,
 | 
			
		||||
    os-project-name:<project_name> os-api-version:<neutron-api-version>
 | 
			
		||||
    """
 | 
			
		||||
    # Add in a validation to ensure that we catch this at build time
 | 
			
		||||
    if len(resource_type) > MAX_RESOURCE_TYPE_LEN:
 | 
			
		||||
        raise exceptions.InvalidInput(
 | 
			
		||||
            error_message=(_('Tag scope name cannot exceed %(max_len)s '
 | 
			
		||||
                             'characters: %(scope)s') %
 | 
			
		||||
                           {'max_len': MAX_RESOURCE_TYPE_LEN,
 | 
			
		||||
                            'scope': resource_type}))
 | 
			
		||||
    _validate_resource_type_length(resource_type)
 | 
			
		||||
    # There may be cases when the plugin creates the port, for example DHCP
 | 
			
		||||
    if not project_name:
 | 
			
		||||
        project_name = 'NSX Neutron plugin'
 | 
			
		||||
@@ -142,6 +146,12 @@ def build_v3_tags_payload(resource, resource_type, project_name):
 | 
			
		||||
             'tag': version.version_info.release_string()[:MAX_TAG_LEN]}]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def add_v3_tag(tags, resource_type, tag):
 | 
			
		||||
    _validate_resource_type_length(resource_type)
 | 
			
		||||
    tags.append({'scope': resource_type, 'tag': tag[:MAX_TAG_LEN]})
 | 
			
		||||
    return tags
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def retry_upon_exception_nsxv3(exc, delay=500, max_delay=2000,
 | 
			
		||||
                               max_attempts=cfg.CONF.nsx_v3.retries):
 | 
			
		||||
    return retrying.retry(retry_on_exception=lambda e: isinstance(e, exc),
 | 
			
		||||
 
 | 
			
		||||
@@ -590,6 +590,9 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin,
 | 
			
		||||
        tags = utils.build_v3_tags_payload(
 | 
			
		||||
            port_data, resource_type=resource_type,
 | 
			
		||||
            project_name=context.tenant_name)
 | 
			
		||||
        if device_id:
 | 
			
		||||
            tags = utils.add_v3_tag(tags, 'os-instance-uuid', device_id)
 | 
			
		||||
 | 
			
		||||
        parent_name, tag = self._get_data_from_binding_profile(
 | 
			
		||||
            context, port_data)
 | 
			
		||||
        address_bindings = self._build_address_bindings(port_data)
 | 
			
		||||
 
 | 
			
		||||
@@ -464,3 +464,20 @@ class TestNsxV3Utils(NsxV3PluginTestCaseMixin):
 | 
			
		||||
                    {'scope': 'os-api-version',
 | 
			
		||||
                     'tag': version.version_info.release_string()}]
 | 
			
		||||
        self.assertEqual(expected, result)
 | 
			
		||||
 | 
			
		||||
    def test_add_v3_tag(self):
 | 
			
		||||
        result = utils.add_v3_tag([], 'fake-scope', 'fake-tag')
 | 
			
		||||
        expected = [{'scope': 'fake-scope', 'tag': 'fake-tag'}]
 | 
			
		||||
        self.assertEqual(expected, result)
 | 
			
		||||
 | 
			
		||||
    def test_add_v3_tag_max_length_payload(self):
 | 
			
		||||
        result = utils.add_v3_tag([], 'fake-scope', 'X' * 255)
 | 
			
		||||
        expected = [{'scope': 'fake-scope', 'tag': 'X' * 40}]
 | 
			
		||||
        self.assertEqual(expected, result)
 | 
			
		||||
 | 
			
		||||
    def test_add_v3_tag_invalid_scope_length(self):
 | 
			
		||||
        self.assertRaises(n_exc.InvalidInput,
 | 
			
		||||
                          utils.add_v3_tag,
 | 
			
		||||
                          [],
 | 
			
		||||
                          'fake-scope-name-is-far-too-long',
 | 
			
		||||
                          'fake-tag')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user