Skip to content

Commit 84c21cd

Browse files
authored
Merge pull request #1751 from ioito/hotfix/qx-bill-summary
fix: add qcloud and aliyun bill qummary cli
2 parents 14ff5e4 + 971b24d commit 84c21cd

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

pkg/multicloud/aliyun/business.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,65 @@ func (self *SAliyunClient) SubscribeBillToOSS(bucket string) error {
124124
log.Debugf("%s", body)
125125
return nil
126126
}
127+
128+
type SBillOverview struct {
129+
BillingCycle string `json:"BillingCycle"`
130+
AccountID string `json:"AccountID"`
131+
AccountName string `json:"AccountName"`
132+
Items SBillItems `json:"Items"`
133+
TotalAmount float64 `json:"TotalAmount"`
134+
}
135+
136+
type SBillItems struct {
137+
Item []SBillItem `json:"Item"`
138+
}
139+
140+
type SBillItem struct {
141+
DeductedByCoupons float64 `json:"DeductedByCoupons"`
142+
RoundDownDiscount float64 `json:"RoundDownDiscount"`
143+
ProductName string `json:"ProductName"`
144+
ProductDetail string `json:"ProductDetail"`
145+
ProductCode string `json:"ProductCode"`
146+
BillAccountID string `json:"BillAccountID"`
147+
ProductType string `json:"ProductType"`
148+
DeductedByCashCoupons float64 `json:"DeductedByCashCoupons"`
149+
OutstandingAmount float64 `json:"OutstandingAmount"`
150+
BizType string `json:"BizType"`
151+
PaymentAmount float64 `json:"PaymentAmount"`
152+
PipCode string `json:"PipCode"`
153+
DeductedByPrepaidCard float64 `json:"DeductedByPrepaidCard"`
154+
InvoiceDiscount float64 `json:"InvoiceDiscount"`
155+
Item string `json:"Item"`
156+
SubscriptionType string `json:"SubscriptionType"`
157+
PretaxGrossAmount float64 `json:"PretaxGrossAmount"`
158+
PretaxAmount float64 `json:"PretaxAmount"`
159+
OwnerID string `json:"OwnerID"`
160+
Currency string `json:"Currency"`
161+
CommodityCode string `json:"CommodityCode"`
162+
BillAccountName string `json:"BillAccountName"`
163+
AdjustAmount float64 `json:"AdjustAmount"`
164+
CashAmount float64 `json:"CashAmount"`
165+
}
166+
167+
func (self *SAliyunClient) QueryBillOverview(billCycle, ownerId string) (*SBillOverview, error) {
168+
params := make(map[string]string)
169+
params["BillingCycle"] = billCycle
170+
if len(ownerId) > 0 {
171+
params["BillOwnerId"] = ownerId
172+
}
173+
body, err := self.businessRequest("QueryBillOverview", params)
174+
if err != nil {
175+
return nil, errors.Wrap(err, "QueryBillOverview")
176+
}
177+
overview := SBillOverview{}
178+
err = body.Unmarshal(&overview, "Data")
179+
if err != nil {
180+
return nil, errors.Wrap(err, "body.Unmarshal")
181+
}
182+
totalAmount := 0.0
183+
for _, item := range overview.Items.Item {
184+
totalAmount += item.PretaxAmount
185+
}
186+
overview.TotalAmount = totalAmount
187+
return &overview, nil
188+
}

pkg/multicloud/aliyun/shell/business.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ func init() {
5959
return nil
6060
})
6161

62+
type AliyunQueryBillOverviewOptions struct {
63+
MONTH string
64+
OwnerId string `help:"Owner ID"`
65+
}
66+
shellutils.R(&AliyunQueryBillOverviewOptions{}, "query-bill-overview", "Query bill overview", func(cli *aliyun.SRegion, args *AliyunQueryBillOverviewOptions) error {
67+
result, err := cli.GetClient().QueryBillOverview(args.MONTH, args.OwnerId)
68+
if err != nil {
69+
return err
70+
}
71+
printObject(result)
72+
return nil
73+
})
74+
6275
}

pkg/multicloud/qcloud/qcloud.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,46 @@ func (client *SQcloudClient) QueryAccountBalance() (*SAccountBalance, error) {
10791079
return balance, nil
10801080
}
10811081

1082+
type SBillSummary struct {
1083+
Ready int `json:"Ready"`
1084+
SummaryDetail []SBillSummaryItem `json:"SummaryDetail"`
1085+
TotalAmount float64 `json:"TotalAmount"`
1086+
}
1087+
1088+
type SBillSummaryItem struct {
1089+
Business []jsonutils.JSONObject `json:"Business"`
1090+
CashPayAmount float64 `json:"CashPayAmount"`
1091+
GroupKey string `json:"GroupKey"`
1092+
GroupValue string `json:"GroupValue"`
1093+
IncentivePayAmount float64 `json:"IncentivePayAmount"`
1094+
RealTotalCost float64 `json:"RealTotalCost"`
1095+
TotalCost float64 `json:"TotalCost"`
1096+
TransferPayAmount float64 `json:"TransferPayAmount"`
1097+
VoucherPayAmount float64 `json:"VoucherPayAmount"`
1098+
}
1099+
1100+
func (client *SQcloudClient) DescribeBillSummary(month string, uin string) (*SBillSummary, error) {
1101+
params := make(map[string]string)
1102+
params["Month"] = month
1103+
params["GroupType"] = "business"
1104+
if len(uin) > 0 {
1105+
params["Uin"] = uin
1106+
}
1107+
body, err := client.billingRequest("DescribeBillSummary", params)
1108+
if err != nil {
1109+
return nil, errors.Wrapf(err, "DescribeBillSummary")
1110+
}
1111+
summary := SBillSummary{}
1112+
err = body.Unmarshal(&summary)
1113+
if err != nil {
1114+
return nil, errors.Wrapf(err, "body.Unmarshal")
1115+
}
1116+
for _, item := range summary.SummaryDetail {
1117+
summary.TotalAmount += item.RealTotalCost
1118+
}
1119+
return &summary, nil
1120+
}
1121+
10821122
func (client *SQcloudClient) GetIProjects() ([]cloudprovider.ICloudProject, error) {
10831123
projects := []SProject{}
10841124
for {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package shell
16+
17+
import (
18+
"yunion.io/x/pkg/util/shellutils"
19+
20+
"yunion.io/x/cloudmux/pkg/multicloud/qcloud"
21+
)
22+
23+
func init() {
24+
type QcloudBillSummaryOptions struct {
25+
MONTH string `help:"Billing month, e.g. 2024-02"`
26+
Uin string `help:"Root account UIN, optional"`
27+
}
28+
shellutils.R(&QcloudBillSummaryOptions{}, "bill-summary", "Query bill summary", func(cli *qcloud.SRegion, args *QcloudBillSummaryOptions) error {
29+
result, err := cli.GetClient().DescribeBillSummary(args.MONTH, args.Uin)
30+
if err != nil {
31+
return err
32+
}
33+
printObject(result)
34+
return nil
35+
})
36+
}

0 commit comments

Comments
 (0)