forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_ram_user_policy_attachment.go
150 lines (127 loc) · 4.56 KB
/
resource_alicloud_ram_user_policy_attachment.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
package alicloud
import (
"fmt"
"time"
"strings"
"github.com/denverdino/aliyungo/ram"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudRamUserPolicyAtatchment() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudRamUserPolicyAttachmentCreate,
Read: resourceAlicloudRamUserPolicyAttachmentRead,
Delete: resourceAlicloudRamUserPolicyAttachmentDelete,
Schema: map[string]*schema.Schema{
"user_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRamName,
},
"policy_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRamPolicyName,
},
"policy_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validatePolicyType,
},
},
}
}
func resourceAlicloudRamUserPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
args := ram.AttachPolicyRequest{
PolicyRequest: ram.PolicyRequest{
PolicyName: d.Get("policy_name").(string),
PolicyType: ram.Type(d.Get("policy_type").(string)),
},
UserName: d.Get("user_name").(string),
}
_, err := client.WithRamClient(func(ramClient ram.RamClientInterface) (interface{}, error) {
return ramClient.AttachPolicyToUser(args)
})
if err != nil {
return fmt.Errorf("AttachPolicyToUser got an error: %#v", err)
}
d.SetId(fmt.Sprintf("%s%s%s%s%s", args.UserName, COLON_SEPARATED, args.PolicyName, COLON_SEPARATED, args.PolicyType))
return resourceAlicloudRamUserPolicyAttachmentRead(d, meta)
}
func resourceAlicloudRamUserPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
// In order to be compatible with previous Id (before 1.9.6) which format to user<policuy_name><policy_type><user_name>
id := fmt.Sprintf("%s%s%s%s%s", d.Get("user_name").(string), COLON_SEPARATED, d.Get("policy_name").(string), COLON_SEPARATED, d.Get("policy_type").(string))
if d.Id() != id {
d.SetId(id)
}
split := strings.Split(d.Id(), COLON_SEPARATED)
args := ram.UserQueryRequest{
UserName: split[0],
}
raw, err := client.WithRamClient(func(ramClient ram.RamClientInterface) (interface{}, error) {
return ramClient.ListPoliciesForUser(args)
})
if err != nil {
return fmt.Errorf("Get list policies for user got an error: %#v", err)
}
response, _ := raw.(ram.PolicyListResponse)
if len(response.Policies.Policy) > 0 {
for _, v := range response.Policies.Policy {
if v.PolicyName == d.Get("policy_name").(string) && v.PolicyType == d.Get("policy_type").(string) {
d.Set("user_name", args.UserName)
d.Set("policy_name", v.PolicyName)
d.Set("policy_type", v.PolicyType)
return nil
}
}
}
d.SetId("")
return nil
}
func resourceAlicloudRamUserPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
// In order to be compatible with previous Id (before 1.9.6) which format to user<policuy_name><policy_type><user_name>
id := fmt.Sprintf("%s%s%s%s%s", d.Get("user_name").(string), COLON_SEPARATED, d.Get("policy_name").(string), COLON_SEPARATED, d.Get("policy_type").(string))
if d.Id() != id {
d.SetId(id)
}
split := strings.Split(d.Id(), COLON_SEPARATED)
args := ram.AttachPolicyRequest{
PolicyRequest: ram.PolicyRequest{
PolicyName: split[1],
PolicyType: ram.Type(split[2]),
},
UserName: split[0],
}
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.WithRamClient(func(ramClient ram.RamClientInterface) (interface{}, error) {
return ramClient.DetachPolicyFromUser(args)
})
if err != nil {
if RamEntityNotExist(err) {
return nil
}
return resource.NonRetryableError(fmt.Errorf("Error deleting user policy attachment: %#v", err))
}
raw, err := client.WithRamClient(func(ramClient ram.RamClientInterface) (interface{}, error) {
return ramClient.ListPoliciesForUser(ram.UserQueryRequest{UserName: args.UserName})
})
if err != nil {
if RamEntityNotExist(err) {
return nil
}
return resource.NonRetryableError(err)
}
response, _ := raw.(ram.PolicyListResponse)
if len(response.Policies.Policy) < 1 {
return nil
}
return resource.RetryableError(fmt.Errorf("Error deleting user policy attachment - trying again while it is deleted."))
})
}