From 25e6ac3e2a728b20b83b2f8fba861b912bbeba27 Mon Sep 17 00:00:00 2001 From: Christophe Jauffret Date: Wed, 13 Apr 2022 08:46:03 +0200 Subject: [PATCH] Improve json parsing (#15) --- cmd/common.go | 44 +++++++++++++++++++++++++++++--------------- cmd/list.go | 2 +- cmd/login.go | 8 +++----- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 0d9b69a..37515e7 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -46,6 +46,25 @@ type nutanixCluster struct { insecure bool } +type karbonClusterList struct { + KubeapiServerIpv4Address string `json:"kubeapi_server_ipv4_address"` + Name string `json:"name"` + Status string `json:"status"` + UUID string `json:"uuid"` + Version string `json:"version"` +} + +type kubeConfig struct { + KubeConfig string `json:"kube_config"` +} + +type sshConfig struct { + Certificate string `json:"certificate"` + ExpiryTime string `json:"expiry_time"` + PrivateKey string `json:"private_key"` + Username string `json:"username"` +} + func (nutanix *nutanixCluster) selectCluster() (string, error) { clusters, err := nutanix.listKarbonClusters() if err != nil { @@ -54,8 +73,7 @@ func (nutanix *nutanixCluster) selectCluster() (string, error) { clustersList := []string{} for _, cluster := range clusters { - clustersList = append(clustersList, cluster["name"].(string)) - // fmt.Fprintf(w, "\n%s\tv%s\t%s\t", cluster["name"], cluster["version"], cluster["status"].(string)[1:]) + clustersList = append(clustersList, cluster.Name) } prompt := promptui.Select{ @@ -73,7 +91,7 @@ func (nutanix *nutanixCluster) selectCluster() (string, error) { return result, nil } -func (nutanix *nutanixCluster) listKarbonClusters() ([]map[string]interface{}, error) { +func (nutanix *nutanixCluster) listKarbonClusters() ([]karbonClusterList, error) { karbonListUrl := "/karbon/v1-beta.1/k8s/clusters" method := "GET" @@ -85,7 +103,7 @@ func (nutanix *nutanixCluster) listKarbonClusters() ([]map[string]interface{}, e ResponseJSON, err := nutanix.clusterRequest(method, karbonListUrl, nil) cobra.CheckErr(err) - var clusters []map[string]interface{} + var clusters []karbonClusterList err = json.Unmarshal([]byte(ResponseJSON), &clusters) if err != nil { @@ -95,10 +113,10 @@ func (nutanix *nutanixCluster) listKarbonClusters() ([]map[string]interface{}, e return clusters, nil } -func saveKeyFile(cluster string, sshResponseJSON map[string]interface{}, force bool) error { +func saveKeyFile(cluster string, ssh sshConfig, force bool) error { - privateKey := []byte(sshResponseJSON["private_key"].(string)) - certificate := []byte(sshResponseJSON["certificate"].(string)) + privateKey := []byte(ssh.PrivateKey) + certificate := []byte(ssh.Certificate) userHomeDir, err := os.UserHomeDir() cobra.CheckErr(err) @@ -167,11 +185,11 @@ func deleteKeyFile(cluster string) error { return nil } -func addKeyAgent(cluster string, sshResponseJSON map[string]interface{}) error { +func addKeyAgent(cluster string, ssh sshConfig) error { - expiryTime := sshResponseJSON["expiry_time"].(string) - privateKey := []byte(sshResponseJSON["private_key"].(string)) - certificate := []byte(sshResponseJSON["certificate"].(string)) + expiryTime := ssh.ExpiryTime + privateKey := []byte(ssh.PrivateKey) + certificate := []byte(ssh.Certificate) // Get the ssh agent socket := os.Getenv("SSH_AUTH_SOCK") @@ -335,9 +353,5 @@ func (c *nutanixCluster) clusterRequest(method string, path string, payload []by return nil, err } - // var ResponseJSON map[string]interface{} - - // json.Unmarshal([]byte(body), &ResponseJSON) - return body, nil } diff --git a/cmd/list.go b/cmd/list.go index 17e728e..00beef7 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -58,7 +58,7 @@ var listCmd = &cobra.Command{ fmt.Fprintf(w, "%s\t%s\t%s\t", "NAME", "VERSION", "STATUS") for _, cluster := range clusters { - fmt.Fprintf(w, "\n%s\tv%s\t%s\t", cluster["name"], cluster["version"], cluster["status"].(string)[1:]) + fmt.Fprintf(w, "\n%s\tv%s\t%s\t", cluster.Name, cluster.Version, cluster.Status[1:]) } fmt.Fprintf(w, "\n") diff --git a/cmd/login.go b/cmd/login.go index 2c5508a..a43150c 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -79,13 +79,11 @@ If option enabled retrieve SSH key/cert and add them to ssh-agent or in file in kubeconfigResponseJSON, err := nutanixCluster.clusterRequest(method, karbonKubeconfigPath, nil) cobra.CheckErr(err) - var kubeconfigResponse map[string]interface{} + var kubeconfigResponse kubeConfig err = json.Unmarshal([]byte(kubeconfigResponseJSON), &kubeconfigResponse) cobra.CheckErr(err) - data := []byte(kubeconfigResponse["kube_config"].(string)) - kubeconfig := viper.GetString("kubeconfig") if viper.GetBool("kubie") { @@ -113,7 +111,7 @@ If option enabled retrieve SSH key/cert and add them to ssh-agent or in file in cobra.CheckErr(fmt.Errorf("file %s already exist, use force option to overwrite it", kubeconfig)) } - err = ioutil.WriteFile(kubeconfig, data, 0600) + err = ioutil.WriteFile(kubeconfig, []byte(kubeconfigResponse.KubeConfig), 0600) cobra.CheckErr(err) if verbose { @@ -132,7 +130,7 @@ If option enabled retrieve SSH key/cert and add them to ssh-agent or in file in karbonSSHJSON, err := nutanixCluster.clusterRequest(method, karbonSSHPath, nil) cobra.CheckErr(err) - var karbonSSH map[string]interface{} + var karbonSSH sshConfig err = json.Unmarshal([]byte(karbonSSHJSON), &karbonSSH) cobra.CheckErr(err)