def update_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    model = request.desiredResourceState
    type_configuration = request.typeConfiguration

    LOG.info(f"Starting the {TYPE_NAME} Update Handler")

    downtime_body = build_downtime_struct(model)

    with v1_client(
            type_configuration.DatadogCredentials.ApiKey,
            type_configuration.DatadogCredentials.ApplicationKey,
            type_configuration.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = downtimes_api.DowntimesApi(api_client)
        try:
            api_instance.update_downtime(model.Id, downtime_body)
        except ApiException as e:
            LOG.exception(
                "Exception when calling DowntimeApi->update_downtime: %s\n" %
                e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=e.body,
                                 errorCode=http_to_handler_error_code(
                                     e.status))

    return read_handler(session, request, callback_context)
예제 #2
0
def create_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    model = request.desiredResourceState
    progress: ProgressEvent = ProgressEvent(
        status=OperationStatus.IN_PROGRESS,
        resourceModel=model,
    )
    LOG.info(f"Starting the {TYPE_NAME} Create Handler")

    downtime_body = build_downtime_struct(model)

    with v1_client(
            model.DatadogCredentials.ApiKey,
            model.DatadogCredentials.ApplicationKey,
            model.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = downtimes_api.DowntimesApi(api_client)
        try:
            api_resp = api_instance.create_downtime(downtime_body)
            model.Id = api_resp.id
        except ApiException as e:
            LOG.error(
                "Exception when calling DowntimeApi->create_downtime: %s\n" %
                e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=e.body)

    return read_handler(session, request, callback_context)
예제 #3
0
def read_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    model = request.desiredResourceState
    LOG.info(f"Starting the {TYPE_NAME} Read Handler")

    with v1_client(
            model.DatadogCredentials.ApiKey,
            model.DatadogCredentials.ApplicationKey,
            model.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = downtimes_api.DowntimesApi(api_client)
        try:
            api_resp = api_instance.get_downtime(model.Id)
        except ApiException as e:
            LOG.error(
                "Exception when calling DowntimeApi->get_downtime: %s\n" % e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=e.body)

    LOG.info(f"Success retrieving downtime {api_resp}")

    # Add hasattr checks for non-nullable fields to ensure they're available to be set
    # Currently in datadog-api-client-python, accessing fields that don't exist return an AttributeError
    if hasattr(api_resp, 'message'):
        model.Message = api_resp.message
    if hasattr(api_resp, 'monitor_tags'):
        model.MonitorTags = api_resp.monitor_tags
    if hasattr(api_resp, 'scope'):
        model.Scope = api_resp.scope
    if hasattr(api_resp, 'timezone'):
        model.Timezone = api_resp.timezone
    if hasattr(api_resp, 'start'):
        model.Start = api_resp.start

    # Nullable fields, these should be None or set as a value
    if api_resp.end:
        model.End = api_resp.end
    if api_resp.monitor_id:
        model.MonitorId = api_resp.monitor_id

    return ProgressEvent(
        status=OperationStatus.SUCCESS,
        resourceModel=model,
    )
def delete_handler(
    session: Optional[SessionProxy],
    request: ResourceHandlerRequest,
    callback_context: MutableMapping[str, Any],
) -> ProgressEvent:
    model = request.desiredResourceState
    type_configuration = request.typeConfiguration
    LOG.info(f"Starting the {TYPE_NAME} Delete Handler")

    with v1_client(
            type_configuration.DatadogCredentials.ApiKey,
            type_configuration.DatadogCredentials.ApplicationKey,
            type_configuration.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = downtimes_api.DowntimesApi(api_client)
        # First get the downtime to check if it's disabled (mostly a hack to make a contract_delete_delete test pass)
        try:
            api_resp = api_instance.get_downtime(model.Id)
            if api_resp.disabled:
                # Return a 404 to indicate the downtime was already deleted
                return ProgressEvent(
                    status=OperationStatus.FAILED,
                    resourceModel=None,
                    message="Downtime {model.Id} already disabled",
                    errorCode=HandlerErrorCode.NotFound)
        except ApiException as e:
            # Log error but continue in case of failure to get, this should not prevent the next call to delete
            LOG.exception(
                "Exception when calling DowntimeApi->get_downtime: %s\n" % e)
        try:
            api_instance.cancel_downtime(model.Id)
        except ApiException as e:
            LOG.exception(
                "Exception when calling DowntimeApi->cancel_downtime: %s\n" %
                e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=e.body,
                                 errorCode=http_to_handler_error_code(
                                     e.status))

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