business.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. package aliyun
  15. import (
  16. "time"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. )
  22. func (self *SAliyunClient) businessRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  23. cli, err := self.getDefaultClient()
  24. if err != nil {
  25. return nil, err
  26. }
  27. return jsonRequest(cli, "business.aliyuncs.com", ALIYUN_BSS_API_VERSION, apiName, params, self.debug)
  28. }
  29. type SAccountBalance struct {
  30. AvailableAmount float64
  31. AvailableCashAmount float64
  32. CreditAmount float64
  33. MybankCreditAmount float64
  34. Currency string
  35. }
  36. type SCashCoupon struct {
  37. ApplicableProducts string
  38. ApplicableScenarios string
  39. Balance float64
  40. CashCouponId string
  41. CashCouponNo string
  42. EffectiveTime time.Time
  43. ExpiryTime time.Time
  44. GrantedTime time.Time
  45. NominalValue float64
  46. Status string
  47. }
  48. type SPrepaidCard struct {
  49. PrepaidCardId string
  50. PrepaidCardNo string
  51. GrantedTime time.Time
  52. EffectiveTime time.Time
  53. ExpiryTime time.Time
  54. NominalValue float64
  55. Balance float64
  56. ApplicableProducts string
  57. ApplicableScenarios string
  58. }
  59. func (self *SAliyunClient) QueryAccountBalance() (*SAccountBalance, error) {
  60. body, err := self.businessRequest("QueryAccountBalance", nil)
  61. if err != nil {
  62. // {"RequestId":"5258BDEF-8975-4EB0-9E0C-08D5E54E7981","HostId":"business.aliyuncs.com","Code":"NotAuthorized","Message":"This API is not authorized for caller."}
  63. if isError(err, "NotApplicable") || isError(err, "NotAuthorized") {
  64. return nil, cloudprovider.ErrNoBalancePermission
  65. }
  66. return nil, errors.Wrapf(err, "QueryAccountBalance")
  67. }
  68. balance := SAccountBalance{}
  69. err = body.Unmarshal(&balance, "Data")
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "Unmarshal AccountBalance")
  72. }
  73. return &balance, nil
  74. }
  75. func (self *SAliyunClient) QueryCashCoupons() ([]SCashCoupon, error) {
  76. params := make(map[string]string)
  77. params["EffectiveOrNot"] = "True"
  78. body, err := self.businessRequest("QueryCashCoupons", params)
  79. if err != nil {
  80. return nil, errors.Wrapf(err, "QueryCashCoupons")
  81. }
  82. coupons := make([]SCashCoupon, 0)
  83. err = body.Unmarshal(&coupons, "Data", "CashCoupon")
  84. if err != nil {
  85. return nil, errors.Wrapf(err, "body.Unmarshal")
  86. }
  87. return coupons, nil
  88. }
  89. func (self *SAliyunClient) QueryPrepaidCards() ([]SPrepaidCard, error) {
  90. params := make(map[string]string)
  91. params["EffectiveOrNot"] = "True"
  92. body, err := self.businessRequest("QueryPrepaidCards", params)
  93. if err != nil {
  94. return nil, errors.Wrapf(err, "QueryPrepaidCards")
  95. }
  96. cards := make([]SPrepaidCard, 0)
  97. err = body.Unmarshal(&cards, "Data", "PrepaidCard")
  98. if err != nil {
  99. return nil, errors.Wrapf(err, "body.Unmarshal")
  100. }
  101. return cards, nil
  102. }
  103. func (self *SAliyunClient) SubscribeBillToOSS(bucket string) error {
  104. params := make(map[string]string)
  105. params["SubscribeBucket"] = bucket
  106. params["SubscribeType.0"] = "BillingItemDetailForBillingPeriod"
  107. params["SubscribeType.1"] = "InstanceDetailForBillingPeriod"
  108. body, err := self.businessRequest("SubscribeBillToOSS", params)
  109. if err != nil {
  110. return errors.Wrap(err, "SubscribeBillToOSS")
  111. }
  112. log.Debugf("%s", body)
  113. return nil
  114. }
  115. type SBillOverview struct {
  116. BillingCycle string `json:"BillingCycle"`
  117. AccountID string `json:"AccountID"`
  118. AccountName string `json:"AccountName"`
  119. Items SBillItems `json:"Items"`
  120. TotalAmount float64 `json:"TotalAmount"`
  121. }
  122. type SBillItems struct {
  123. Item []SBillItem `json:"Item"`
  124. }
  125. type SBillItem struct {
  126. DeductedByCoupons float64 `json:"DeductedByCoupons"`
  127. RoundDownDiscount float64 `json:"RoundDownDiscount"`
  128. ProductName string `json:"ProductName"`
  129. ProductDetail string `json:"ProductDetail"`
  130. ProductCode string `json:"ProductCode"`
  131. BillAccountID string `json:"BillAccountID"`
  132. ProductType string `json:"ProductType"`
  133. DeductedByCashCoupons float64 `json:"DeductedByCashCoupons"`
  134. OutstandingAmount float64 `json:"OutstandingAmount"`
  135. BizType string `json:"BizType"`
  136. PaymentAmount float64 `json:"PaymentAmount"`
  137. PipCode string `json:"PipCode"`
  138. DeductedByPrepaidCard float64 `json:"DeductedByPrepaidCard"`
  139. InvoiceDiscount float64 `json:"InvoiceDiscount"`
  140. Item string `json:"Item"`
  141. SubscriptionType string `json:"SubscriptionType"`
  142. PretaxGrossAmount float64 `json:"PretaxGrossAmount"`
  143. PretaxAmount float64 `json:"PretaxAmount"`
  144. OwnerID string `json:"OwnerID"`
  145. Currency string `json:"Currency"`
  146. CommodityCode string `json:"CommodityCode"`
  147. BillAccountName string `json:"BillAccountName"`
  148. AdjustAmount float64 `json:"AdjustAmount"`
  149. CashAmount float64 `json:"CashAmount"`
  150. }
  151. func (self *SAliyunClient) QueryBillOverview(billCycle, ownerId string) (*SBillOverview, error) {
  152. params := make(map[string]string)
  153. params["BillingCycle"] = billCycle
  154. if len(ownerId) > 0 {
  155. params["BillOwnerId"] = ownerId
  156. }
  157. body, err := self.businessRequest("QueryBillOverview", params)
  158. if err != nil {
  159. return nil, errors.Wrap(err, "QueryBillOverview")
  160. }
  161. overview := SBillOverview{}
  162. err = body.Unmarshal(&overview, "Data")
  163. if err != nil {
  164. return nil, errors.Wrap(err, "body.Unmarshal")
  165. }
  166. totalAmount := 0.0
  167. for _, item := range overview.Items.Item {
  168. totalAmount += item.PretaxAmount
  169. }
  170. overview.TotalAmount = totalAmount
  171. return &overview, nil
  172. }