|
| 1 | +package account |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
| 9 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" |
| 10 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" |
| 11 | + "github.com/spf13/pflag" |
| 12 | +) |
| 13 | + |
| 14 | +// BillingCommand creates the 'account billing' command |
| 15 | +func BillingCommand() commands.Command { |
| 16 | + return &billingCommand{ |
| 17 | + BaseCommand: commands.New( |
| 18 | + "billing", |
| 19 | + "Show billing information", |
| 20 | + "upctl account billing --year 2025 --month 7", |
| 21 | + ), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +type billingCommand struct { |
| 26 | + *commands.BaseCommand |
| 27 | + year int |
| 28 | + month int |
| 29 | + resourceID string |
| 30 | + username string |
| 31 | +} |
| 32 | + |
| 33 | +// InitCommand implements Command.InitCommand |
| 34 | +func (s *billingCommand) InitCommand() { |
| 35 | + flagSet := &pflag.FlagSet{} |
| 36 | + |
| 37 | + flagSet.IntVar(&s.year, "year", 0, "Year for billing information.") |
| 38 | + flagSet.IntVar(&s.month, "month", 0, "Month for billing information.") |
| 39 | + flagSet.StringVar(&s.resourceID, "resource-id", "", "For IP addresses: the address itself, others, resource UUID") |
| 40 | + flagSet.StringVar(&s.username, "username", "", "Valid username") |
| 41 | + |
| 42 | + s.AddFlags(flagSet) |
| 43 | + |
| 44 | + commands.Must(s.Cobra().MarkFlagRequired("year")) |
| 45 | + commands.Must(s.Cobra().MarkFlagRequired("month")) |
| 46 | +} |
| 47 | + |
| 48 | +func firstElementAsString(row output.TableRow) string { |
| 49 | + s, ok := row[0].(string) |
| 50 | + if !ok { |
| 51 | + return "" |
| 52 | + } |
| 53 | + return s |
| 54 | +} |
| 55 | + |
| 56 | +// ExecuteWithoutArguments implements commands.NoArgumentCommand |
| 57 | +func (s *billingCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) { |
| 58 | + if s.year < 1900 || s.year > 9999 { |
| 59 | + return nil, fmt.Errorf("invalid year: %d", s.year) |
| 60 | + } |
| 61 | + if s.month < 1 || s.month > 12 { |
| 62 | + return nil, fmt.Errorf("invalid month: %d", s.month) |
| 63 | + } |
| 64 | + |
| 65 | + svc := exec.Account() |
| 66 | + summary, err := svc.GetBillingSummary(exec.Context(), &request.GetBillingSummaryRequest{ |
| 67 | + YearMonth: fmt.Sprintf("%d-%02d", s.year, s.month), |
| 68 | + ResourceID: s.resourceID, |
| 69 | + Username: s.username, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + createCategorySections := func() []output.CombinedSection { |
| 76 | + var sections []output.CombinedSection |
| 77 | + var summaryRows []output.TableRow |
| 78 | + |
| 79 | + categories := map[string]*upcloud.BillingCategory{ |
| 80 | + "Servers": summary.Servers, |
| 81 | + "Managed Databases": summary.ManagedDatabases, |
| 82 | + "Managed Object Storages": summary.ManagedObjectStorages, |
| 83 | + "Managed Load Balancers": summary.ManagedLoadbalancers, |
| 84 | + "Managed Kubernetes": summary.ManagedKubernetes, |
| 85 | + "Network Gateways": summary.NetworkGateways, |
| 86 | + "Networks": summary.Networks, |
| 87 | + "Storages": summary.Storages, |
| 88 | + } |
| 89 | + |
| 90 | + for categoryName, category := range categories { |
| 91 | + if category != nil { |
| 92 | + summaryRows = append(summaryRows, output.TableRow{categoryName, category.TotalAmount}) |
| 93 | + resourceGroups := map[string]*upcloud.BillingResourceGroup{ |
| 94 | + "Server": category.Server, |
| 95 | + "Managed Database": category.ManagedDatabase, |
| 96 | + "Managed Object Storage": category.ManagedObjectStorage, |
| 97 | + "Managed Load Balancer": category.ManagedLoadbalancer, |
| 98 | + "Managed Kubernetes": category.ManagedKubernetes, |
| 99 | + "Network Gateway": category.NetworkGateway, |
| 100 | + "IPv4 Address": category.IPv4Address, |
| 101 | + "Backup": category.Backup, |
| 102 | + "Storage": category.Storage, |
| 103 | + "Template": category.Template, |
| 104 | + } |
| 105 | + |
| 106 | + for groupName, group := range resourceGroups { |
| 107 | + if group != nil && len(group.Resources) > 0 { |
| 108 | + var resourceRows []output.TableRow |
| 109 | + for _, resource := range group.Resources { |
| 110 | + resourceRows = append(resourceRows, output.TableRow{ |
| 111 | + resource.ResourceID, |
| 112 | + resource.Amount, |
| 113 | + resource.Hours, |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + sections = append(sections, output.CombinedSection{ |
| 118 | + Key: fmt.Sprintf("%s_%s_resources", categoryName, groupName), |
| 119 | + Title: fmt.Sprintf("%s - %s Resources:", categoryName, groupName), |
| 120 | + Contents: output.Table{ |
| 121 | + Columns: []output.TableColumn{ |
| 122 | + {Key: "resource_id", Header: "Resource ID"}, |
| 123 | + {Key: "amount", Header: "Amount"}, |
| 124 | + {Key: "hours", Header: "Hours"}, |
| 125 | + }, |
| 126 | + Rows: resourceRows, |
| 127 | + EmptyMessage: fmt.Sprintf("No resources for %s.", groupName), |
| 128 | + }, |
| 129 | + }) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + sort.Slice(summaryRows, func(i, j int) bool { |
| 136 | + return firstElementAsString(summaryRows[i]) < firstElementAsString(summaryRows[j]) |
| 137 | + }) |
| 138 | + summaryRows = append(summaryRows, output.TableRow{"Total", summary.TotalAmount}) |
| 139 | + |
| 140 | + sort.Slice(sections, func(i, j int) bool { |
| 141 | + return sections[i].Title < sections[j].Title |
| 142 | + }) |
| 143 | + sections = append([]output.CombinedSection{{ |
| 144 | + Key: "summary", |
| 145 | + Title: "Summary:", |
| 146 | + Contents: output.Table{ |
| 147 | + Columns: []output.TableColumn{ |
| 148 | + {Key: "resource", Header: "Resource"}, |
| 149 | + {Key: "total_amount", Header: "Amount"}, |
| 150 | + }, |
| 151 | + Rows: summaryRows, |
| 152 | + }, |
| 153 | + }}, sections...) |
| 154 | + return sections |
| 155 | + } |
| 156 | + |
| 157 | + combined := output.Combined(createCategorySections()) |
| 158 | + |
| 159 | + return output.MarshaledWithHumanOutput{ |
| 160 | + Value: summary, |
| 161 | + Output: combined, |
| 162 | + }, nil |
| 163 | +} |
0 commit comments