Skip to content

Commit

Permalink
support exportings all kvms #62
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Aug 23, 2022
1 parent 82cba4f commit 5dbb222
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/kvm/expentries.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/apigee/apigeecli/apiclient"
"github.com/apigee/apigeecli/client/kvm"
"github.com/apigee/apigeecli/clilog"
"github.com/spf13/cobra"
)

Expand All @@ -42,6 +43,8 @@ var ExpEntryCmd = &cobra.Command{
var payload [][]byte
var fileName string

clilog.Warning.Println("Running this command against a large number of KVMs or entries can exhaust the API quota")

if payload, err = kvm.ExportEntries(proxyName, mapName); err != nil {
return
}
Expand All @@ -51,7 +54,7 @@ var ExpEntryCmd = &cobra.Command{
} else if proxyName != "" {
fileName = strings.Join([]string{"proxy", proxyName, mapName, "kvmfile"}, "_")
} else {
fileName = strings.Join([]string{"org_", mapName, "kvmfile"}, "_")
fileName = strings.Join([]string{"org", mapName, "kvmfile"}, "_")
}

for i := range payload {
Expand Down
87 changes: 87 additions & 0 deletions cmd/kvm/expkvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2021 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 (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/apigee/apigeecli/apiclient"
"github.com/apigee/apigeecli/client/kvm"
"github.com/spf13/cobra"
)

//ExpCmd to export map entries to files
var ExpCmd = &cobra.Command{
Use: "export",
Short: "Export all KV Map entries for all KV Maps",
Long: "Export all KV Map entries for all KV Maps in a given scope",
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")
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
var payload [][]byte
var fileName string

apiclient.SetPrintOutput(false)
listKVMBytes, err := kvm.List(proxyName)
if err != nil {
return err
}

var listKVM []string
if err = json.Unmarshal(listKVMBytes, &listKVM); err != nil {
return err
}

for _, mapName := range listKVM {
if payload, err = kvm.ExportEntries(proxyName, mapName); err != nil {
return
}

if env != "" {
fileName = strings.Join([]string{"env", env, mapName, "kvmfile"}, "_")
} else if proxyName != "" {
fileName = strings.Join([]string{"proxy", proxyName, mapName, "kvmfile"}, "_")
} else {
fileName = strings.Join([]string{"org", mapName, "kvmfile"}, "_")
}

for i := range payload {
if err = apiclient.WriteByteArrayToFile(fileName+"_"+strconv.Itoa(i)+".json", false, payload[i]); err != nil {
return
}
}
}
apiclient.SetPrintOutput(false)
fmt.Println("KVMs exports successfully")
return
},
}

func init() {
ExpCmd.Flags().StringVarP(&env, "env", "e",
"", "Environment name")
ExpCmd.Flags().StringVarP(&proxyName, "proxy", "p",
"", "API Proxy name")
}
1 change: 1 addition & 0 deletions cmd/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func init() {
Cmd.AddCommand(ListCmd)
Cmd.AddCommand(DelCmd)
Cmd.AddCommand(CreateCmd)
Cmd.AddCommand(ExpCmd)
Cmd.AddCommand(EntryCmd)
}

0 comments on commit 5dbb222

Please sign in to comment.