yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/business.go (about)

     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 aliyun
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  func (self *SAliyunClient) businessRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
    28  	cli, err := self.getDefaultClient()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return jsonRequest(cli, "business.aliyuncs.com", ALIYUN_BSS_API_VERSION, apiName, params, self.debug)
    33  }
    34  
    35  type SAccountBalance struct {
    36  	AvailableAmount     float64
    37  	AvailableCashAmount float64
    38  	CreditAmount        float64
    39  	MybankCreditAmount  float64
    40  	Currency            string
    41  }
    42  
    43  type SCashCoupon struct {
    44  	ApplicableProducts  string
    45  	ApplicableScenarios string
    46  	Balance             float64
    47  	CashCouponId        string
    48  	CashCouponNo        string
    49  	EffectiveTime       time.Time
    50  	ExpiryTime          time.Time
    51  	GrantedTime         time.Time
    52  	NominalValue        float64
    53  	Status              string
    54  }
    55  
    56  type SPrepaidCard struct {
    57  	PrepaidCardId       string
    58  	PrepaidCardNo       string
    59  	GrantedTime         time.Time
    60  	EffectiveTime       time.Time
    61  	ExpiryTime          time.Time
    62  	NominalValue        float64
    63  	Balance             float64
    64  	ApplicableProducts  string
    65  	ApplicableScenarios string
    66  }
    67  
    68  func (self *SAliyunClient) QueryAccountBalance() (*SAccountBalance, error) {
    69  	body, err := self.businessRequest("QueryAccountBalance", nil)
    70  	if err != nil {
    71  		// {"RequestId":"5258BDEF-8975-4EB0-9E0C-08D5E54E7981","HostId":"business.aliyuncs.com","Code":"NotAuthorized","Message":"This API is not authorized for caller."}
    72  		if isError(err, "NotApplicable") || isError(err, "NotAuthorized") {
    73  			return nil, cloudprovider.ErrNoBalancePermission
    74  		}
    75  		return nil, errors.Wrapf(err, "QueryAccountBalance")
    76  	}
    77  	balance := SAccountBalance{}
    78  	err = body.Unmarshal(&balance, "Data")
    79  	if err != nil {
    80  		return nil, errors.Wrapf(err, "Unmarshal AccountBalance")
    81  	}
    82  	return &balance, nil
    83  }
    84  
    85  func (self *SAliyunClient) QueryCashCoupons() ([]SCashCoupon, error) {
    86  	params := make(map[string]string)
    87  	params["EffectiveOrNot"] = "True"
    88  	body, err := self.businessRequest("QueryCashCoupons", params)
    89  	if err != nil {
    90  		return nil, errors.Wrapf(err, "QueryCashCoupons")
    91  	}
    92  	coupons := make([]SCashCoupon, 0)
    93  	err = body.Unmarshal(&coupons, "Data", "CashCoupon")
    94  	if err != nil {
    95  		return nil, errors.Wrapf(err, "body.Unmarshal")
    96  	}
    97  	return coupons, nil
    98  }
    99  
   100  func (self *SAliyunClient) QueryPrepaidCards() ([]SPrepaidCard, error) {
   101  	params := make(map[string]string)
   102  	params["EffectiveOrNot"] = "True"
   103  	body, err := self.businessRequest("QueryPrepaidCards", params)
   104  	if err != nil {
   105  		return nil, errors.Wrapf(err, "QueryPrepaidCards")
   106  	}
   107  	cards := make([]SPrepaidCard, 0)
   108  	err = body.Unmarshal(&cards, "Data", "PrepaidCard")
   109  	if err != nil {
   110  		return nil, errors.Wrapf(err, "body.Unmarshal")
   111  	}
   112  	return cards, nil
   113  }
   114  
   115  func (self *SAliyunClient) SubscribeBillToOSS(bucket string) error {
   116  	params := make(map[string]string)
   117  	params["SubscribeBucket"] = bucket
   118  	params["SubscribeType.0"] = "BillingItemDetailForBillingPeriod"
   119  	params["SubscribeType.1"] = "InstanceDetailForBillingPeriod"
   120  	body, err := self.businessRequest("SubscribeBillToOSS", params)
   121  	if err != nil {
   122  		return errors.Wrap(err, "SubscribeBillToOSS")
   123  	}
   124  	log.Debugf("%s", body)
   125  	return nil
   126  }