/
client.go
215 lines (167 loc) · 4.56 KB
/
client.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package iam
import (
"errors"
"net/http"
_aws "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
_iam "github.com/aws/aws-sdk-go/service/iam"
"github.com/coldbrewcloud/coldbrew-cli/utils/conv"
)
type Client struct {
svc *_iam.IAM
}
func New(session *session.Session, config *_aws.Config) *Client {
return &Client{
svc: _iam.New(session, config),
}
}
func (c *Client) RetrieveRole(roleName string) (*_iam.Role, error) {
if roleName == "" {
return nil, errors.New("roleName is empty")
}
params := &_iam.GetRoleInput{
RoleName: _aws.String(roleName),
}
res, err := c.svc.GetRole(params)
if err != nil {
if reqFail, ok := err.(awserr.RequestFailure); ok {
if reqFail.StatusCode() == http.StatusNotFound {
return nil, nil
}
}
return nil, err
}
return res.Role, nil
}
func (c *Client) CreateRole(assumeRolePolicyDocument, roleName string) (*_iam.Role, error) {
if assumeRolePolicyDocument == "" {
return nil, errors.New("assumeRolePolicyDocument is empty")
}
if roleName == "" {
return nil, errors.New("roleName is empty")
}
params := &_iam.CreateRoleInput{
AssumeRolePolicyDocument: _aws.String(assumeRolePolicyDocument),
RoleName: _aws.String(roleName),
Path: _aws.String("/"),
}
res, err := c.svc.CreateRole(params)
if err != nil {
return nil, err
}
return res.Role, nil
}
func (c *Client) AttachRolePolicy(policyARN, roleName string) error {
if policyARN == "" {
return errors.New("policyARN is empty")
}
if roleName == "" {
return errors.New("roleName is empty")
}
params := &_iam.AttachRolePolicyInput{
PolicyArn: _aws.String(policyARN),
RoleName: _aws.String(roleName),
}
_, err := c.svc.AttachRolePolicy(params)
if err != nil {
return err
}
return nil
}
func (c *Client) ListRolePolicyNames(roleName string) ([]string, error) {
policyNames := []string{}
var marker *string
for {
params := &_iam.ListRolePoliciesInput{
Marker: marker,
RoleName: _aws.String(roleName),
}
res, err := c.svc.ListRolePolicies(params)
if err != nil {
return nil, err
}
for _, p := range res.PolicyNames {
policyNames = append(policyNames, conv.S(p))
}
if !conv.B(res.IsTruncated) {
break
}
marker = res.Marker
}
return policyNames, nil
}
func (c *Client) DetachRolePolicy(policyARN, roleName string) error {
params := &_iam.DetachRolePolicyInput{
PolicyArn: _aws.String(policyARN),
RoleName: _aws.String(roleName),
}
_, err := c.svc.DetachRolePolicy(params)
return err
}
func (c *Client) DeleteRolePolicy(policyName, roleName string) error {
params := &_iam.DeleteRolePolicyInput{
PolicyName: _aws.String(policyName),
RoleName: _aws.String(roleName),
}
_, err := c.svc.DeleteRolePolicy(params)
return err
}
func (c *Client) DeleteRole(roleName string) error {
params := &_iam.DeleteRoleInput{
RoleName: _aws.String(roleName),
}
_, err := c.svc.DeleteRole(params)
return err
}
func (c *Client) CreateInstanceProfile(profileName string) (*_iam.InstanceProfile, error) {
params := &_iam.CreateInstanceProfileInput{
InstanceProfileName: _aws.String(profileName),
}
res, err := c.svc.CreateInstanceProfile(params)
if err != nil {
return nil, err
}
if res != nil {
return res.InstanceProfile, nil
}
return nil, nil
}
func (c *Client) AddRoleToInstanceProfile(profileName, roleName string) error {
params := &_iam.AddRoleToInstanceProfileInput{
InstanceProfileName: _aws.String(profileName),
RoleName: _aws.String(roleName),
}
_, err := c.svc.AddRoleToInstanceProfile(params)
return err
}
func (c *Client) RemoveRoleFromInstanceProfile(profileName, roleName string) error {
params := &_iam.RemoveRoleFromInstanceProfileInput{
InstanceProfileName: _aws.String(profileName),
RoleName: _aws.String(roleName),
}
_, err := c.svc.RemoveRoleFromInstanceProfile(params)
return err
}
func (c *Client) RetrieveInstanceProfile(profileName string) (*_iam.InstanceProfile, error) {
params := &_iam.GetInstanceProfileInput{
InstanceProfileName: _aws.String(profileName),
}
res, err := c.svc.GetInstanceProfile(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NoSuchEntity" {
return nil, nil // instance profile not found
}
}
return nil, err
}
return res.InstanceProfile, nil
}
func (c *Client) DeleteInstanceProfile(profileName string) error {
params := &_iam.DeleteInstanceProfileInput{
InstanceProfileName: _aws.String(profileName),
}
_, err := c.svc.DeleteInstanceProfile(params)
return err
}