4
4
"context"
5
5
6
6
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
9
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
9
10
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -79,24 +80,36 @@ func resourceScalewayLbIPRead(ctx context.Context, d *schema.ResourceData, meta
79
80
return diag .FromErr (err )
80
81
}
81
82
82
- res , err := lbAPI .GetIP (& lb.ZonedAPIGetIPRequest {
83
- Zone : zone ,
84
- IPID : ID ,
85
- }, scw .WithContext (ctx ))
83
+ var ip * lb.IP
84
+ err = resource .RetryContext (ctx , retryLbIPInterval , func () * resource.RetryError {
85
+ res , errGet := lbAPI .GetIP (& lb.ZonedAPIGetIPRequest {
86
+ Zone : zone ,
87
+ IPID : ID ,
88
+ }, scw .WithContext (ctx ))
89
+ if err != nil {
90
+ if is403Error (errGet ) {
91
+ return resource .RetryableError (errGet )
92
+ }
93
+ return resource .NonRetryableError (errGet )
94
+ }
95
+
96
+ ip = res
97
+ return nil
98
+ })
86
99
87
100
if err != nil {
88
- if is404Error (err ) || is403Error ( err ) {
101
+ if is404Error (err ) {
89
102
d .SetId ("" )
90
103
return nil
91
104
}
92
105
return diag .FromErr (err )
93
106
}
94
107
95
108
// check lb state if it is attached
96
- if res .LBID != nil {
109
+ if ip .LBID != nil {
97
110
_ , err = lbAPI .WaitForLb (& lb.ZonedAPIWaitForLBRequest {
98
111
Zone : zone ,
99
- LBID : * res .LBID ,
112
+ LBID : * ip .LBID ,
100
113
Timeout : scw .TimeDurationPtr (defaultInstanceServerWaitTimeout ),
101
114
RetryInterval : scw .TimeDurationPtr (DefaultWaitLBRetryInterval ),
102
115
}, scw .WithContext (ctx ))
@@ -116,12 +129,12 @@ func resourceScalewayLbIPRead(ctx context.Context, d *schema.ResourceData, meta
116
129
}
117
130
118
131
_ = d .Set ("region" , string (region ))
119
- _ = d .Set ("zone" , res .Zone .String ())
120
- _ = d .Set ("organization_id" , res .OrganizationID )
121
- _ = d .Set ("project_id" , res .ProjectID )
122
- _ = d .Set ("ip_address" , res .IPAddress )
123
- _ = d .Set ("reverse" , res .Reverse )
124
- _ = d .Set ("lb_id" , flattenStringPtr (res .LBID ))
132
+ _ = d .Set ("zone" , ip .Zone .String ())
133
+ _ = d .Set ("organization_id" , ip .OrganizationID )
134
+ _ = d .Set ("project_id" , ip .ProjectID )
135
+ _ = d .Set ("ip_address" , ip .IPAddress )
136
+ _ = d .Set ("reverse" , ip .Reverse )
137
+ _ = d .Set ("lb_id" , flattenStringPtr (ip .LBID ))
125
138
126
139
return nil
127
140
}
@@ -132,23 +145,35 @@ func resourceScalewayLbIPUpdate(ctx context.Context, d *schema.ResourceData, met
132
145
return diag .FromErr (err )
133
146
}
134
147
135
- res , err := lbAPI .GetIP (& lb.ZonedAPIGetIPRequest {
136
- Zone : zone ,
137
- IPID : ID ,
138
- }, scw .WithContext (ctx ))
148
+ var ip * lb.IP
149
+ err = resource .RetryContext (ctx , retryLbIPInterval , func () * resource.RetryError {
150
+ res , errGet := lbAPI .GetIP (& lb.ZonedAPIGetIPRequest {
151
+ Zone : zone ,
152
+ IPID : ID ,
153
+ }, scw .WithContext (ctx ))
154
+ if err != nil {
155
+ if is403Error (errGet ) {
156
+ return resource .RetryableError (errGet )
157
+ }
158
+ return resource .NonRetryableError (errGet )
159
+ }
160
+
161
+ ip = res
162
+ return nil
163
+ })
139
164
140
165
if err != nil {
141
- if is404Error (err ) || is403Error ( err ) {
166
+ if is404Error (err ) {
142
167
d .SetId ("" )
143
168
return nil
144
169
}
145
170
return diag .FromErr (err )
146
171
}
147
172
148
- if res .LBID != nil {
173
+ if ip .LBID != nil {
149
174
_ , err = lbAPI .WaitForLb (& lb.ZonedAPIWaitForLBRequest {
150
175
Zone : zone ,
151
- LBID : * res .LBID ,
176
+ LBID : * ip .LBID ,
152
177
Timeout : scw .TimeDurationPtr (defaultInstanceServerWaitTimeout ),
153
178
RetryInterval : scw .TimeDurationPtr (DefaultWaitLBRetryInterval ),
154
179
}, scw .WithContext (ctx ))
@@ -174,10 +199,10 @@ func resourceScalewayLbIPUpdate(ctx context.Context, d *schema.ResourceData, met
174
199
}
175
200
}
176
201
177
- if res .LBID != nil {
202
+ if ip .LBID != nil {
178
203
_ , err = lbAPI .WaitForLb (& lb.ZonedAPIWaitForLBRequest {
179
204
Zone : zone ,
180
- LBID : * res .LBID ,
205
+ LBID : * ip .LBID ,
181
206
Timeout : scw .TimeDurationPtr (defaultInstanceServerWaitTimeout ),
182
207
RetryInterval : scw .TimeDurationPtr (DefaultWaitLBRetryInterval ),
183
208
}, scw .WithContext (ctx ))
@@ -199,25 +224,33 @@ func resourceScalewayLbIPDelete(ctx context.Context, d *schema.ResourceData, met
199
224
return diag .FromErr (err )
200
225
}
201
226
202
- res , err := lbAPI .GetIP (& lb.ZonedAPIGetIPRequest {
203
- Zone : zone ,
204
- IPID : ID ,
205
- }, scw .WithContext (ctx ))
227
+ var ip * lb.IP
228
+ err = resource .RetryContext (ctx , retryLbIPInterval , func () * resource.RetryError {
229
+ res , errGet := lbAPI .GetIP (& lb.ZonedAPIGetIPRequest {
230
+ Zone : zone ,
231
+ IPID : ID ,
232
+ }, scw .WithContext (ctx ))
233
+ if err != nil {
234
+ if is403Error (errGet ) {
235
+ return resource .RetryableError (errGet )
236
+ }
237
+ return resource .NonRetryableError (errGet )
238
+ }
239
+
240
+ ip = res
241
+ return nil
242
+ })
206
243
207
244
if err != nil {
208
- if is404Error (err ) || is403Error (err ) {
209
- d .SetId ("" )
210
- return nil
211
- }
212
245
return diag .FromErr (err )
213
246
}
214
247
215
248
// check lb state
216
- if res .LBID != nil {
217
- _ , err = lbAPI .WaitForLb (& lb.ZonedAPIWaitForLBRequest {
249
+ if ip != nil && ip .LBID != nil {
250
+ _ , err = lbAPI .WaitForLbInstances (& lb.ZonedAPIWaitForLBInstancesRequest {
251
+ LBID : ID ,
218
252
Zone : zone ,
219
- LBID : * res .LBID ,
220
- Timeout : scw .TimeDurationPtr (defaultInstanceServerWaitTimeout ),
253
+ Timeout : scw .TimeDurationPtr (LbWaitForTimeout ),
221
254
RetryInterval : scw .TimeDurationPtr (DefaultWaitLBRetryInterval ),
222
255
}, scw .WithContext (ctx ))
223
256
if err != nil {
@@ -239,11 +272,11 @@ func resourceScalewayLbIPDelete(ctx context.Context, d *schema.ResourceData, met
239
272
}
240
273
241
274
// check lb state
242
- if res .LBID != nil {
243
- _ , err = lbAPI .WaitForLb (& lb.ZonedAPIWaitForLBRequest {
275
+ if ip != nil && ip .LBID != nil {
276
+ _ , err = lbAPI .WaitForLbInstances (& lb.ZonedAPIWaitForLBInstancesRequest {
277
+ LBID : ID ,
244
278
Zone : zone ,
245
- LBID : * res .LBID ,
246
- Timeout : scw .TimeDurationPtr (defaultInstanceServerWaitTimeout ),
279
+ Timeout : scw .TimeDurationPtr (LbWaitForTimeout ),
247
280
RetryInterval : scw .TimeDurationPtr (DefaultWaitLBRetryInterval ),
248
281
}, scw .WithContext (ctx ))
249
282
if err != nil {
0 commit comments