def _validate_name_not_conflicting(self,
                                       tenant_id,
                                       name,
                                       expected_id=None):
        definitions = self._alarm_definitions_repo.get_alarm_definitions(
            tenant_id=tenant_id,
            name=name,
            dimensions=None,
            severity=None,
            sort_by=None,
            offset=None,
            limit=0)
        if definitions:
            if not expected_id:
                LOG.warning(
                    "Found existing definition for {} with tenant_id {}".
                    format(name, tenant_id))
                raise exceptions.AlreadyExistsException(
                    "An alarm definition with the name {} already exists".
                    format(name))

            found_definition_id = definitions[0]['id']
            if found_definition_id != expected_id:
                LOG.warning(
                    "Found existing alarm definition for {} with tenant_id {} with unexpected id {}"
                    .format(name, tenant_id, found_definition_id))
                raise exceptions.AlreadyExistsException(
                    "An alarm definition with the name {} already exists with id {}"
                    .format(name, found_definition_id))
    def create_notification(self, tenant_id, name, notification_type, address,
                            period):

        with self._db_engine.connect() as conn:
            row = conn.execute(self._select_nm_count_name_query,
                               b_tenant_id=tenant_id,
                               b_name=name.encode('utf8')).fetchone()

            if int(row[0]) > 0:
                raise exceptions.AlreadyExistsException('Notification already '
                                                        'exists')

            now = datetime.datetime.utcnow()
            notification_id = uuidutils.generate_uuid()

            conn.execute(self._insert_nm_query,
                         b_id=notification_id,
                         b_tenant_id=tenant_id,
                         b_name=name.encode('utf8'),
                         b_type=notification_type.encode('utf8'),
                         b_address=address.encode('utf8'),
                         b_period=period,
                         b_created_at=now,
                         b_updated_at=now)

        return notification_id
Beispiel #3
0
    def _validate_name_not_conflicting(self, tenant_id, name, expected_id=None):
        notification = self._notifications_repo.find_notification_by_name(tenant_id, name)

        if notification:
            if not expected_id:
                LOG.warning("Found existing notification method for {} with tenant_id {}".format(name, tenant_id))
                raise exceptions.AlreadyExistsException(
                    "A notification method with the name {} already exists".format(name))

            found_notification_id = notification['id']
            if found_notification_id != expected_id:
                LOG.warning("Found existing notification method for {} with tenant_id {} with unexpected id {}"
                            .format(name, tenant_id, found_notification_id))
                raise exceptions.AlreadyExistsException(
                    "A notification method with name {} already exists with id {}"
                    .format(name, found_notification_id))
    def create_notification(self, tenant_id, name,
                            notification_type, address, period):

        cnxn, cursor = self._get_cnxn_cursor_tuple()

        with cnxn:
            query = """
                select *
                from notification_method
                where tenant_id = %s and name = %s"""

            parms = [tenant_id, name.encode('utf8')]

            cursor.execute(query, parms)

            if cursor.rowcount > 0:
                raise exceptions.AlreadyExistsException('Notification already '
                                                        'exists')

            now = datetime.datetime.utcnow()
            notification_id = uuidutils.generate_uuid()
            query = """
                insert into notification_method(
                  id,
                  tenant_id,
                  name,
                  type,
                  address,
                  period,
                  created_at,
                  updated_at
                ) values (%s, %s, %s, %s, %s, %s, %s, %s)"""

            parms = [notification_id,
                     tenant_id,
                     name.encode('utf8'),
                     notification_type.encode('utf8'),
                     address.encode('utf8'),
                     period,
                     now,
                     now]

            cursor.execute(query, parms)

        return notification_id