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