endpoints.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. type EndpointListOptions struct {
  23. options.BaseListOptions
  24. Region string `help:"Search by region"`
  25. Service string `help:"Search by service id or name"`
  26. Interface string `help:"Search by interface"`
  27. }
  28. R(&EndpointListOptions{}, "endpoint-list", "List service endpoints", func(s *mcclient.ClientSession, args *EndpointListOptions) error {
  29. query, err := options.ListStructToParams(args)
  30. if err != nil {
  31. return err
  32. }
  33. result, err := modules.EndpointsV3.List(s, query)
  34. if err != nil {
  35. return err
  36. }
  37. printList(result, modules.EndpointsV3.GetColumns(s))
  38. return nil
  39. })
  40. type EndpointDetailOptions struct {
  41. ID string `help:"ID or name of endpoints"`
  42. }
  43. R(&EndpointDetailOptions{}, "endpoint-show", "Show details of an endpoint", func(s *mcclient.ClientSession, args *EndpointDetailOptions) error {
  44. ep, err := modules.EndpointsV3.Get(s, args.ID, nil)
  45. if err != nil {
  46. return err
  47. }
  48. printObject(ep)
  49. return nil
  50. })
  51. R(&EndpointDetailOptions{}, "endpoint-delete", "Delete an endpoint", func(s *mcclient.ClientSession, args *EndpointDetailOptions) error {
  52. ep, err := modules.EndpointsV3.Delete(s, args.ID, nil)
  53. if err != nil {
  54. return err
  55. }
  56. printObject(ep)
  57. return nil
  58. })
  59. type EndpointCreateOptions struct {
  60. SERVICE string `help:"Service ID or Name"`
  61. REGION string `help:"Region"`
  62. INTERFACE string `help:"Interface types" choices:"internal|public|admin|console|slave"`
  63. URL string `help:"URL"`
  64. Zone string `help:"Zone"`
  65. Name string `help:"Name"`
  66. Enabled bool `help:"Enabled"`
  67. Disabled bool `help:"Disabled"`
  68. ServiceCertificate string `help:"Service certificate id or name"`
  69. }
  70. R(&EndpointCreateOptions{}, "endpoint-create", "Create endpoint", func(s *mcclient.ClientSession, args *EndpointCreateOptions) error {
  71. params := jsonutils.NewDict()
  72. srvId, err := modules.ServicesV3.GetId(s, args.SERVICE, nil)
  73. if err != nil {
  74. return err
  75. }
  76. params.Add(jsonutils.NewString(srvId), "service_id")
  77. regionId, err := modules.Regions.GetId(s, mcclient.RegionID(args.REGION, args.Zone), nil)
  78. if err != nil {
  79. return err
  80. }
  81. params.Add(jsonutils.NewString(regionId), "region_id")
  82. params.Add(jsonutils.NewString(args.INTERFACE), "interface")
  83. params.Add(jsonutils.NewString(args.URL), "url")
  84. if len(args.Name) > 0 {
  85. params.Add(jsonutils.NewString(args.Name), "name")
  86. }
  87. if args.Enabled && !args.Disabled {
  88. params.Add(jsonutils.JSONTrue, "enabled")
  89. } else if !args.Enabled && args.Disabled {
  90. params.Add(jsonutils.JSONFalse, "enabled")
  91. }
  92. if len(args.ServiceCertificate) > 0 {
  93. params.Add(jsonutils.NewString(args.ServiceCertificate), "service_certificate")
  94. }
  95. ep, err := modules.EndpointsV3.Create(s, params)
  96. if err != nil {
  97. return err
  98. }
  99. printObject(ep)
  100. return nil
  101. })
  102. type EndpointUpdateOptions struct {
  103. ID string `help:"ID or name of endpoint"`
  104. Url string `help:"URL"`
  105. Name string `help:"Name"`
  106. Enabled bool `help:"Enabled"`
  107. Disabled bool `help:"Disabled"`
  108. ServiceCertificate string `help:"Service certificate id or name"`
  109. }
  110. R(&EndpointUpdateOptions{}, "endpoint-update", "Update a endpoint", func(s *mcclient.ClientSession, args *EndpointUpdateOptions) error {
  111. params := jsonutils.NewDict()
  112. if len(args.Url) > 0 {
  113. params.Add(jsonutils.NewString(args.Url), "url")
  114. }
  115. if len(args.Name) > 0 {
  116. params.Add(jsonutils.NewString(args.Name), "name")
  117. }
  118. if args.Enabled && !args.Disabled {
  119. params.Add(jsonutils.JSONTrue, "enabled")
  120. } else if !args.Enabled && args.Disabled {
  121. params.Add(jsonutils.JSONFalse, "enabled")
  122. }
  123. if len(args.ServiceCertificate) > 0 {
  124. params.Add(jsonutils.NewString(args.ServiceCertificate), "service_certificate")
  125. }
  126. ep, err := modules.EndpointsV3.Patch(s, args.ID, params)
  127. if err != nil {
  128. return err
  129. }
  130. printObject(ep)
  131. return nil
  132. })
  133. }