Skip to content

Commit

Permalink
Improve json parsing (#15)
Browse files Browse the repository at this point in the history
tuxtof authored Apr 13, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent f6cc001 commit 25e6ac3
Showing 3 changed files with 33 additions and 21 deletions.
44 changes: 29 additions & 15 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
@@ -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")

8 changes: 3 additions & 5 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 25e6ac3

Please sign in to comment.