From 4fd38cab22f41156943e9bfe4e01bd0e2798caf1 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Tue, 16 Jan 2024 12:36:41 +0530 Subject: [PATCH 1/3] PLT-701: Added new macros support in sdk --- client/macro.go | 140 +++++++++++++++++++++++++++++++++++++++++++++++ client/macros.go | 87 ++++++++++++++++------------- 2 files changed, 189 insertions(+), 38 deletions(-) create mode 100644 client/macro.go diff --git a/client/macro.go b/client/macro.go new file mode 100644 index 00000000..19291924 --- /dev/null +++ b/client/macro.go @@ -0,0 +1,140 @@ +package client + +import ( + "hash/fnv" + "strconv" + + "github.com/spectrocloud/hapi/models" + userC "github.com/spectrocloud/hapi/user/client/v1" +) + +func (h *V1Client) CreateMacro(uid string, macros *models.V1Macros) error { + if uid != "" { + params := userC.NewV1ProjectsUIDMacrosCreateParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) + _, err := h.GetUserClient().V1ProjectsUIDMacrosCreate(params) + if err != nil { + return err + } + } else { + tenantUID, err := h.GetTenantUID() + if err != nil { + return err + } + // As discussed with hubble team, we should not set context for tenant macros. + params := userC.NewV1TenantsUIDMacrosCreateParams().WithTenantUID(tenantUID).WithBody(macros) + _, err = h.GetUserClient().V1TenantsUIDMacrosCreate(params) + if err != nil { + return err + } + } + + return nil +} + +func (h *V1Client) GetMacro(name, projectUID string) (*models.V1Macro, error) { + macros, err := h.GetMacros(projectUID) + if err != nil { + return nil, err + } + id := h.GetMacroId(projectUID, name) + + for _, macro := range macros { + if h.GetMacroId(projectUID, macro.Name) == id { + return macro, nil + } + } + + return nil, nil +} + +func (h *V1Client) GetMacros(projectUID string) ([]*models.V1Macro, error) { + var macros []*models.V1Macro + + if projectUID != "" { + params := userC.NewV1ProjectsUIDMacrosListParams().WithContext(h.Ctx).WithUID(projectUID) + macrosListOk, err := h.GetUserClient().V1ProjectsUIDMacrosList(params) + if err != nil { + return nil, err + } + macros = macrosListOk.Payload.Macros + + } else { + tenantUID, err := h.GetTenantUID() + if err != nil || tenantUID == "" { + return nil, err + } + // As discussed with hubble team, we should not set context for tenant macros. + params := userC.NewV1TenantsUIDMacrosListParams().WithTenantUID(tenantUID) + macrosListOk, err := h.GetUserClient().V1TenantsUIDMacrosList(params) + if err != nil { + return nil, err + } + macros = macrosListOk.Payload.Macros + + } + + return macros, nil +} + +func (h *V1Client) StringHash(name string) string { + return strconv.FormatUint(uint64(hash(name)), 10) +} + +func hash(s string) uint32 { + h := fnv.New32a() + _, _ = h.Write([]byte(s)) + return h.Sum32() +} + +func (h *V1Client) UpdateMacro(uid string, macros *models.V1Macros) error { + if uid != "" { + params := userC.NewV1ProjectsUIDMacrosUpdateByMacroNameParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) + _, err := h.GetUserClient().V1ProjectsUIDMacrosUpdateByMacroName(params) + return err + } else { + tenantUID, err := h.GetTenantUID() + if err != nil || tenantUID == "" { + return err + } + // As discussed with hubble team, we should not set context for tenant macros. + params := userC.NewV1TenantsUIDMacrosUpdateByMacroNameParams().WithTenantUID(tenantUID).WithBody(macros) + _, err = h.GetUserClient().V1TenantsUIDMacrosUpdateByMacroName(params) + return err + } +} + +func (h *V1Client) DeleteMacro(uid string, body *models.V1Macros) error { + if uid != "" { + params := userC.NewV1ProjectsUIDMacrosDeleteByMacroNameParams().WithContext(h.Ctx).WithUID(uid).WithBody(body) + _, err := h.GetUserClient().V1ProjectsUIDMacrosDeleteByMacroName(params) + if err != nil { + return err + } + } else { + tenantUID, err := h.GetTenantUID() + if err != nil { + return err + } + // As discussed with hubble team, we should not set context for tenant macros. + params := userC.NewV1TenantsUIDMacrosDeleteByMacroNameParams().WithTenantUID(tenantUID).WithBody(body) + _, err = h.GetUserClient().V1TenantsUIDMacrosDeleteByMacroName(params) + if err != nil { + return err + } + } + _, err := h.GetMacros(uid) + if err != nil { + return err + } + return nil +} + +func (h *V1Client) GetMacroId(uid, name string) string { + var hash string + if uid != "" { + hash = h.StringHash(name + uid) + } else { + hash = h.StringHash(name + "%tenant") + } + return hash +} diff --git a/client/macros.go b/client/macros.go index beba8bcd..dbb181be 100644 --- a/client/macros.go +++ b/client/macros.go @@ -1,53 +1,74 @@ package client import ( - "hash/fnv" - "strconv" - + "fmt" "github.com/spectrocloud/hapi/models" userC "github.com/spectrocloud/hapi/user/client/v1" ) -func (h *V1Client) CreateMacros(uid string, macros *models.V1Macros) error { +func (h *V1Client) CreateMacros(uid string, macros *models.V1Macros) (string, error) { + if uid != "" { params := userC.NewV1ProjectsUIDMacrosCreateParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) _, err := h.GetUserClient().V1ProjectsUIDMacrosCreate(params) if err != nil { - return err + return "", err } } else { tenantUID, err := h.GetTenantUID() if err != nil { - return err + return "", err } - // As discussed with hubble team, we should not set context for tenant macros. params := userC.NewV1TenantsUIDMacrosCreateParams().WithTenantUID(tenantUID).WithBody(macros) _, err = h.GetUserClient().V1TenantsUIDMacrosCreate(params) if err != nil { - return err + return "", err } } - return nil + return h.GetMacrosId(uid), nil } -func (h *V1Client) GetMacro(name, projectUID string) (*models.V1Macro, error) { - macros, err := h.GetMacros(projectUID) +func (h *V1Client) GetTFMacrosV2(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) { + allMacros, err := h.GetMacrosV2(projectUID) + if err != nil { return nil, err } - id := h.GetMacroId(projectUID, name) + var tfMacros []*models.V1Macro + + for _, v := range allMacros { + if value, ok := tfMacrosMap[v.Name]; ok && value != nil { + tfMacros = append(tfMacros, &models.V1Macro{ + Name: v.Name, + Value: v.Value, + }) + } + } - for _, macro := range macros { - if h.GetMacroId(projectUID, macro.Name) == id { - return macro, nil + return tfMacros, nil +} + +func (h *V1Client) GetExistMacros(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) { + allMacros, err := h.GetMacrosV2(projectUID) + var existMacros []*models.V1Macro + if err != nil { + return nil, err + } + for _, v := range allMacros { + if _, ok := tfMacrosMap[v.Name]; !ok { + existMacros = append(existMacros, &models.V1Macro{ + Name: v.Name, + Value: v.Value, + }) } } - return nil, nil + return existMacros, nil + } -func (h *V1Client) GetMacros(projectUID string) ([]*models.V1Macro, error) { +func (h *V1Client) GetMacrosV2(projectUID string) ([]*models.V1Macro, error) { var macros []*models.V1Macro if projectUID != "" { @@ -76,20 +97,10 @@ func (h *V1Client) GetMacros(projectUID string) ([]*models.V1Macro, error) { return macros, nil } -func (h *V1Client) StringHash(name string) string { - return strconv.FormatUint(uint64(hash(name)), 10) -} - -func hash(s string) uint32 { - h := fnv.New32a() - _, _ = h.Write([]byte(s)) - return h.Sum32() -} - func (h *V1Client) UpdateMacros(uid string, macros *models.V1Macros) error { if uid != "" { - params := userC.NewV1ProjectsUIDMacrosUpdateByMacroNameParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) - _, err := h.GetUserClient().V1ProjectsUIDMacrosUpdateByMacroName(params) + params := userC.NewV1ProjectsUIDMacrosUpdateParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) + _, err := h.GetUserClient().V1ProjectsUIDMacrosUpdate(params) return err } else { tenantUID, err := h.GetTenantUID() @@ -97,8 +108,8 @@ func (h *V1Client) UpdateMacros(uid string, macros *models.V1Macros) error { return err } // As discussed with hubble team, we should not set context for tenant macros. - params := userC.NewV1TenantsUIDMacrosUpdateByMacroNameParams().WithTenantUID(tenantUID).WithBody(macros) - _, err = h.GetUserClient().V1TenantsUIDMacrosUpdateByMacroName(params) + params := userC.NewV1TenantsUIDMacrosUpdateParams().WithTenantUID(tenantUID).WithBody(macros) + _, err = h.GetUserClient().V1TenantsUIDMacrosUpdate(params) return err } } @@ -122,19 +133,19 @@ func (h *V1Client) DeleteMacros(uid string, body *models.V1Macros) error { return err } } - _, err := h.GetMacros(uid) - if err != nil { - return err - } return nil } -func (h *V1Client) GetMacroId(uid, name string) string { +func (h *V1Client) GetMacrosId(uid string) string { var hash string if uid != "" { - hash = h.StringHash(name + uid) + hash = fmt.Sprintf("%s-%s-%s", "project", "macros", uid) } else { - hash = h.StringHash(name + "%tenant") + tenantID, err := h.GetTenantUID() + if err != nil { + return "" + } + hash = fmt.Sprintf("%s-%s-%s", "tenant", "macros", tenantID) } return hash } From 4ed4288d67c543d9848c47b5e498f6e579f494f1 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Fri, 19 Jan 2024 12:13:45 +0530 Subject: [PATCH 2/3] Addressed review --- client/macros.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/client/macros.go b/client/macros.go index dbb181be..875a7f5d 100644 --- a/client/macros.go +++ b/client/macros.go @@ -25,8 +25,11 @@ func (h *V1Client) CreateMacros(uid string, macros *models.V1Macros) (string, er return "", err } } - - return h.GetMacrosId(uid), nil + macrosId, err := h.GetMacrosId(uid) + if err != nil { + return "", err + } + return macrosId, nil } func (h *V1Client) GetTFMacrosV2(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) { @@ -136,16 +139,16 @@ func (h *V1Client) DeleteMacros(uid string, body *models.V1Macros) error { return nil } -func (h *V1Client) GetMacrosId(uid string) string { - var hash string +func (h *V1Client) GetMacrosId(uid string) (string, error) { + hashId := "" if uid != "" { - hash = fmt.Sprintf("%s-%s-%s", "project", "macros", uid) + hashId = fmt.Sprintf("%s-%s-%s", "project", "macros", uid) } else { tenantID, err := h.GetTenantUID() if err != nil { - return "" + return "", err } - hash = fmt.Sprintf("%s-%s-%s", "tenant", "macros", tenantID) + hashId = fmt.Sprintf("%s-%s-%s", "tenant", "macros", tenantID) } - return hash + return hashId, nil } From 713214bca651d8dba4c688cb081c9264d6af3d8c Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Fri, 19 Jan 2024 14:00:30 +0530 Subject: [PATCH 3/3] Added unit test support for provider macrounit test --- client/client.go | 8 ++++++++ client/macros.go | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index cd20a80f..3586d734 100644 --- a/client/client.go +++ b/client/client.go @@ -100,6 +100,14 @@ type V1Client struct { // Edge Native GetCloudConfigEdgeNativeFn func(uid, clusterContext string) (*models.V1EdgeNativeCloudConfig, error) + + // Macros + CreateMacrosFn func(uid string, macros *models.V1Macros) (string, error) + GetTFMacrosV2Fn func(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) + GetExistMacrosFn func(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) + UpdateMacrosFn func(uid string, macros *models.V1Macros) error + DeleteMacrosFn func(uid string, body *models.V1Macros) error + GetMacrosIdFn func(uid string) (string, error) } func New(options ...func(*V1Client)) *V1Client { diff --git a/client/macros.go b/client/macros.go index 875a7f5d..f4887af8 100644 --- a/client/macros.go +++ b/client/macros.go @@ -7,7 +7,9 @@ import ( ) func (h *V1Client) CreateMacros(uid string, macros *models.V1Macros) (string, error) { - + if h.CreateMacrosFn != nil { + return h.CreateMacrosFn(uid, macros) + } if uid != "" { params := userC.NewV1ProjectsUIDMacrosCreateParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) _, err := h.GetUserClient().V1ProjectsUIDMacrosCreate(params) @@ -33,6 +35,9 @@ func (h *V1Client) CreateMacros(uid string, macros *models.V1Macros) (string, er } func (h *V1Client) GetTFMacrosV2(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) { + if h.GetTFMacrosV2Fn != nil { + return h.GetTFMacrosV2Fn(tfMacrosMap, projectUID) + } allMacros, err := h.GetMacrosV2(projectUID) if err != nil { @@ -53,6 +58,9 @@ func (h *V1Client) GetTFMacrosV2(tfMacrosMap map[string]interface{}, projectUID } func (h *V1Client) GetExistMacros(tfMacrosMap map[string]interface{}, projectUID string) ([]*models.V1Macro, error) { + if h.GetExistMacrosFn != nil { + return h.GetExistMacrosFn(tfMacrosMap, projectUID) + } allMacros, err := h.GetMacrosV2(projectUID) var existMacros []*models.V1Macro if err != nil { @@ -101,6 +109,9 @@ func (h *V1Client) GetMacrosV2(projectUID string) ([]*models.V1Macro, error) { } func (h *V1Client) UpdateMacros(uid string, macros *models.V1Macros) error { + if h.UpdateMacrosFn != nil { + return h.UpdateMacrosFn(uid, macros) + } if uid != "" { params := userC.NewV1ProjectsUIDMacrosUpdateParams().WithContext(h.Ctx).WithUID(uid).WithBody(macros) _, err := h.GetUserClient().V1ProjectsUIDMacrosUpdate(params) @@ -118,6 +129,9 @@ func (h *V1Client) UpdateMacros(uid string, macros *models.V1Macros) error { } func (h *V1Client) DeleteMacros(uid string, body *models.V1Macros) error { + if h.DeleteMacrosFn != nil { + return h.DeleteMacrosFn(uid, body) + } if uid != "" { params := userC.NewV1ProjectsUIDMacrosDeleteByMacroNameParams().WithContext(h.Ctx).WithUID(uid).WithBody(body) _, err := h.GetUserClient().V1ProjectsUIDMacrosDeleteByMacroName(params) @@ -140,6 +154,9 @@ func (h *V1Client) DeleteMacros(uid string, body *models.V1Macros) error { } func (h *V1Client) GetMacrosId(uid string) (string, error) { + if h.GetMacrosIdFn != nil { + return h.GetMacrosIdFn(uid) + } hashId := "" if uid != "" { hashId = fmt.Sprintf("%s-%s-%s", "project", "macros", uid)