/
resource_huaweicloud_er_association.go
272 lines (238 loc) · 8.23 KB
/
resource_huaweicloud_er_association.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package er
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/openstack/er/v3/associations"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API ER POST /v3/{project_id}/enterprise-router/{er_id}/route-tables/{route_table_id}/associate
// @API ER GET /v3/{project_id}/enterprise-router/{er_id}/route-tables/{route_table_id}/associations
// @API ER POST /v3/{project_id}/enterprise-router/{er_id}/route-tables/{route_table_id}/disassociate
func ResourceAssociation() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAssociationCreate,
ReadContext: resourceAssociationRead,
DeleteContext: resourceAssociationDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceAssociationImportState,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(2 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The region where the ER instance and route table are located.`,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The ID of the ER instance to which the route table and the attachment belongs.`,
},
"route_table_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The ID of the route table to which the association belongs.`,
},
"attachment_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The ID of the attachment corresponding to the association.`,
},
"attachment_type": {
Type: schema.TypeString,
Computed: true,
Description: `The type of the attachment corresponding to the association.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The current status of the association.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The creation time.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `The latest update time.`,
},
},
}
}
func resourceAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
client, err := cfg.ErV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ER v3 client: %s", err)
}
var (
instanceId = d.Get("instance_id").(string)
routeTableId = d.Get("route_table_id").(string)
opts = associations.CreateOpts{
AttachmentId: d.Get("attachment_id").(string),
}
)
resp, err := associations.Create(client, instanceId, routeTableId, opts)
if err != nil {
return diag.Errorf("error creating the association to the route table: %s", err)
}
d.SetId(resp.ID)
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING"},
Target: []string{"COMPLETED"},
Refresh: associationStatusRefreshFunc(client, instanceId, routeTableId, d.Id(), []string{"available"}),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
PollInterval: 10 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.FromErr(err)
}
return resourceAssociationRead(ctx, d, meta)
}
// QueryAssociationById is a method to query association details from a specified route table using given parameters.
func QueryAssociationById(client *golangsdk.ServiceClient, instanceId, routeTableId,
associationId string) (*associations.Association, error) {
resp, err := associations.List(client, instanceId, routeTableId, associations.ListOpts{})
if err != nil {
return nil, err
}
filter := map[string]interface{}{
"ID": associationId,
}
result, err := utils.FilterSliceWithField(resp, filter)
if err != nil {
return nil, err
}
if len(result) < 1 {
return nil, golangsdk.ErrDefault404{
ErrUnexpectedResponseCode: golangsdk.ErrUnexpectedResponseCode{
Body: []byte(fmt.Sprintf("the association (%s) does not exist", associationId)),
},
}
}
log.Printf("[DEBUG] The result filtered by resource ID (%s) is: %#v", associationId, result)
association, ok := result[0].(associations.Association)
if !ok {
return nil, fmt.Errorf("the element type of filter result is incorrect, want 'associations.Association', "+
"but got '%T'", result[0])
}
return &association, nil
}
func associationStatusRefreshFunc(client *golangsdk.ServiceClient, instanceId, routeTableId, associationId string,
targets []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := QueryAssociationById(client, instanceId, routeTableId, associationId)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok && len(targets) < 1 {
return resp, "COMPLETED", nil
}
return nil, "", err
}
if utils.StrSliceContains([]string{"failed"}, resp.Status) {
return resp, "", fmt.Errorf("unexpected status '%s'", resp.Status)
}
if utils.StrSliceContains(targets, resp.Status) {
return resp, "COMPLETED", nil
}
return resp, "PENDING", nil
}
}
func resourceAssociationRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.ErV3Client(region)
if err != nil {
return diag.Errorf("error creating ER v3 client: %s", err)
}
var (
instanceId = d.Get("instance_id").(string)
routeTableId = d.Get("route_table_id").(string)
associationId = d.Id()
)
resp, err := QueryAssociationById(client, instanceId, routeTableId, associationId)
if err != nil {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "ER association")
}
mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("route_table_id", resp.RouteTableId),
d.Set("attachment_id", resp.AttachmentId),
d.Set("attachment_type", resp.ResourceType),
d.Set("status", resp.Status),
d.Set("created_at", resp.CreatedAt),
d.Set("updated_at", resp.UpdatedAt),
)
if mErr.ErrorOrNil() != nil {
return diag.Errorf("error saving association (%s) fields: %s", associationId, mErr)
}
return nil
}
func resourceAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.ErV3Client(region)
if err != nil {
return diag.Errorf("error creating ER v3 client: %s", err)
}
var (
instanceId = d.Get("instance_id").(string)
routeTableId = d.Get("route_table_id").(string)
associationId = d.Id()
opts = associations.DeleteOpts{
AttachmentId: d.Get("attachment_id").(string),
}
)
err = associations.Delete(client, instanceId, routeTableId, opts)
if err != nil {
return diag.Errorf("error deleting association (%s): %s", associationId, err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING"},
Target: []string{"COMPLETED"},
Refresh: associationStatusRefreshFunc(client, instanceId, routeTableId, associationId, nil),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
PollInterval: 10 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceAssociationImportState(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData,
error) {
parts := strings.SplitN(d.Id(), "/", 3)
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid format for import ID, want '<instance_id>/<route_table_id>/<association_id>', "+
"but '%s'", d.Id())
}
d.SetId(parts[2])
mErr := multierror.Append(nil,
d.Set("instance_id", parts[0]),
d.Set("route_table_id", parts[1]),
)
return []*schema.ResourceData{d}, mErr.ErrorOrNil()
}