-
Notifications
You must be signed in to change notification settings - Fork 7
/
show.go
177 lines (164 loc) · 4.9 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package database
import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
)
// ShowCommand creates the "database show" command
func ShowCommand() commands.Command {
return &showCommand{
BaseCommand: commands.New(
"show",
"Show database details",
"upctl database show 9a8effcb-80e6-4a63-a7e5-066a6d093c14",
"upctl database show my-pg-database",
"upctl database show my-mysql-database",
),
}
}
type showCommand struct {
*commands.BaseCommand
resolver.CachingDatabase
completion.Database
}
// Execute implements commands.MultipleArgumentCommand
func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
db, err := svc.GetManagedDatabase(exec.Context(), &request.GetManagedDatabaseRequest{UUID: uuid})
if err != nil {
return nil, err
}
nodeRows := []output.TableRow{}
for _, node := range db.NodeStates {
nodeRows = append(nodeRows, output.TableRow{
node.Name,
node.Role,
node.State,
})
}
componentsRows := []output.TableRow{}
for _, component := range db.Components {
componentsRows = append(componentsRows, output.TableRow{
component.Component,
component.Host,
component.Port,
component.Route,
component.Usage,
})
}
detailSections := []output.DetailSection{
{
Title: "Overview:",
Rows: []output.DetailRow{
{Title: "UUID:", Value: db.UUID, Colour: ui.DefaultUUUIDColours},
{Title: "Title:", Value: db.Title},
{Title: "Name:", Value: db.Name},
{Title: "Type:", Value: prettyDatabaseType(db.Type)},
{Title: "Version:", Value: getVersion(db), Format: format.PossiblyUnknownString},
{Title: "Plan:", Value: db.Plan},
{Title: "Zone:", Value: db.Zone},
{Title: "State:", Value: db.State, Format: format.DatabaseState},
},
},
{
Title: "Maintenance schedule:",
Rows: []output.DetailRow{
{Title: "Weekday:", Value: db.Maintenance.DayOfWeek},
{Title: "Time:", Value: db.Maintenance.Time},
},
},
{
Title: "Authentication:",
Rows: []output.DetailRow{
{Title: "Service URI:", Value: db.ServiceURI},
{Title: "Database name:", Value: db.ServiceURIParams.DatabaseName},
{Title: "Host:", Value: db.ServiceURIParams.Host},
{Title: "Password:", Value: db.ServiceURIParams.Password},
{Title: "Port:", Value: db.ServiceURIParams.Port},
{Title: "SSL mode:", Value: db.ServiceURIParams.SSLMode},
{Title: "User:", Value: db.ServiceURIParams.User},
},
},
}
if db.Type == upcloud.ManagedDatabaseServiceTypeOpenSearch {
acl, err := svc.GetManagedDatabaseAccessControl(exec.Context(), &request.GetManagedDatabaseAccessControlRequest{ServiceUUID: uuid})
if err != nil {
return nil, err
}
detailSections = append(detailSections, output.DetailSection{
Title: "Access control settings:",
Rows: []output.DetailRow{
{Title: "Access control:", Value: acl.ACLsEnabled, Format: format.Boolean},
{Title: "Extended access control:", Value: acl.ExtendedACLsEnabled, Format: format.Boolean},
},
})
}
// For JSON and YAML output, passthrough API response
return output.MarshaledWithHumanOutput{
Value: db,
Output: output.Combined{
output.CombinedSection{
Contents: output.Details{
Sections: detailSections,
},
},
output.CombinedSection{
Title: "Nodes:",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "name", Header: "Name"},
{Key: "role", Header: "Type"},
{Key: "state", Header: "State"},
},
Rows: nodeRows,
},
},
output.CombinedSection{
Title: "Components:",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "component", Header: "Component"},
{Key: "host", Header: "Host"},
{Key: "port", Header: "Port"},
{Key: "route", Header: "Route"},
{Key: "usage", Header: "Usage"},
},
Rows: componentsRows,
},
},
},
}, nil
}
func getVersion(db *upcloud.ManagedDatabase) string {
if db == nil || db.Metadata == nil {
return ""
}
switch db.Type {
case "mysql":
return db.Metadata.MySQLVersion
case "opensearch":
return db.Metadata.OpenSearchVersion
case "pg":
return db.Metadata.PGVersion
case "redis":
return db.Metadata.RedisVersion
}
return ""
}
func prettyDatabaseType(serviceType upcloud.ManagedDatabaseServiceType) string {
switch serviceType {
case upcloud.ManagedDatabaseServiceTypeMySQL:
return "MySQL"
case upcloud.ManagedDatabaseServiceTypeOpenSearch:
return "OpenSearch"
case upcloud.ManagedDatabaseServiceTypePostgreSQL:
return "PostgreSQL"
default:
return string(serviceType)
}
}