Skip to content

Commit

Permalink
hide kvm export behind flag #62
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Aug 25, 2022
1 parent 48df199 commit ebc56e7
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions cmd/org/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

"github.com/apigee/apigeecli/apiclient"
"github.com/apigee/apigeecli/client/apis"
Expand All @@ -38,7 +40,7 @@ import (
"github.com/spf13/cobra"
)

//ExportCmd to get org details
// ExportCmd to get org details
var ExportCmd = &cobra.Command{
Use: "export",
Short: "Export Apigee Configuration",
Expand All @@ -49,7 +51,7 @@ var ExportCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {

var productResponse, appsResponse, targetServerResponse, referencesResponse [][]byte
var respBody []byte
var respBody, listKVMBytes []byte

runtimeType, _ := orgs.GetOrgField("runtimeType")

Expand Down Expand Up @@ -78,13 +80,17 @@ var ExportCmd = &cobra.Command{
}

fmt.Printf("\tExporting KV Map names for org %s\n", org)
if respBody, err = kvm.List(""); err != nil {
if listKVMBytes, err = kvm.List(""); err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(org+"_"+kVMFileName, false, respBody); err != nil {
return err
}

if err = exportKVMEntries("org", "", listKVMBytes); err != nil {
return err
}

fmt.Println("Exporting Developers...")
if respBody, err = developers.Export(); err != nil {
return err
Expand Down Expand Up @@ -151,13 +157,17 @@ var ExportCmd = &cobra.Command{
}

fmt.Printf("\tExporting KV Map names for environment %s...\n", environment)
if respBody, err = kvm.List(""); err != nil {
if listKVMBytes, err = kvm.List(""); err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(environment+"_"+kVMFileName, false, respBody); err != nil {
return err
}

if err = exportKVMEntries("env", environment, listKVMBytes); err != nil {
return err
}

fmt.Println("\tExporting Key store names...")
if respBody, err = keystores.List(); err != nil {
return err
Expand Down Expand Up @@ -217,3 +227,43 @@ func createFolders() (err error) {
}
return nil
}

func exportKVMEntries(scope string, env string, listKVMBytes []byte) (err error) {

var kvmEntries [][]byte
var listKVM []string
var fileName string

//hide kvm exports behind a feature flag
if os.Getenv("APIGEECLI_KVM_EXPORT") == "" {
return nil
}

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

for _, mapName := range listKVM {

fmt.Printf("\tExporting KVM entries for %s in org %s\n", org, mapName)
if kvmEntries, err = kvm.ExportEntries("", mapName); err != nil {
return err
}

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

if len(kvmEntries) > 0 {
for i := range kvmEntries {
if err = apiclient.WriteByteArrayToFile(fileName+"_"+strconv.Itoa(i)+".json", false, kvmEntries[i]); err != nil {
return err
}
}
}
}

return nil
}

0 comments on commit ebc56e7

Please sign in to comment.