Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: add JSON output option for display #1358

Merged
merged 1 commit into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
95 changes: 76 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,25 @@ type InstInfo struct {
Port int
}

// DashboardInfo hold the structure for the JSON output of the dashboard info
type DashboardInfo struct {
dveeden marked this conversation as resolved.
Show resolved Hide resolved
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"`
}

// JSONOutput holds the structure for the JSON output of `tiup cluster display --json`
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 +87,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 +179,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