policy.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 volcengine
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. )
  22. const (
  23. POLICY_TYPE_SYSTEM = "System"
  24. POLICY_TYPE_CUSTOM = "Custom"
  25. )
  26. type SPolicy struct {
  27. client *SVolcEngineClient
  28. CreateDate string
  29. UpdateDate string
  30. PolicyDocument string
  31. Status string
  32. PolicyName string
  33. PolicyType string
  34. Description string
  35. Category string
  36. IsServiceRolePolicy int
  37. AttachmentCount int
  38. }
  39. func (policy *SPolicy) GetName() string {
  40. return policy.PolicyName
  41. }
  42. func (policy *SPolicy) GetDescription() string {
  43. return policy.Description
  44. }
  45. func (policy *SPolicy) GetGlobalId() string {
  46. return policy.PolicyName
  47. }
  48. func (policy *SPolicy) GetPolicyType() api.TPolicyType {
  49. if policy.PolicyType == "System" {
  50. return api.PolicyTypeSystem
  51. }
  52. return api.PolicyTypeCustom
  53. }
  54. func (policy *SPolicy) UpdateDocument(document *jsonutils.JSONDict) error {
  55. return cloudprovider.ErrNotImplemented
  56. }
  57. func (policy *SPolicy) Delete() error {
  58. return policy.client.DeletePolicy(policy.PolicyName)
  59. }
  60. func (policy *SPolicy) GetDocument() (*jsonutils.JSONDict, error) {
  61. doc, err := jsonutils.Parse([]byte(policy.PolicyDocument))
  62. if err != nil {
  63. return nil, err
  64. }
  65. ret, ok := doc.(*jsonutils.JSONDict)
  66. if !ok {
  67. return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", policy.PolicyDocument)
  68. }
  69. return ret, nil
  70. }
  71. func (self *SVolcEngineClient) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  72. policies, err := self.ListPolicies("")
  73. if err != nil {
  74. return nil, err
  75. }
  76. ret := []cloudprovider.ICloudpolicy{}
  77. for i := range policies {
  78. policies[i].client = self
  79. ret = append(ret, &policies[i])
  80. }
  81. return ret, nil
  82. }
  83. func (client *SVolcEngineClient) ListPolicies(scope string) ([]SPolicy, error) {
  84. params := map[string]string{
  85. "Limit": "50",
  86. }
  87. if len(scope) > 0 {
  88. params["Scope"] = scope
  89. }
  90. offset := 0
  91. ret := []SPolicy{}
  92. for {
  93. params["Offset"] = fmt.Sprintf("%d", offset)
  94. resp, err := client.iamRequest("", "ListPolicies", params)
  95. if err != nil {
  96. return nil, err
  97. }
  98. part := struct {
  99. PolicyMetadata []SPolicy
  100. Total int
  101. }{}
  102. err = resp.Unmarshal(&part)
  103. if err != nil {
  104. return nil, err
  105. }
  106. ret = append(ret, part.PolicyMetadata...)
  107. if len(part.PolicyMetadata) == 0 || len(ret) >= part.Total {
  108. break
  109. }
  110. offset = len(ret)
  111. }
  112. return ret, nil
  113. }
  114. func (client *SVolcEngineClient) DeletePolicy(name string) error {
  115. params := map[string]string{
  116. "PolicyName": name,
  117. }
  118. _, err := client.iamRequest("", "DeletePolicy", params)
  119. return err
  120. }