Skip to content

Commit

Permalink
feat(influxdb): Add labels create endpoints
Browse files Browse the repository at this point in the history
- Notification Rules
- Notification Endpoints
- Checks

Co-authored-by: Deniz Kusefoglu <deniz@influxdata.com>
  • Loading branch information
bthesorceror and ebb-tide committed Oct 23, 2019
1 parent 0fc47bc commit 88bf178
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 80 deletions.
70 changes: 61 additions & 9 deletions http/check_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ type checkResponse struct {
Links checkLinks `json:"links"`
}

type postCheckRequest struct {
influxdb.CheckCreate
Labels []string `json:"labels"`
}

type decodeLabels struct {
Labels []string `json:"labels"`
}

func (resp checkResponse) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(resp.Check)
if err != nil {
Expand Down Expand Up @@ -349,21 +358,21 @@ type decodeStatus struct {
Status influxdb.Status `json:"status"`
}

func decodePostCheckRequest(ctx context.Context, r *http.Request) (influxdb.CheckCreate, error) {
var cc influxdb.CheckCreate
func decodePostCheckRequest(ctx context.Context, r *http.Request) (postCheckRequest, error) {
var req postCheckRequest

buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
if err != nil {
return cc, &influxdb.Error{
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
defer r.Body.Close()
chk, err := check.UnmarshalJSON(buf.Bytes())
if err != nil {
return cc, &influxdb.Error{
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
Expand All @@ -372,15 +381,24 @@ func decodePostCheckRequest(ctx context.Context, r *http.Request) (influxdb.Chec
var ds decodeStatus
err = json.Unmarshal(buf.Bytes(), &ds)
if err != nil {
return cc, &influxdb.Error{
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

cc = influxdb.CheckCreate{Check: chk, Status: ds.Status}
var dl decodeLabels
err = json.Unmarshal(buf.Bytes(), &dl)
if err != nil {
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

return cc, nil
req = postCheckRequest{CheckCreate: influxdb.CheckCreate{Check: chk, Status: ds.Status}, Labels: dl.Labels}

return req, nil
}

func decodePutCheckRequest(ctx context.Context, r *http.Request) (influxdb.CheckCreate, error) {
Expand Down Expand Up @@ -498,13 +516,15 @@ func (h *CheckHandler) handlePostCheck(w http.ResponseWriter, r *http.Request) {
return
}

if err := h.CheckService.CreateCheck(ctx, chk, auth.GetUserID()); err != nil {
if err := h.CheckService.CreateCheck(ctx, chk.CheckCreate, auth.GetUserID()); err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
h.Logger.Debug("check created", zap.String("check", fmt.Sprint(chk)))

cr, err := h.newCheckResponse(ctx, chk, []*influxdb.Label{})
labels := h.mapNewCheckLabels(ctx, chk.CheckCreate, chk.Labels)

cr, err := h.newCheckResponse(ctx, chk, labels)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
Expand All @@ -516,6 +536,38 @@ func (h *CheckHandler) handlePostCheck(w http.ResponseWriter, r *http.Request) {
}
}

// mapNewCheckLabels takes label ids from create check and maps them to the newly created check
func (h *CheckHandler) mapNewCheckLabels(ctx context.Context, chk influxdb.CheckCreate, labels []string) []*influxdb.Label {
var ls []*influxdb.Label
for _, sid := range labels {
var lid influxdb.ID
err := lid.DecodeFromString(sid)

if err != nil {
continue
}

label, err := h.LabelService.FindLabelByID(ctx, lid)
if err != nil {
continue
}

mapping := influxdb.LabelMapping{
LabelID: label.ID,
ResourceID: chk.GetID(),
ResourceType: influxdb.ChecksResourceType,
}

err = h.LabelService.CreateLabelMapping(ctx, &mapping)
if err != nil {
continue
}

ls = append(ls, label)
}
return ls
}

// handlePutCheck is the HTTP handler for the PUT /api/v2/checks route.
func (h *CheckHandler) handlePutCheck(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
63 changes: 57 additions & 6 deletions http/notification_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ type notificationEndpointResponse struct {
Links notificationEndpointLinks `json:"links"`
}

type postNotificationEndpointRequest struct {
influxdb.NotificationEndpoint
Labels []string `json:"labels"`
}

func (resp notificationEndpointResponse) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(resp.NotificationEndpoint)
if err != nil {
Expand Down Expand Up @@ -283,24 +288,37 @@ func decodeNotificationEndpointFilter(ctx context.Context, r *http.Request) (*in
return f, opts, err
}

func decodePostNotificationEndpointRequest(ctx context.Context, r *http.Request) (influxdb.NotificationEndpoint, error) {
func decodePostNotificationEndpointRequest(ctx context.Context, r *http.Request) (postNotificationEndpointRequest, error) {
var req postNotificationEndpointRequest
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
if err != nil {
return nil, &influxdb.Error{
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
defer r.Body.Close()
edp, err := endpoint.UnmarshalJSON(buf.Bytes())
if err != nil {
return nil, &influxdb.Error{
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
return edp, nil

var dl decodeLabels
if err := json.Unmarshal(buf.Bytes(), &dl); err != nil {
return req, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

req.NotificationEndpoint = edp
req.Labels = dl.Labels

return req, nil
}

func decodePutNotificationEndpointRequest(ctx context.Context, r *http.Request) (influxdb.NotificationEndpoint, error) {
Expand Down Expand Up @@ -392,7 +410,7 @@ func (h *NotificationEndpointHandler) handlePostNotificationEndpoint(w http.Resp
return
}

if err := h.NotificationEndpointService.CreateNotificationEndpoint(ctx, edp, auth.GetUserID()); err != nil {
if err := h.NotificationEndpointService.CreateNotificationEndpoint(ctx, edp.NotificationEndpoint, auth.GetUserID()); err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
Expand All @@ -409,14 +427,47 @@ func (h *NotificationEndpointHandler) handlePostNotificationEndpoint(w http.Resp
}
}

labels := h.mapNewNotificationEndpointLabels(ctx, edp.NotificationEndpoint, edp.Labels)

h.Logger.Debug("notificationEndpoint created", zap.String("notificationEndpoint", fmt.Sprint(edp)))

if err := encodeResponse(ctx, w, http.StatusCreated, newNotificationEndpointResponse(edp, []*influxdb.Label{})); err != nil {
if err := encodeResponse(ctx, w, http.StatusCreated, newNotificationEndpointResponse(edp, labels)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
}

func (h *NotificationEndpointHandler) mapNewNotificationEndpointLabels(ctx context.Context, nre influxdb.NotificationEndpoint, labels []string) []*influxdb.Label {
var ls []*influxdb.Label
for _, sid := range labels {
var lid influxdb.ID
err := lid.DecodeFromString(sid)

if err != nil {
continue
}

label, err := h.LabelService.FindLabelByID(ctx, lid)
if err != nil {
continue
}

mapping := influxdb.LabelMapping{
LabelID: label.ID,
ResourceID: nre.GetID(),
ResourceType: influxdb.NotificationEndpointResourceType,
}

err = h.LabelService.CreateLabelMapping(ctx, &mapping)
if err != nil {
continue
}

ls = append(ls, label)
}
return ls
}

// handlePutNotificationEndpoint is the HTTP handler for the PUT /api/v2/notificationEndpoints route.
func (h *NotificationEndpointHandler) handlePutNotificationEndpoint(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
69 changes: 58 additions & 11 deletions http/notification_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,37 +389,51 @@ func decodeUserResourceMappingFilter(ctx context.Context, r *http.Request, typ i
return f, nil
}

func decodePostNotificationRuleRequest(ctx context.Context, r *http.Request) (influxdb.NotificationRuleCreate, error) {
var nrc influxdb.NotificationRuleCreate
type postNotificationRuleRequest struct {
influxdb.NotificationRuleCreate
Labels []string `json:"labels"`
}

func decodePostNotificationRuleRequest(ctx context.Context, r *http.Request) (postNotificationRuleRequest, error) {
var pnrr postNotificationRuleRequest
var sts statusDecode
var dl decodeLabels

buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
if err != nil {
return nrc, &influxdb.Error{
return pnrr, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
defer r.Body.Close()

nr, err := rule.UnmarshalJSON(buf.Bytes())
if err != nil {
return nrc, &influxdb.Error{
return pnrr, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

if err := json.Unmarshal(buf.Bytes(), &sts); err != nil {
return nrc, err
return pnrr, err
}

nrc = influxdb.NotificationRuleCreate{
NotificationRule: nr,
Status: *sts.Status,
if err := json.Unmarshal(buf.Bytes(), &dl); err != nil {
return pnrr, err
}

return nrc, nil
pnrr = postNotificationRuleRequest{
NotificationRuleCreate: influxdb.NotificationRuleCreate{
NotificationRule: nr,
Status: *sts.Status,
},
Labels: dl.Labels,
}

return pnrr, nil
}

func decodePutNotificationRuleRequest(ctx context.Context, r *http.Request) (influxdb.NotificationRuleCreate, error) {
Expand Down Expand Up @@ -525,13 +539,15 @@ func (h *NotificationRuleHandler) handlePostNotificationRule(w http.ResponseWrit
return
}

if err := h.NotificationRuleStore.CreateNotificationRule(ctx, nr, auth.GetUserID()); err != nil {
if err := h.NotificationRuleStore.CreateNotificationRule(ctx, nr.NotificationRuleCreate, auth.GetUserID()); err != nil {
h.HandleHTTPError(ctx, err, w)
return
}
h.Logger.Debug("notification rule created", zap.String("notificationRule", fmt.Sprint(nr)))

res, err := h.newNotificationRuleResponse(ctx, nr, []*influxdb.Label{})
labels := h.mapNewNotificationRuleLabels(ctx, nr.NotificationRuleCreate, nr.Labels)

res, err := h.newNotificationRuleResponse(ctx, nr, labels)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
Expand All @@ -543,6 +559,37 @@ func (h *NotificationRuleHandler) handlePostNotificationRule(w http.ResponseWrit
}
}

func (h *NotificationRuleHandler) mapNewNotificationRuleLabels(ctx context.Context, nrc influxdb.NotificationRuleCreate, labels []string) []*influxdb.Label {
var ls []*influxdb.Label
for _, sid := range labels {
var lid influxdb.ID
err := lid.DecodeFromString(sid)

if err != nil {
continue
}

label, err := h.LabelService.FindLabelByID(ctx, lid)
if err != nil {
continue
}

mapping := influxdb.LabelMapping{
LabelID: label.ID,
ResourceID: nrc.GetID(),
ResourceType: influxdb.NotificationRuleResourceType,
}

err = h.LabelService.CreateLabelMapping(ctx, &mapping)
if err != nil {
continue
}

ls = append(ls, label)
}
return ls
}

// handlePutNotificationRule is the HTTP handler for the PUT /api/v2/notificationRule route.
func (h *NotificationRuleHandler) handlePutNotificationRule(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
Loading

0 comments on commit 88bf178

Please sign in to comment.