def getAutostackForNameAndRegion(conn, name, region, fields=None): """ Get Autostack given name and region :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param name: Autostack name :param region: AWS region :param fields: Sequence of columns to be returned by underlying query :returns: AutoStack :rtype: sqlalchemy.engine.RowProxy :raises: YOMP.app.exceptions.ObjectNotFoundError if no match """ fields = fields or [schema.autostack] sel = select(fields).where((schema.autostack.c.name == name) & (schema.autostack.c.region == region)) result = conn.execute(sel) autostack = result.first() if autostack is None: raise ObjectNotFoundError( "Autostack not found for name=%s and region=%s" % (name, region)) return autostack
def testPUTNotificationSettingsCreate(self, repositoryMock, engineMock): """ Test PUT notification settings (create) """ repositoryMock.getDeviceNotificationSettings.side_effect = ( ObjectNotFoundError("No settings yet")) update = { "windowsize": 3601, "sensitivity": 0.999999, "email_addr": "*****@*****.**"} response = self.app.put("/%s/settings" % self.notification["device"], app_utils.jsonEncode(update), headers=self.headers) self.assertEqual(response.status, 201) self.assertFalse(response.body) self.assertTrue(repositoryMock.getDeviceNotificationSettings.called) repositoryMock.addDeviceNotificationSettings.assert_called_with( engineMock.return_value.connect.return_value.__enter__.return_value, self.notification["device"], update["windowsize"], update["sensitivity"], update["email_addr"])
def getAutostackFromMetric(conn, metricId): """Get the Autostack instance that metric belongs to. :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param metricId: Metric uid :type metricId: str :returns: Autostack :rtype: sqlalchemy.engine.RowProxy """ joinAutostack = (schema.autostack.join( schema.metric_set, schema.metric_set.c.autostack == schema.autostack.c.uid)) sel = (select( [schema.autostack], from_obj=joinAutostack).where(schema.metric_set.c.metric == metricId)) result = conn.execute(sel) autostack = result.first() if autostack is None: raise ObjectNotFoundError("Autostack not found for metric %s" % metricId) # There should never be multiple matches. assert result.rowcount == 1, "metric=%s matched %d autostacks" % ( metricId, result.rowcount) return autostack
def updateNotificationDeviceTimestamp(conn, deviceId): """Updates last access timestamp for the specified device. :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param deviceId: Device uid :type deviceId: str :raises: ObjectNotFoundError when there is no device with deviceId configured """ query = ( schema.notification_settings #pylint: disable=E1120 .update().where(schema.notification_settings.c.uid == deviceId).values( last_timestamp=func.utc_timestamp())) result = conn.execute(query) if result.rowcount == 0: raise ObjectNotFoundError("No notification settings for device: %s" % deviceId)
def addAnnotation(conn, timestamp, device, user, server, message, data, created, uid): """ :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param timestamp: The date and time to be annotated :param device: Device ID if the annotation was created by the mobile app or Service UID if the annotation was created by a service :param user: User name who created the annotation if the annotation was created by the mobile app or service name if the annotation was created by a service :param server: Instance ID associated with this annotation :param message: Optional annotation message :param data: Optional annotation data :param created: The date and time when the annotation was created :param uid: Annotation ID :rtype: sqlalchemy.engine.ResultProxy :raises: ObjectNotFoundError when the Instance ID is not found """ result = None with conn.begin(): # Make sure Instance ID exists prior to adding a new annotations sel = (select([func.count(schema.metric.c.server.distinct()) ]).where(schema.metric.c.server == server)) instanceRows = conn.execute(sel) if instanceRows.scalar() == 0: raise ObjectNotFoundError("Failed to add annotation. " "Server '%s' was not found." % server) # Add new annotation add = ( schema.annotation.insert() #pylint: disable=E1120 .values(timestamp=timestamp, device=device, user=user, server=server, message=message, data=data, created=created, uid=uid)) result = conn.execute(add) return result
def deleteAnnotationById(conn, annotationId): """Delete Annotation given annotation uid :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param annotationId: Annotation uid :rtype: sqlalchemy.engine.ResultProxy """ stmt = ( schema.annotation.delete() #pylint: disable=E1120 .where(schema.annotation.c.uid == annotationId)) result = conn.execute(stmt) if result.rowcount == 0: raise ObjectNotFoundError("Annotation not found for uid=%s" % annotationId) return result
def getDeviceNotificationSettings(conn, deviceId): """Get notification settings for device :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param deviceId: Device uid :returns: Notification settings :rtype: sqlalchemy.engine.RowProxy """ sel = (select([schema.notification_settings ]).where(schema.notification_settings.c.uid == deviceId)) result = conn.execute(sel) notificationSettings = result.first() if notificationSettings is None: raise ObjectNotFoundError( "Notification settings not found for deviceId=%s" % (deviceId)) return notificationSettings
def getAutostack(conn, autostackId, fields=None): """Get Autostack :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param autostackId: Autostack uid :type autostackId: str :returns: Autostack :rtype: sqlalchemy.engine.RowProxy """ fields = fields or [schema.autostack] sel = select(fields).where(schema.autostack.c.uid == autostackId) result = conn.execute(sel) autostack = result.first() if autostack is None: raise ObjectNotFoundError("Autostack not found for uid=%s" % autostackId) return autostack
def getAnnotationById(conn, annotationId, fields=None): """Get Annotation given annotation uid :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param annotationId: Annotation uid :param fields: Sequence of columns to be returned by underlying query :returns: Annotation :rtype: sqlalchemy.engine.RowProxy """ fields = fields or [schema.annotation] stmt = select(fields).where(schema.annotation.c.uid == annotationId) result = conn.execute(stmt) annotation = result.first() if annotation is None: raise ObjectNotFoundError("Annotation not found for uid=%s" % annotationId) return annotation
def testMonitorMetricNoExistingAutostack(self, getAutostackMock, _mockEngineFactory): adapter = datasource_adapter_factory.createAutostackDatasourceAdapter() modelSpec = { "datasource": "autostack", "metricSpec": { "autostackId": "9y2wn39y823nw9y8", "slaveDatasource": "cloudwatch", "slaveMetric": { "namespace": "AWS/EC2", "metric": "CPUUtilization", "dimensions": { "InstanceId": None }, "period": 300 } } } getAutostackMock.side_effect = ObjectNotFoundError() self.assertRaises(ObjectNotFoundError, adapter.monitorMetric, modelSpec)
def getNotification(conn, notificationId, fields=None): """Get Notification :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.base.Connection :param notificationId: Notification uid :param fields: Sequence of columns to be returned by underlying query :returns: Notification :rtype: sqlalchemy.engine.RowProxy :raises: YOMP.app.exceptions.ObjectNotFoundError if Notification doesn't exist for given uid """ fields = fields or [schema.notification] sel = select(fields).where(schema.notification.c.uid == notificationId) result = conn.execute(sel) notification = result.first() if notification is None: raise ObjectNotFoundError("Notification not found for uid=%s" % (notificationId)) return notification
def deleteAutostack(conn, autostackId): """Delete autostack :param conn: SQLAlchemy connection object :type conn: sqlalchemy.engine.Connection :param autostackId: Autostack uid :returns: Result of delete operation :rtype: sqlalchemy.engine.result.ResultProxy """ assert type(conn) is Connection with conn.begin(): # Delete metrics first subselect = (select([ schema.metric_set.c.metric ]).where(schema.metric_set.c.autostack == autostackId)) delete = schema.metric.delete().where( #pylint: disable=E1120 schema.metric.c.uid.in_(subselect)) conn.execute(delete) # Then delete autostack delete = ( schema.autostack.delete() #pylint: disable=E1120 .where(schema.autostack.c.uid == autostackId)) result = conn.execute(delete) # "result.rowcount" returns the number of rows matching the where expression if result.rowcount == 0: raise ObjectNotFoundError("Autostack not found for uid=%s" % autostackId) return result