forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_common_bandwidth_package_attachment.go
111 lines (100 loc) · 3.68 KB
/
resource_alicloud_common_bandwidth_package_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
package alicloud
import (
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAliyunCommonBandwidthPackageAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunCommonBandwidthPackageAttachmentCreate,
Read: resourceAliyunCommonBandwidthPackageAttachmentRead,
Delete: resourceAliyunCommonBandwidthPackageAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bandwidth_package_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAliyunCommonBandwidthPackageAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
request := vpc.CreateAddCommonBandwidthPackageIpRequest()
request.BandwidthPackageId = Trim(d.Get("bandwidth_package_id").(string))
request.IpInstanceId = Trim(d.Get("instance_id").(string))
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.AddCommonBandwidthPackageIp(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_common_bandwidth_package_attachment", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
//check the common bandwidth package attachment
d.SetId(request.BandwidthPackageId + COLON_SEPARATED + request.IpInstanceId)
if err := vpcService.WaitForCommonBandwidthPackageAttachment(d.Id(), Available, 5*DefaultTimeout); err != nil {
return WrapError(err)
}
return resourceAliyunCommonBandwidthPackageAttachmentRead(d, meta)
}
func resourceAliyunCommonBandwidthPackageAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
bandwidthPackageId, ipInstanceId := parts[0], parts[1]
_, err = vpcService.DescribeCommonBandwidthPackageAttachment(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("bandwidth_package_id", bandwidthPackageId)
d.Set("instance_id", ipInstanceId)
return nil
}
func resourceAliyunCommonBandwidthPackageAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
bandwidthPackageId, ipInstanceId := parts[0], parts[1]
request := vpc.CreateRemoveCommonBandwidthPackageIpRequest()
request.BandwidthPackageId = bandwidthPackageId
request.IpInstanceId = ipInstanceId
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.RemoveCommonBandwidthPackageIp(request)
})
//Waiting for unassociate the common bandwidth package
if err != nil {
if IsExceptedError(err, TaskConflict) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
return WrapError(vpcService.WaitForCommonBandwidthPackageAttachment(d.Id(), Deleted, DefaultTimeoutMedium))
}