Skip to content
Open
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
22 changes: 21 additions & 1 deletion internal/client/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import (
)

// Create
func Create(proxyName string, name string, encrypt bool) (respBody []byte, err error) {
func Create(proxyName string, name string, encrypt bool, masked bool) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeBaseURL())
kvm := []string{}

kvm = append(kvm, "\"name\":\""+name+"\"")
if encrypt {
kvm = append(kvm, "\"encrypted\":"+strconv.FormatBool(encrypt))
}
if masked {
kvm = append(kvm, "\"masked\":"+strconv.FormatBool(masked))
}
payload := "{" + strings.Join(kvm, ",") + "}"

if apiclient.GetApigeeEnv() != "" {
Expand All @@ -45,6 +48,23 @@ func Create(proxyName string, name string, encrypt bool) (respBody []byte, err e
return respBody, err
}

// Update
func Update(proxyName string, name string, masked bool) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeBaseURL())
payload := "{\"masked\":" + strconv.FormatBool(masked) + "}"

if apiclient.GetApigeeEnv() != "" {
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments", apiclient.GetApigeeEnv(), "keyvaluemaps", name)
} else if proxyName != "" {
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apis", proxyName, "keyvaluemaps", name)
} else {
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "keyvaluemaps", name)
}

respBody, err = apiclient.HttpClient(u.String(), payload, "PATCH")
return respBody, err
}

// Delete
func Delete(proxyName string, name string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeBaseURL())
Expand Down
6 changes: 3 additions & 3 deletions internal/client/kvm/kvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ func TestCreate(t *testing.T) {
apiclient.SetApigeeEnv("")

// test org KVM
if _, err := Create("", kvmName, true); err != nil {
if _, err := Create("", kvmName, true, false); err != nil {
t.Fatalf("%v", err)
}

// test proxy KVM
if _, err := apis.CreateProxy(proxyName, path.Join(cliPath, testFolder, "test_proxy.zip")); err != nil {
t.Fatalf("%v", err)
}
if _, err := Create(proxyName, kvmName, true); err != nil {
if _, err := Create(proxyName, kvmName, true, false); err != nil {
t.Fatalf("%v", err)
}

// test env KVM
apiclient.SetApigeeEnv(env)
if _, err := Create("", kvmName, true); err != nil {
if _, err := Create("", kvmName, true, false); err != nil {
t.Fatalf("%v", err)
}
}
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/kvm/crtkvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@ var CreateCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = kvm.Create(proxyName, name, true)
_, err = kvm.Create(proxyName, name, true, masked)
return
},
}

var masked bool

func init() {
CreateCmd.Flags().StringVarP(&env, "env", "e",
"", "Environment name")
CreateCmd.Flags().StringVarP(&proxyName, "proxy", "p",
"", "API Proxy name")
CreateCmd.Flags().StringVarP(&name, "name", "n",
"", "KVM Map name")
CreateCmd.Flags().BoolVarP(&masked, "masked", "m",
false, "Mask the KVM values in the debug session")

_ = CreateCmd.MarkFlagRequired("name")
}
6 changes: 3 additions & 3 deletions internal/cmd/kvm/impkvms.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var ImpCmd = &cobra.Command{
kvmFile := filepath.Base(orgKVMFile)
kvmMetadata := strings.Split(kvmFile, utils.DefaultFileSplitter)
clilog.Info.Printf("\tCreating KVM %s\n", orgKVMFile)
if _, err = kvm.Create("", kvmMetadata[1], true); proceedOnError(err) != nil {
if _, err = kvm.Create("", kvmMetadata[1], true, false); proceedOnError(err) != nil {
return err
}
clilog.Info.Printf("\tImporting entries for %s\n", orgKVMFile)
Expand All @@ -66,7 +66,7 @@ var ImpCmd = &cobra.Command{
kvmFile := filepath.Base(proxyKVMFile)
kvmMetadata := strings.Split(kvmFile, utils.DefaultFileSplitter)
clilog.Info.Printf("\tCreating KVM %s\n", proxyKVMFile)
if _, err = kvm.Create(kvmMetadata[1], kvmMetadata[2], true); proceedOnError(err) != nil {
if _, err = kvm.Create(kvmMetadata[1], kvmMetadata[2], true, false); proceedOnError(err) != nil {
return err
}
clilog.Info.Printf("\tImporting entries for %s\n", proxyKVMFile)
Expand All @@ -83,7 +83,7 @@ var ImpCmd = &cobra.Command{
kvmMetadata := strings.Split(kvmFile, utils.DefaultFileSplitter)
apiclient.SetApigeeEnv(kvmMetadata[1])
clilog.Info.Printf("\tCreating KVM %s\n", envKVMFile)
if _, err = kvm.Create("", kvmMetadata[2], true); proceedOnError(err) != nil {
if _, err = kvm.Create("", kvmMetadata[2], true, false); proceedOnError(err) != nil {
return err
}
clilog.Info.Printf("\tImporting entries for %s\n", envKVMFile)
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
Cmd.AddCommand(ListCmd)
Cmd.AddCommand(DelCmd)
Cmd.AddCommand(CreateCmd)
Cmd.AddCommand(UpdateCmd)
Cmd.AddCommand(ExpCmd)
Cmd.AddCommand(EntryCmd)
Cmd.AddCommand(ImpCmd)
Expand Down
59 changes: 59 additions & 0 deletions internal/cmd/kvm/updkvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kvm

import (
"fmt"
"internal/apiclient"
"internal/client/kvm"

"github.com/spf13/cobra"
)

// UpdateCmd to update a kvm
var UpdateCmd = &cobra.Command{
Use: "update",
Short: "Update a KV Map",
Long: "Update a KV Map",
Args: func(cmd *cobra.Command, args []string) (err error) {
if env != "" {
apiclient.SetApigeeEnv(env)
}
if env != "" && proxyName != "" {
return fmt.Errorf("proxy and env flags cannot be used together")
}
apiclient.SetRegion(region)
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = kvm.Update(proxyName, name, masked)
return
},
}

func init() {
UpdateCmd.Flags().StringVarP(&env, "env", "e",
"", "Environment name")
UpdateCmd.Flags().StringVarP(&proxyName, "proxy", "p",
"", "API Proxy name")
UpdateCmd.Flags().StringVarP(&name, "name", "n",
"", "KVM Map name")
UpdateCmd.Flags().BoolVarP(&masked, "masked", "m",
false, "Mask the KVM values in the debug session")

_ = UpdateCmd.MarkFlagRequired("name")
}
4 changes: 2 additions & 2 deletions internal/cmd/org/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var ImportCmd = &cobra.Command{
}
for _, kvmName := range kvmList {
// create only encrypted KVMs
if _, err = kvm.Create("", kvmName, true); err != nil {
if _, err = kvm.Create("", kvmName, true, false); err != nil {
return err
}
if orgKVMFileList[kvmName] != "" {
Expand Down Expand Up @@ -185,7 +185,7 @@ var ImportCmd = &cobra.Command{
}
for _, kvmName := range kvmList {
// create only encrypted KVMs
if _, err = kvm.Create("", kvmName, true); err != nil {
if _, err = kvm.Create("", kvmName, true, false); err != nil {
return err
}
if envKVMFileList[kvmName] != "" {
Expand Down