Skip to content

Commit dee6620

Browse files
committed
feat(database): add database plans command
1 parent 2a88a3f commit dee6620

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

internal/commands/all/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
104104
commands.BuildCommand(database.ListCommand(), databaseCommand.Cobra(), conf)
105105
commands.BuildCommand(database.ShowCommand(), databaseCommand.Cobra(), conf)
106106
commands.BuildCommand(database.TypesCommand(), databaseCommand.Cobra(), conf)
107+
commands.BuildCommand(database.PlansCommand(), databaseCommand.Cobra(), conf)
107108

108109
// Misc
109110
commands.BuildCommand(
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package database
2+
3+
import (
4+
"sort"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/internal/output"
8+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
9+
)
10+
11+
// PlansCommand creates the "database plans" command
12+
func PlansCommand() commands.Command {
13+
return &plansCommand{
14+
BaseCommand: commands.New("plans", "List available plans for given database type", "upctl database plans pg", "upctl database plans mysql"),
15+
}
16+
}
17+
18+
type plansCommand struct {
19+
*commands.BaseCommand
20+
}
21+
22+
// Execute implements commands.MultipleArgumentCommand
23+
func (s *plansCommand) Execute(exec commands.Executor, serviceType string) (output.Output, error) {
24+
svc := exec.All()
25+
dbType, err := svc.GetManagedDatabaseServiceType(&request.GetManagedDatabaseServiceTypeRequest{Type: serviceType})
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
plans := dbType.ServicePlans
31+
sort.Slice(plans, func(i, j int) bool {
32+
if plans[i].NodeCount != plans[j].NodeCount {
33+
return plans[i].NodeCount < plans[j].NodeCount
34+
}
35+
36+
if plans[i].CoreNumber != plans[j].CoreNumber {
37+
return plans[i].CoreNumber < plans[j].CoreNumber
38+
}
39+
40+
if plans[i].MemoryAmount != plans[j].MemoryAmount {
41+
return plans[i].MemoryAmount < plans[j].MemoryAmount
42+
}
43+
44+
return plans[i].StorageSize < plans[j].StorageSize
45+
})
46+
47+
rows := []output.TableRow{}
48+
for _, plan := range plans {
49+
rows = append(rows, output.TableRow{
50+
plan.Plan,
51+
plan.NodeCount,
52+
plan.CoreNumber,
53+
plan.MemoryAmount / 1024,
54+
plan.StorageSize / 1024,
55+
plan.BackupConfig.Interval,
56+
plan.BackupConfig.MaxCount,
57+
plan.BackupConfig.RecoveryMode,
58+
})
59+
}
60+
61+
return output.MarshaledWithHumanOutput{
62+
Value: plans,
63+
Output: output.Table{
64+
Columns: []output.TableColumn{
65+
{Key: "plan", Header: "Plan"},
66+
{Key: "nodes", Header: "Nodes"},
67+
{Key: "cores", Header: "Cores"},
68+
{Key: "memory", Header: "Memory (GB)"},
69+
{Key: "storage", Header: "Storage (GB)"},
70+
{Key: "bu_interval", Header: "Backup interval"},
71+
{Key: "bu_max_count", Header: "Max backup count"},
72+
},
73+
Rows: rows,
74+
},
75+
}, nil
76+
}

0 commit comments

Comments
 (0)