Example #1
0
    def get_many(self, list_of_model_instances):
        """
        Similar to StorageClient.get(), but takes a list of model instances and
        returns a list of model instances.  The models must be of the same type
        or else the function throws a TypeError.

        :param list_of_model_instances: List of model instances with
                                        identifying data
        :type list_of_model_instances: [commissaire.models.Model, ...]
        :returns: List of new model instances with stored data
        :rtype: [commissaire.models.Model, ...]
        :raises: TypeError, commissaire.bus.RemoteProcedureCallError
        """
        # Handle the trivial case immediately
        if len(list_of_model_instances) == 0:
            return []

        model_class = get_uniform_model_type(list_of_model_instances)

        try:
            model_json_data = [x.to_dict() for x in list_of_model_instances]
            params = {
                'model_type_name': model_class.__name__,
                'model_json_data': model_json_data
            }
            response = self.bus_mixin.request('storage.get', params=params)
            return [model_class.new(**x) for x in response['result']]
        except RemoteProcedureCallError as error:
            self.bus_mixin.logger.error(
                '%s: Unable to get multiple %s records: %s',
                self.bus_mixin.__class__.__name__,
                model_class.__name__,
                error)
            raise error
Example #2
0
    def delete_many(self, list_of_model_instances):
        """
        Similar to StorageClient.delete(), but takes a list of model instances.
        The models must be of the same type or else the function throws a
        TypeError.

        :param list_of_model_instances: List of model instances with
                                        identifying data
        :type list_of_model_instances: [commissaire.models.Model, ...]
        :raises: TypeError, commissaire.bus.RemoteProcedureCallError
        """
        # Handle the trivial case immediately
        if len(list_of_model_instances) == 0:
            return

        model_class = get_uniform_model_type(list_of_model_instances)

        try:
            model_json_data = [x.to_dict() for x in list_of_model_instances]
            params = {
                'model_type_name': model_class.__name__,
                'model_json_data': model_json_data
            }
            self.bus_mixin.request('storage.delete', params=params)
        except RemoteProcedureCallError as error:
            self.bus_mixin.logger.error(
                '{}: Unable to delete multiple {} records: {}'.format(
                    self.bus_mixin.__class__.__name__, model_class.__name__,
                    error))
            raise error
Example #3
0
 def test_get_uniform_model_type_with_valid_types(self):
     """
     Verify get_uniform_model_type works when types are the same.
     """
     self.assertEquals(
         Host,
         storage.get_uniform_model_type([
             Host.new(address='127.0.0.1'),
             Host.new(address='127.0.0.2')]))
Example #4
0
    def save_many(self, list_of_model_instances):
        """
        Similar to StorageClient.save(), but takes a list of model instances
        and returns a list of model instances.  The models must be of the same
        type or else the function throws a TypeError.

        :param list_of_model_instances: List of model instances with data to
                                        save
        :type list_of_model_instances: [commissaire.models.Model, ...]
        :returns: List of new model instances with all saved fields
        :rtype: [commissaire.models.Model, ...]
        :raises: TypeError, commissaire.bus.RemoteProcedureCallError,
                 commissaire.models.ValidationError
        """
        # Handle the trivial case immediately
        if len(list_of_model_instances) == 0:
            return []

        model_class = get_uniform_model_type(list_of_model_instances)

        try:
            for model_instance in list_of_model_instances:
                model_instance._validate()
            model_json_data = [x.to_dict() for x in list_of_model_instances]
            params = {
                'model_type_name': model_class.__name__,
                'model_json_data': model_json_data
            }
            response = self.bus_mixin.request('storage.save', params=params)
            return [model_class.new(**x) for x in response['result']]
        except (RemoteProcedureCallError, models.ValidationError) as error:
            self.bus_mixin.logger.error(
                '{}: Unable to save multiple {} records: {}'.format(
                    self.bus_mixin.__class__.__name__, model_class.__name__,
                    error))
            raise error