"Fossies" - the Fresh Open Source Software Archive

Member "zun-4.0.0/zun/objects/resource_provider.py" (16 Oct 2019, 6643 Bytes) of package /linux/misc/openstack/zun-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "resource_provider.py" see the Fossies "Dox" file reference documentation.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 from oslo_versionedobjects import fields
   14 
   15 from zun.db import api as dbapi
   16 from zun.objects import base
   17 
   18 
   19 @base.ZunObjectRegistry.register
   20 class ResourceProvider(base.ZunPersistentObject, base.ZunObject):
   21     # Version 1.0: Initial version
   22     VERSION = '1.0'
   23 
   24     fields = {
   25         'id': fields.IntegerField(read_only=True),
   26         'uuid': fields.UUIDField(nullable=False),
   27         'name': fields.StringField(nullable=False),
   28         'root_provider': fields.UUIDField(nullable=False),
   29         'parent_provider': fields.UUIDField(nullable=True),
   30         'can_host': fields.IntegerField(default=0),
   31     }
   32 
   33     @staticmethod
   34     def _from_db_object(provider, db_provider):
   35         """Converts a database entity to a formal object."""
   36         for field in provider.fields:
   37             setattr(provider, field, db_provider[field])
   38 
   39         provider.obj_reset_changes()
   40         return provider
   41 
   42     @staticmethod
   43     def _from_db_object_list(db_objects, cls, context):
   44         """Converts a list of database entities to a list of formal objects."""
   45         return [ResourceProvider._from_db_object(cls(context), obj)
   46                 for obj in db_objects]
   47 
   48     @base.remotable_classmethod
   49     def get_by_uuid(cls, context, uuid):
   50         """Find a resource provider based on uuid.
   51 
   52         :param uuid: the uuid of a resource provider.
   53         :param context: Security context
   54         :returns: a :class:`ResourceProvider` object.
   55         """
   56         db_provider = dbapi.get_resource_provider(context, uuid)
   57         provider = ResourceProvider._from_db_object(cls(context), db_provider)
   58         return provider
   59 
   60     @base.remotable_classmethod
   61     def get_by_name(cls, context, name):
   62         """Find a resource provider based on name.
   63 
   64         :param name: the logical name of a resource provider.
   65         :param context: Security context
   66         :returns: a :class:`ResourceProvider` object.
   67         """
   68         db_provider = dbapi.get_resource_provider(context, name)
   69         provider = ResourceProvider._from_db_object(cls(context), db_provider)
   70         return provider
   71 
   72     @base.remotable_classmethod
   73     def list(cls, context, limit=None, marker=None,
   74              sort_key=None, sort_dir=None, filters=None):
   75         """Return a list of ResourceProvider objects.
   76 
   77         :param context: Security context.
   78         :param limit: maximum number of resources to return in a single result.
   79         :param marker: pagination marker for large data sets.
   80         :param sort_key: column to sort results by.
   81         :param sort_dir: direction to sort. "asc" or "desc".
   82         :param filters: filters when list resource providers.
   83         :returns: a list of :class:`ResourceProvider` object.
   84 
   85         """
   86         db_providers = dbapi.list_resource_providers(
   87             context, limit=limit, marker=marker, sort_key=sort_key,
   88             sort_dir=sort_dir, filters=filters)
   89         return ResourceProvider._from_db_object_list(
   90             db_providers, cls, context)
   91 
   92     @base.remotable
   93     def create(self, context):
   94         """Create a ResourceProvider record in the DB.
   95 
   96         :param context: Security context. NOTE: This should only
   97                         be used internally by the indirection_api.
   98                         Unfortunately, RPC requires context as the first
   99                         argument, even though we don't use it.
  100                         A context should be set when instantiating the
  101                         object, e.g.: ResourceProvider(context)
  102 
  103         """
  104         values = self.obj_get_changes()
  105         db_provider = dbapi.create_resource_provider(context, values)
  106         self._from_db_object(self, db_provider)
  107 
  108     @base.remotable
  109     def destroy(self, context=None):
  110         """Delete the ResourceProvider from the DB.
  111 
  112         :param context: Security context. NOTE: This should only
  113                         be used internally by the indirection_api.
  114                         Unfortunately, RPC requires context as the first
  115                         argument, even though we don't use it.
  116                         A context should be set when instantiating the
  117                         object, e.g.: ResourceProvider(context)
  118         """
  119         dbapi.destroy_resource_provider(context, self.uuid)
  120         self.obj_reset_changes()
  121 
  122     @base.remotable
  123     def save(self, context=None):
  124         """Save updates to this ResourceProvider.
  125 
  126         Updates will be made column by column based on the result
  127         of self.what_changed().
  128 
  129         :param context: Security context. NOTE: This should only
  130                         be used internally by the indirection_api.
  131                         Unfortunately, RPC requires context as the first
  132                         argument, even though we don't use it.
  133                         A context should be set when instantiating the
  134                         object, e.g.: ResourceProvider(context)
  135         """
  136         updates = self.obj_get_changes()
  137         dbapi.update_resource_provider(context, self.uuid, updates)
  138 
  139         self.obj_reset_changes()
  140 
  141     @base.remotable
  142     def refresh(self, context=None):
  143         """Loads updates for this ResourceProvider.
  144 
  145         Loads a resource provider with the same uuid from the database and
  146         checks for updated attributes. Updates are applied from
  147         the loaded resource provider column by column, if there are any
  148         updates.
  149 
  150         :param context: Security context. NOTE: This should only
  151                         be used internally by the indirection_api.
  152                         Unfortunately, RPC requires context as the first
  153                         argument, even though we don't use it.
  154                         A context should be set when instantiating the
  155                         object, e.g.: ResourceProvider(context)
  156         """
  157         current = self.__class__.get_by_uuid(self._context, uuid=self.uuid)
  158         for field in self.fields:
  159             if self.obj_attr_is_set(field) and \
  160                getattr(self, field) != getattr(current, field):
  161                 setattr(self, field, getattr(current, field))