-
Notifications
You must be signed in to change notification settings - Fork 533
/
resource_alicloud_resource_manager_shared_target.go
143 lines (136 loc) · 4.99 KB
/
resource_alicloud_resource_manager_shared_target.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
package alicloud
import (
"fmt"
"log"
"time"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceAlicloudResourceManagerSharedTarget() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudResourceManagerSharedTargetCreate,
Read: resourceAlicloudResourceManagerSharedTargetRead,
Delete: resourceAlicloudResourceManagerSharedTargetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(11 * time.Minute),
Delete: schema.DefaultTimeout(11 * time.Minute),
},
Schema: map[string]*schema.Schema{
"resource_share_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"target_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAlicloudResourceManagerSharedTargetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
resourcesharingService := ResourcesharingService{client}
var response map[string]interface{}
action := "AssociateResourceShare"
request := make(map[string]interface{})
conn, err := client.NewRessharingClient()
if err != nil {
return WrapError(err)
}
request["ResourceShareId"] = d.Get("resource_share_id")
request["Targets"] = []string{d.Get("target_id").(string)}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-01-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_resource_manager_shared_target", action, AlibabaCloudSdkGoERROR)
}
response = response["ResourceShareAssociations"].([]interface{})[0].(map[string]interface{})
d.SetId(fmt.Sprint(response["ResourceShareId"], ":", response["EntityId"]))
stateConf := BuildStateConf([]string{}, []string{"Associated"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, resourcesharingService.ResourceManagerSharedTargetStateRefreshFunc(d.Id(), []string{"Failed"}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
return resourceAlicloudResourceManagerSharedTargetRead(d, meta)
}
func resourceAlicloudResourceManagerSharedTargetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
resourcesharingService := ResourcesharingService{client}
object, err := resourcesharingService.DescribeResourceManagerSharedTarget(d.Id())
if err != nil {
if NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_resource_manager_shared_target resourcesharingService.DescribeResourceManagerSharedTarget Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
d.Set("resource_share_id", parts[0])
d.Set("target_id", parts[1])
d.Set("status", object["AssociationStatus"])
return nil
}
func resourceAlicloudResourceManagerSharedTargetDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
resourcesharingService := ResourcesharingService{client}
action := "DisassociateResourceShare"
var response map[string]interface{}
conn, err := client.NewRessharingClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"ResourceShareId": parts[0],
"Targets": []string{parts[1]},
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-01-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
stateConf := BuildStateConf([]string{}, []string{"Disassociated"}, d.Timeout(schema.TimeoutDelete), 5*time.Second, resourcesharingService.ResourceManagerSharedTargetStateRefreshFunc(d.Id(), []string{"Failed"}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
return nil
}