Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
feat(warden): updated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudheer Pal committed Oct 20, 2023
1 parent d2968d8 commit c284a9e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 36 deletions.
8 changes: 4 additions & 4 deletions internal/server/v1/iam/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/server/v1/iam/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
15 changes: 8 additions & 7 deletions internal/server/v1/iam/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
}
18 changes: 3 additions & 15 deletions warden/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions warden/error.go
Original file line number Diff line number Diff line change
@@ -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")
)
16 changes: 8 additions & 8 deletions warden/warden.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -30,11 +38,3 @@ type teamListResponse struct {
type teamsData struct {
Teams []Team `json:"teams"`
}

type TeamListRequest struct {
Email string
}

type TeamByUUIDRequest struct {
TeamUUID string
}

0 comments on commit c284a9e

Please sign in to comment.