From 4b590cff63ba21047dc9b4ec948202bbac9098ae Mon Sep 17 00:00:00 2001 From: Devang Gaur Date: Thu, 1 Aug 2024 19:14:51 +0530 Subject: [PATCH] DRY using GetTenantUID helper --- client/macro.go | 30 ++++++------------------------ client/macros.go | 37 ++++++++----------------------------- client/tenant.go | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 53 deletions(-) create mode 100644 client/tenant.go diff --git a/client/macro.go b/client/macro.go index 51753e7c..26206154 100644 --- a/client/macro.go +++ b/client/macro.go @@ -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" @@ -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). @@ -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) @@ -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). @@ -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). diff --git a/client/macros.go b/client/macros.go index 488d7df3..99cc728d 100644 --- a/client/macros.go +++ b/client/macros.go @@ -1,7 +1,6 @@ package client import ( - "errors" "fmt" clientv1 "github.com/spectrocloud/palette-api-go/client/v1" @@ -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). @@ -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) @@ -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). @@ -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). @@ -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 } diff --git a/client/tenant.go b/client/tenant.go new file mode 100644 index 00000000..1b87f314 --- /dev/null +++ b/client/tenant.go @@ -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 +}