def deleteModel(metricId): try: with web.ctx.connFactory() as conn: metricRow = repository.getMetric(conn, metricId) except app_exceptions.ObjectNotFoundError: raise web.notfound("ObjectNotFoundError Metric not found: Metric ID: %s" % metricId) if metricRow.datasource == "autostack": raise NotAllowedResponse( {"result": ("Not a standalone model=%s; datasource=%s. Unable" " to DELETE from this endpoint") % (metricId, metricRow.datasource,) }) log.debug("Deleting model for %s metric=%s", metricRow.datasource, metricId) with web.ctx.connFactory() as conn: repository.deleteModel(conn, metricId) # NOTE: this is the new way using datasource adapters try: createDatasourceAdapter(metricRow.datasource).unmonitorMetric(metricId) except app_exceptions.ObjectNotFoundError: raise web.notfound( "ObjectNotFoundError Metric not found: Metric ID: %s" % (metricId,)) return utils.jsonEncode({'result': 'success'})
def DELETE(self, metricName): adapter = createDatasourceAdapter("custom") try: adapter.deleteMetricByName(metricName) except app_exceptions.ObjectNotFoundError: raise web.notfound("Metric not found. Metric name=%s" % (metricName,)) self.addStandardHeaders() return json.dumps({'result': 'success'})
def _exportNativeMetric(metric): return createDatasourceAdapter(metric.datasource).exportModel(metric.uid)
def createModel(cls, modelSpec=None): """ NOTE MER-3479: this code path is presently incorrectly used for two purposes: * Creating CloudWatch models (correct) * Importing of all types of metrics (not desirable; there should be a separate endpoint or an import-specific flag in this endpoint for importing that facilitates slightly different behavior, such as suppressing certain errors to allow for re-import in case of tranisent error part way through the prior import) """ if not modelSpec: # Metric data is missing log.error("Data is missing in request, raising BadRequest exception") raise InvalidRequestResponse({"result": "Metric data is missing"}) # TODO MER-3479: import using import-specific endpoint # NOTE: pending MER-3479, this is presently a hack for exercising # the adapter import API importing = False if modelSpec.get("datasource") == "custom": # Convert to new htm-it-custom metric modelSpec format # NOTE: backward compatibility during first phase refactoring modelSpec = cls.upgradeCustomModelSpec(modelSpec) if "data" in modelSpec: importing = True elif (modelSpec.get("datasource") == "cloudwatch" and "filters" not in modelSpec): if "type" in modelSpec: # The legacy cloudwatch import modelSpec had the "type" property assert modelSpec["type"] == "metric", repr(modelSpec) importing = True # Convert to new htm-it-custom metric modelSpec format # NOTE: backward compatibility during first phase refactoring modelSpec = cls.upgradeCloudwatchModelSpec(modelSpec) elif (modelSpec.get("datasource") == "autostack" or modelSpec.get("type") == "autostack"): importing = True # Convert to new autostack metric modelSpec format # NOTE: backward compatibility during first phase refactoring modelSpec = cls.upgradeAutostackModelSpec(modelSpec) try: with web.ctx.connFactory() as conn: with conn.begin(): adapter = createDatasourceAdapter(modelSpec["datasource"]) if modelSpec["datasource"] == "custom": checkQuotaForCustomMetricAndRaise(conn) else: checkQuotaForInstanceAndRaise( conn, adapter.getInstanceNameForModelSpec(modelSpec)) try: if importing: # TODO MER-3479: import using import-specific endpoint # NOTE: pending MER-3479, this is presently a hack for exercising # the adapter import API metricId = adapter.importModel(modelSpec) else: metricId = adapter.monitorMetric(modelSpec) except app_exceptions.MetricAlreadyMonitored as e: metricId = e.uid return repository.getMetric(conn, metricId) except (ValueError, app_exceptions.MetricNotSupportedError) as e: raise InvalidRequestResponse({"result": repr(e)})