forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_forward.go
189 lines (159 loc) · 5.24 KB
/
resource_alicloud_forward.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
package alicloud
import (
"fmt"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAliyunForwardEntry() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunForwardEntryCreate,
Read: resourceAliyunForwardEntryRead,
Update: resourceAliyunForwardEntryUpdate,
Delete: resourceAliyunForwardEntryDelete,
Schema: map[string]*schema.Schema{
"forward_table_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"external_ip": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"external_port": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateForwardPort,
},
"ip_protocol": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue([]string{"tcp", "udp", "any"}),
},
"internal_ip": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"internal_port": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateForwardPort,
},
},
}
}
func resourceAliyunForwardEntryCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).vpcconn
args := vpc.CreateCreateForwardEntryRequest()
args.RegionId = string(getRegion(d, meta))
args.ForwardTableId = d.Get("forward_table_id").(string)
args.ExternalIp = d.Get("external_ip").(string)
args.ExternalPort = d.Get("external_port").(string)
args.IpProtocol = d.Get("ip_protocol").(string)
args.InternalIp = d.Get("internal_ip").(string)
args.InternalPort = d.Get("internal_port").(string)
if err := resource.Retry(2*time.Minute, func() *resource.RetryError {
ar := args
resp, err := conn.CreateForwardEntry(ar)
if err != nil {
if IsExceptedError(err, InvalidIpNotInNatgw) {
return resource.RetryableError(fmt.Errorf("CreateForwardEntry timeout and got error: %#v", err))
}
return resource.NonRetryableError(fmt.Errorf("CreateNatGateway got error: %#v", err))
}
d.SetId(resp.ForwardEntryId)
d.Set("forward_table_id", d.Get("forward_table_id").(string))
return nil
}); err != nil {
return err
}
return resourceAliyunForwardEntryRead(d, meta)
}
func resourceAliyunForwardEntryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
forwardEntry, err := client.DescribeForwardEntry(d.Get("forward_table_id").(string), d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return err
}
d.Set("forward_table_id", forwardEntry.ForwardTableId)
d.Set("external_ip", forwardEntry.ExternalIp)
d.Set("external_port", forwardEntry.ExternalPort)
d.Set("ip_protocol", forwardEntry.IpProtocol)
d.Set("internal_ip", forwardEntry.InternalIp)
d.Set("internal_port", forwardEntry.InternalPort)
return nil
}
func resourceAliyunForwardEntryUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
forwardEntry, err := client.DescribeForwardEntry(d.Get("forward_table_id").(string), d.Id())
if err != nil {
return err
}
d.Partial(true)
attributeUpdate := false
args := vpc.CreateModifyForwardEntryRequest()
args.RegionId = string(getRegion(d, meta))
args.ForwardTableId = forwardEntry.ForwardTableId
args.ForwardEntryId = forwardEntry.ForwardEntryId
args.ExternalIp = forwardEntry.ExternalIp
args.IpProtocol = forwardEntry.IpProtocol
args.ExternalPort = forwardEntry.ExternalPort
args.InternalIp = forwardEntry.InternalIp
args.InternalPort = forwardEntry.InternalPort
if d.HasChange("external_port") {
d.SetPartial("external_port")
args.ExternalPort = d.Get("external_port").(string)
attributeUpdate = true
}
if d.HasChange("ip_protocol") {
d.SetPartial("ip_protocol")
args.IpProtocol = d.Get("ip_protocol").(string)
attributeUpdate = true
}
if d.HasChange("internal_port") {
d.SetPartial("internal_port")
args.InternalPort = d.Get("internal_port").(string)
attributeUpdate = true
}
if attributeUpdate {
if _, err := client.vpcconn.ModifyForwardEntry(args); err != nil {
return err
}
}
d.Partial(false)
return resourceAliyunForwardEntryRead(d, meta)
}
func resourceAliyunForwardEntryDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
args := vpc.CreateDeleteForwardEntryRequest()
args.RegionId = string(getRegion(d, meta))
args.ForwardTableId = d.Get("forward_table_id").(string)
args.ForwardEntryId = d.Id()
return resource.Retry(3*time.Minute, func() *resource.RetryError {
if _, err := client.vpcconn.DeleteForwardEntry(args); err != nil {
if IsExceptedError(err, InvalidForwardEntryIdNotFound) ||
IsExceptedError(err, InvalidForwardTableIdNotFound) {
return nil
}
return resource.NonRetryableError(err)
}
forwardEntry, err := client.DescribeForwardEntry(d.Get("forward_table_id").(string), d.Id())
if err != nil {
if NotFoundError(err) {
return nil
}
return resource.NonRetryableError(err)
}
if forwardEntry.ForwardEntryId == d.Id() {
return resource.RetryableError(fmt.Errorf("Delete Forward Entry timeout and got an error:%#v.", err))
}
return nil
})
}