policy.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ksyun
  15. import (
  16. "time"
  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 *SKsyunClient
  28. CreateDate time.Time
  29. DefaultVersionId string
  30. Description string
  31. Krn string
  32. PolicyKrn string
  33. Path string
  34. PolicyId string
  35. PolicyName string
  36. ServiceId string
  37. ServiceName string
  38. ServiceViewName string
  39. PolicyType int
  40. CreateMode int
  41. UpdateDate time.Time
  42. AttachmentCount int
  43. }
  44. func (policy *SPolicy) GetName() string {
  45. return policy.PolicyName
  46. }
  47. func (policy *SPolicy) GetDescription() string {
  48. return policy.Description
  49. }
  50. func (policy *SPolicy) GetGlobalId() string {
  51. return policy.Krn + policy.PolicyKrn
  52. }
  53. func (policy *SPolicy) GetPolicyType() api.TPolicyType {
  54. if policy.PolicyType == 1 {
  55. return api.PolicyTypeSystem
  56. }
  57. return api.PolicyTypeCustom
  58. }
  59. func (policy *SPolicy) UpdateDocument(document *jsonutils.JSONDict) error {
  60. return cloudprovider.ErrNotImplemented
  61. }
  62. func (policy *SPolicy) Delete() error {
  63. return policy.client.DeletePolicy(policy.Krn)
  64. }
  65. func (policy *SPolicy) GetDocument() (*jsonutils.JSONDict, error) {
  66. doc, err := policy.client.GetPolicyVersion(policy.Krn, policy.DefaultVersionId)
  67. if err != nil {
  68. return nil, err
  69. }
  70. obj, err := jsonutils.ParseString(doc.Document)
  71. if err != nil {
  72. return nil, errors.Wrapf(err, "ParseString %s", doc.Document)
  73. }
  74. return obj.(*jsonutils.JSONDict), nil
  75. }
  76. func (client *SKsyunClient) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  77. policies, err := client.ListPolicies("")
  78. if err != nil {
  79. return nil, err
  80. }
  81. ret := []cloudprovider.ICloudpolicy{}
  82. for i := range policies {
  83. policies[i].client = client
  84. ret = append(ret, &policies[i])
  85. }
  86. return ret, nil
  87. }
  88. func (client *SKsyunClient) ListPolicies(scope string) ([]SPolicy, error) {
  89. params := map[string]interface{}{
  90. "MaxItems": "100",
  91. }
  92. if len(scope) > 0 {
  93. params["Scope"] = scope
  94. }
  95. ret := []SPolicy{}
  96. for {
  97. resp, err := client.iamRequest("", "ListPolicies", params)
  98. if err != nil {
  99. return nil, err
  100. }
  101. part := struct {
  102. Policies struct {
  103. Member []SPolicy
  104. }
  105. Marker string
  106. }{}
  107. err = resp.Unmarshal(&part)
  108. if err != nil {
  109. return nil, err
  110. }
  111. ret = append(ret, part.Policies.Member...)
  112. if len(part.Policies.Member) == 0 || len(part.Marker) == 0 {
  113. break
  114. }
  115. params["Marker"] = part.Marker
  116. }
  117. return ret, nil
  118. }
  119. func (client *SKsyunClient) DeletePolicy(krn string) error {
  120. params := map[string]interface{}{
  121. "PolicyKrn": krn,
  122. }
  123. _, err := client.iamRequest("", "DeletePolicy", params)
  124. return err
  125. }
  126. type SPolicyVersion struct {
  127. Document string
  128. }
  129. func (client *SKsyunClient) GetPolicyVersion(krn, version string) (*SPolicyVersion, error) {
  130. params := map[string]interface{}{
  131. "PolicyKrn": krn,
  132. "VersionId": version,
  133. }
  134. resp, err := client.iamRequest("", "GetPolicyVersion", params)
  135. if err != nil {
  136. return nil, err
  137. }
  138. ret := &SPolicyVersion{}
  139. err = resp.Unmarshal(ret, "PolicyVersion")
  140. if err != nil {
  141. return nil, err
  142. }
  143. return ret, nil
  144. }