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, )
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 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: api_instance.delete_dashboard(dashboard_id) except ApiException as e: LOG.error( "Exception when calling DashboardsApi->delete_dashboard: %s\n", e) return ProgressEvent(status=OperationStatus.FAILED, resourceModel=model, message=f"Error deleting dashboard: {e}") return ProgressEvent( status=OperationStatus.SUCCESS, resourceModel=None, )
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 try: json_payload = json.loads(model.DashboardDefinition) except ValueError as e: LOG.exception("Exception parsing dashboard payload: %s\n", e) return ProgressEvent( status=OperationStatus.FAILED, resourceModel=model, message=f"Error parsing dashboard payload: {e}", errorCode=HandlerErrorCode.InternalFailure, ) 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 False api_instance.update_dashboard(dashboard_id, json_payload, _check_input_type=False, _preload_content=False) except TypeError as e: LOG.exception( "Exception when deserializing the Dashboard payload definition: %s\n", e) return ProgressEvent( status=OperationStatus.FAILED, resourceModel=model, message=f"Error deserializing dashboard: {e}", errorCode=HandlerErrorCode.InternalFailure, ) except ApiException as e: LOG.exception( "Exception when calling DashboardsApi->update_dashboard: %s\n", e) return ProgressEvent(status=OperationStatus.FAILED, resourceModel=model, message=f"Error updating dashboard: {e}", errorCode=http_to_handler_error_code( e.status)) return read_handler(session, request, callback_context)
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 try: json_payload = json.loads(model.DashboardDefinition) except json.JSONDecodeError as e: LOG.error("Exception when loading the Dashboard JSON definition: %s\n", e) return ProgressEvent( status=OperationStatus.FAILED, resourceModel=model, message=f"Error loading Dashboard JSON definition: {e}") 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: # Call the deserialization function of the python client. # It expects the loaded JSON payload, the python client type of the model, # some path to the data (not sure what this one does), # whether or not the payload is a server payload, so true in our case, # whether or not to do type conversion, true in our case too # and importantly the api_client configuration, needed to perform the type conversions dashboard = validate_and_convert_types( json_payload, (Dashboard, ), ["resource_data"], True, True, configuration=api_client.configuration) res = api_instance.create_dashboard(dashboard) model.Id = res.id except TypeError as e: LOG.error( "Exception when deserializing the Dashboard payload definition: %s\n", e) return ProgressEvent(status=OperationStatus.FAILED, resourceModel=model, message=f"Error deserializing dashboard: {e}") except ApiException as e: LOG.error( "Exception when calling DashboardsApi->create_dashboard: %s\n", e) return ProgressEvent(status=OperationStatus.FAILED, resourceModel=model, message=f"Error creating dashboard: {e}") return read_handler(session, request, callback_context)
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, )
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 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 False api_instance.delete_dashboard(dashboard_id, _preload_content=False) except ApiException as e: LOG.exception( "Exception when calling DashboardsApi->delete_dashboard: %s\n", e) return ProgressEvent( status=OperationStatus.FAILED, resourceModel=model, message=f"Error deleting dashboard: {e}", errorCode=http_to_handler_error_code(e.status), ) return ProgressEvent( status=OperationStatus.SUCCESS, resourceModel=None, )
def setUp(self): self.api = DashboardsApi() # noqa: E501