Skip to content

Commit

Permalink
cluster: Add JSON output option for 'display'
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed May 11, 2021
1 parent a229992 commit e40c797
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 20 deletions.
1 change: 1 addition & 0 deletions components/cluster/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func newDisplayCmd() *cobra.Command {
cmd.Flags().BoolVar(&gOpt.ShowUptime, "uptime", false, "Display with uptime")
cmd.Flags().BoolVar(&showDashboardOnly, "dashboard", false, "Only display TiDB Dashboard information")
cmd.Flags().BoolVar(&showVersionOnly, "version", false, "Only display TiDB cluster version")
cmd.Flags().BoolVar(&gOpt.Json, "json", false, "Output in JSON format when applicable")

return cmd
}
Expand Down
93 changes: 74 additions & 19 deletions pkg/cluster/manager/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package manager

import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -53,6 +54,23 @@ type InstInfo struct {
Port int
}

type DashboardInfo struct {
ClusterType string `json:"cluster_type"`
ClusterName string `json:"cluster_name"`
ClusterVersion string `json:"cluster_version"`
SSHType string `json:"ssh_type"`
TLSEnabled bool `json:"tls_enabled"`
TLSCACert string `json:"tls_ca_cert"`
TLSClientCert string `json:"tls_client_cert"`
TLSClientKey string `json:"tls_client_key"`
DashboardURL string `json:"dashboard_url"`
}

type JsonOutput struct {
DashboardInfo DashboardInfo `json:"dashboard"`
InstanceInfos []InstInfo `json:"instances"`
}

// Display cluster meta and topology.
func (m *Manager) Display(name string, opt operator.Options) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
Expand All @@ -67,25 +85,49 @@ func (m *Manager) Display(name string, opt operator.Options) error {
metadata, _ := m.meta(name)
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
// display cluster meta
cyan := color.New(color.FgCyan, color.Bold)
fmt.Printf("Cluster type: %s\n", cyan.Sprint(m.sysName))
fmt.Printf("Cluster name: %s\n", cyan.Sprint(name))
fmt.Printf("Cluster version: %s\n", cyan.Sprint(base.Version))
fmt.Printf("SSH type: %s\n", cyan.Sprint(topo.BaseTopo().GlobalOptions.SSHType))

// display TLS info
if topo.BaseTopo().GlobalOptions.TLSEnabled {
fmt.Printf("TLS encryption: %s\n", cyan.Sprint("enabled"))
fmt.Printf("CA certificate: %s\n", cyan.Sprint(
m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert),
))
fmt.Printf("Client private key: %s\n", cyan.Sprint(
m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey),
))
fmt.Printf("Client certificate: %s\n", cyan.Sprint(
m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert),
))
// display cluster meta
var j *JsonOutput
if opt.Json {
j = &JsonOutput{
DashboardInfo: DashboardInfo{
m.sysName,
name,
base.Version,
string(topo.BaseTopo().GlobalOptions.SSHType),
topo.BaseTopo().GlobalOptions.TLSEnabled,
"", // CA Cert
"", // Client Cert
"", // Client Key
"",
},
InstanceInfos: clusterInstInfos,
}

if topo.BaseTopo().GlobalOptions.TLSEnabled {
j.DashboardInfo.TLSCACert = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert)
j.DashboardInfo.TLSClientKey = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey)
j.DashboardInfo.TLSClientCert = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert)
}
} else {
fmt.Printf("Cluster type: %s\n", cyan.Sprint(m.sysName))
fmt.Printf("Cluster name: %s\n", cyan.Sprint(name))
fmt.Printf("Cluster version: %s\n", cyan.Sprint(base.Version))
fmt.Printf("SSH type: %s\n", cyan.Sprint(topo.BaseTopo().GlobalOptions.SSHType))

// display TLS info
if topo.BaseTopo().GlobalOptions.TLSEnabled {
fmt.Printf("TLS encryption: %s\n", cyan.Sprint("enabled"))
fmt.Printf("CA certificate: %s\n", cyan.Sprint(
m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert),
))
fmt.Printf("Client private key: %s\n", cyan.Sprint(
m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey),
))
fmt.Printf("Client certificate: %s\n", cyan.Sprint(
m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert),
))
}
}

// display topology
Expand Down Expand Up @@ -135,8 +177,21 @@ func (m *Manager) Display(name string, opt operator.Options) error {
if tlsCfg != nil {
scheme = "https"
}
fmt.Printf("Dashboard URL: %s\n", cyan.Sprintf("%s://%s/dashboard", scheme, dashboardAddr))
if opt.Json {
j.DashboardInfo.DashboardURL = fmt.Sprintf("%s://%s/dashboard", scheme, dashboardAddr)
} else {
fmt.Printf("Dashboard URL: %s\n", cyan.Sprintf("%s://%s/dashboard", scheme, dashboardAddr))
}
}
}

if opt.Json {
d, err := json.MarshalIndent(j, "", " ")
if err != nil {
return err
}
fmt.Println(string(d))
return nil
}

cliutil.PrintTable(clusterTable, true)
Expand Down
4 changes: 3 additions & 1 deletion pkg/cluster/operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ type Options struct {

// Show uptime or not
ShowUptime bool
Operation Operation

Json bool
Operation Operation
}

// Operation represents the type of cluster operation
Expand Down

0 comments on commit e40c797

Please sign in to comment.