diff --git a/internal/server/v1/iam/handler.go b/internal/server/v1/iam/handler.go index 81af7ee..d92e165 100644 --- a/internal/server/v1/iam/handler.go +++ b/internal/server/v1/iam/handler.go @@ -17,7 +17,7 @@ func NewHandler(service *Service) *handler { return &handler{service: service} } -func (h *handler) teamList(w http.ResponseWriter, r *http.Request) { +func (h *handler) listUserWardenTeams(w http.ResponseWriter, r *http.Request) { reqCtx := reqctx.From(r.Context()) const errEmailMissedInHeader = "user email not in header" @@ -26,7 +26,7 @@ func (h *handler) teamList(w http.ResponseWriter, r *http.Request) { return } - teamListResp, err := h.service.TeamList(r.Context(), reqCtx.UserEmail) + teamListResp, err := h.service.UserWardenTeamList(r.Context(), reqCtx.UserEmail) if err != nil { utils.WriteErr(w, err) return @@ -37,7 +37,7 @@ func (h *handler) teamList(w http.ResponseWriter, r *http.Request) { }) } -func (h *handler) updateGroupMetadata(w http.ResponseWriter, r *http.Request) { +func (h *handler) linkGroupToWarden(w http.ResponseWriter, r *http.Request) { groupID := chi.URLParam(r, "group_id") var body struct { @@ -51,7 +51,7 @@ func (h *handler) updateGroupMetadata(w http.ResponseWriter, r *http.Request) { return } - resShield, err := h.service.UpdateGroupMetadata(r.Context(), groupID, body.WardenTeamID) + resShield, err := h.service.LinkGroupToWarden(r.Context(), groupID, body.WardenTeamID) if err != nil { utils.WriteErr(w, err) return diff --git a/internal/server/v1/iam/routes.go b/internal/server/v1/iam/routes.go index c30b616..38cf301 100644 --- a/internal/server/v1/iam/routes.go +++ b/internal/server/v1/iam/routes.go @@ -9,8 +9,8 @@ func Routes(shieldClient shieldv1beta1rpc.ShieldServiceClient, wardenClient Ward service := NewService(shieldClient, wardenClient) handler := NewHandler(service) return func(r chiv5.Router) { - r.Get("/users/me/warden_teams", handler.teamList) + r.Get("/users/me/warden_teams", handler.listUserWardenTeams) - r.Put("/groups/{group_id}/metadata/warden", handler.updateGroupMetadata) + r.Put("/groups/{group_id}/metadata/warden", handler.linkGroupToWarden) } } diff --git a/internal/server/v1/iam/service.go b/internal/server/v1/iam/service.go index 278cae7..20d2075 100644 --- a/internal/server/v1/iam/service.go +++ b/internal/server/v1/iam/service.go @@ -2,6 +2,7 @@ package iam import ( "context" + "fmt" shieldv1beta1rpc "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/shield/v1beta1/shieldv1beta1grpc" shieldv1beta1 "buf.build/gen/go/gotocompany/proton/protocolbuffers/go/gotocompany/shield/v1beta1" @@ -29,7 +30,7 @@ func NewService(shieldClient shieldv1beta1rpc.ShieldServiceClient, wardenClient } } -func (svc *Service) TeamList(ctx context.Context, userEmail string) ([]warden.Team, error) { +func (svc *Service) UserWardenTeamList(ctx context.Context, userEmail string) ([]warden.Team, error) { teams, err := svc.wardenClient.ListUserTeams(ctx, warden.TeamListRequest{ Email: userEmail, }) @@ -40,7 +41,7 @@ func (svc *Service) TeamList(ctx context.Context, userEmail string) ([]warden.Te return teams, nil } -func (svc *Service) UpdateGroupMetadata(ctx context.Context, groupID, wardenTeamID string) (map[string]any, error) { +func (svc *Service) LinkGroupToWarden(ctx context.Context, groupID, wardenTeamID string) (map[string]any, error) { team, err := svc.wardenClient.TeamByUUID(ctx, warden.TeamByUUIDRequest{ TeamUUID: wardenTeamID, }) @@ -52,7 +53,7 @@ func (svc *Service) UpdateGroupMetadata(ctx context.Context, groupID, wardenTeam Id: groupID, }) if err != nil { - return nil, err + return nil, fmt.Errorf("error getting shield group: %w", err) } group := getGroupRes.Group @@ -69,10 +70,10 @@ func (svc *Service) UpdateGroupMetadata(ctx context.Context, groupID, wardenTeam updatedMetaData, err := structpb.NewStruct(metaData) if err != nil { - return nil, err + return nil, fmt.Errorf("error creating metadata struct: %w", err) } - UpdatedGroupRes, err := svc.shieldClient.UpdateGroup(ctx, &shieldv1beta1.UpdateGroupRequest{ + updatedGroupRes, err := svc.shieldClient.UpdateGroup(ctx, &shieldv1beta1.UpdateGroupRequest{ Id: groupID, Body: &shieldv1beta1.GroupRequestBody{ Metadata: updatedMetaData, @@ -82,8 +83,8 @@ func (svc *Service) UpdateGroupMetadata(ctx context.Context, groupID, wardenTeam }, }) if err != nil { - return nil, err + return nil, fmt.Errorf("error updating group: %w", err) } - return UpdatedGroupRes.Group.Metadata.AsMap(), nil + return updatedGroupRes.Group.Metadata.AsMap(), nil } diff --git a/warden/client.go b/warden/client.go index a13e028..60e1e8a 100644 --- a/warden/client.go +++ b/warden/client.go @@ -22,13 +22,7 @@ func NewClient(baseURL string) *Client { } func (c *Client) ListUserTeams(ctx context.Context, req TeamListRequest) ([]Team, error) { - const ( - endpoint = "/api/v1" - userPath = "/users/" - teamsEndpoint = "/teams" - ) - url := fmt.Sprintf("%s%s%s%s%s", c.BaseURL, endpoint, userPath, req.Email, teamsEndpoint) - fmt.Println("URL: ", url) + url := fmt.Sprintf("%s/api/v1/users/%s/teams", c.BaseURL, req.Email) httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err @@ -51,19 +45,13 @@ func (c *Client) ListUserTeams(ctx context.Context, req TeamListRequest) ([]Team var data teamListResponse err = json.NewDecoder(resp.Body).Decode(&data) if err != nil { - return nil, err + return nil, fmt.Errorf("error decoding warden teamListResponse: %w", err) } - return data.Data.Teams, nil } func (c *Client) TeamByUUID(ctx context.Context, req TeamByUUIDRequest) (*Team, error) { - const ( - endpoint = "/api/v2" - teamPath = "/teams/" - ) - - url := fmt.Sprintf("%s%s%s%s", c.BaseURL, endpoint, teamPath, req.TeamUUID) + url := fmt.Sprintf("%s/api/v2/teams/%s", c.BaseURL, req.TeamUUID) httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err diff --git a/warden/error.go b/warden/error.go new file mode 100644 index 0000000..a3e68ea --- /dev/null +++ b/warden/error.go @@ -0,0 +1,8 @@ +package warden + +import "github.com/goto/dex/pkg/errors" + +var ( + ErrOptimusHostNotFound = errors.New("No Optimus jobs found in this project") + ErrOptimusHostInvalid = errors.New("Optimus host is not valid") +) diff --git a/warden/warden.go b/warden/warden.go index 4923e92..2661c42 100644 --- a/warden/warden.go +++ b/warden/warden.go @@ -17,6 +17,14 @@ type Team struct { ShortCode string `json:"short_code"` } +type TeamListRequest struct { + Email string +} + +type TeamByUUIDRequest struct { + TeamUUID string +} + type teamResponse struct { Success bool `json:"success"` Data Team `json:"data"` @@ -30,11 +38,3 @@ type teamListResponse struct { type teamsData struct { Teams []Team `json:"teams"` } - -type TeamListRequest struct { - Email string -} - -type TeamByUUIDRequest struct { - TeamUUID string -}