roles.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 hcso
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. )
  21. type SRole struct {
  22. DomainId string
  23. Flag string
  24. DescriptionCn string
  25. Catelog string
  26. Description string
  27. Id string
  28. DisplayName string
  29. Type string
  30. UpdatedTime string
  31. CreatedTime string
  32. Links SLink
  33. Policy jsonutils.JSONDict
  34. }
  35. func (role *SRole) GetName() string {
  36. return role.DisplayName
  37. }
  38. func (role *SRole) GetDescription() string {
  39. return role.DescriptionCn
  40. }
  41. func (role *SRole) GetPolicyType() api.TPolicyType {
  42. return api.PolicyTypeSystem
  43. }
  44. func (role *SRole) GetGlobalId() string {
  45. return role.DisplayName
  46. }
  47. func (role *SRole) UpdateDocument(document *jsonutils.JSONDict) error {
  48. return cloudprovider.ErrNotImplemented
  49. }
  50. func (role *SRole) GetDocument() (*jsonutils.JSONDict, error) {
  51. return &role.Policy, nil
  52. }
  53. func (role *SRole) Delete() error {
  54. return cloudprovider.ErrNotImplemented
  55. }
  56. func (self *SHuaweiClient) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  57. roles, err := self.GetRoles("", "")
  58. if err != nil {
  59. return nil, errors.Wrap(err, "GetRoles")
  60. }
  61. ret := []cloudprovider.ICloudpolicy{}
  62. for i := range roles {
  63. ret = append(ret, &roles[i])
  64. }
  65. return ret, nil
  66. }
  67. func (self *SHuaweiClient) GetCustomRoles() ([]SRole, error) {
  68. params := map[string]string{}
  69. client, err := self.newGeneralAPIClient()
  70. if err != nil {
  71. return nil, errors.Wrap(err, "newGeneralAPIClient")
  72. }
  73. client.Roles.SetVersion("v3.0/OS-ROLE")
  74. defer client.Roles.SetVersion("v3.0")
  75. roles := []SRole{}
  76. err = doListAllWithNextLink(client.Roles.List, params, &roles)
  77. if err != nil {
  78. return nil, errors.Wrap(err, "doListAllWithOffset")
  79. }
  80. return roles, nil
  81. }
  82. func (self *SHuaweiClient) CreateICloudpolicy(opts *cloudprovider.SCloudpolicyCreateOptions) (cloudprovider.ICloudpolicy, error) {
  83. client, err := self.newGeneralAPIClient()
  84. if err != nil {
  85. return nil, errors.Wrap(err, "newGeneralAPIClient")
  86. }
  87. client.Roles.SetVersion("v3.0/OS-ROLE")
  88. defer client.Roles.SetVersion("v3.0")
  89. params := map[string]interface{}{
  90. "role": map[string]interface{}{
  91. "display_name": opts.Name,
  92. "type": "XA",
  93. "description": opts.Desc,
  94. "policy": opts.Document,
  95. },
  96. }
  97. resp, err := client.Roles.Create(jsonutils.Marshal(params))
  98. if err != nil {
  99. return nil, err
  100. }
  101. role := &SRole{}
  102. err = resp.Unmarshal(role)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return role, nil
  107. }
  108. func (self *SHuaweiClient) GetRoles(domainId, name string) ([]SRole, error) {
  109. params := map[string]string{}
  110. if len(domainId) > 0 {
  111. params["domain_id"] = self.ownerId
  112. }
  113. if len(name) > 0 {
  114. params["display_name"] = name
  115. }
  116. client, err := self.newGeneralAPIClient()
  117. if err != nil {
  118. return nil, errors.Wrap(err, "newGeneralAPIClient")
  119. }
  120. roles := []SRole{}
  121. err = doListAllWithNextLink(client.Roles.List, params, &roles)
  122. if err != nil {
  123. return nil, errors.Wrap(err, "doListAllWithOffset")
  124. }
  125. return roles, nil
  126. }