forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_router_interface_connection.go
232 lines (201 loc) · 7.83 KB
/
resource_alicloud_router_interface_connection.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package alicloud
import (
"fmt"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAlicloudRouterInterfaceConnection() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudRouterInterfaceConnectionCreate,
Read: resourceAlicloudRouterInterfaceConnectionRead,
Delete: resourceAlicloudRouterInterfaceConnectionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"interface_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"opposite_interface_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"opposite_router_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAllowedStringValue([]string{
string(VRouter), string(VBR)}),
Default: VRouter,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return !d.HasChange("opposite_interface_owner_id")
},
},
"opposite_router_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return !d.HasChange("opposite_interface_owner_id")
},
},
"opposite_interface_owner_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}
func resourceAlicloudRouterInterfaceConnectionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
oppsiteId := d.Get("opposite_interface_id").(string)
interfaceId := d.Get("interface_id").(string)
ri, err := client.DescribeRouterInterface(getRegionId(d, meta), interfaceId)
if err != nil {
return err
}
// At present, the interface with "active/inactive" status can not be modify opposite connection information
// and it is RouterInterface product limitation.
if ri.OppositeInterfaceId == oppsiteId {
if ri.Status == string(Active) {
return fmt.Errorf("The specified router interface connection has existed, and please import it using id %s.", interfaceId)
}
if ri.Status == string(Inactive) {
if err := client.ActivateRouterInterface(interfaceId); err != nil {
return err
}
if err := client.WaitForRouterInterface(getRegionId(d, meta), interfaceId, Active, DefaultTimeout); err != nil {
return fmt.Errorf("When activing router interface %s got an error: %#v.", interfaceId, err)
}
d.SetId(interfaceId)
return resourceAlicloudRouterInterfaceConnectionRead(d, meta)
}
}
req := vpc.CreateModifyRouterInterfaceAttributeRequest()
req.RouterInterfaceId = interfaceId
req.OppositeInterfaceId = oppsiteId
if owner_id, ok := d.GetOk("opposite_interface_owner_id"); ok && owner_id.(string) != "" {
req.OppositeInterfaceOwnerId = requests.Integer(owner_id.(string))
if v, o := d.GetOk("opposite_router_type"); !o || v.(string) == "" {
return fmt.Errorf("'opposite_router_type' is required when 'opposite_interface_owner_id' is set.")
} else {
req.OppositeRouterType = v.(string)
}
if v, o := d.GetOk("opposite_router_id"); !o || v.(string) == "" {
return fmt.Errorf("'opposite_router_id' is required when 'opposite_interface_owner_id' is set.")
} else {
req.OppositeRouterId = v.(string)
}
} else {
owner := ri.OppositeInterfaceOwnerId
if owner == "" {
owner = client.AccountId
}
if owner == "" {
return fmt.Errorf("Opposite router interface owner id is empty. Please use field 'opposite_interface_owner_id' or globle field 'account_id' to set.")
}
oppositeRi, err := client.DescribeRouterInterface(ri.OppositeRegionId, oppsiteId)
if err != nil {
return err
}
req.OppositeRouterId = oppositeRi.RouterId
req.OppositeRouterType = oppositeRi.RouterType
req.OppositeInterfaceOwnerId = requests.Integer(owner)
}
if err := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, err := client.vpcconn.ModifyRouterInterfaceAttribute(req); err != nil {
return resource.NonRetryableError(fmt.Errorf("Modifying RouterInterface %s Connection got an error: %#v.", interfaceId, err))
}
ri, err := client.DescribeRouterInterface(getRegionId(d, meta), interfaceId)
if err != nil {
return resource.NonRetryableError(fmt.Errorf("When modifying RouterInterface %s Connection, describing it got an error: %#v.", interfaceId, err))
}
if ri.OppositeInterfaceId == "" || ri.OppositeRouterType == "" ||
ri.OppositeRouterId == "" || ri.OppositeInterfaceOwnerId == "" {
return resource.RetryableError(fmt.Errorf("Modifying RouterInterface %s Connection timeout with opposite interface id is %s, "+
"opposite router id is %s, opposite router type is %s and opposite interface owner id is %s.", interfaceId, ri.OppositeInterfaceId,
ri.OppositeRouterId, ri.OppositeRouterType, ri.OppositeInterfaceOwnerId))
}
return nil
}); err != nil {
return err
}
if ri.Role == string(InitiatingSide) {
connReq := vpc.CreateConnectRouterInterfaceRequest()
connReq.RouterInterfaceId = interfaceId
if err := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, err := client.vpcconn.ConnectRouterInterface(connReq); err != nil {
if IsExceptedErrors(err, []string{IncorrectOppositeInterfaceInfoNotSet}) {
return resource.RetryableError(fmt.Errorf("Connecting router interface %s timeout.", interfaceId))
}
return resource.NonRetryableError(fmt.Errorf("Connecting router interface %s got an error: %#v.", interfaceId, err))
}
return nil
}); err != nil {
return err
}
if err := client.WaitForRouterInterface(getRegionId(d, meta), interfaceId, Active, DefaultTimeout); err != nil {
return fmt.Errorf("Connecting router interface %s got an error: %#v.", interfaceId, err)
}
}
d.SetId(interfaceId)
return resourceAlicloudRouterInterfaceConnectionRead(d, meta)
}
func resourceAlicloudRouterInterfaceConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
ri, err := client.DescribeRouterInterface(getRegionId(d, meta), d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
}
if ri.Status == string(Inactive) {
if err := client.ActivateRouterInterface(d.Id()); err != nil {
return err
}
if err := client.WaitForRouterInterface(getRegionId(d, meta), d.Id(), Active, DefaultTimeout); err != nil {
return fmt.Errorf("When activing router interface %s got an error: %#v.", d.Id(), err)
}
}
d.Set("interface_id", ri.RouterInterfaceId)
d.Set("opposite_interface_id", ri.OppositeInterfaceId)
d.Set("opposite_router_type", ri.OppositeRouterType)
d.Set("opposite_router_id", ri.OppositeRouterId)
d.Set("opposite_interface_owner_id", ri.OppositeInterfaceOwnerId)
return nil
}
func resourceAlicloudRouterInterfaceConnectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
ri, err := client.DescribeRouterInterface(getRegionId(d, meta), d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
}
if ri.Status == string(Idle) {
d.SetId("")
return nil
}
// At present, the interface with "active/inactive" status can not be modify opposite connection information
// and it is RouterInterface product limitation. So, the connection delete action is only modifying it to inactive.
if ri.Status == string(Active) {
if err := client.DeactivateRouterInterface(d.Id()); err != nil {
return err
}
}
if err := client.WaitForRouterInterface(getRegionId(d, meta), d.Id(), Inactive, DefaultTimeoutMedium); err != nil {
return fmt.Errorf("Deleting routerinterface %s connection got an error: %#v.", d.Id(), err)
}
return nil
}