roles.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 huawei
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. )
  23. type SRole struct {
  24. DomainId string
  25. Flag string
  26. DescriptionCn string
  27. Catelog string
  28. Description string
  29. Id string
  30. DisplayName string
  31. Type string
  32. UpdatedTime string
  33. CreatedTime string
  34. Links SLink
  35. Policy jsonutils.JSONDict
  36. }
  37. func (role *SRole) GetName() string {
  38. return role.DisplayName
  39. }
  40. func (role *SRole) GetDescription() string {
  41. return role.DescriptionCn
  42. }
  43. func (role *SRole) GetPolicyType() api.TPolicyType {
  44. return api.PolicyTypeSystem
  45. }
  46. func (role *SRole) GetGlobalId() string {
  47. return role.Id
  48. }
  49. func (role *SRole) UpdateDocument(document *jsonutils.JSONDict) error {
  50. return cloudprovider.ErrNotImplemented
  51. }
  52. func (role *SRole) GetDocument() (*jsonutils.JSONDict, error) {
  53. return &role.Policy, nil
  54. }
  55. func (role *SRole) Delete() error {
  56. return cloudprovider.ErrNotImplemented
  57. }
  58. func (self *SHuaweiClient) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  59. roles, err := self.GetRoles("", "")
  60. if err != nil {
  61. return nil, errors.Wrap(err, "GetRoles")
  62. }
  63. ret := []cloudprovider.ICloudpolicy{}
  64. for i := range roles {
  65. ret = append(ret, &roles[i])
  66. }
  67. return ret, nil
  68. }
  69. // https://console.huaweicloud.com/apiexplorer/#/openapi/IAM/doc?api=KeystoneListPermissions
  70. func (self *SHuaweiClient) GetRoles(domainId, name string) ([]SRole, error) {
  71. query := url.Values{}
  72. if len(domainId) > 0 {
  73. query.Set("domain_id", self.ownerId)
  74. }
  75. if len(name) > 0 {
  76. query.Set("name", name)
  77. }
  78. query.Set("type", "domain")
  79. query.Set("per_page", "300")
  80. page := 1
  81. query.Set("page", fmt.Sprintf("%d", page))
  82. ret := []SRole{}
  83. for {
  84. resp, err := self.list(SERVICE_IAM_V3, "", "roles", query)
  85. if err != nil {
  86. return nil, err
  87. }
  88. part := struct {
  89. Roles []SRole
  90. TotalNumber int
  91. }{}
  92. err = resp.Unmarshal(&part)
  93. if err != nil {
  94. return nil, err
  95. }
  96. ret = append(ret, part.Roles...)
  97. if len(ret) >= part.TotalNumber || len(part.Roles) == 0 {
  98. break
  99. }
  100. page++
  101. query.Set("page", fmt.Sprintf("%d", page))
  102. }
  103. return ret, nil
  104. }