regions.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 identity
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  20. )
  21. func init() {
  22. type RegionListOptions struct {
  23. Limit int64 `help:"Limit, default 0, i.e. no limit" default:"20"`
  24. Offset int64 `help:"Offset, default 0, i.e. no offset"`
  25. Search string `help:"Search by name"`
  26. }
  27. R(&RegionListOptions{}, "region-list", "List regions", func(s *mcclient.ClientSession, args *RegionListOptions) error {
  28. query := jsonutils.NewDict()
  29. if args.Limit > 0 {
  30. query.Add(jsonutils.NewInt(args.Limit), "limit")
  31. }
  32. if args.Offset > 0 {
  33. query.Add(jsonutils.NewInt(args.Offset), "offset")
  34. }
  35. if len(args.Search) > 0 {
  36. query.Add(jsonutils.NewString(args.Search), "id__icontains")
  37. }
  38. result, err := modules.Regions.List(s, query)
  39. if err != nil {
  40. return err
  41. }
  42. printList(result, modules.Regions.GetColumns(s))
  43. return nil
  44. })
  45. type RegionShowOptions struct {
  46. REGION string `help:"ID of region"`
  47. Zone string `help:"ID of region"`
  48. }
  49. R(&RegionShowOptions{}, "region-show", "Show details of region", func(s *mcclient.ClientSession, args *RegionShowOptions) error {
  50. ID := mcclient.RegionID(args.REGION, args.Zone)
  51. result, err := modules.Regions.Get(s, ID, nil)
  52. if err != nil {
  53. return err
  54. }
  55. printObject(result)
  56. return nil
  57. })
  58. R(&RegionShowOptions{}, "region-delete", "Delete region", func(s *mcclient.ClientSession, args *RegionShowOptions) error {
  59. ID := mcclient.RegionID(args.REGION, args.Zone)
  60. _, err := modules.Regions.Delete(s, ID, nil)
  61. if err != nil {
  62. return err
  63. }
  64. return nil
  65. })
  66. type RegionCreateOptions struct {
  67. REGION string `help:"ID of the region"`
  68. Zone string `help:"ID of the zone"`
  69. Name string `help:"Name of the region"`
  70. Desc string `help:"Description"`
  71. }
  72. R(&RegionCreateOptions{}, "region-create", "Create a region", func(s *mcclient.ClientSession, args *RegionCreateOptions) error {
  73. params := jsonutils.NewDict()
  74. ID := mcclient.RegionID(args.REGION, args.Zone)
  75. params.Add(jsonutils.NewString(ID), "id")
  76. if len(args.Name) > 0 {
  77. params.Add(jsonutils.NewString(args.Name), "name")
  78. }
  79. if len(args.Desc) > 0 {
  80. params.Add(jsonutils.NewString(args.Desc), "description")
  81. }
  82. if len(args.Zone) > 0 {
  83. params.Add(jsonutils.NewString(args.REGION), "parent_region_id")
  84. }
  85. region, err := modules.Regions.Create(s, params)
  86. if err != nil {
  87. return err
  88. }
  89. printObject(region)
  90. return nil
  91. })
  92. type RegionUpdateOptions struct {
  93. REGION string `help:"ID of region"`
  94. Zone string `help:"Zone"`
  95. Name string `help:"New name of the region"`
  96. Desc string `help:"New description of the region"`
  97. }
  98. R(&RegionUpdateOptions{}, "region-update", "Update a region", func(s *mcclient.ClientSession, args *RegionUpdateOptions) error {
  99. ID := mcclient.RegionID(args.REGION, args.Zone)
  100. params := jsonutils.NewDict()
  101. if len(args.Name) > 0 {
  102. params.Add(jsonutils.NewString(args.Name), "name")
  103. }
  104. if len(args.Desc) > 0 {
  105. params.Add(jsonutils.NewString(args.Desc), "description")
  106. }
  107. if params.Size() == 0 {
  108. return fmt.Errorf("No data to update")
  109. }
  110. region, err := modules.Regions.Patch(s, ID, params)
  111. if err != nil {
  112. return err
  113. }
  114. printObject(region)
  115. return nil
  116. })
  117. }