|
| 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