def testMonitorMetric(self):
        adapter = datasource_adapter_factory.createAutostackDatasourceAdapter()
        autostack = adapter.createAutostack(self.stackSpec)
        self.addCleanup(self._deleteAutostack, autostack.uid)

        modelSpec = self.getModelSpec("cloudwatch", "CPUUtilization", autostack)
        modelId = adapter.monitorMetric(modelSpec)
        self.validateModel(modelId, modelSpec, autostack)

        modelSpec = self.getModelSpec("cloudwatch", "DiskReadBytes", autostack)
        modelId = adapter.monitorMetric(modelSpec)
        self.validateModel(modelId, modelSpec, autostack)

        modelSpec = self.getModelSpec("cloudwatch", "DiskWriteBytes", autostack)
        modelId = adapter.monitorMetric(modelSpec)
        self.validateModel(modelId, modelSpec, autostack)

        modelSpec = self.getModelSpec("cloudwatch", "NetworkIn", autostack)
        modelId = adapter.monitorMetric(modelSpec)
        self.validateModel(modelId, modelSpec, autostack)

        modelSpec = self.getModelSpec("cloudwatch", "NetworkOut", autostack)
        modelId = adapter.monitorMetric(modelSpec)
        self.validateModel(modelId, modelSpec, autostack)

        modelSpec = self.getModelSpec("autostack", "InstanceCount", autostack)
        modelId = adapter.monitorMetric(modelSpec)
        self.validateModel(modelId, modelSpec, autostack)

        with self.engine.connect() as conn:
            metrics = repository.getAutostackMetrics(conn, autostack.uid)
        self.assertEqual(len([metricObj for metricObj in metrics]), 6)
    def testImportModel(self):
        adapter = datasource_adapter_factory.createAutostackDatasourceAdapter()

        autostack = adapter.createAutostack(self.stackSpec)

        modelSpec = self.getModelSpec("cloudwatch", "CPUUtilization", autostack)
        modelId = adapter.monitorMetric(modelSpec)

        spec = adapter.exportModel(modelId)
        adapter.unmonitorMetric(modelId)

        modelId = adapter.importModel(spec)
        self.validateModel(modelId, modelSpec, autostack)
        with self.engine.connect() as conn:
            metrics = repository.getAutostackMetrics(conn, autostack.uid)
            self.assertEqual(len([metricObj for metricObj in metrics]), 1)

            # Ensure that import can create an autostack if it doesn't exist
            repository.deleteAutostack(conn, autostack.uid)

        adapter = datasource_adapter_factory.createAutostackDatasourceAdapter()

        modelId = adapter.importModel(spec)
        newModelSpec = dict(modelSpec)

        with self.engine.connect() as conn:
            repository.getMetric(conn, modelId)
            autostack = repository.getAutostackFromMetric(conn, modelId)
        self.addCleanup(self._deleteAutostack, autostack.uid)
        newModelSpec["metricSpec"]["autostackId"] = autostack.uid

        self.validateModel(modelId, modelSpec, autostack)
  def GET(self, autostackId, *args): # pylint: disable=C0103,W0613
    """
      Get Metrics associated with autostack

      ::

          GET /_autostacks/{autostackId}/metrics

      NOTE: args is ignored.  Function signature for all method handlers must
      be compatible with the regexp pattern that matches.  POST optionally
      takes a second argument, DELETE requires it.
    """
    try:
      self.addStandardHeaders()
      engine = repository.engineFactory()
      metricRows = repository.getAutostackMetrics(engine,
                                                  autostackId,
                                                  getMetricDisplayFields(engine))
      metricsList = [convertMetricRowToMetricDict(metricRow)
                     for metricRow in metricRows]

      return utils.jsonEncode(metricsList)

    except ObjectNotFoundError:
      raise web.notfound("Autostack not found: Autostack ID: %s" % autostackId)
    except web.HTTPError as ex:
      if bool(re.match(r"([45][0-9][0-9])\s?", web.ctx.status)):
        # Log 400-599 status codes as errors, ignoring 200-399
        log.error(str(ex) or repr(ex))
      raise
    except Exception as ex:
      raise web.internalerror(str(ex) or repr(ex))