コード例 #1
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

    dashboard_id = model.Id

    with v1_client(
            model.DatadogCredentials.ApiKey,
            model.DatadogCredentials.ApplicationKey,
            model.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = DashboardsApi(api_client)
        try:
            dash = api_instance.get_dashboard(dashboard_id)
            json_dict = ApiClient.sanitize_for_serialization(dash)
            model.DashboardDefinition = json.dumps(json_dict)
        except ApiException as e:
            LOG.error(
                "Exception when calling DashboardsApi->get_dashboard: %s\n", e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=f"Error getting dashboard: {e}")
    return ProgressEvent(
        status=OperationStatus.SUCCESS,
        resourceModel=model,
    )
コード例 #2
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
    type_configuration = request.typeConfiguration

    dashboard_id = model.Id

    with v1_client(
            type_configuration.DatadogCredentials.ApiKey,
            type_configuration.DatadogCredentials.ApplicationKey,
            type_configuration.DatadogCredentials.ApiURL,
            TELEMETRY_TYPE_NAME,
            __version__,
    ) as api_client:
        api_instance = DashboardsApi(api_client)
        try:
            # Get raw http response with _preload_content  set to False
            resp = api_instance.get_dashboard(dashboard_id,
                                              _preload_content=False)
            json_dict = json.loads(resp.data)
            model.Url = json_dict["url"]
            for k in [
                    "author_handle", "id", "created_at", "modified_at", "url",
                    "author_name"
            ]:
                try:
                    del json_dict[k]
                except KeyError:
                    pass
            model.DashboardDefinition = json.dumps(json_dict, sort_keys=True)
        except ApiException as e:
            LOG.exception(
                "Exception when calling DashboardsApi->get_dashboard: %s\n", e)
            return ProgressEvent(status=OperationStatus.FAILED,
                                 resourceModel=model,
                                 message=f"Error getting dashboard: {e}",
                                 errorCode=http_to_handler_error_code(
                                     e.status))
    return ProgressEvent(
        status=OperationStatus.SUCCESS,
        resourceModel=model,
    )