Skip to content

Commit

Permalink
bug: fixes environment export #449 (#450)
Browse files Browse the repository at this point in the history
* bug: fixes environment export #449

* bug: proceed on error #449
  • Loading branch information
srinandan authored Apr 9, 2024
1 parent 54270a1 commit 95ff6c9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 58 deletions.
25 changes: 16 additions & 9 deletions internal/client/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ import (
"internal/clilog"
)

type environments struct {
Environment []environment `json:"environment,omitempty"`
type Environments struct {
Environment []Environment `json:"environment,omitempty"`
}

type environment struct {
type Environment struct {
Name string `json:"name,omitempty"`
DisplayName string `json:"displayName,omitempty"`
DeploymentType string `json:"deploymentType,omitempty"`
ApiProxyType string `json:"apiProxyType,omitempty"`
ForwardProxyUri string `json:"forwardProxyUri,omitempty"`
Type string `json:"type,omitempty"`
Properties []property `json:"properties,omitempty"`
Properties []Property `json:"properties,omitempty"`
}

type property struct {
type Property struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func Export() (respBody []byte, err error) {
defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())

var envList []string
environmentList := environments{}
environmentList := Environments{}

envRespBody, err := List()
if err != nil {
Expand All @@ -321,7 +321,7 @@ func Export() (respBody []byte, err error) {
if err != nil {
return nil, err
}
environ := environment{}
environ := Environment{}
err = json.Unmarshal(envRespBody, &environ)
if err != nil {
return nil, err
Expand Down Expand Up @@ -361,8 +361,8 @@ func Import(filePath string) (err error) {
return nil
}

func readEnvironmentsFile(filePath string) (environmentList environments, err error) {
environmentList = environments{}
func readEnvironmentsFile(filePath string) (environmentList Environments, err error) {
environmentList = Environments{}

jsonFile, err := os.Open(filePath)
if err != nil {
Expand All @@ -383,3 +383,10 @@ func readEnvironmentsFile(filePath string) (environmentList environments, err er

return environmentList, nil
}

func MarshalEnvironmentList(contents []byte) (envronmentList Environments, err error) {
if err = json.Unmarshal(contents, &envronmentList); err != nil {
return envronmentList, err
}
return envronmentList, nil
}
99 changes: 50 additions & 49 deletions internal/cmd/org/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ var ExportCmd = &cobra.Command{
}
}

var envRespBody, envDetailsRespBody []byte
var _, envDetailsRespBody []byte
clilog.Info.Println("Exporting list of environments...")

if envDetailsRespBody, err = env.Export(); proceedOnError(err) != nil {
Expand All @@ -220,74 +220,35 @@ var ExportCmd = &cobra.Command{
return err
}

if envRespBody, err = env.List(); proceedOnError(err) != nil {
if _, err = env.List(); proceedOnError(err) != nil {
return err
}

environments := []string{}
if err = json.Unmarshal(envRespBody, &environments); proceedOnError(err) != nil {
environments, err := env.MarshalEnvironmentList(envDetailsRespBody)
if proceedOnError(err) != nil {
return err
}

for _, environment := range environments {
clilog.Info.Println("Exporting configuration for environment " + environment)
apiclient.SetApigeeEnv(environment)
for _, environment := range environments.Environment {
clilog.Info.Println("Exporting configuration for environment " + environment.Name)
apiclient.SetApigeeEnv(environment.Name)
clilog.Info.Println("\tExporting Target servers...")
if targetServerResponse, err = targetservers.Export(conn); proceedOnError(err) != nil {
return err
}
if err = apiclient.WriteArrayByteArrayToFile(
environment+utils.DefaultFileSplitter+targetServerFileName,
environment.Name+utils.DefaultFileSplitter+targetServerFileName,
false,
targetServerResponse); proceedOnError(err) != nil {
return err
}

clilog.Info.Printf("\tExporting KV Map names for environment %s...\n", environment)
if listKVMBytes, err = kvm.List(""); err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment+utils.DefaultFileSplitter+kvmFileName,
false,
listKVMBytes); proceedOnError(err) != nil {
return err
}

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

clilog.Info.Println("\tExporting Key store names...")
if respBody, err = keystores.List(); proceedOnError(err) != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment+utils.DefaultFileSplitter+keyStoresFileName,
false,
respBody); proceedOnError(err) != nil {
return err
}

clilog.Info.Println("\tExporting debugmask configuration...")
if respBody, err = env.GetDebug(); err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment+debugmaskFileName,
false,
respBody); proceedOnError(err) != nil {
return err
}

clilog.Info.Println("\tExporting traceconfig...")
if respBody, err = env.GetTraceConfig(); err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment+tracecfgFileName,
environment.Name+utils.DefaultFileSplitter+keyStoresFileName,
false,
respBody); proceedOnError(err) != nil {
return err
Expand All @@ -298,12 +259,52 @@ var ExportCmd = &cobra.Command{
return err
}
if err = apiclient.WriteArrayByteArrayToFile(
environment+utils.DefaultFileSplitter+referencesFileName,
environment.Name+utils.DefaultFileSplitter+referencesFileName,
false,
referencesResponse); proceedOnError(err) != nil {
return err
}

if environment.Type != "BASE" {
clilog.Info.Printf("\tExporting KV Map names for environment %s...\n", environment)
if listKVMBytes, err = kvm.List(""); proceedOnError(err) != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment.Name+utils.DefaultFileSplitter+kvmFileName,
false,
listKVMBytes); proceedOnError(err) != nil {
return err
}

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

clilog.Info.Println("\tExporting debugmask configuration...")
if respBody, err = env.GetDebug(); proceedOnError(err) != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment.Name+debugmaskFileName,
false,
respBody); proceedOnError(err) != nil {
return err
}

clilog.Info.Println("\tExporting traceconfig...")
if respBody, err = env.GetTraceConfig(); proceedOnError(err) != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
environment.Name+tracecfgFileName,
false,
respBody); proceedOnError(err) != nil {
return err
}
}
}

return err
Expand Down

0 comments on commit 95ff6c9

Please sign in to comment.