price.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 baidu
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. )
  18. type SInstancePrice struct {
  19. SpecId string
  20. SpecPrices []struct {
  21. Spec string
  22. SpecPrice float64
  23. Discount float64
  24. TradePrice float64
  25. Status string
  26. }
  27. }
  28. func (region *SRegion) GetInstancePrice(zoneName string, specId, spec string) ([]SInstancePrice, error) {
  29. params := map[string]interface{}{
  30. "zoneName": zoneName,
  31. "specId": specId,
  32. "spec": spec,
  33. "paymentTiming": "Postpaid",
  34. }
  35. resp, err := region.bccPost("v2/instance/price", nil, params)
  36. if err != nil {
  37. return nil, errors.Wrap(err, "get instance price")
  38. }
  39. ret := []SInstancePrice{}
  40. err = resp.Unmarshal(&ret, "price")
  41. if err != nil {
  42. return nil, errors.Wrap(err, "unmarshal instance price")
  43. }
  44. prepaid := []SInstancePrice{}
  45. params["paymentTiming"] = "Prepaid"
  46. params["purchaseLength"] = 1
  47. resp, err = region.bccPost("v2/instance/price", nil, params)
  48. if err != nil {
  49. return nil, errors.Wrap(err, "get prepaid instance price")
  50. }
  51. err = resp.Unmarshal(&prepaid, "price")
  52. if err != nil {
  53. return nil, errors.Wrap(err, "unmarshal prepaid price")
  54. }
  55. ret = append(ret, prepaid...)
  56. return ret, nil
  57. }