price.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 ctyun
  15. import "yunion.io/x/pkg/errors"
  16. type GetPriceOptions struct {
  17. ResourceType string `default:"VM" choices:"VM|EBS"`
  18. OnDemand bool `default:"true" negative:"non-demand"`
  19. FlavorName string
  20. DiskType string
  21. }
  22. type SPrice struct {
  23. TotalPrice float64
  24. SubOrderPrices []struct {
  25. OrderItemPrices []struct {
  26. ItemId string
  27. ResourceType string
  28. TotalPrice float64
  29. FinalPrice float64
  30. }
  31. }
  32. }
  33. func (r *SRegion) GetPrice(opts *GetPriceOptions) (*SPrice, error) {
  34. params := map[string]interface{}{
  35. "regionID": r.RegionId,
  36. "resourceType": opts.ResourceType,
  37. "count": 1,
  38. "onDemand": opts.OnDemand,
  39. }
  40. if !opts.OnDemand {
  41. params["cycleType"] = "MONTH"
  42. params["cycleCount"] = 1
  43. }
  44. switch opts.ResourceType {
  45. case "VM":
  46. params["flavorName"] = opts.FlavorName
  47. params["sysDiskType"] = "SATA"
  48. params["sysDiskSize"] = 40
  49. images, err := r.GetImages("public")
  50. if err != nil {
  51. return nil, err
  52. }
  53. for _, image := range images {
  54. if image.OsType == "linux" {
  55. params["imageUUID"] = image.ImageId
  56. break
  57. }
  58. }
  59. case "EBS":
  60. params["diskMode"] = "VBD"
  61. params["diskSize"] = 10
  62. params["diskType"] = opts.DiskType
  63. }
  64. resp, err := r.post(SERVICE_ECS, "/v4/new-order/query-price", params)
  65. if err != nil {
  66. return nil, err
  67. }
  68. ret := &SPrice{}
  69. err = resp.Unmarshal(ret, "returnObj")
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "Unmarshal")
  72. }
  73. return ret, nil
  74. }