From a9003e3a79f39a3005324011589271488b883df4 Mon Sep 17 00:00:00 2001 From: Srinandan Sridhar <13950006+srinandan@users.noreply.github.com> Date: Thu, 18 Jul 2024 07:39:48 -0700 Subject: [PATCH] bug: fixes importing kvm entries #510 (#511) --- internal/client/kvm/entries.go | 42 ++++++++++------------------- internal/client/kvm/entries_test.go | 14 +++++----- internal/cmd/kvm/crtentry.go | 4 +-- internal/cmd/kvm/entries.go | 16 ----------- internal/cmd/kvm/updentry.go | 3 +-- 5 files changed, 24 insertions(+), 55 deletions(-) diff --git a/internal/client/kvm/entries.go b/internal/client/kvm/entries.go index af75dbedd..7cc49f12a 100644 --- a/internal/client/kvm/entries.go +++ b/internal/client/kvm/entries.go @@ -17,6 +17,7 @@ package kvm import ( "encoding/json" "errors" + "fmt" "io" "net/url" "os" @@ -37,18 +38,13 @@ type keyvalueentry struct { Value json.RawMessage `json:"value,omitempty"` } -type kventry struct { - Name string `json:"name,omitempty"` - Value string `json:"value,omitempty"` -} - type keyvalueentries struct { KeyValueEntries []keyvalueentry `json:"keyValueEntries,omitempty"` NextPageToken string `json:"nextPageToken,omitempty"` } // CreateEntry -func CreateEntry(proxyName string, mapName string, keyName string, value []byte) (respBody []byte, err error) { +func CreateEntry(proxyName string, mapName string, keyName string, value string, stringyfied bool) (respBody []byte, err error) { u, _ := url.Parse(apiclient.GetApigeeBaseURL()) if apiclient.GetApigeeEnv() != "" { u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments", apiclient.GetApigeeEnv(), "keyvaluemaps", mapName, "entries") @@ -57,7 +53,7 @@ func CreateEntry(proxyName string, mapName string, keyName string, value []byte) } else { u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "keyvaluemaps", mapName, "entries") } - payload, err := getKVPayload(keyName, value) + payload, err := getKVPayload(keyName, value, stringyfied) if err != nil { return nil, err } @@ -94,7 +90,7 @@ func GetEntry(proxyName string, mapName string, keyName string) (respBody []byte } // UpdateEntry -func UpdateEntry(proxyName string, mapName string, keyName string, value []byte) (respBody []byte, err error) { +func UpdateEntry(proxyName string, mapName string, keyName string, value string, stringyfied bool) (respBody []byte, err error) { u, _ := url.Parse(apiclient.GetApigeeBaseURL()) if apiclient.GetApigeeEnv() != "" { u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments", apiclient.GetApigeeEnv(), "keyvaluemaps", mapName, "entries", keyName) @@ -103,32 +99,22 @@ func UpdateEntry(proxyName string, mapName string, keyName string, value []byte) } else { u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "keyvaluemaps", mapName, "entries", keyName) } - payload, err := getKVPayload(keyName, value) + payload, err := getKVPayload(keyName, value, stringyfied) if err != nil { return nil, err } - respBody, err = apiclient.HttpClient(u.String(), string(payload), "PUT") + respBody, err = apiclient.HttpClient(u.String(), payload, "PUT") return respBody, err } -func getKVPayload(keyName string, value []byte) (payload string, err error) { - - var p []byte - - // attempt as json - k1 := keyvalueentry{Name: keyName, Value: value} - p, err = json.Marshal(k1) - if err != nil { - k2 := kventry{Name: keyName, Value: string(value)} - p, err = json.Marshal(k2) - if err != nil { - return "", err - } +func getKVPayload(keyName string, value string, stringyfied bool) (payload string, err error) { + if stringyfied { + return fmt.Sprintf("{\"name\":\"%s\",\"value\":%s}", keyName, value), nil } - return string(p), nil + return fmt.Sprintf("{\"name\":\"%s\",\"value\":\"%s\"}", keyName, value), nil } -func upsertEntry(proxyName string, mapName string, keyName string, value []byte) (respBody []byte, err error) { +func upsertEntry(proxyName string, mapName string, keyName string, value string, stringyfied bool) (respBody []byte, err error) { update := false apiclient.ClientPrintHttpResponse.Set(false) _, err = GetEntry(proxyName, mapName, keyName) @@ -138,10 +124,10 @@ func upsertEntry(proxyName string, mapName string, keyName string, value []byte) apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) if update { clilog.Info.Printf("Updating entry in map [%s] with key [%s]\n", mapName, keyName) - return UpdateEntry(proxyName, mapName, keyName, value) + return UpdateEntry(proxyName, mapName, keyName, value, stringyfied) } clilog.Info.Printf("Creating a new entry in map [%s] with key [%s]\n", mapName, keyName) - return CreateEntry(proxyName, mapName, keyName, value) + return CreateEntry(proxyName, mapName, keyName, value, stringyfied) } // ListEntries @@ -326,7 +312,7 @@ func batchImport(wg *sync.WaitGroup, proxyName string, mapName string, jobs <-ch return } - _, err = upsertEntry(proxyName, mapName, job.Name, job.Value) + _, err = upsertEntry(proxyName, mapName, job.Name, string(job.Value), true) if err != nil { errs <- err diff --git a/internal/client/kvm/entries_test.go b/internal/client/kvm/entries_test.go index 0c1f6c9f0..975aa66be 100644 --- a/internal/client/kvm/entries_test.go +++ b/internal/client/kvm/entries_test.go @@ -33,18 +33,18 @@ func TestCreateEntry(t *testing.T) { apiclient.SetApigeeEnv("") // add entry to org kvm - if _, err := CreateEntry("", kvmName, keyName, []byte(value)); err != nil { + if _, err := CreateEntry("", kvmName, keyName, value, false); err != nil { t.Fatalf("%v", err) } // add entry to proxy kvm - if _, err := CreateEntry(proxyName, kvmName, keyName, []byte(value)); err != nil { + if _, err := CreateEntry(proxyName, kvmName, keyName, value, false); err != nil { t.Fatalf("%v", err) } // add entry to env kvm apiclient.SetApigeeEnv(env) - if _, err := CreateEntry("", kvmName, keyName, []byte(value)); err != nil { + if _, err := CreateEntry("", kvmName, keyName, value, false); err != nil { t.Fatalf("%v", err) } } @@ -81,21 +81,21 @@ func TestUpdateEntry(t *testing.T) { env := apiclient.GetApigeeEnv() apiclient.SetApigeeEnv("") - updatedValue := []byte("updatedTest") + updatedValue := "updatedTest" // add entry to org kvm - if _, err := UpdateEntry("", kvmName, keyName, updatedValue); err != nil { + if _, err := UpdateEntry("", kvmName, keyName, updatedValue, false); err != nil { t.Fatalf("%v", err) } // add entry to proxy kvm - if _, err := UpdateEntry(proxyName, kvmName, keyName, updatedValue); err != nil { + if _, err := UpdateEntry(proxyName, kvmName, keyName, updatedValue, false); err != nil { t.Fatalf("%v", err) } // add entry to env kvm apiclient.SetApigeeEnv(env) - if _, err := UpdateEntry("", kvmName, keyName, updatedValue); err != nil { + if _, err := UpdateEntry("", kvmName, keyName, updatedValue, false); err != nil { t.Fatalf("%v", err) } } diff --git a/internal/cmd/kvm/crtentry.go b/internal/cmd/kvm/crtentry.go index 189d1a8c7..fc2074cc1 100644 --- a/internal/cmd/kvm/crtentry.go +++ b/internal/cmd/kvm/crtentry.go @@ -40,8 +40,8 @@ var CreateEntryCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true - value = getKVMString(value) - _, err = kvm.CreateEntry(proxyName, mapName, keyName, []byte(value)) + //value = getKVMString(value) + _, err = kvm.CreateEntry(proxyName, mapName, keyName, value, false) return }, } diff --git a/internal/cmd/kvm/entries.go b/internal/cmd/kvm/entries.go index 5bd4595bc..acb9e9291 100644 --- a/internal/cmd/kvm/entries.go +++ b/internal/cmd/kvm/entries.go @@ -15,9 +15,6 @@ package kvm import ( - "fmt" - "strconv" - "github.com/spf13/cobra" ) @@ -39,16 +36,3 @@ func init() { EntryCmd.AddCommand(ImpEntryCmd) EntryCmd.AddCommand(UpdateEntryCmd) } - -func getKVMString(value string) string { - var err error - // convert any boolean, float or integer to string - if _, err = strconv.ParseBool(value); err == nil { - value = fmt.Sprintf("\"%s\"", value) - } else if _, err = strconv.ParseInt(value, 10, 0); err == nil { - value = fmt.Sprintf("\"%s\"", value) - } else if _, err = strconv.ParseFloat(value, 0); err == nil { - value = fmt.Sprintf("\"%s\"", value) - } - return value -} diff --git a/internal/cmd/kvm/updentry.go b/internal/cmd/kvm/updentry.go index a32a5dd0d..cc36bcc76 100644 --- a/internal/cmd/kvm/updentry.go +++ b/internal/cmd/kvm/updentry.go @@ -40,8 +40,7 @@ var UpdateEntryCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true - value = getKVMString(value) - _, err = kvm.UpdateEntry(proxyName, mapName, keyName, []byte(value)) + _, err = kvm.UpdateEntry(proxyName, mapName, keyName, value, false) return }, }