-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_gaussdb_redis_eip_associate.go
289 lines (247 loc) · 9.81 KB
/
resource_huaweicloud_gaussdb_redis_eip_associate.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product GaussDB
// ---------------------------------------------------------------
package gaussdb
import (
"context"
"fmt"
"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/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API GaussDBforNoSQL POST /v3/{project_id}/instances/{instance_id}/nodes/{node_id}/public-ip
// @API GaussDBforNoSQL GET /v3/{project_id}/instances
func ResourceGaussRedisEipAssociate() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGaussRedisEipAssociateCreate,
ReadContext: resourceGaussRedisEipAssociateRead,
DeleteContext: resourceGaussRedisEipAssociateDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the ID of a GaussDB Redis instance.`,
},
"node_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the ID of a GaussDB Redis node.`,
},
"public_ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Indicates the EIP address to associate.`,
},
},
}
}
func resourceGaussRedisEipAssociateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createGaussRedisEipAssociate: create GaussDB Redis node EIP associate
var (
createGaussRedisEipAssociateHttpUrl = "v3/{project_id}/instances/{instance_id}/nodes/{node_id}/public-ip"
createGaussRedisEipAssociateProduct = "geminidb"
)
createGaussRedisEipAssociateClient, err := cfg.NewServiceClient(createGaussRedisEipAssociateProduct, region)
if err != nil {
return diag.Errorf("error creating GaussDB for Redis client: %s", err)
}
vpcClient, err := cfg.NetworkingV1Client(region)
if err != nil {
return diag.Errorf("error creating VPC client: %s", err)
}
instanceID := d.Get("instance_id").(string)
nodeID := d.Get("node_id").(string)
publicIP := d.Get("public_ip").(string)
epsID := "all_granted_eps"
publicID, err := common.GetEipIDbyAddress(vpcClient, publicIP, epsID)
if err != nil {
return diag.Errorf("unable to get ID of public IP %s: %s", publicIP, err)
}
createGaussRedisEipAssociatePath := createGaussRedisEipAssociateClient.Endpoint + createGaussRedisEipAssociateHttpUrl
createGaussRedisEipAssociatePath = strings.ReplaceAll(createGaussRedisEipAssociatePath, "{project_id}",
createGaussRedisEipAssociateClient.ProjectID)
createGaussRedisEipAssociatePath = strings.ReplaceAll(createGaussRedisEipAssociatePath, "{instance_id}",
instanceID)
createGaussRedisEipAssociatePath = strings.ReplaceAll(createGaussRedisEipAssociatePath, "{node_id}", nodeID)
createGaussRedisEipAssociateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
createGaussRedisEipAssociateOpt.JSONBody = utils.RemoveNil(buildGaussRedisEipAssociateBodyParams("BIND", publicIP,
publicID))
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
_, err = createGaussRedisEipAssociateClient.Request("POST", createGaussRedisEipAssociatePath,
&createGaussRedisEipAssociateOpt)
isRetry, err := handleOperationError(err)
if isRetry {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return diag.Errorf("error creating EipAssociate: %s", err)
}
d.SetId(instanceID + "/" + nodeID)
stateConf := &resource.StateChangeConf{
Pending: []string{"BIND_EIP"},
Target: []string{"available"},
Refresh: GaussRedisInstanceUpdateRefreshFunc(createGaussRedisEipAssociateClient, instanceID,
[]string{"BIND_EIP"}),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 5 * time.Second,
PollInterval: 10 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for instance (%s) to become ready: %s", instanceID, err)
}
return resourceGaussRedisEipAssociateRead(ctx, d, meta)
}
func resourceGaussRedisEipAssociateRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getGaussRedisEipAssociate: Query GaussDB Redis node EIP associate
var (
getGaussRedisEipAssociateHttpUrl = "v3/{project_id}/instances"
getGaussRedisEipAssociateProduct = "geminidb"
)
getGaussRedisEipAssociateClient, err := cfg.NewServiceClient(getGaussRedisEipAssociateProduct, region)
if err != nil {
return diag.Errorf("error creating GaussDB for Redis Client: %s", err)
}
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
return diag.Errorf("invalid id format, must be <instance_id>/<node_id>")
}
instanceID := parts[0]
nodeID := parts[1]
getGaussRedisEipAssociatePath := getGaussRedisEipAssociateClient.Endpoint + getGaussRedisEipAssociateHttpUrl
getGaussRedisEipAssociatePath = strings.ReplaceAll(getGaussRedisEipAssociatePath, "{project_id}",
getGaussRedisEipAssociateClient.ProjectID)
getGaussRedisEipAssociateQueryParams := buildGetGaussRedisEipAssociateQueryParams(instanceID)
getGaussRedisEipAssociatePath += getGaussRedisEipAssociateQueryParams
getGaussRedisEipAssociateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
}
getGaussRedisEipAssociateResp, err := getGaussRedisEipAssociateClient.Request("GET",
getGaussRedisEipAssociatePath, &getGaussRedisEipAssociateOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving EipAssociate")
}
getGaussRedisEipAssociateRespBody, err := utils.FlattenResponse(getGaussRedisEipAssociateResp)
if err != nil {
return diag.FromErr(err)
}
publicIP := utils.PathSearch(fmt.Sprintf("instances[?id=='%s']|[0].groups[0].nodes[?id=='%s']|[0].public_ip",
instanceID, nodeID), getGaussRedisEipAssociateRespBody, "")
if publicIP == "" {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "")
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("instance_id", instanceID),
d.Set("node_id", nodeID),
d.Set("public_ip", publicIP),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func buildGetGaussRedisEipAssociateQueryParams(instanceID string) string {
return fmt.Sprintf("?id=%s", instanceID)
}
func resourceGaussRedisEipAssociateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteGaussRedisEipAssociate: Delete GaussDB Redis node EIP associate
var (
deleteGaussRedisEipAssociateHttpUrl = "v3/{project_id}/instances/{instance_id}/nodes/{node_id}/public-ip"
deleteGaussRedisEipAssociateProduct = "geminidb"
)
deleteGaussRedisEipAssociateClient, err := cfg.NewServiceClient(deleteGaussRedisEipAssociateProduct, region)
if err != nil {
return diag.Errorf("error creating GaussDB for Redis client: %s", err)
}
instanceID := d.Get("instance_id").(string)
deleteGaussRedisEipAssociatePath := deleteGaussRedisEipAssociateClient.Endpoint + deleteGaussRedisEipAssociateHttpUrl
deleteGaussRedisEipAssociatePath = strings.ReplaceAll(deleteGaussRedisEipAssociatePath, "{project_id}",
deleteGaussRedisEipAssociateClient.ProjectID)
deleteGaussRedisEipAssociatePath = strings.ReplaceAll(deleteGaussRedisEipAssociatePath, "{instance_id}",
instanceID)
deleteGaussRedisEipAssociatePath = strings.ReplaceAll(deleteGaussRedisEipAssociatePath, "{node_id}",
fmt.Sprintf("%v", d.Get("node_id")))
deleteGaussRedisEipAssociateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
publicIP := d.Get("public_ip").(string)
deleteGaussRedisEipAssociateOpt.JSONBody = utils.RemoveNil(buildGaussRedisEipAssociateBodyParams("UNBIND",
publicIP, nil))
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
_, err = deleteGaussRedisEipAssociateClient.Request("POST", deleteGaussRedisEipAssociatePath,
&deleteGaussRedisEipAssociateOpt)
isRetry, err := handleOperationError(err)
if isRetry {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return diag.Errorf("error deleting EipAssociate: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"UNBIND_EIP"},
Target: []string{"available"},
Refresh: GaussRedisInstanceUpdateRefreshFunc(deleteGaussRedisEipAssociateClient, instanceID,
[]string{"UNBIND_EIP"}),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 5 * time.Second,
PollInterval: 10 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for instance (%s) to become ready: %s", instanceID, err)
}
return diag.FromErr(err)
}
func buildGaussRedisEipAssociateBodyParams(action, publicIP, publicID interface{}) map[string]interface{} {
bodyParams := map[string]interface{}{
"action": action,
"public_ip": publicIP,
"public_ip_id": publicID,
}
return bodyParams
}