cloudregions.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 compute
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/printutils"
  19. "yunion.io/x/onecloud/cmd/climc/shell"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  24. )
  25. func init() {
  26. cmd := shell.NewResourceCmd(&modules.Cloudregions).WithKeyword("cloud-region")
  27. cmd.PerformClass("sync-skus", &compute.CloudregionSkuSyncOptions{})
  28. cmd.Perform("sync-images", &compute.CloudregionIdOptions{})
  29. cmd.Perform("purge", &compute.CloudregionPurgeOptions{})
  30. type CloudregionListOptions struct {
  31. options.BaseListOptions
  32. Usable *bool `help:"List regions where networks are usable"`
  33. UsableVpc *bool `help:"List regions where VPC are usable"`
  34. Service string `help:"List regions which service has available skus" choices:"dbinstances|servers|elasticcaches"`
  35. City string `help:"List regions in the specified city"`
  36. Capability []string `help:"capability filter" choices:"project|compute|network|loadbalancer|objectstore|rds|cache|event"`
  37. DistinctField string `help:"list the specified distinct field, e.g. city, region"`
  38. OrderByZoneCount string
  39. OrderByVpcCount string
  40. OrderByGuestCount string
  41. }
  42. R(&CloudregionListOptions{}, "cloud-region-list", "List cloud regions", func(s *mcclient.ClientSession, opts *CloudregionListOptions) error {
  43. params, err := options.ListStructToParams(opts)
  44. if err != nil {
  45. return err
  46. }
  47. if len(opts.DistinctField) > 0 {
  48. params.Add(jsonutils.NewString(opts.DistinctField), "extra_field")
  49. result, err := modules.Cloudregions.Get(s, "distinct-field", params)
  50. if err != nil {
  51. return err
  52. }
  53. fmt.Println(result)
  54. return nil
  55. }
  56. result, err := modules.Cloudregions.List(s, params)
  57. if err != nil {
  58. return err
  59. }
  60. printList(result, modules.Cloudregions.GetColumns(s))
  61. return nil
  62. })
  63. type CloudregionCityListOptions struct {
  64. Manager string `help:"List objects belonging to the cloud provider"`
  65. Account string `help:"List objects belonging to the cloud account"`
  66. Provider string `help:"List objects from the provider" choices:"VMware|Aliyun|Qcloud|Azure|Aws|Huawei|Openstack|Ucloud|ZStack|Google|Ctyun"`
  67. City string `help:"List regions in the specified city"`
  68. PublicCloud *bool `help:"List objects belonging to public cloud" json:"public_cloud"`
  69. PrivateCloud *bool `help:"List objects belonging to private cloud" json:"private_cloud"`
  70. Usable *bool `help:"List regions where networks are usable"`
  71. UsableVpc *bool `help:"List regions where VPC are usable"`
  72. Scope string `help:"query scope" choices:"system|domain"`
  73. ProjectDomain string `help:"query domain"`
  74. }
  75. R(&CloudregionCityListOptions{}, "cloud-region-cities", "List cities where cloud region resides", func(s *mcclient.ClientSession, args *CloudregionCityListOptions) error {
  76. params, err := options.StructToParams(args)
  77. if err != nil {
  78. return err
  79. }
  80. results, err := modules.Cloudregions.GetRegionCities(s, params)
  81. if err != nil {
  82. return err
  83. }
  84. listResult := printutils.ListResult{}
  85. listResult.Data, err = results.GetArray()
  86. if err != nil {
  87. return err
  88. }
  89. printList(&listResult, nil)
  90. return nil
  91. })
  92. R(&CloudregionCityListOptions{}, "cloud-region-providers", "List cities where cloud region resides", func(s *mcclient.ClientSession, args *CloudregionCityListOptions) error {
  93. params, err := options.StructToParams(args)
  94. if err != nil {
  95. return err
  96. }
  97. results, err := modules.Cloudregions.GetRegionProviders(s, params)
  98. if err != nil {
  99. return err
  100. }
  101. listResult := printutils.ListResult{}
  102. listResult.Data, err = results.GetArray()
  103. if err != nil {
  104. return err
  105. }
  106. printList(&listResult, nil)
  107. return nil
  108. })
  109. R(&CloudregionCityListOptions{}, "cloud-city-servers", "List cities where cloud region resides", func(s *mcclient.ClientSession, args *CloudregionCityListOptions) error {
  110. params, err := options.StructToParams(args)
  111. if err != nil {
  112. return err
  113. }
  114. results, err := modules.Cloudregions.GetCityServers(s, params)
  115. if err != nil {
  116. return err
  117. }
  118. listResult := printutils.ListResult{}
  119. listResult.Data, err = results.GetArray()
  120. if err != nil {
  121. return err
  122. }
  123. printList(&listResult, nil)
  124. return nil
  125. })
  126. type CloudregionCreateOptions struct {
  127. Id string `help:"ID of the region"`
  128. NAME string `help:"Name of the region"`
  129. Provider string `help:"Cloud provider"`
  130. Desc string `help:"Description" json:"description" token:"desc"`
  131. Latitude float32 `help:"region geographical location - latitude"`
  132. Longitude float32 `help:"region geographical location - longitude"`
  133. City string `help:"region geograpical location - city, e.g. Beijing, Frankfurt"`
  134. CountryCode string `help:"region geographical location - ISO country code, e.g. CN"`
  135. }
  136. R(&CloudregionCreateOptions{}, "cloud-region-create", "Create a cloud region", func(s *mcclient.ClientSession, args *CloudregionCreateOptions) error {
  137. params, err := options.StructToParams(args)
  138. if err != nil {
  139. return err
  140. }
  141. results, err := modules.Cloudregions.Create(s, params)
  142. if err != nil {
  143. return err
  144. }
  145. printObject(results)
  146. return nil
  147. })
  148. type CloudregionShowOptions struct {
  149. ID string `help:"ID or name of the region"`
  150. }
  151. R(&CloudregionShowOptions{}, "cloud-region-show", "Show a cloud region", func(s *mcclient.ClientSession, args *CloudregionShowOptions) error {
  152. results, err := modules.Cloudregions.Get(s, args.ID, nil)
  153. if err != nil {
  154. return err
  155. }
  156. printObject(results)
  157. return nil
  158. })
  159. R(&CloudregionShowOptions{}, "cloud-region-delete", "Delete a cloud region", func(s *mcclient.ClientSession, args *CloudregionShowOptions) error {
  160. results, err := modules.Cloudregions.Delete(s, args.ID, nil)
  161. if err != nil {
  162. return err
  163. }
  164. printObject(results)
  165. return nil
  166. })
  167. type CloudregionUpdateOptions struct {
  168. ID string `help:"ID or name of the region to update" json:"-"`
  169. Name string `help:"New name of the region"`
  170. Desc string `help:"Description of the region" json:"description" token:"desc"`
  171. Latitude float32 `help:"region geographical location - latitude"`
  172. Longitude float32 `help:"region geographical location - longitude"`
  173. City string `help:"region geograpical location - city, e.g. Beijing, Frankfurt"`
  174. CountryCode string `help:"region geographical location - ISO country code, e.g. CN"`
  175. }
  176. R(&CloudregionUpdateOptions{}, "cloud-region-update", "Update a cloud region", func(s *mcclient.ClientSession, args *CloudregionUpdateOptions) error {
  177. params, err := options.StructToParams(args)
  178. if err != nil {
  179. return err
  180. }
  181. results, err := modules.Cloudregions.Update(s, args.ID, params)
  182. if err != nil {
  183. return err
  184. }
  185. printObject(results)
  186. return nil
  187. })
  188. type CloudregionSetDefaultVpcOptions struct {
  189. ID string `help:"ID or name of the region"`
  190. VPC string `help:"ID or name of VPC to make default"`
  191. }
  192. R(&CloudregionSetDefaultVpcOptions{}, "cloud-region-set-default-vpc", "Set default vpc for a region", func(s *mcclient.ClientSession, args *CloudregionSetDefaultVpcOptions) error {
  193. params := jsonutils.NewDict()
  194. params.Add(jsonutils.NewString(args.VPC), "vpc")
  195. result, err := modules.Cloudregions.PerformAction(s, args.ID, "default-vpc", params)
  196. if err != nil {
  197. return err
  198. }
  199. printObject(result)
  200. return nil
  201. })
  202. type CloudregionCapabiltyOptions struct {
  203. ID string `help:"ID or name of cloud region to check" json:"-"`
  204. Domain string `help:"cloud region domain"`
  205. ShowEmulated bool `help:"show emulated cloud region"`
  206. }
  207. R(&CloudregionCapabiltyOptions{}, "cloud-region-capability", "Show region's capacibilities", func(s *mcclient.ClientSession, args *CloudregionCapabiltyOptions) error {
  208. query, err := options.StructToParams(args)
  209. result, err := modules.Cloudregions.GetSpecific(s, args.ID, "capability", query)
  210. if err != nil {
  211. return err
  212. }
  213. printObject(result)
  214. return nil
  215. })
  216. }