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 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, ) 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 resp = api_instance.create_dashboard(json_payload, _check_input_type=False, _preload_content=False) json_dict = json.loads(resp.data) model.Id = json_dict["id"] 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->create_dashboard: %s\n", e) return ProgressEvent(status=OperationStatus.FAILED, resourceModel=model, message=f"Error creating 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)