mod_roles.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/pkg/util/httputils"
  19. "yunion.io/x/pkg/util/printutils"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules"
  23. )
  24. type RolesManager struct {
  25. modulebase.ResourceManager
  26. }
  27. var (
  28. Roles RolesManager
  29. RolesV3 RolesManager
  30. )
  31. func (this *RolesManager) Delete(session *mcclient.ClientSession, id string, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  32. return this.DeleteInContexts(session, id, body, nil)
  33. }
  34. func (this *RolesManager) DeleteInContexts(session *mcclient.ClientSession, id string, body jsonutils.JSONObject, ctxs []modulebase.ManagerContext) (jsonutils.JSONObject, error) {
  35. if ctxs == nil {
  36. err := httputils.JSONClientError{}
  37. err.Code = 403
  38. err.Details = fmt.Sprintf("role %s did not allowed deleted", id)
  39. if id == "admin" || id == "_member_" {
  40. return nil, &err
  41. }
  42. resp, e := this.Get(session, id, body)
  43. if e != nil {
  44. return nil, e
  45. } else {
  46. name, _ := resp.GetString("name")
  47. if name == "admin" || name == "_member_" {
  48. return nil, &err
  49. }
  50. }
  51. }
  52. return this.ResourceManager.DeleteInContexts(session, id, body, ctxs)
  53. }
  54. func (this *RolesManager) BatchDelete(session *mcclient.ClientSession, idlist []string, body jsonutils.JSONObject) []printutils.SubmitResult {
  55. return this.BatchDeleteInContexts(session, idlist, body, nil)
  56. }
  57. func (this *RolesManager) BatchDeleteInContexts(session *mcclient.ClientSession, idlist []string, body jsonutils.JSONObject, ctxs []modulebase.ManagerContext) []printutils.SubmitResult {
  58. return modulebase.BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  59. return this.DeleteInContexts(session, id, body, ctxs)
  60. })
  61. }
  62. func init() {
  63. Roles = RolesManager{ResourceManager: modules.NewIdentityManager("role", "roles",
  64. []string{},
  65. []string{"ID", "Name"})}
  66. Roles.SetVersion("v2.0/OS-KSADM")
  67. modules.Register(&Roles)
  68. RolesV3 = RolesManager{ResourceManager: modules.NewIdentityV3Manager("role", "roles",
  69. []string{},
  70. []string{"ID", "Name", "Domain_Id", "Project_Domain", "Description", "is_public"})}
  71. modules.Register(&RolesV3)
  72. }