diff --git a/http/notification_rule.go b/http/notification_rule.go index 4259db7911..c0724d5882 100644 --- a/http/notification_rule.go +++ b/http/notification_rule.go @@ -20,11 +20,12 @@ type NotificationRuleBackend struct { influxdb.HTTPErrorHandler Logger *zap.Logger - NotificationRuleStore influxdb.NotificationRuleStore - UserResourceMappingService influxdb.UserResourceMappingService - LabelService influxdb.LabelService - UserService influxdb.UserService - OrganizationService influxdb.OrganizationService + NotificationRuleStore influxdb.NotificationRuleStore + NotificationEndpointService influxdb.NotificationEndpointService + UserResourceMappingService influxdb.UserResourceMappingService + LabelService influxdb.LabelService + UserService influxdb.UserService + OrganizationService influxdb.OrganizationService } // NewNotificationRuleBackend returns a new instance of NotificationRuleBackend. @@ -33,11 +34,12 @@ func NewNotificationRuleBackend(b *APIBackend) *NotificationRuleBackend { HTTPErrorHandler: b.HTTPErrorHandler, Logger: b.Logger.With(zap.String("handler", "notification_rule")), - NotificationRuleStore: b.NotificationRuleStore, - UserResourceMappingService: b.UserResourceMappingService, - LabelService: b.LabelService, - UserService: b.UserService, - OrganizationService: b.OrganizationService, + NotificationRuleStore: b.NotificationRuleStore, + NotificationEndpointService: b.NotificationEndpointService, + UserResourceMappingService: b.UserResourceMappingService, + LabelService: b.LabelService, + UserService: b.UserService, + OrganizationService: b.OrganizationService, } } @@ -47,16 +49,18 @@ type NotificationRuleHandler struct { influxdb.HTTPErrorHandler Logger *zap.Logger - NotificationRuleStore influxdb.NotificationRuleStore - UserResourceMappingService influxdb.UserResourceMappingService - LabelService influxdb.LabelService - UserService influxdb.UserService - OrganizationService influxdb.OrganizationService + NotificationRuleStore influxdb.NotificationRuleStore + NotificationEndpointService influxdb.NotificationEndpointService + UserResourceMappingService influxdb.UserResourceMappingService + LabelService influxdb.LabelService + UserService influxdb.UserService + OrganizationService influxdb.OrganizationService } const ( notificationRulesPath = "/api/v2/notificationRules" notificationRulesIDPath = "/api/v2/notificationRules/:id" + notificationRulesIDQueryPath = "/api/v2/notificationRules/:id/query" notificationRulesIDMembersPath = "/api/v2/notificationRules/:id/members" notificationRulesIDMembersIDPath = "/api/v2/notificationRules/:id/members/:userID" notificationRulesIDOwnersPath = "/api/v2/notificationRules/:id/owners" @@ -72,15 +76,17 @@ func NewNotificationRuleHandler(b *NotificationRuleBackend) *NotificationRuleHan HTTPErrorHandler: b.HTTPErrorHandler, Logger: b.Logger, - NotificationRuleStore: b.NotificationRuleStore, - UserResourceMappingService: b.UserResourceMappingService, - LabelService: b.LabelService, - UserService: b.UserService, - OrganizationService: b.OrganizationService, + NotificationRuleStore: b.NotificationRuleStore, + NotificationEndpointService: b.NotificationEndpointService, + UserResourceMappingService: b.UserResourceMappingService, + LabelService: b.LabelService, + UserService: b.UserService, + OrganizationService: b.OrganizationService, } h.HandlerFunc("POST", notificationRulesPath, h.handlePostNotificationRule) h.HandlerFunc("GET", notificationRulesPath, h.handleGetNotificationRules) h.HandlerFunc("GET", notificationRulesIDPath, h.handleGetNotificationRule) + h.HandlerFunc("GET", notificationRulesIDQueryPath, h.handleGetNotificationRuleQuery) h.HandlerFunc("DELETE", notificationRulesIDPath, h.handleDeleteNotificationRule) h.HandlerFunc("PUT", notificationRulesIDPath, h.handlePutNotificationRule) h.HandlerFunc("PATCH", notificationRulesIDPath, h.handlePatchNotificationRule) @@ -230,6 +236,39 @@ func (h *NotificationRuleHandler) handleGetNotificationRules(w http.ResponseWrit } } +func (h *NotificationRuleHandler) handleGetNotificationRuleQuery(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + id, err := decodeGetNotificationRuleRequest(ctx, r) + if err != nil { + h.HandleHTTPError(ctx, err, w) + return + } + nr, err := h.NotificationRuleStore.FindNotificationRuleByID(ctx, id) + if err != nil { + h.HandleHTTPError(ctx, err, w) + return + } + edp, err := h.NotificationEndpointService.FindNotificationEndpointByID(ctx, nr.GetEndpointID()) + if err != nil { + h.HandleHTTPError(ctx, &influxdb.Error{ + Code: influxdb.EInternal, + Op: "http/handleGetNotificationRuleQuery", + Err: err, + }, w) + return + } + flux, err := nr.GenerateFlux(edp) + if err != nil { + h.HandleHTTPError(ctx, err, w) + return + } + h.Logger.Debug("notification rule query retrieved", zap.String("notificationRuleQuery", fmt.Sprint(flux))) + if err := encodeResponse(ctx, w, http.StatusOK, newFluxResponse(flux)); err != nil { + logEncodingError(h.Logger, r, err) + return + } +} + func (h *NotificationRuleHandler) handleGetNotificationRule(w http.ResponseWriter, r *http.Request) { ctx := r.Context() h.Logger.Debug("notification rule retrieve request", zap.String("r", fmt.Sprint(r))) diff --git a/http/swagger.yml b/http/swagger.yml index b157394199..97556a62ce 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -5691,6 +5691,45 @@ paths: application/json: schema: $ref: "#/components/schemas/Error" + '/notificationRules/{ruleID}/query': + get: + operationId: GetNotificationRulesIDQuery + tags: + - Rules + summary: Get a notification rule query + parameters: + - $ref: '#/components/parameters/TraceSpan' + - in: path + name: ruleID + schema: + type: string + required: true + description: ID of notification rule + responses: + '200': + description: the notification rule query requested + content: + application/json: + schema: + $ref: "#/components/schemas/FluxResponse" + '400': + description: invalid request + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + '404': + description: notification rule not found + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" /notificationEndpoints: get: operationId: GetNotificationEndpoints