forked from yunionio/cloudmux
/
ram_role.go
137 lines (114 loc) · 3.11 KB
/
ram_role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apsara
import (
"fmt"
"time"
"github.com/pkg/errors"
)
type sRoles struct {
Role []SRole
}
type SRoles struct {
Roles sRoles
Marker string
IsTruncated bool
}
type SRole struct {
client *SApsaraClient
Arn string
CreateDate time.Time
Description string
RoleId string
RoleName string
AssumeRolePolicyDocument string
}
func (self *SApsaraClient) ListRoles(offset string, limit int) (*SRoles, error) {
if limit < 0 || limit > 1000 {
limit = 1000
}
params := map[string]string{}
if len(offset) > 0 {
params["Marker"] = offset
}
if limit > 0 {
params["MaxItems"] = fmt.Sprintf("%d", limit)
}
body, err := self.ramRequest("ListRoles", params)
if err != nil {
return nil, errors.Wrapf(err, "ListRoles")
}
roles := &SRoles{}
err = body.Unmarshal(roles)
if err != nil {
return nil, errors.Wrap(err, "body.Unmarshal(&")
}
return roles, nil
}
func (self *SApsaraClient) CreateRole(roleName string, document string, desc string) (*SRole, error) {
params := make(map[string]string)
params["RoleName"] = roleName
params["AssumeRolePolicyDocument"] = document
if len(desc) > 0 {
params["Description"] = desc
}
body, err := self.ramRequest("CreateRole", params)
if err != nil {
return nil, errors.Wrap(err, "CreateRole")
}
role := SRole{client: self}
err = body.Unmarshal(&role, "Role")
if err != nil {
return nil, errors.Wrap(err, "body.Unmarshal")
}
return &role, nil
}
func (self *SApsaraClient) GetRole(roleName string) (*SRole, error) {
params := make(map[string]string)
params["RoleName"] = roleName
body, err := self.ramRequest("GetRole", params)
if err != nil {
return nil, errors.Wrap(err, "GetRole")
}
role := SRole{client: self}
err = body.Unmarshal(&role, "Role")
if err != nil {
return nil, errors.Wrap(err, "body.Unmarshal")
}
return &role, nil
}
func (self *SApsaraClient) ListPoliciesForRole(name string) ([]SPolicy, error) {
params := map[string]string{
"RoleName": name,
}
resp, err := self.ramRequest("ListPoliciesForRole", params)
if err != nil {
return nil, errors.Wrapf(err, "ListPoliciesForRole")
}
policies := []SPolicy{}
err = resp.Unmarshal(&policies, "Policies", "Policy")
if err != nil {
return nil, errors.Wrapf(err, "resp.Unmarshal")
}
return policies, nil
}
func (self *SApsaraClient) DetachPolicyFromRole(policyType, policyName, roleName string) error {
params := map[string]string{
"PolicyName": policyName,
"PolicyType": policyType,
"RoleName": roleName,
}
_, err := self.ramRequest("DetachPolicyFromRole", params)
return err
}