mod_policies.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  20. "yunion.io/x/onecloud/pkg/mcclient/modules"
  21. "yunion.io/x/onecloud/pkg/util/rbacutils"
  22. )
  23. type SPolicyManager struct {
  24. modulebase.ResourceManager
  25. }
  26. var Policies SPolicyManager
  27. func policyReadFilter(session *mcclient.ClientSession, s jsonutils.JSONObject, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  28. ss, ok := s.(*jsonutils.JSONDict)
  29. if !ok {
  30. return s, nil
  31. }
  32. ret := ss.CopyExcludes("blob", "type")
  33. blobJson, _ := ss.Get("blob")
  34. if blobJson != nil {
  35. blobStr, _ := blobJson.GetString()
  36. if len(blobStr) > 0 {
  37. blobJson, _ = jsonutils.ParseString(blobStr)
  38. }
  39. policy, err := rbacutils.DecodeRawPolicyData(blobJson)
  40. if err != nil {
  41. return nil, errors.Wrap(err, "rbacutils.DecodePolicyData")
  42. }
  43. blobJson = policy.EncodeRawData()
  44. var format string
  45. if query != nil {
  46. format, _ = query.GetString("format")
  47. }
  48. if format == "yaml" {
  49. var policy string
  50. if blobJson != nil {
  51. policy = blobJson.YAMLString()
  52. }
  53. ret.Add(jsonutils.NewString(policy), "policy")
  54. } else {
  55. ret.Add(blobJson, "policy")
  56. }
  57. }
  58. return ret, nil
  59. }
  60. func policyWriteFilter(session *mcclient.ClientSession, s jsonutils.JSONObject, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  61. ret := s.(*jsonutils.JSONDict).CopyExcludes("policy")
  62. if s.Contains("policy") {
  63. blobJson, err := s.Get("policy")
  64. if err != nil {
  65. return nil, err
  66. }
  67. switch blob := blobJson.(type) {
  68. case *jsonutils.JSONString:
  69. blobStr, _ := blob.GetString()
  70. blobJson, err = jsonutils.ParseYAML(blobStr)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. // ret.Add(jsonutils.NewString(blobJson.String()), "blob")
  76. ret.Add(blobJson, "blob")
  77. }
  78. return ret, nil
  79. }
  80. func init() {
  81. Policies = SPolicyManager{modules.NewIdentityV3Manager(
  82. "policy",
  83. "policies",
  84. []string{"id", "name", "scope", "enabled",
  85. "domain_id", "domain", "project_domain", "public_scope",
  86. "is_public", "description", "is_system",
  87. },
  88. []string{})}
  89. Policies.SetReadFilter(policyReadFilter).SetWriteFilter(policyWriteFilter) // .SetNameField("type")
  90. modules.Register(&Policies)
  91. }