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)
Example #2
0
  def DELETE(self, autostackId): # pylint: disable=C0103,R0201
    """
      Delete an Autostack

      ::

          DELETE /_autostacks/{autostackId}
    """
    try:
      with web.ctx.connFactory() as conn:
        modelIDs = tuple(m.uid for m in
                         repository.
                           getAutostackMetrics(conn,
                                               autostackId))

      with web.ctx.connFactory() as conn:
        repository.deleteAutostack(conn, autostackId)

      # Delete the corresponding Engine models, if any
      for modelID in modelIDs:
        model_swapper_utils.deleteHTMModel(modelID)
      raise web.HTTPError(status="204 No Content")
    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:
      log.exception("DELETE Failed")
      raise web.internalerror(str(ex) or repr(ex))
 def _deleteAutostack(self, autostackId):
   with self.engine.connect() as conn:
     repository.deleteAutostack(conn, autostackId)