From e8677ec2cf24d944391eacbbfedea8b1b95be2f4 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 7 Jan 2019 14:35:43 +0200 Subject: [PATCH] NSX|P support init configuration by NSX tags Like in the v3 plugin, the user can configure the main resource IDs by searching their tags. If the config has init_objects_by_tags = True, the object id givven in the config will be search on the NSX Policy manager as the tag, where the scope is the value configured in search_objects_scope. Change-Id: I9aa62b675a5be80b62eb260d9d61d9c00b20f5c6 --- vmware_nsx/common/config.py | 22 +++---- .../plugins/nsx_p/availability_zones.py | 61 ++++++++++++------- vmware_nsx/plugins/nsx_p/plugin.py | 11 +++- 3 files changed, 58 insertions(+), 36 deletions(-) diff --git a/vmware_nsx/common/config.py b/vmware_nsx/common/config.py index 3cf5941aa1..a66e3192cd 100644 --- a/vmware_nsx/common/config.py +++ b/vmware_nsx/common/config.py @@ -391,6 +391,17 @@ nsx_v3_and_p = [ help=_("List of transit networks used by NSX tier0 routers. " "Neutron subnets will not be allowed to use those " "cidrs")), + cfg.BoolOpt('init_objects_by_tags', + default=False, + help=_("When True, the configured transport zones, router and " + "profiles will be found by tags on the NSX. The scope " + "of the tag will be the value of search_objects_" + "scope. The value of the search tag will be the name " + "configured in each respective configuration.")), + cfg.StrOpt('search_objects_scope', + help=_("This is the scope of the tag that will be used for " + "finding the objects uuids on the NSX during plugin " + "init.")), ] nsx_v3_opts = nsx_v3_and_p + [ @@ -451,17 +462,6 @@ nsx_v3_opts = nsx_v3_and_p + [ cfg.IntOpt('dhcp_lease_time', default=86400, help=_("DHCP default lease time.")), - cfg.BoolOpt('init_objects_by_tags', - default=False, - help=_("When True, the configured transport zones, router and " - "profiles will be found by tags on the NSX. The scope " - "of the tag will be the value of search_objects_" - "scope. The value of the search tag will be the name " - "configured in each respective configuration.")), - cfg.StrOpt('search_objects_scope', - help=_("This is the scope of the tag that will be used for " - "finding the objects uuids on the NSX during plugin " - "init.")), cfg.ListOpt('switching_profiles', default=[], help=_("Optional parameter defining a list switching profiles " diff --git a/vmware_nsx/plugins/nsx_p/availability_zones.py b/vmware_nsx/plugins/nsx_p/availability_zones.py index 72404a58ab..5948651813 100644 --- a/vmware_nsx/plugins/nsx_p/availability_zones.py +++ b/vmware_nsx/plugins/nsx_p/availability_zones.py @@ -43,10 +43,11 @@ class NsxPAvailabilityZone(v3_az.NsxV3AvailabilityZone): self.dns_domain = cfg.CONF.nsx_p.dns_domain self.nameservers = cfg.CONF.nsx_p.nameservers - def _init_default_resource(self, resource_api, config_name, + def _init_default_resource(self, nsxpolicy, resource_api, config_name, filter_list_results=None, auto_config=False, - is_mandatory=True): + is_mandatory=True, + search_scope=None): # NOTE(annak): we may need to generalize this for API calls # requiring path ids name_or_id = getattr(self, config_name) @@ -77,49 +78,63 @@ class NsxPAvailabilityZone(v3_az.NsxV3AvailabilityZone): resource_api.get(name_or_id, silent=True) return name_or_id except nsx_lib_exc.ResourceNotFound: + # Search by tags + if search_scope: + resource_type = resource_api.entry_def.resource_type() + resource_id = nsxpolicy.get_id_by_resource_and_tag( + resource_type, + search_scope, + name_or_id) + if resource_id: + return resource_id + # Check if the configured value is the name resource = resource_api.get_by_name(name_or_id) if resource: return resource['id'] - else: - if self.is_default(): - raise cfg.RequiredOptError(config_name, - group=cfg.OptGroup('nsx_p')) - else: - msg = (_("Could not find %(res)s %(id)s for availability " - "zone %(az)s") % { - 'res': config_name, - 'id': name_or_id, - 'az': self.name}) - raise nsx_exc.NsxPluginException(err_msg=msg) - def translate_configured_names_to_uuids(self, nsxpolicy, nsxlib=None): + # Resource not found + if self.is_default(): + raise cfg.RequiredOptError(config_name, + group=cfg.OptGroup('nsx_p')) + else: + msg = (_("Could not find %(res)s %(id)s for availability " + "zone %(az)s") % { + 'res': config_name, + 'id': name_or_id, + 'az': self.name}) + raise nsx_exc.NsxPluginException(err_msg=msg) + + def translate_configured_names_to_uuids(self, nsxpolicy, nsxlib=None, + search_scope=None): super(NsxPAvailabilityZone, self).translate_configured_names_to_uuids( nsxpolicy) - # TODO(asarfaty): add support for init_objects_by_tags self._default_overlay_tz_uuid = self._init_default_resource( - nsxpolicy.transport_zone, 'default_overlay_tz', + nsxpolicy, nsxpolicy.transport_zone, 'default_overlay_tz', auto_config=True, is_mandatory=True, filter_list_results=lambda tzs: [ - tz for tz in tzs if tz['tz_type'].startswith('OVERLAY')]) + tz for tz in tzs if tz['tz_type'].startswith('OVERLAY')], + search_scope=search_scope) self._default_vlan_tz_uuid = self._init_default_resource( - nsxpolicy.transport_zone, 'default_vlan_tz', + nsxpolicy, nsxpolicy.transport_zone, 'default_vlan_tz', auto_config=True, is_mandatory=False, filter_list_results=lambda tzs: [ - tz for tz in tzs if tz['tz_type'].startswith('VLAN')]) + tz for tz in tzs if tz['tz_type'].startswith('VLAN')], + search_scope=search_scope) self._default_tier0_router = self._init_default_resource( - nsxpolicy.tier0, 'default_tier0_router', - auto_config=True, is_mandatory=True) + nsxpolicy, nsxpolicy.tier0, 'default_tier0_router', + auto_config=True, is_mandatory=True, + search_scope=search_scope) self.dhcp_relay_service = cfg.CONF.nsx_p.dhcp_relay_service # If passthrough api is supported, also initialize those NSX objects if nsxlib: - self._translate_dhcp_profile(nsxlib) - self._translate_metadata_proxy(nsxlib) + self._translate_dhcp_profile(nsxlib, search_scope=search_scope) + self._translate_metadata_proxy(nsxlib, search_scope=search_scope) else: self._native_dhcp_profile_uuid = None self._native_md_proxy_uuid = None diff --git a/vmware_nsx/plugins/nsx_p/plugin.py b/vmware_nsx/plugins/nsx_p/plugin.py index 66585da19d..d0b2fbe193 100644 --- a/vmware_nsx/plugins/nsx_p/plugin.py +++ b/vmware_nsx/plugins/nsx_p/plugin.py @@ -176,7 +176,6 @@ class NsxPolicyPlugin(nsx_plugin_common.NsxPluginV3Base): self._init_default_config() self._prepare_default_rules() self._init_segment_profiles() - self._init_dhcp_metadata() # Init QoS @@ -201,10 +200,18 @@ class NsxPolicyPlugin(nsx_plugin_common.NsxPluginV3Base): raise cfg.RequiredOptError("metadata_proxy", group=cfg.OptGroup('nsx_p')) + # If using tags to find the objects, make sure tag scope is configured + if (cfg.CONF.nsx_p.init_objects_by_tags and + not cfg.CONF.nsx_p.search_objects_scope): + raise cfg.RequiredOptError("search_objects_scope", + group=cfg.OptGroup('nsx_p')) + # Init AZ resources + search_scope = (cfg.CONF.nsx_p.search_objects_scope + if cfg.CONF.nsx_p.init_objects_by_tags else None) for az in self.get_azs_list(): az.translate_configured_names_to_uuids( - self.nsxpolicy, nsxlib=self.nsxlib) + self.nsxpolicy, nsxlib=self.nsxlib, search_scope=search_scope) def _init_dhcp_metadata(self): if (cfg.CONF.dhcp_agent_notification and