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 all commits
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
8 changes: 8 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
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
}
113 changes: 72 additions & 41 deletions client/macros.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,85 @@
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 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)
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
macrosId, err := h.GetMacrosId(uid)
if err != nil {
return "", err
}
return macrosId, 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) {
if h.GetTFMacrosV2Fn != nil {
return h.GetTFMacrosV2Fn(tfMacrosMap, projectUID)
}
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,
})
}
}

return tfMacros, nil
}

for _, macro := range macros {
if h.GetMacroId(projectUID, macro.Name) == id {
return macro, nil
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 {
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,34 +108,30 @@ 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 h.UpdateMacrosFn != nil {
return h.UpdateMacrosFn(uid, macros)
}
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
}
}

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)
Expand All @@ -122,19 +150,22 @@ 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 {
var hash string
func (h *V1Client) GetMacrosId(uid string) (string, error) {
if h.GetMacrosIdFn != nil {
return h.GetMacrosIdFn(uid)
}
hashId := ""
if uid != "" {
hash = h.StringHash(name + uid)
hashId = fmt.Sprintf("%s-%s-%s", "project", "macros", uid)
} else {
hash = h.StringHash(name + "%tenant")
tenantID, err := h.GetTenantUID()
if err != nil {
return "", err
}
hashId = fmt.Sprintf("%s-%s-%s", "tenant", "macros", tenantID)
}
return hash
return hashId, nil
}