Esempio n. 1
0
def delete_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    LOG.info("Starting %s Delete Handler", TYPE_NAME)
    model = request.desiredResourceState

    with v1_client(
            model.DatadogCredentials.ApiKey,
            model.DatadogCredentials.ApplicationKey,
            model.DatadogCredentials.ApiURL or "https://api.datadoghq.com",
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = MonitorsApi(api_client)
        try:
            api_instance.delete_monitor(model.Id)
        except ApiException as e:
            LOG.error(
                "Exception when calling MonitorsApi->delete_monitor: %s\n", e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=f"Error deleting monitor: {e}")

    return ProgressEvent(
        status=OperationStatus.SUCCESS,
        resourceModel=model,
    )
def update_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    LOG.info("Starting %s Update Handler", TYPE_NAME)
    model = request.desiredResourceState
    type_configuration = request.typeConfiguration

    monitor = ApiMonitorUpdateRequest()
    monitor.query = model.Query
    monitor.type = ApiMonitorType(model.Type)
    if model.Message is not None:
        monitor.message = model.Message
    if model.Name is not None:
        monitor.name = model.Name
    if model.Tags is not None:
        monitor.tags = model.Tags
    if model.Priority is not None:
        monitor.priority = model.Priority
    options = build_monitor_options_from_model(model)
    if options:
        monitor.options = options

    with v1_client(
            type_configuration.DatadogCredentials.ApiKey,
            type_configuration.DatadogCredentials.ApplicationKey,
            type_configuration.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = MonitorsApi(api_client)
        try:
            api_instance.update_monitor(model.Id, monitor)
        except ApiException as e:
            LOG.exception(
                "Exception when calling MonitorsApi->update_monitor: %s\n", e)
            return ProgressEvent(
                status=OperationStatus.FAILED,
                resourceModel=model,
                message=f"Error updating monitor: {e}",
                errorCode=http_to_handler_error_code(e.status),
            )

    return read_handler(session, request, callback_context)
Esempio n. 3
0
def create_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    LOG.info("Starting %s Create Handler", TYPE_NAME)
    model = request.desiredResourceState

    monitor = ApiMonitor()
    monitor.query = model.Query
    monitor.type = ApiMonitorType(model.Type)
    if model.Message is not None:
        monitor.message = model.Message
    if model.Name is not None:
        monitor.name = model.Name
    if model.Tags is not None:
        monitor.tags = model.Tags
    options = build_monitor_options_from_model(model)
    if options:
        monitor.options = options

    with v1_client(
            model.DatadogCredentials.ApiKey,
            model.DatadogCredentials.ApplicationKey,
            model.DatadogCredentials.ApiURL or "https://api.datadoghq.com",
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = MonitorsApi(api_client)
        try:
            monitor_resp = api_instance.create_monitor(monitor)
        except ApiException as e:
            LOG.error(
                "Exception when calling MonitorsApi->create_monitor: %s\n", e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=f"Error creating monitor: {e}")

    model.Id = monitor_resp.id
    return read_handler(session, request, callback_context)
Esempio n. 4
0
 def setUp(self):
     self.api = MonitorsApi()  # noqa: E501
Esempio n. 5
0
def read_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    LOG.info("Starting %s Read Handler", TYPE_NAME)
    model = request.desiredResourceState
    with v1_client(
            model.DatadogCredentials.ApiKey,
            model.DatadogCredentials.ApplicationKey,
            model.DatadogCredentials.ApiURL or "https://api.datadoghq.com",
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = MonitorsApi(api_client)
        monitor_id = model.Id
        try:
            monitor = api_instance.get_monitor(monitor_id)
        except ApiException as e:
            LOG.error("Exception when calling MonitorsApi->get_monitor: %s\n",
                      e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=f"Error getting monitor: {e}")

    model.Created = monitor.created.isoformat()
    model.Modified = monitor.modified.isoformat()
    model.Message = monitor.message
    model.Name = monitor.name
    model.Tags = monitor.tags
    model.Query = monitor.query
    model.Multi = monitor.multi
    if monitor.deleted:
        model.Deleted = monitor.deleted.isoformat()
    if not ((model.Type == "query alert"
             and monitor.type.value == "metric alert") or
            (model.Type == "metric alert"
             and monitor.type.value == "query alert")):
        # metric alert and query alert are interchangeable, so don't update from one to the other
        model.Type = monitor.type.value
    if monitor.creator:
        model.Creator = Creator(Name=monitor.creator.name,
                                Email=monitor.creator.email,
                                Handle=monitor.creator.handle)

    # Add hasattr checks for options since not all of them are applicable to all monitor types, so some attributes
    # might not always be present
    options = monitor.options if hasattr(monitor, "options") else None
    if options:
        model.Options = MonitorOptions(
            EnableLogsSample=options.enable_logs_sample if hasattr(
                options, "enable_logs_sample") else None,
            EscalationMessage=options.escalation_message if hasattr(
                options, "escalation_message") else None,
            EvaluationDelay=options.evaluation_delay if hasattr(
                options, "evaluation_delay") else None,
            IncludeTags=options.include_tags if hasattr(
                options, "include_tags") else None,
            Locked=options.locked if hasattr(options, "locked") else None,
            MinLocationFailed=options.min_location_failed if hasattr(
                options, "min_location_failed") else None,
            NewHostDelay=options.new_host_delay if hasattr(
                options, "new_host_delay") else None,
            NoDataTimeframe=options.no_data_timeframe if hasattr(
                options, "no_data_timeframe") else None,
            NotifyAudit=options.notify_audit if hasattr(
                options, "notify_audit") else None,
            NotifyNoData=options.notify_no_data if hasattr(
                options, "notify_no_data") else None,
            RenotifyInterval=options.renotify_interval if hasattr(
                options, "renotify_interval") else None,
            RequireFullWindow=options.require_full_window if hasattr(
                options, "require_full_window") else None,
            SyntheticsCheckID=options.synthetics_check_id if hasattr(
                options, "synthetics_check_id") else None,
            Thresholds=None,
            ThresholdWindows=None,
            TimeoutH=options.timeout_h
            if hasattr(options, "timeout_h") else None,
        )
        thresholds = options.thresholds if hasattr(options,
                                                   "thresholds") else None
        if thresholds:
            model.Options.Thresholds = MonitorThresholds(
                Critical=thresholds.critical if hasattr(
                    thresholds, "critical") else None,
                CriticalRecovery=thresholds.critical_recovery if hasattr(
                    thresholds, "critical_recovery") else None,
                Warning=thresholds.warning
                if hasattr(thresholds, "warning") else None,
                WarningRecovery=thresholds.warning_recovery if hasattr(
                    thresholds, "warning_recovery") else None,
                OK=thresholds.ok if hasattr(thresholds, "ok") else None,
            )
        tw = options.threshold_windows if hasattr(
            options, "threshold_windows") else None
        if tw:
            model.Options.ThresholdWindows = MonitorThresholdWindows(
                TriggerWindow=tw.trigger_window if hasattr(
                    tw, "trigger_window") else None,
                RecoveryWindow=tw.recovery_window if hasattr(
                    tw, "recovery_window") else None,
            )
    model.Id = monitor.id

    return ProgressEvent(
        status=OperationStatus.SUCCESS,
        resourceModel=model,
    )