offering.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 zstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "github.com/pkg/errors"
  19. "yunion.io/x/jsonutils"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SInstanceOffering struct {
  25. multicloud.SServerSku
  26. ZStackTags
  27. region *SRegion
  28. ZStackBasic
  29. MemorySize int `json:"memorySize"`
  30. CPUNum int `json:"cpuNum"`
  31. CPUSpeed int `json:"cpuSpeed"`
  32. Type string `json:"type"`
  33. AllocatorStrategy string `json:"allocatorStrategy"`
  34. State string `json:"state"`
  35. ZStackTime
  36. }
  37. func (region *SRegion) GetInstanceOffering(offerId string) (*SInstanceOffering, error) {
  38. offer := &SInstanceOffering{region: region}
  39. return offer, region.client.getResource("instance-offerings", offerId, offer)
  40. }
  41. func (region *SRegion) GetInstanceOfferingByType(instanceType string) (*SInstanceOffering, error) {
  42. offerings, err := region.GetInstanceOfferings("", instanceType, 0, 0)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if len(offerings) >= 1 {
  47. return &offerings[0], nil
  48. }
  49. return nil, cloudprovider.ErrNotFound
  50. }
  51. func (region *SRegion) CreateISku(opts *cloudprovider.SServerSkuCreateOption) (cloudprovider.ICloudSku, error) {
  52. sku, err := region.CreateInstanceOffering(opts.Name, opts.CpuCount, opts.VmemSizeMb, "UserVm")
  53. if err != nil {
  54. return nil, errors.Wrapf(err, "CreateISku")
  55. }
  56. return sku, nil
  57. }
  58. func (region *SRegion) CreateInstanceOffering(name string, cpu int, memoryMb int, offeringType string) (*SInstanceOffering, error) {
  59. parmas := map[string]interface{}{
  60. "params": map[string]interface{}{
  61. "name": name,
  62. "cpuNum": cpu,
  63. "memorySize": memoryMb * 1024 * 1024,
  64. "type": offeringType,
  65. },
  66. }
  67. resp, err := region.client.post("instance-offerings", jsonutils.Marshal(parmas))
  68. if err != nil {
  69. return nil, errors.Wrapf(err, "CreateInstanceOffering")
  70. }
  71. offering := &SInstanceOffering{region: region}
  72. err = resp.Unmarshal(offering, "inventory")
  73. if err != nil {
  74. return nil, errors.Wrapf(err, "resp.Unmarshal")
  75. }
  76. return offering, nil
  77. }
  78. func (region *SRegion) GetInstanceOfferings(offerId string, name string, cpu int, memorySizeMb int) ([]SInstanceOffering, error) {
  79. offerings := []SInstanceOffering{}
  80. params := url.Values{}
  81. params.Add("q", "type=UserVM")
  82. params.Add("q", "state=Enabled")
  83. if len(offerId) > 0 {
  84. params.Add("q", "uid="+offerId)
  85. }
  86. if len(name) > 0 {
  87. params.Add("q", "name="+name)
  88. }
  89. if cpu != 0 {
  90. params.Add("q", fmt.Sprintf("cpuNum=%d", cpu))
  91. }
  92. if memorySizeMb != 0 {
  93. params.Add("q", fmt.Sprintf("memorySize=%d", memorySizeMb*1024*1024))
  94. }
  95. if err := region.client.listAll("instance-offerings", params, &offerings); err != nil {
  96. return nil, err
  97. }
  98. for i := 0; i < len(offerings); i++ {
  99. offerings[i].region = region
  100. }
  101. return offerings, nil
  102. }
  103. func (offering *SInstanceOffering) IsEmulated() bool {
  104. return false
  105. }
  106. func (offering *SInstanceOffering) Refresh() error {
  107. new, err := offering.region.GetInstanceOffering(offering.UUID)
  108. if err != nil {
  109. return err
  110. }
  111. return jsonutils.Update(offering, new)
  112. }
  113. func (offering *SInstanceOffering) GetName() string {
  114. return offering.Name
  115. }
  116. func (offering *SInstanceOffering) GetStatus() string {
  117. switch offering.State {
  118. case "Enabled":
  119. return api.SkuStatusReady
  120. }
  121. return api.SkuStatusSoldout
  122. }
  123. func (offering *SInstanceOffering) GetId() string {
  124. return offering.UUID
  125. }
  126. func (offering *SInstanceOffering) GetGlobalId() string {
  127. return offering.UUID
  128. }
  129. func (offering *SInstanceOffering) Delete() error {
  130. return offering.region.DeleteOffering(offering.UUID)
  131. }
  132. func (region *SRegion) DeleteOffering(offeringId string) error {
  133. return region.client.delete("instance-offerings", offeringId, "")
  134. }
  135. func (offering *SInstanceOffering) GetInstanceTypeFamily() string {
  136. return api.InstanceFamilies[api.SkuCategoryGeneralPurpose]
  137. }
  138. func (offering *SInstanceOffering) GetInstanceTypeCategory() string {
  139. return api.SkuCategoryGeneralPurpose
  140. }
  141. func (offering *SInstanceOffering) GetPrepaidStatus() string {
  142. return api.SkuStatusSoldout
  143. }
  144. func (offering *SInstanceOffering) GetCpuArch() string {
  145. return ""
  146. }
  147. func (offering *SInstanceOffering) GetPostpaidStatus() string {
  148. return api.SkuStatusAvailable
  149. }
  150. func (offering *SInstanceOffering) GetCpuCoreCount() int {
  151. return offering.CPUNum
  152. }
  153. func (offering *SInstanceOffering) GetMemorySizeMB() int {
  154. return offering.MemorySize / 1024 / 1024
  155. }
  156. func (offering *SInstanceOffering) GetOsName() string {
  157. return "Any"
  158. }
  159. func (offering *SInstanceOffering) GetSysDiskResizable() bool {
  160. return true
  161. }
  162. func (offering *SInstanceOffering) GetSysDiskType() string {
  163. return ""
  164. }
  165. func (offering *SInstanceOffering) GetSysDiskMinSizeGB() int {
  166. return 0
  167. }
  168. func (offering *SInstanceOffering) GetSysDiskMaxSizeGB() int {
  169. return 0
  170. }
  171. func (offering *SInstanceOffering) GetAttachedDiskType() string {
  172. return ""
  173. }
  174. func (offering *SInstanceOffering) GetAttachedDiskSizeGB() int {
  175. return 0
  176. }
  177. func (offering *SInstanceOffering) GetAttachedDiskCount() int {
  178. return 0
  179. }
  180. func (offering *SInstanceOffering) GetDataDiskTypes() string {
  181. return ""
  182. }
  183. func (offering *SInstanceOffering) GetDataDiskMaxCount() int {
  184. return 0
  185. }
  186. func (offering *SInstanceOffering) GetNicType() string {
  187. return "vpc"
  188. }
  189. func (offering *SInstanceOffering) GetNicMaxCount() int {
  190. return 1
  191. }
  192. func (offering *SInstanceOffering) GetGpuAttachable() bool {
  193. return false
  194. }
  195. func (offering *SInstanceOffering) GetGpuSpec() string {
  196. return ""
  197. }
  198. func (offering *SInstanceOffering) GetGpuCount() string {
  199. return ""
  200. }
  201. func (offering *SInstanceOffering) GetGpuMaxCount() int {
  202. return 0
  203. }