#1027 Make MBSResolver._get_modules public
Closed 10 months ago by cqi. Opened 10 months ago by cqi.
cqi/fm-orchestrator make-resolver-get_modules-public  into  master

@@ -37,18 +37,10 @@ 

      def __init__(self, config):

          self.config = config

  

-     def get_module_modulemds(self, name, stream, version=None, context=None, strict=False):

-         """

-         Gets the module modulemds from the resolver.

-         :param name: a string of the module's name

-         :param stream: a string of the module's stream

-         :param version: a string or int of the module's version. When None, latest version will

-             be returned.

-         :param context: a string of the module's context. When None, all contexts will

-             be returned.

-         :kwarg strict: Normally this function returns [] if no module can be

-             found.  If strict=True, then a UnprocessableEntity is raised.

-         :return: List of Modulemd metadata instances matching the query

+     def get_modules(self, name, stream, version=None, context=None, state="ready", strict=False):

+         """Find out specific modules from database

+ 

+         Refer to :meth:`GenericResolver.get_modules` for details of arguments.

          """

          with models.make_session(self.config) as session:

              if version and context:

@@ -61,10 +53,41 @@ 

                  raise NotImplemented(

                      "This combination of name/stream/version/context is not implemented")

  

-             if not builds and strict:

+         if not builds:

+             if strict:

                  raise UnprocessableEntity(

                      "Cannot find any module builds for %s:%s" % (name, stream))

-             return [build.mmd() for build in builds]

+             else:

+                 return None

+ 

+         result = []

+         for b in builds:

+             build_json = b.json()

+             build_json['modulemd'] = b.modulemd

+             result.append(build_json)

+         return result

+ 

+     def get_module_modulemds(self, name, stream, version=None, context=None, strict=False):

+         """

+         Gets the module modulemds from the resolver.

+         :param name: a string of the module's name

+         :param stream: a string of the module's stream

+         :param version: a string or int of the module's version. When None, latest version will

+             be returned.

+         :param context: a string of the module's context. When None, all contexts will

+             be returned.

+         :kwarg strict: Normally this function returns [] if no module can be

+             found.  If strict=True, then a UnprocessableEntity is raised.

+         :return: List of Modulemd metadata instances matching the query

+         """

+         builds = self.get_modules(name, stream,

+                                   version=version, context=context,

+                                   strict=strict)

+ 

+         if not builds and strict:

+             raise UnprocessableEntity(

+                 "Cannot find any module builds for %s:%s" % (name, stream))

+         return [self.extract_modulemd(build['modulemd']) for build in builds]

  

      def resolve_profiles(self, mmd, keys):

          """

@@ -69,19 +69,10 @@ 

              query["context"] = context

          return query

  

-     def _get_modules(self, name, stream, version=None, context=None, state="ready", strict=False):

-         """Query and return modules from MBS with specific info

- 

-         :param str name: module's name.

-         :param str stream: module's stream.

-         :kwarg str version: module's version. Optional.

-         :kwarg str context: module's context. Optional.

-         :kwarg str state: module's state. Defaults to ``ready``.

-         :kwarg bool strict: Normally this function returns None if no module can be

-             found. If strict=True, then an UnprocessableEntity is raised.

-         :return: final list of module_info which pass repoclosure

-         :rtype: list[dict]

-         :raises UnprocessableEntity: if no modules are found and ``strict`` is True.

+     def get_modules(self, name, stream, version=None, context=None, state="ready", strict=False):

+         """Query and return modules from MBS

+ 

+         Refer to :meth:`GenericResolver.get_modules` for details of arguments.

          """

          query = self._query_from_nsvc(name, stream, version, context, state)

          query["page"] = 1

@@ -113,9 +104,6 @@ 

  

          return modules

  

-     def _get_module(self, name, stream, version, context, state="ready", strict=False):

-         return self._get_modules(name, stream, version, context, state, strict)[0]

- 

      def get_module_modulemds(self, name, stream, version=None, context=None, strict=False):

          """

          Gets the module modulemds from the resolver.

@@ -135,7 +123,7 @@ 

          if local_modules:

              return [m.mmd() for m in local_modules]

  

-         modules = self._get_modules(name, stream, version, context, strict=strict)

+         modules = self.get_modules(name, stream, version, context, strict=strict)

          if not modules:

              return []

  

@@ -186,7 +174,7 @@ 

                  continue

  

              # Find the dep in the built modules in MBS

-             modules = self._get_modules(

+             modules = self.get_modules(

                  module_name, module_info['stream'], module_info['version'],

                  module_info['context'], strict=True)

  

@@ -232,7 +220,7 @@ 

          if mmd:

              queried_mmd = mmd

          else:

-             queried_module = self._get_module(

+             queried_module = self.get_module(

                  name, stream, version, context, strict=strict)

              yaml = queried_module['modulemd']

              queried_mmd = self.extract_modulemd(yaml, strict=strict)

@@ -255,7 +243,7 @@ 

  

              if "context" not in details:

                  details["context"] = models.DEFAULT_MODULE_CONTEXT

-             modules = self._get_modules(

+             modules = self.get_modules(

                  name, details['stream'], details['version'],

                  details['context'], strict=True)

              for m in modules:

@@ -95,6 +95,34 @@ 

          return mmd

  

      @abstractmethod

+     def get_modules(self, name, stream, version=None, context=None, state="ready", strict=False):

+         """Query and return modules from specific module data source

+ 

+         Subclass has to implement this method to query modules from its

+         specific data source. Known sources, so far, could be a remote MBS

+         service or a configured database.

+ 

+         :param str name: module's name.

+         :param str stream: module's stream.

+         :kwarg str version: module's version. Optional.

+         :kwarg str context: module's context. Optional.

+         :kwarg str state: module's state. Defaults to ``ready``.

+         :kwarg bool strict: Normally this function returns None if no module can be

+             found. If strict=True, then an UnprocessableEntity is raised.

+         :return: final list of module_info which pass repoclosure

+         :rtype: list[dict]

+         :raises UnprocessableEntity: if no modules are found and ``strict`` is True.

+         """

+         raise NotImplementedError()

+ 

+     def get_module(self, name, stream, version, context, state="ready", strict=False):

+         """Get an exact module which matches arguments

+ 

+         Refer to :meth:`GenericResolver.get_modules` for details of arguments.

+         """

+         return self.get_modules(name, stream, version, context, state, strict)[0]

+ 

+     @abstractmethod

      def get_module_modulemds(self, name, stream, version=None, context=None, strict=False):

          raise NotImplementedError()

  

For an issue I'm working on, I need to get koji_tag from the latest
platform module. For local build, MBSResolver is used to get module from
a local mbs database and remote MBS if local database does not have the
module. On the other hand, for a MBS service, it uses DBResolver to
query modules from connected database directly. So, my case mentioned
has to ensure platform module is queried from MBS service database
whatever current module is being built locally or in a MBS service.

This patch makes MBSResolver._get_modules public so that it can be
called dynamically based on resolver configuration. As a result, I can
get a platform module in this way,

  resolver = module_build_service.resolver.GenericResolver.create(conf)
  platform_modules = resolver.get_modules(name=name, stream=stream)
  return platform_modules[0]['koji_tag']

MBSResolver._get_module is changed accordingly.

Signed-off-by: Chenxiong Qi cqi@redhat.com

I created another PR https://pagure.io/fm-orchestrator/pull-request/1029 that should be a better way to get koji_tag. So, close this PR.

Pull-Request has been closed by cqi

10 months ago