Skip to content

Commit

Permalink
DRY using GetTenantUID helper
Browse files Browse the repository at this point in the history
  • Loading branch information
devang-gaur committed Aug 1, 2024
1 parent 40d5a85 commit 4b590cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 53 deletions.
30 changes: 6 additions & 24 deletions client/macro.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package client

import (
"errors"

clientv1 "github.com/spectrocloud/palette-api-go/client/v1"
"github.com/spectrocloud/palette-api-go/models"
"github.com/spectrocloud/palette-sdk-go/client/apiutil"
Expand All @@ -17,14 +15,10 @@ func (h *V1Client) CreateMacro(uid string, macros *models.V1Macros) error {
_, err := h.Client.V1ProjectsUIDMacrosCreate(params)
return err
}
userInfo, err := h.GetUsersInfo()
tenantUID, err := h.GetTenantUID()
if err != nil {
return err
}
if userInfo == nil {
return errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosCreateParams().
WithTenantUID(tenantUID).
Expand Down Expand Up @@ -63,14 +57,10 @@ func (h *V1Client) GetMacros(projectUID string) ([]*models.V1Macro, error) {
}
macros = resp.Payload.Macros
} else {
userInfo, err := h.GetUsersInfo()
if err != nil {
tenantUID, err := h.GetTenantUID()
if err != nil || tenantUID == "" {
return nil, err
}
if userInfo == nil {
return nil, errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosListParams().
WithTenantUID(tenantUID)
Expand All @@ -93,14 +83,10 @@ func (h *V1Client) UpdateMacro(uid string, macros *models.V1Macros) error {
_, err := h.Client.V1ProjectsUIDMacrosUpdateByMacroName(params)
return err
}
userInfo, err := h.GetUsersInfo()
if err != nil {
tenantUID, err := h.GetTenantUID()
if err != nil || tenantUID == "" {
return err
}
if userInfo == nil {
return errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosUpdateByMacroNameParams().
WithTenantUID(tenantUID).
Expand All @@ -120,14 +106,10 @@ func (h *V1Client) DeleteMacro(uid string, body *models.V1Macros) error {
return err
}
} else {
userInfo, err := h.GetUsersInfo()
tenantUID, err := h.GetTenantUID()
if err != nil {
return err
}
if userInfo == nil {
return errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosDeleteByMacroNameParams().
WithTenantUID(tenantUID).
Expand Down
37 changes: 8 additions & 29 deletions client/macros.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"errors"
"fmt"

clientv1 "github.com/spectrocloud/palette-api-go/client/v1"
Expand All @@ -19,14 +18,10 @@ func (h *V1Client) CreateMacros(uid string, macros *models.V1Macros) (string, er
return "", err
}
} else {
userInfo, err := h.GetUsersInfo()
tenantUID, err := h.GetTenantUID()
if err != nil {
return "", err
}
if userInfo == nil {
return "", errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosCreateParams().
WithTenantUID(tenantUID).
Expand Down Expand Up @@ -96,14 +91,10 @@ func (h *V1Client) GetMacrosV2(projectUID string) ([]*models.V1Macro, error) {
}
return macrosListOk.Payload.Macros, nil
}
userInfo, err := h.GetUsersInfo()
if err != nil {
tenantUID, err := h.GetTenantUID()
if err != nil || tenantUID == "" {
return nil, err
}
if userInfo == nil {
return nil, errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosListParams().
WithTenantUID(tenantUID)
Expand All @@ -123,14 +114,10 @@ func (h *V1Client) UpdateMacros(uid string, macros *models.V1Macros) error {
_, err := h.Client.V1ProjectsUIDMacrosUpdate(params)
return err
}
userInfo, err := h.GetUsersInfo()
if err != nil {
tenantUID, err := h.GetTenantUID()
if err != nil || tenantUID == "" {
return err
}
if userInfo == nil {
return errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosUpdateParams().
WithTenantUID(tenantUID).
Expand All @@ -148,14 +135,10 @@ func (h *V1Client) DeleteMacros(uid string, body *models.V1Macros) error {
_, err := h.Client.V1ProjectsUIDMacrosDeleteByMacroName(params)
return err
}
userInfo, err := h.GetUsersInfo()
tenantUID, err := h.GetTenantUID()
if err != nil {
return err
}
if userInfo == nil {
return errors.New("empty userinfo received from GetUsersInfo()")
}
tenantUID := userInfo.TenantUID
// As discussed with hubble team, we should not set context for tenant macros.
params := clientv1.NewV1TenantsUIDMacrosDeleteByMacroNameParams().
WithTenantUID(tenantUID).
Expand All @@ -171,15 +154,11 @@ func (h *V1Client) GetMacrosID(uid string) (string, error) {
if uid != "" {
hashID = fmt.Sprintf("%s-%s-%s", "project", "macros", uid)
} else {
userInfo, err := h.GetUsersInfo()
tenantUID, err := h.GetTenantUID()
if err != nil {
return "", err
}
if userInfo == nil {
return "", errors.New("empty userinfo received from GetUsersInfo()")
}
tenantID := userInfo.TenantUID
hashID = fmt.Sprintf("%s-%s-%s", "tenant", "macros", tenantID)
hashID = fmt.Sprintf("%s-%s-%s", "tenant", "macros", tenantUID)
}
return hashID, nil
}
15 changes: 15 additions & 0 deletions client/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package client

import "errors"

// GetTenantUID retrieves the tenant UID of the authenticated user.
func (h *V1Client) GetTenantUID() (string, error) {
resp, err := h.GetUsersInfo()
if err != nil {
return "", err
}
if resp == nil {
return "", errors.New("empty response received from GetUsersInfo()")
}
return resp.TenantUID, nil
}

0 comments on commit 4b590cf

Please sign in to comment.