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
118 lines (101 loc) · 3.67 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
package alicloud
import (
"strings"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"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": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validatePolicyType,
},
},
}
}
func resourceAlicloudRamUserPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := ram.CreateAttachPolicyToUserRequest()
request.UserName = d.Get("user_name").(string)
request.PolicyName = d.Get("policy_name").(string)
request.PolicyType = d.Get("policy_type").(string)
raw, err := client.WithRamClient(func(ramClient *ram.Client) (interface{}, error) {
return ramClient.AttachPolicyToUser(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ram_user_policy_attachment", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
d.SetId(strings.Join([]string{"user", request.PolicyName, request.PolicyType, request.UserName}, COLON_SEPARATED))
return resourceAlicloudRamUserPolicyAttachmentRead(d, meta)
}
func resourceAlicloudRamUserPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
ramService := RamService{client}
// In order to be compatible with previous Id (before 1.9.6) which format to user:<policy_name>:<policy_type>:<user_name>
id := strings.Join([]string{"user", d.Get("policy_name").(string), d.Get("policy_type").(string), d.Get("user_name").(string)}, COLON_SEPARATED)
if d.Id() != id {
d.SetId(id)
}
object, err := ramService.DescribeRamUserPolicyAttachment(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
parts, err := ParseResourceId(id, 4)
if err != nil {
return WrapError(err)
}
d.Set("user_name", parts[3])
d.Set("policy_name", object.PolicyName)
d.Set("policy_type", object.PolicyType)
return nil
}
func resourceAlicloudRamUserPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
ramService := RamService{client}
// In order to be compatible with previous Id (before 1.9.6) which format to user:<policy_name>:<policy_type>:<user_name>
id := strings.Join([]string{"user", d.Get("policy_name").(string), d.Get("policy_type").(string), d.Get("user_name").(string)}, COLON_SEPARATED)
if d.Id() != id {
d.SetId(id)
}
parts, err := ParseResourceId(id, 4)
if err != nil {
return WrapError(err)
}
request := ram.CreateDetachPolicyFromUserRequest()
request.PolicyName = parts[1]
request.PolicyType = parts[2]
request.UserName = parts[3]
raw, err := client.WithRamClient(func(ramClient *ram.Client) (interface{}, error) {
return ramClient.DetachPolicyFromUser(request)
})
if err != nil {
if RamEntityNotExist(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
return WrapError(ramService.WaitForRamUserPolicyAttachment(d.Id(), Deleted, DefaultTimeout))
}