Example #1
0
    def _get_driver_for_project(self, context, project):
        """Get the l2gw driver by the plugin of the project"""
        mapping = nsx_db.get_project_plugin_mapping(
            context.session, project)
        if mapping:
            plugin_type = mapping['plugin']
        else:
            msg = _("Couldn't find the plugin project %s is using") % project
            raise n_exc.InvalidInput(error_message=msg)

        if plugin_type not in self.drivers:
            msg = (_("Project %(project)s with plugin %(plugin)s has no "
                     "support for L2GW") % {'project': project,
                                            'plugin': plugin_type})
            raise n_exc.InvalidInput(error_message=msg)

        # make sure the core plugin is supported
        core_plugin = directory.get_plugin()
        if not core_plugin.get_plugin_by_type(plugin_type):
            msg = (_("Plugin %(plugin)s for project %(project)s is not "
                     "supported by the core plugin") % {'project': project,
                                                        'plugin': plugin_type})
            raise n_exc.InvalidInput(error_message=msg)

        return self.drivers[plugin_type]
Example #2
0
    def create_project_plugin_map(self,
                                  context,
                                  project_plugin_map,
                                  internal=False):
        data = project_plugin_map['project_plugin_map']

        # validations:
        # 1. validate it doesn't already exist
        if nsx_db.get_project_plugin_mapping(context.session, data['project']):
            raise projectpluginmap.ProjectPluginAlreadyExists(
                project_id=data['project'])
        if not internal:
            # 2. only admin user is allowed
            if not context.is_admin:
                raise projectpluginmap.ProjectPluginAdminOnly()
            # 3. Validate the project id
            # TODO(asarfaty): Validate project id exists in keystone
            if not uuidutils.is_uuid_like(data['project']):
                raise projectpluginmap.ProjectPluginIllegalId(
                    project_id=data['project'])

        # Add the entry to the DB and return it
        LOG.info(
            "Adding mapping between project %(project)s and plugin "
            "%(plugin)s", {
                'project': data['project'],
                'plugin': data['plugin']
            })
        nsx_db.add_project_plugin_mapping(context.session, data['project'],
                                          data['plugin'])
        return self._get_project_plugin_dict(data)
Example #3
0
    def get_plugin_type_from_project(self, context, project_id):
        """Get the correct plugin type for this project.

        Look for the project in the DB.
        If not there - add an entry with the default plugin
        """
        plugin_type = self.default_plugin
        if not project_id:
            # if the project_id is empty - return the default one and do not
            # add to db (used by admin context to get actions)
            return plugin_type

        mapping = nsx_db.get_project_plugin_mapping(
            context.session, project_id)
        if mapping:
            plugin_type = mapping['plugin']
        else:
            # add a new entry with the default plugin
            try:
                self.create_project_plugin_map(
                    context,
                    {'project_plugin_map': {'plugin': plugin_type,
                                            'project': project_id}},
                    internal=True)
            except projectpluginmap.ProjectPluginAlreadyExists:
                # Maybe added by another thread
                pass
        if not self.plugins.get(plugin_type):
            msg = (_("Cannot use unsupported plugin %(plugin)s for project "
                     "%(project)s") % {'plugin': plugin_type,
                                       'project': project_id})
            raise nsx_exc.NsxPluginException(err_msg=msg)

        LOG.debug("Using %s plugin for project %s", plugin_type, project_id)
        return plugin_type
Example #4
0
    def _get_driver_for_project(self, context, project):
        """Get the l2gw driver by the plugin of the project"""
        mapping = nsx_db.get_project_plugin_mapping(
            context.session, project)
        if mapping:
            plugin_type = mapping['plugin']
        else:
            msg = _("Couldn't find the plugin project %s is using") % project
            raise n_exc.InvalidInput(error_message=msg)

        if plugin_type not in self.drivers:
            msg = (_("Project %(project)s with plugin %(plugin)s has no "
                     "support for L2GW") % {'project': project,
                                            'plugin': plugin_type})
            raise n_exc.InvalidInput(error_message=msg)

        # make sure the core plugin is supported
        core_plugin = directory.get_plugin()
        if not core_plugin.get_plugin_by_type(plugin_type):
            msg = (_("Plugin %(plugin)s for project %(project)s is not "
                     "supported by the core plugin") % {'project': project,
                                                        'plugin': plugin_type})
            raise n_exc.InvalidInput(error_message=msg)

        return self.drivers[plugin_type]
Example #5
0
    def create_project_plugin_map(self, context, project_plugin_map,
                                  internal=False):
        data = project_plugin_map['project_plugin_map']

        # validations:
        # 1. validate it doesn't already exist
        if nsx_db.get_project_plugin_mapping(
            context.session, data['project']):
            raise projectpluginmap.ProjectPluginAlreadyExists(
                project_id=data['project'])
        if not internal:
            # 2. only admin user is allowed
            if not context.is_admin:
                raise projectpluginmap.ProjectPluginAdminOnly()
            # 3. Validate the project id
            # TODO(asarfaty): Validate project id exists in keystone
            if not uuidutils.is_uuid_like(data['project']):
                raise projectpluginmap.ProjectPluginIllegalId(
                    project_id=data['project'])
            # 4. Check that plugin is available
            if data['plugin'] not in self.plugins:
                raise projectpluginmap.ProjectPluginNotAvailable(
                    plugin=data['plugin'])

        # Add the entry to the DB and return it
        LOG.info("Adding mapping between project %(project)s and plugin "
                 "%(plugin)s", {'project': data['project'],
                                'plugin': data['plugin']})
        nsx_db.add_project_plugin_mapping(context.session,
                                          data['project'],
                                          data['plugin'])
        return self._get_project_plugin_dict(data)
