|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/UpCloudLtd/upcloud-cli/internal/commands" |
| 5 | + "github.com/UpCloudLtd/upcloud-cli/internal/output" |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/internal/ui" |
| 7 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud" |
| 8 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request" |
| 9 | +) |
| 10 | + |
| 11 | +// ShowCommand creates the "database show" command |
| 12 | +func ShowCommand() commands.Command { |
| 13 | + return &showCommand{ |
| 14 | + BaseCommand: commands.New( |
| 15 | + "show", |
| 16 | + "Show database details", |
| 17 | + "upctl database show 9a8effcb-80e6-4a63-a7e5-066a6d093c14", |
| 18 | + "upctl database show my-pg-database", |
| 19 | + "upctl database show my-mysql-database", |
| 20 | + ), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +type showCommand struct { |
| 25 | + *commands.BaseCommand |
| 26 | +} |
| 27 | + |
| 28 | +// Execute implements commands.MultipleArgumentCommand |
| 29 | +func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) { |
| 30 | + svc := exec.All() |
| 31 | + db, err := svc.GetManagedDatabase(&request.GetManagedDatabaseRequest{UUID: uuid}) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + nodeRows := []output.TableRow{} |
| 37 | + for _, node := range db.NodeStates { |
| 38 | + nodeRows = append(nodeRows, output.TableRow{ |
| 39 | + node.Name, |
| 40 | + node.Role, |
| 41 | + node.State, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + componentsRows := []output.TableRow{} |
| 46 | + for _, component := range db.Components { |
| 47 | + componentsRows = append(componentsRows, output.TableRow{ |
| 48 | + component.Component, |
| 49 | + component.Host, |
| 50 | + component.Port, |
| 51 | + component.Route, |
| 52 | + component.Usage, |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + // For JSON and YAML output, passthrough API response |
| 57 | + return output.MarshaledWithHumanOutput{ |
| 58 | + Value: db, |
| 59 | + Output: output.Combined{ |
| 60 | + output.CombinedSection{ |
| 61 | + Contents: output.Details{ |
| 62 | + Sections: []output.DetailSection{ |
| 63 | + { |
| 64 | + Title: "Overview:", |
| 65 | + Rows: []output.DetailRow{ |
| 66 | + {Title: "UUID:", Value: db.UUID, Colour: ui.DefaultUUUIDColours}, |
| 67 | + {Title: "Title:", Value: db.Title}, |
| 68 | + {Title: "Name:", Value: db.Name}, |
| 69 | + {Title: "Type:", Value: prettyDatabaseType(db.Type)}, |
| 70 | + {Title: "Version:", Value: db.Properties.Get("version")}, |
| 71 | + {Title: "Plan:", Value: db.Plan}, |
| 72 | + {Title: "Zone:", Value: db.Zone}, |
| 73 | + {Title: "State:", Value: db.State, Colour: commands.DatabaseStateColour(db.State)}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + Title: "Maintenance schedule:", |
| 78 | + Rows: []output.DetailRow{ |
| 79 | + {Title: "Weekday:", Value: db.Maintenance.DayOfWeek}, |
| 80 | + {Title: "Time:", Value: db.Maintenance.Time}, |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + Title: "Authentication:", |
| 85 | + Rows: []output.DetailRow{ |
| 86 | + {Title: "Service URI:", Value: db.ServiceURI}, |
| 87 | + {Title: "Database name:", Value: db.ServiceURIParams.DatabaseName}, |
| 88 | + {Title: "Host:", Value: db.ServiceURIParams.Host}, |
| 89 | + {Title: "Password:", Value: db.ServiceURIParams.Password}, |
| 90 | + {Title: "Port:", Value: db.ServiceURIParams.Port}, |
| 91 | + {Title: "SSL mode:", Value: db.ServiceURIParams.SSLMode}, |
| 92 | + {Title: "User:", Value: db.ServiceURIParams.User}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + output.CombinedSection{ |
| 99 | + Title: "Nodes:", |
| 100 | + Contents: output.Table{ |
| 101 | + Columns: []output.TableColumn{ |
| 102 | + {Key: "name", Header: "Name"}, |
| 103 | + {Key: "role", Header: "Type"}, |
| 104 | + {Key: "state", Header: "State"}, |
| 105 | + }, |
| 106 | + Rows: nodeRows, |
| 107 | + }, |
| 108 | + }, |
| 109 | + output.CombinedSection{ |
| 110 | + Title: "Components:", |
| 111 | + Contents: output.Table{ |
| 112 | + Columns: []output.TableColumn{ |
| 113 | + {Key: "component", Header: "Component"}, |
| 114 | + {Key: "host", Header: "Host"}, |
| 115 | + {Key: "port", Header: "Port"}, |
| 116 | + {Key: "route", Header: "Route"}, |
| 117 | + {Key: "usage", Header: "Usage"}, |
| 118 | + }, |
| 119 | + Rows: componentsRows, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, nil |
| 124 | +} |
| 125 | + |
| 126 | +func prettyDatabaseType(serviceType upcloud.ManagedDatabaseServiceType) string { |
| 127 | + switch serviceType { |
| 128 | + case upcloud.ManagedDatabaseServiceTypeMySQL: |
| 129 | + return "MySQL" |
| 130 | + case upcloud.ManagedDatabaseServiceTypePostgreSQL: |
| 131 | + return "PostgreSQL" |
| 132 | + default: |
| 133 | + return string(serviceType) |
| 134 | + } |
| 135 | +} |
0 commit comments