Skip to content

Commit

Permalink
feat: export entries of proxy scoped kvms #306 (#316)
Browse files Browse the repository at this point in the history
* feat: export entries of proxy scoped kvms #306

* chore: fix linting issues #306
  • Loading branch information
srinandan authored Oct 16, 2023
1 parent d37587b commit d8b1e90
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/kvm/expkvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var ExpCmd = &cobra.Command{
var fileName string

apiclient.DisableCmdPrintHttpResponse()

// return all kvm entries from all proxies
if env == "" && proxyName == "" {
return kvm.ExportAllEntries()
}

listKVMBytes, err := kvm.List(proxyName)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/org/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ var ExportCmd = &cobra.Command{
}
}

clilog.Info.Printf("Exporting Proxy scoped KV Map entries for org %s\n", org)
if err = kvm.ExportAllEntries(); err != nil {
return err
}

clilog.Info.Println("Exporting Developers...")
if respBody, err = developers.Export(); proceedOnError(err) != nil {
return err
Expand Down
62 changes: 62 additions & 0 deletions internal/client/kvm/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"os"
"path"
"strconv"
"strings"
"sync"

"internal/apiclient"
"internal/client/apis"

"internal/clilog"
)
Expand Down Expand Up @@ -107,6 +109,66 @@ func ListEntries(proxyName string, mapName string, pageSize int, pageToken strin
return respBody, err
}

// ExportAllEntries
func ExportAllEntries() (err error) {
type proxy struct {
Name string `json:"name,omitempty"`
APIProxyType string `json:"apiProxyType,omitempty"`
}

type proxies struct {
Proxies []proxy `json:"proxies,omitempty"`
}

p := proxies{}
programmableProxies := []string{}

apiList, err := apis.ListProxies(false)
if err != nil {
return err
}

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

for _, proxy := range p.Proxies {
//search for only programmable proxies. standard proxies can't have KVMs
if proxy.APIProxyType == "PROGRAMMABLE" {
programmableProxies = append(programmableProxies, proxy.Name)
}
}

for _, programmableProxy := range programmableProxies {
kvmList := []string{}
kvmListResp, err := List(programmableProxy)
if err != nil {
return err
}
if err = json.Unmarshal(kvmListResp, &kvmList); err != nil {
return err
}
if len(kvmList) > 0 {
clilog.Info.Printf("Found %d scoped KVMs for proxy %s\n", len(kvmList), programmableProxy)
for _, proxyKVM := range kvmList {
clilog.Info.Printf("Eporting entries for KVM %s of proxy %s\n", programmableProxy, proxyKVM)
proxyKVMEntries, err := ExportEntries(programmableProxy, proxyKVM)
if err != nil {
return err
}
fileName := strings.Join([]string{"proxy", programmableProxy, proxyKVM, "kvmfile"}, "_")
for i := range proxyKVMEntries {
if err = apiclient.WriteByteArrayToFile(fileName+"_"+strconv.Itoa(i)+".json", false, proxyKVMEntries[i]); err != nil {
return err
}
}
}
}
}

return nil
}

// ExportEntries
func ExportEntries(proxyName string, mapName string) (payload [][]byte, err error) {
var respBody []byte
Expand Down

0 comments on commit d8b1e90

Please sign in to comment.