def delete_archive_policy(self, name): with self.facade.writer() as session: try: if session.query(ArchivePolicy).filter( ArchivePolicy.name == name).delete() == 0: raise indexer.NoSuchArchivePolicy(name) except exception.DBReferenceError as e: if (e.constraint == 'fk_metric_archive_policy_name_archive_policy_name'): raise indexer.ArchivePolicyInUse(name) raise
def update_archive_policy(self, name, ap_items): with self.facade.independent_writer() as session: ap = session.query(ArchivePolicy).get(name) if not ap: raise indexer.NoSuchArchivePolicy(name) current = sorted(ap.definition, key=operator.attrgetter('granularity')) new = sorted(ap_items, key=operator.attrgetter('granularity')) if len(current) != len(new): raise indexer.UnsupportedArchivePolicyChange( name, 'Cannot add or drop granularities') for c, n in zip(current, new): if c.granularity != n.granularity: raise indexer.UnsupportedArchivePolicyChange( name, '%s granularity interval was changed' % c.granularity) # NOTE(gordc): ORM doesn't update JSON column unless new ap.definition = ap_items return ap
def _set_metrics_for_resource(session, r, metrics): for name, value in six.iteritems(metrics): if isinstance(value, uuid.UUID): try: update = session.query(Metric).filter( Metric.id == value, Metric.status == 'active', (Metric.created_by_user_id == r.created_by_user_id), (Metric.created_by_project_id == r.created_by_project_id), ).update({ "resource_id": r.id, "name": name }) except exception.DBDuplicateEntry: raise indexer.NamedMetricAlreadyExists(name) if update == 0: raise indexer.NoSuchMetric(value) else: ap_name = value['archive_policy_name'] m = Metric(id=uuid.uuid4(), created_by_user_id=r.created_by_user_id, created_by_project_id=r.created_by_project_id, archive_policy_name=ap_name, name=name, resource_id=r.id) session.add(m) try: session.flush() except exception.DBDuplicateEntry: raise indexer.NamedMetricAlreadyExists(name) except exception.DBReferenceError as e: if (e.constraint == 'fk_metric_archive_policy_name_archive_policy_name' ): raise indexer.NoSuchArchivePolicy(ap_name) raise session.expire(r, ['metrics'])
def create_metric(self, id, created_by_user_id, created_by_project_id, archive_policy_name, name=None, resource_id=None): m = Metric(id=id, created_by_user_id=created_by_user_id, created_by_project_id=created_by_project_id, archive_policy_name=archive_policy_name, name=name, resource_id=resource_id) try: with self.facade.writer() as session: session.add(m) except exception.DBReferenceError as e: if (e.constraint == 'fk_metric_archive_policy_name_archive_policy_name'): raise indexer.NoSuchArchivePolicy(archive_policy_name) raise return m