Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-701: Added new macros resource support in SDK #85

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions client/macro.go
Original file line number Diff line number Diff line change
@@ -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
}
87 changes: 49 additions & 38 deletions client/macros.go
Original file line number Diff line number Diff line change
@@ -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 != "" {
Expand Down Expand Up @@ -76,29 +97,19 @@ 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()
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)
params := userC.NewV1TenantsUIDMacrosUpdateParams().WithTenantUID(tenantUID).WithBody(macros)
_, err = h.GetUserClient().V1TenantsUIDMacrosUpdate(params)
return err
}
}
Expand All @@ -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 {
SivaanandM marked this conversation as resolved.
Show resolved Hide resolved
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
}