Example #6
0
 def _get_project_mapping(self, context, project_id):
     mapping = nsx_db.get_project_plugin_mapping(
             context.session, project_id)
     if mapping:
         return mapping['plugin']
     else:
         raise exceptions.ObjectNotFound(id=project_id)
Example #7
0
 def get_project_mapping(context, project_id):
     """Return the plugin associated with this project"""
     mapping = nsx_db.get_project_plugin_mapping(
             context.session, project_id)
     if mapping:
         return mapping['plugin']
     else:
         raise exceptions.ObjectNotFound(id=project_id)
Example #8
0
 def get_project_mapping(context, project_id):
     """Return the plugin associated with this project"""
     mapping = nsx_db.get_project_plugin_mapping(
             context.session, project_id)
     if mapping:
         return mapping['plugin']
     else:
         raise exceptions.ObjectNotFound(id=project_id)
Example #9
0
def import_projects(resource, event, trigger, **kwargs):
    """Import existing openstack projects to the current plugin"""
    # TODO(asarfaty): get the projects list from keystone
    # get the plugin name from the user
    if not kwargs.get('property'):
        LOG.error("Need to specify plugin and project parameters")
        return
    else:
        properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
        plugin = properties.get('plugin')
        project = properties.get('project')
        if not plugin or not project:
            LOG.error("Need to specify plugin and project parameters")
            return
    if plugin not in projectpluginmap.VALID_TYPES:
        LOG.error("The supported plugins are %s", projectpluginmap.VALID_TYPES)
        return

    ctx = n_context.get_admin_context()
    if not db.get_project_plugin_mapping(ctx.session, project):
        db.add_project_plugin_mapping(ctx.session, project, plugin)
Example #10
0
 def get_project_plugin_map(self, context, id, fields=None):
     data = nsx_db.get_project_plugin_mapping(context.session, id)
     if data:
         return self._get_project_plugin_dict(data)
     else:
         raise n_exc.ObjectNotFound(id=id)
Example #11
0
def migrate_v_project_to_t(resource, event, trigger, **kwargs):
    """Migrate 1 project from v to t with all its resources"""

    # filter out the plugins INFO logging
    # TODO(asarfaty): Consider this for all admin utils
    LOG.logger.setLevel(logging.INFO)
    logging.getLogger(None).logger.setLevel(logging.WARN)

    # get the configuration: tenant + public network + from file flag
    usage = ("Usage: nsxadmin -r projects -o %s --property project-id=<> "
             "--property external-net=<NSX-T external network to be used> "
             "<--property from-file=True>" %
             shell.Operations.NSX_MIGRATE_V_V3.value)
    if not kwargs.get('property'):
        LOG.error("Missing parameters: %s", usage)
        return
    properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
    project = properties.get('project-id')
    ext_net_id = properties.get('external-net')
    from_file = properties.get('from-file', 'false').lower() == "true"
    # TODO(asarfaty): get files path
    if not project:
        LOG.error("Missing project-id parameter: %s", usage)
        return
    if not ext_net_id:
        LOG.error("Missing external-net parameter: %s", usage)
        return

    # check if files exist in the current directory
    try:
        filename = get_resource_file_name(project, 'network')
        file = open(filename, 'r')
        if file.read():
            if not from_file:
                from_file = admin_utils.query_yes_no(
                    "Use existing resources files for this project?",
                    default="yes")
        file.close()
    except Exception:
        sys.exc_clear()
        if from_file:
            LOG.error("Cannot run from file: files not found")
            return

    # validate tenant id and public network
    ctx = n_context.get_admin_context()
    mapping = db.get_project_plugin_mapping(ctx.session, project)
    current_plugin = mapping.plugin
    if not mapping:
        LOG.error("Project %s is unknown", project)
        return
    if not from_file and current_plugin != projectpluginmap.NsxPlugins.NSX_V:
        LOG.error("Project %s belongs to plugin %s.", project, mapping.plugin)
        return

    with v3_utils.NsxV3PluginWrapper() as plugin:
        try:
            plugin.get_network(ctx, ext_net_id)
        except exceptions.NetworkNotFound:
            LOG.error("Network %s was not found", ext_net_id)
            return
        if not plugin._network_is_external(ctx, ext_net_id):
            LOG.error("Network %s is not external", ext_net_id)
            return

    if from_file:
        # read resources from files
        objects = read_v_resources_from_files(project)
    else:
        # read all V resources and dump to a file
        objects = read_v_resources_to_files(ctx, project)

    # delete all the V resources (reading it from the files)
    if current_plugin == projectpluginmap.NsxPlugins.NSX_V:
        delete_v_resources(ctx, objects)

    # change the mapping of this tenant to T
    db.update_project_plugin_mapping(ctx.session, project,
                                     projectpluginmap.NsxPlugins.NSX_T)

    # use api replay flag to allow keeping the IDs
    cfg.CONF.set_override('api_replay_mode', True)

    # add resources 1 by one after adapting them to T (api-replay code)
    create_t_resources(ctx, objects, ext_net_id)

    # reset api replay flag to allow keeping the IDs
    cfg.CONF.set_override('api_replay_mode', False)
Example #12
0
 def get_project_plugin_map(self, context, id, fields=None):
     data = nsx_db.get_project_plugin_mapping(context.session, id)
     if data:
         return self._get_project_plugin_dict(data)
     else:
         raise n_exc.ObjectNotFound(id=id)