def create_service(self, context, name, auth_fields): """ Create a new service. Raises a Duplicate exception in the event that the new service is a duplicate of an existing service. :param context: The current context for accessing the database. :param name: The canonical name of the service, i.e., 'nova', 'glance', etc. :param auth_fields: A sequence listing the names of the fields of authentication and authorization data that the service passes to Boson to uniquely identify the user. :returns: An instance of ``boson.db.models.Service``. """ service = context.session.query(sa_models.Service).\ filter(sa_models.Service.name==name).first() try: if service is not None: LOG.error('Error in creating new service.Service of same' 'name %s already present' % name) raise Duplicate(klass=service) except Exception as err: LOG.exception(err) new_service = sa_models.Service(id=utils.generate_uuid(), name=name, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), auth_fields=auth_fields) context.session.add(new_service) return new_service
def create_service(self, context, name, auth_fields): """ Create a new service. Raises a Duplicate exception in the event that the new service is a duplicate of an existing service. :param context: The current context for accessing the database. :param name: The canonical name of the service, i.e., 'nova', 'glance', etc. :param auth_fields: A sequence listing the names of the fields of authentication and authorization data that the service passes to Boson to uniquely identify the user. :returns: An instance of ``boson.db.models.Service``. """ service = context.session.query(sa_models.Service).\ filter(sa_models.Service.name==name).first() try: if service is not None: LOG.error('Error in creating new service.Service of same' 'name %s already present'%name) raise Duplicate(klass=service) except Exception as err: LOG.exception(err) new_service = sa_models.Service(id=utils.generate_uuid(), name=name, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), auth_fields=auth_fields) context.session.add(new_service) return new_service
def create_category(self, context, service, name, usage_fset, quota_fsets): """ Create a new category on a service. Raises a Duplicate exception in the event that the new category is a duplicate of an existing category for the service. :param context: The current context for accessing the database. :param service: The service the category is for. Can be either a ``Service`` object or a UUID of an existing service. :param name: The canonical name of the category. :param usage_fset: A sequence listing the names of the fields of authentication and authorization data, passed by the service to Boson, which are to be used when looking up a ``Usage`` record. :param quota_fsets: A list of sequences of the names of the fields of authentication and authorization data, which are to be used when looking up ``Quota`` records. The list must be in order from the most specific to the least specific. For instance, this list could contain a set referencing the ``tenant_id``, followed by a set referencing the ``quota_class``, followed by an empty set; in this example, a quota applicable to the tenant would be used in preference to one applicable to the quota class, which would be used in preference to the default quota. :returns: An instance of ``boson.db.models.Category``. """ if isinstance(sa_models.Service, service): service = service.id category = context.session.query(sa_models.Category).\ filter(sa_models.Category.name==name).\ filter(sa_models.Category.service_id==service).\ first() try: if category is not None: LOG.error('Error in creating new category.Category of same' 'name %s for service %s is already present' % (name, service)) raise Duplicate(klass=category) except Exception as err: LOG.exception(err) new_category = sa_models.Category(id=utils.generate_uuid(), name=name, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), service_id=service, usage_fset=usage_fset, quota_fsets=quota_fsets) context.session.add(new_category) return new_category
def create_category(self, context, service, name, usage_fset, quota_fsets): """ Create a new category on a service. Raises a Duplicate exception in the event that the new category is a duplicate of an existing category for the service. :param context: The current context for accessing the database. :param service: The service the category is for. Can be either a ``Service`` object or a UUID of an existing service. :param name: The canonical name of the category. :param usage_fset: A sequence listing the names of the fields of authentication and authorization data, passed by the service to Boson, which are to be used when looking up a ``Usage`` record. :param quota_fsets: A list of sequences of the names of the fields of authentication and authorization data, which are to be used when looking up ``Quota`` records. The list must be in order from the most specific to the least specific. For instance, this list could contain a set referencing the ``tenant_id``, followed by a set referencing the ``quota_class``, followed by an empty set; in this example, a quota applicable to the tenant would be used in preference to one applicable to the quota class, which would be used in preference to the default quota. :returns: An instance of ``boson.db.models.Category``. """ if isinstance(sa_models.Service, service): service = service.id category = context.session.query(sa_models.Category).\ filter(sa_models.Category.name==name).\ filter(sa_models.Category.service_id==service).\ first() try: if category is not None: LOG.error('Error in creating new category.Category of same' 'name %s for service %s is already present'%(name,service)) raise Duplicate(klass=category) except Exception as err: LOG.exception(err) new_category = sa_models.Category(id=utils.generate_uuid(), name=name, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), service_id=service, usage_fset=usage_fset, quota_fsets=quota_fsets) context.session.add(new_category) return new_category
def create_reservation(self, context, expire): """ Create a new reservation. :param context: The current context for accessing the database. :param expire: A date and time at which the reservation will expire. :returns: An instance of ``boson.db.models.Reservation``. """ new_reservation = sa_models.Reservation(id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), expire=expire) context.session.add(new_reservation) return new_reservation
def create_reservation(self, context, expire): """ Create a new reservation. :param context: The current context for accessing the database. :param expire: A date and time at which the reservation will expire. :returns: An instance of ``boson.db.models.Reservation``. """ new_reservation = sa_models.Reservation( id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), expire=expire) context.session.add(new_reservation) return new_reservation
def create_quota(self, context, resource, auth_data, limit=None): """ Create a new quota for a given resource and user. Raises a Duplicate exception in the event that the new quota is a duplicate of an existing quota. :param context: The current context for accessing the database. :param resource: The resource the quota is for. Can be either a ``Resource`` object or a UUID of an existing resource. :param auth_data: Authentication and authorization data (a dictionary). This is used to match up a quota with a particular user of the system. :param limit: The limit on the number of the resource that the user is permitted to allocate. Defaults to ``None`` (unlimited). :returns: An instance of ``boson.db.models.Quota``. """ if isinstance(sa_models.Resource,resource): resource = resource.id quota = context.session.query(sa_models.Quota).\ filter(sa_models.Quota.resource_id==resource).\ filter(sa_models.Quota.auth_data==auth_data).\ first() try: if quota is not None: LOG.error('Error in creating new quota.Quota for same' 'resource %s is already present'%(resource)) raise Duplicate(klass=quota) except Exception as err: LOG.exception(err) new_quota = sa_models.Quota(id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), resorce_id=resource, auth_data=auth_data, limit=limit) context.session.add(new_quota) return new_quota
def create_quota(self, context, resource, auth_data, limit=None): """ Create a new quota for a given resource and user. Raises a Duplicate exception in the event that the new quota is a duplicate of an existing quota. :param context: The current context for accessing the database. :param resource: The resource the quota is for. Can be either a ``Resource`` object or a UUID of an existing resource. :param auth_data: Authentication and authorization data (a dictionary). This is used to match up a quota with a particular user of the system. :param limit: The limit on the number of the resource that the user is permitted to allocate. Defaults to ``None`` (unlimited). :returns: An instance of ``boson.db.models.Quota``. """ if isinstance(sa_models.Resource, resource): resource = resource.id quota = context.session.query(sa_models.Quota).\ filter(sa_models.Quota.resource_id==resource).\ filter(sa_models.Quota.auth_data==auth_data).\ first() try: if quota is not None: LOG.error('Error in creating new quota.Quota for same' 'resource %s is already present' % (resource)) raise Duplicate(klass=quota) except Exception as err: LOG.exception(err) new_quota = sa_models.Quota(id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), resorce_id=resource, auth_data=auth_data, limit=limit) context.session.add(new_quota) return new_quota
def reserve(self, context, reservation, resource, usage, delta): """ Reserve a particular amount of a specific resource. :param context: The current context for accessing the database. :param reservation: The reservation the item is reserved in. Can be either a ``Reservation`` object or a UUID of an existing reservation. :param resource: The resource the reserved item is for. Can be either a ``Resource`` object or a UUID of an existing resource. :param usage: The usage record for the resource reservation. Can be either a ``Usage`` object or a UUID of an existing usage. :param delta: The amount of the resource to reserve. May be negative for deallocation. :returns: An instance of ``boson.db.models.ReservedItem``. """ if isinstance(sa_models.Reservation, reservation): reservation = reservation.id if isinstance(sa_models.Resource, resource): resource = resource.id if isinstance(sa_models.Usage, usage): usage = usage.id new_reserved_items = sa_models.ReservedItem( id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), reservation_id=reservation, resorce_id=resource, usage_id=usage, delta=delta) context.session.add(new_reserved_items) return new_reserved_items
def reserve(self, context, reservation, resource, usage, delta): """ Reserve a particular amount of a specific resource. :param context: The current context for accessing the database. :param reservation: The reservation the item is reserved in. Can be either a ``Reservation`` object or a UUID of an existing reservation. :param resource: The resource the reserved item is for. Can be either a ``Resource`` object or a UUID of an existing resource. :param usage: The usage record for the resource reservation. Can be either a ``Usage`` object or a UUID of an existing usage. :param delta: The amount of the resource to reserve. May be negative for deallocation. :returns: An instance of ``boson.db.models.ReservedItem``. """ if isinstance(sa_models.Reservation, reservation): reservation = reservation.id if isinstance(sa_models.Resource, resource): resource = resource.id if isinstance(sa_models.Usage,usage): usage = usage.id new_reserved_items = sa_models.ReservedItem(id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), reservation_id=reservation, resorce_id=resource, usage_id=usage, delta=delta) context.session.add(new_reserved_items) return new_reserved_items
def create_usage(self, context, resource, param_data, auth_data, used=0, reserved=0, until_refresh=0, refresh_id=None): """ Create a new usage for a given resource and user. Raises a Duplicate exception in the event that the new usage is a duplicate of an existing usage. :param context: The current context for accessing the database. :param resource: The resource the usage is for. Can be either a ``Resource`` object or a UUID of an existing resource. :param param_data: Resource parameter data (a dictionary). This is used to allow for usages of resources which are children of another resource, where the limit should apply only within that parent resource. This allows, for example, a restriction on the number of IP addresses for a given Nova instance, without limiting the total number of IP addresses that can be allocated. :param auth_data: Authentication and authorization data (a dictionary). This is used to match up a usage with a particular user of the system. :param used: The amount of the resource currently in use. Defaults to 0. :param reserved: The amount of the resource currently reserved. Note that negative reservations are not counted here. Defaults to 0. :param until_refresh: A counter which decrements each time the usage record is used in a quota computation. When it reaches 0, the usage record will be refreshed. Defaults to 0. :param refresh_id: A UUID generated when the usage record needs refreshing. Refreshed usage information will only be accepted if the refresh has the same ID as stored in this field. Defaults to None. :returns: An instance of ``boson.db.models.Usage``. """ if isinstance(sa_models.Resource,resource): resource = resource.id usage = context.session.query(sa_models.Usage).\ filter(sa_models.Usage.resource_id==resource).\ filter(sa_models.Usage.auth_data==auth_data).\ filter(sa_models.Usage.parameter_data==param_data).\ first() try: if usage is not None: LOG.error('Error in creating new usage.Usage for same' 'resource %s is already present'%(resource)) raise Duplicate(klass=usage) except Exception as err: LOG.exception(err) new_usage = sa_models.Usage(id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), parameter_data=param_data, auth_data=auth_data, resource_id=resource, until_refresh=until_refresh, refresh_id=refresh_id) context.session.add(new_usage) return new_usage
def create_resource(self, context, service, category, name, parameters, absolute=False): """ Create a new resource on a service. Raises a Duplicate exception in the event that the new resource is a duplicate of an existing resource for the service. :param context: The current context for accessing the database. :param service: The service the resource is for. Can be either a ``Service`` object or a UUID of an existing service. :param category: The category the resource is in. Can be either a ``Category`` object or a UUID of an existing category. :param name: The canonical name of the resource. :param parameters: A sequence listing the names of the fields of resource parameter data, passed by the service to Boson, which are to be used when looking up a ``Usage`` record. Parameters allow application of limits to resources contained within other resources; that is, if a resource has a limit of 5, using parameter data would allow that limit to be interpreted as 5 per parent resource. :param absolute: A boolean indicating whether the resource is "absolute." An absolute resource does not maintain any usage records or allocate any reservations. Quota enforcement consists of a simple numerical comparison of the requested delta against the quota limit. This is designed to accommodate ephemeral resources, such as the number of files to inject into a Nova instance on boot. :returns: An instance of ``boson.db.models.Resource``. """ if isinstance(service, sa_models.Service): service = service.id if isinstance(category, sa_models.Category): category = category.id resource = context.session.query(sa_models.Resource).\ filter(sa_models.Resource.name==name).\ filter(sa_models.Resource.service_id==service).\ filter(sa_models.Resource.category_id==category).\ first() try: if resource is not None: LOG.error('Error in creating new resource.Resource of same' 'name %s for service %s and category %s is already' 'present'%(name,service,category)) raise Duplicate(klass=resource) except Exception as err: LOG.exception(err) new_resource = sa_models.Resource(id=utils.generate_uuid(), name=name, parameters=parameters, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), service_id=service, category_id=category, absolute=absolute) context.session.add(new_resource) return new_resource
def generate_request_id(): """Generate a unique request ID.""" return 'req-' + utils.generate_uuid()
def create_usage(self, context, resource, param_data, auth_data, used=0, reserved=0, until_refresh=0, refresh_id=None): """ Create a new usage for a given resource and user. Raises a Duplicate exception in the event that the new usage is a duplicate of an existing usage. :param context: The current context for accessing the database. :param resource: The resource the usage is for. Can be either a ``Resource`` object or a UUID of an existing resource. :param param_data: Resource parameter data (a dictionary). This is used to allow for usages of resources which are children of another resource, where the limit should apply only within that parent resource. This allows, for example, a restriction on the number of IP addresses for a given Nova instance, without limiting the total number of IP addresses that can be allocated. :param auth_data: Authentication and authorization data (a dictionary). This is used to match up a usage with a particular user of the system. :param used: The amount of the resource currently in use. Defaults to 0. :param reserved: The amount of the resource currently reserved. Note that negative reservations are not counted here. Defaults to 0. :param until_refresh: A counter which decrements each time the usage record is used in a quota computation. When it reaches 0, the usage record will be refreshed. Defaults to 0. :param refresh_id: A UUID generated when the usage record needs refreshing. Refreshed usage information will only be accepted if the refresh has the same ID as stored in this field. Defaults to None. :returns: An instance of ``boson.db.models.Usage``. """ if isinstance(sa_models.Resource, resource): resource = resource.id usage = context.session.query(sa_models.Usage).\ filter(sa_models.Usage.resource_id==resource).\ filter(sa_models.Usage.auth_data==auth_data).\ filter(sa_models.Usage.parameter_data==param_data).\ first() try: if usage is not None: LOG.error('Error in creating new usage.Usage for same' 'resource %s is already present' % (resource)) raise Duplicate(klass=usage) except Exception as err: LOG.exception(err) new_usage = sa_models.Usage(id=utils.generate_uuid(), created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), parameter_data=param_data, auth_data=auth_data, resource_id=resource, until_refresh=until_refresh, refresh_id=refresh_id) context.session.add(new_usage) return new_usage
def create_resource(self, context, service, category, name, parameters, absolute=False): """ Create a new resource on a service. Raises a Duplicate exception in the event that the new resource is a duplicate of an existing resource for the service. :param context: The current context for accessing the database. :param service: The service the resource is for. Can be either a ``Service`` object or a UUID of an existing service. :param category: The category the resource is in. Can be either a ``Category`` object or a UUID of an existing category. :param name: The canonical name of the resource. :param parameters: A sequence listing the names of the fields of resource parameter data, passed by the service to Boson, which are to be used when looking up a ``Usage`` record. Parameters allow application of limits to resources contained within other resources; that is, if a resource has a limit of 5, using parameter data would allow that limit to be interpreted as 5 per parent resource. :param absolute: A boolean indicating whether the resource is "absolute." An absolute resource does not maintain any usage records or allocate any reservations. Quota enforcement consists of a simple numerical comparison of the requested delta against the quota limit. This is designed to accommodate ephemeral resources, such as the number of files to inject into a Nova instance on boot. :returns: An instance of ``boson.db.models.Resource``. """ if isinstance(service, sa_models.Service): service = service.id if isinstance(category, sa_models.Category): category = category.id resource = context.session.query(sa_models.Resource).\ filter(sa_models.Resource.name==name).\ filter(sa_models.Resource.service_id==service).\ filter(sa_models.Resource.category_id==category).\ first() try: if resource is not None: LOG.error('Error in creating new resource.Resource of same' 'name %s for service %s and category %s is already' 'present' % (name, service, category)) raise Duplicate(klass=resource) except Exception as err: LOG.exception(err) new_resource = sa_models.Resource(id=utils.generate_uuid(), name=name, parameters=parameters, created_at=datetime.datetime.now(), updated_at=datetime.datetime.now(), service_id=service, category_id=category, absolute=absolute) context.session.add(new_resource) return new_resource
def test_generate_uuid(self, _mock_uuid4): self.assertEqual(utils.generate_uuid(), '9bb4060a-3a1d-49e0-8c9b-b6f16b430cac')