domains.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/cmd/climc/shell"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. identity_options "yunion.io/x/onecloud/pkg/mcclient/options/identity"
  22. )
  23. func init() {
  24. cmd := shell.NewResourceCmd(&modules.Domains)
  25. cmd.List(&identity_options.DomainListOptions{})
  26. cmd.Perform("user-metadata", &options.ResourceMetadataOptions{})
  27. cmd.Perform("set-user-metadata", &options.ResourceMetadataOptions{})
  28. cmd.GetProperty(&identity_options.DomainGetPropertyTagValuePairOptions{})
  29. cmd.GetProperty(&identity_options.DomainGetPropertyTagValueTreeOptions{})
  30. type DomainDetailOptions struct {
  31. ID string `help:"ID or domain"`
  32. }
  33. R(&DomainDetailOptions{}, "domain-show", "Show detail of domain", func(s *mcclient.ClientSession, args *DomainDetailOptions) error {
  34. result, err := modules.Domains.Get(s, args.ID, nil)
  35. if err != nil {
  36. return err
  37. }
  38. printObject(result)
  39. return nil
  40. })
  41. R(&DomainDetailOptions{}, "domain-delete", "Delete a domain", func(s *mcclient.ClientSession, args *DomainDetailOptions) error {
  42. objId, err := modules.Domains.GetId(s, args.ID, nil)
  43. if err != nil {
  44. return err
  45. }
  46. result, err := modules.Domains.Delete(s, objId, nil)
  47. if err != nil {
  48. return err
  49. }
  50. printObject(result)
  51. return nil
  52. })
  53. /* R(&DomainDetailOptions{}, "domain-config-sql", "Config a domain with SQL driver", func(s *mcclient.ClientSession, args *DomainDetailOptions) error {
  54. config := jsonutils.NewDict()
  55. config.Add(jsonutils.NewString("sql"), "config", "identity", "driver")
  56. objId, err := modules.Domains.GetId(s, args.ID, nil)
  57. if err != nil {
  58. return err
  59. }
  60. nconf, err := modules.Domains.UpdateConfig(s, objId, config)
  61. if err != nil {
  62. return err
  63. }
  64. fmt.Println(nconf.PrettyString())
  65. return nil
  66. }) */
  67. type DomainCreateOptions struct {
  68. NAME string `help:"Name of domain"`
  69. Desc string `help:"Description"`
  70. Enabled bool `help:"Set the domain enabled"`
  71. Disabled bool `help:"Set the domain disabled"`
  72. Displayname string `help:"display name"`
  73. }
  74. R(&DomainCreateOptions{}, "domain-create", "Create a new domain", func(s *mcclient.ClientSession, args *DomainCreateOptions) error {
  75. params := jsonutils.NewDict()
  76. params.Add(jsonutils.NewString(args.NAME), "name")
  77. if len(args.Desc) > 0 {
  78. params.Add(jsonutils.NewString(args.Desc), "description")
  79. }
  80. if args.Enabled && !args.Disabled {
  81. params.Add(jsonutils.JSONTrue, "enabled")
  82. } else if !args.Enabled && args.Disabled {
  83. params.Add(jsonutils.JSONFalse, "enabled")
  84. }
  85. if len(args.Displayname) > 0 {
  86. params.Add(jsonutils.NewString(args.Displayname), "displayname")
  87. }
  88. result, err := modules.Domains.Create(s, params)
  89. if err != nil {
  90. return err
  91. }
  92. printObject(result)
  93. return nil
  94. })
  95. type DomainUpdateOptions struct {
  96. ID string `help:"ID of domain to update"`
  97. Name string `help:"Name of domain"`
  98. Desc string `help:"Description"`
  99. Enabled bool `help:"Set the domain enabled"`
  100. Disabled bool `help:"Set the domain disabled"`
  101. Driver string `help:"Set the domain Driver"`
  102. Displayname string `help:"display name"`
  103. }
  104. R(&DomainUpdateOptions{}, "domain-update", "Update a domain", func(s *mcclient.ClientSession, args *DomainUpdateOptions) error {
  105. obj, err := modules.Domains.Get(s, args.ID, nil)
  106. if err != nil {
  107. return err
  108. }
  109. objId, err := obj.GetString("id")
  110. if err != nil {
  111. return err
  112. }
  113. params := jsonutils.NewDict()
  114. if len(args.Name) > 0 {
  115. params.Add(jsonutils.NewString(args.Name), "name")
  116. }
  117. if len(args.Desc) > 0 {
  118. params.Add(jsonutils.NewString(args.Desc), "description")
  119. }
  120. if len(args.Driver) > 0 {
  121. params.Add(jsonutils.NewString(args.Driver), "driver")
  122. }
  123. if args.Enabled && !args.Disabled {
  124. params.Add(jsonutils.JSONTrue, "enabled")
  125. } else if !args.Enabled && args.Disabled {
  126. params.Add(jsonutils.JSONFalse, "enabled")
  127. }
  128. if len(args.Displayname) > 0 {
  129. params.Add(jsonutils.NewString(args.Displayname), "displayname")
  130. }
  131. result, err := modules.Domains.Patch(s, objId, params)
  132. if err != nil {
  133. return err
  134. }
  135. printObject(result)
  136. return nil
  137. })
  138. type DomainUnlinkIdpOptions struct {
  139. DOMAIN string `help:"ID or name of domain to operate" json:"-"`
  140. }
  141. R(&DomainUnlinkIdpOptions{}, "domain-unlink-idp", "Unlink domain from an entity in the speicified identity provider", func(s *mcclient.ClientSession, args *DomainUnlinkIdpOptions) error {
  142. result, err := modules.Domains.PerformAction(s, args.DOMAIN, "unlink-idp", nil)
  143. if err != nil {
  144. return err
  145. }
  146. printObject(result)
  147. return nil
  148. })
  149. }