From ecb37d7cc40f613411e0409d829a3838a617ef8b Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Thu, 21 Feb 2019 16:16:28 -0800 Subject: [PATCH] fix(task): restore functionality for creating task with org name This is a partial rollback of changes #12004. Issue to track adding a test: #12089. --- http/task_service.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/http/task_service.go b/http/task_service.go index 9549c065b4..aa93d30169 100644 --- a/http/task_service.go +++ b/http/task_service.go @@ -435,6 +435,15 @@ func (h *TaskHandler) handlePostTask(w http.ResponseWriter, r *http.Request) { return } + if err := h.populateTaskCreateOrg(ctx, &req.TaskCreate); err != nil { + err = &platform.Error{ + Err: err, + Msg: "could not identify organization", + } + EncodeError(ctx, err, w) + return + } + if !req.TaskCreate.OrganizationID.Valid() { err := &platform.Error{ Code: platform.EInvalid, @@ -1126,6 +1135,31 @@ func decodeRetryRunRequest(ctx context.Context, r *http.Request) (*retryRunReque }, nil } +func (h *TaskHandler) populateTaskCreateOrg(ctx context.Context, tc *platform.TaskCreate) error { + if tc.OrganizationID.Valid() && tc.Organization != "" { + return nil + } + + if !tc.OrganizationID.Valid() && tc.Organization == "" { + return errors.New("missing orgID and organization name") + } + + if tc.OrganizationID.Valid() { + o, err := h.OrganizationService.FindOrganizationByID(ctx, tc.OrganizationID) + if err != nil { + return err + } + tc.Organization = o.Name + } else { + o, err := h.OrganizationService.FindOrganization(ctx, platform.OrganizationFilter{Name: &tc.Organization}) + if err != nil { + return err + } + tc.OrganizationID = o.ID + } + return nil +} + // TaskService connects to Influx via HTTP using tokens to manage tasks. type TaskService struct { Addr string