Skip to content

Commit

Permalink
bug: fixes importing kvm entries #510 (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan authored Jul 18, 2024
1 parent 3ea45ca commit a9003e3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 55 deletions.
42 changes: 14 additions & 28 deletions internal/client/kvm/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package kvm
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
Expand All @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions internal/client/kvm/entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/kvm/crtentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
16 changes: 0 additions & 16 deletions internal/cmd/kvm/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package kvm

import (
"fmt"
"strconv"

"github.com/spf13/cobra"
)

Expand All @@ -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
}
3 changes: 1 addition & 2 deletions internal/cmd/kvm/updentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down

0 comments on commit a9003e3

Please sign in to comment.