forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_snat.go
151 lines (126 loc) · 3.93 KB
/
resource_alicloud_snat.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
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"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAliyunSnatEntry() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunSnatEntryCreate,
Read: resourceAliyunSnatEntryRead,
Update: resourceAliyunSnatEntryUpdate,
Delete: resourceAliyunSnatEntryDelete,
Schema: map[string]*schema.Schema{
"snat_table_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"source_vswitch_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"snat_ip": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceAliyunSnatEntryCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := vpc.CreateCreateSnatEntryRequest()
request.RegionId = string(client.Region)
request.SnatTableId = d.Get("snat_table_id").(string)
request.SourceVSwitchId = d.Get("source_vswitch_id").(string)
request.SnatIp = d.Get("snat_ip").(string)
if err := resource.Retry(3*time.Minute, func() *resource.RetryError {
ar := request
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.CreateSnatEntry(ar)
})
if err != nil {
if IsExceptedError(err, EIP_NOT_IN_GATEWAY) {
return resource.RetryableError(fmt.Errorf("CreateSnatEntry timeout and got an error: %#v.", err))
}
return resource.NonRetryableError(fmt.Errorf("CreateSnatEntry got error: %#v.", err))
}
resp, _ := raw.(*vpc.CreateSnatEntryResponse)
d.SetId(resp.SnatEntryId)
return nil
}); err != nil {
return err
}
return resourceAliyunSnatEntryRead(d, meta)
}
func resourceAliyunSnatEntryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
snatEntry, err := vpcService.DescribeSnatEntry(d.Get("snat_table_id").(string), d.Id())
if err != nil {
if NotFoundError(err) {
return nil
}
return err
}
d.Set("snat_table_id", snatEntry.SnatTableId)
d.Set("source_vswitch_id", snatEntry.SourceVSwitchId)
d.Set("snat_ip", snatEntry.SnatIp)
return nil
}
func resourceAliyunSnatEntryUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
snatEntry, err := vpcService.DescribeSnatEntry(d.Get("snat_table_id").(string), d.Id())
if err != nil {
return err
}
d.Partial(true)
attributeUpdate := false
request := vpc.CreateModifySnatEntryRequest()
request.RegionId = string(client.Region)
request.SnatTableId = snatEntry.SnatTableId
request.SnatEntryId = snatEntry.SnatEntryId
if d.HasChange("snat_ip") {
d.SetPartial("snat_ip")
var snat_ip string
if v, ok := d.GetOk("snat_ip"); ok {
snat_ip = v.(string)
} else {
return fmt.Errorf("cann't change snap_ip to empty string")
}
request.SnatIp = snat_ip
attributeUpdate = true
}
if attributeUpdate {
_, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.ModifySnatEntry(request)
})
if err != nil {
return err
}
}
d.Partial(false)
return resourceAliyunSnatEntryRead(d, meta)
}
func resourceAliyunSnatEntryDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := vpc.CreateDeleteSnatEntryRequest()
request.RegionId = string(client.Region)
request.SnatTableId = d.Get("snat_table_id").(string)
request.SnatEntryId = d.Id()
_, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.DeleteSnatEntry(request)
})
if err != nil {
if IsExceptedError(err, InvalidSnatTableIdNotFound) {
return nil
}
return err
}
return nil
}