9
9
"github.com/aws/aws-sdk-go/aws"
10
10
"github.com/aws/aws-sdk-go/aws/awserr"
11
11
"github.com/aws/aws-sdk-go/service/lightsail"
12
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
15
14
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -93,6 +92,11 @@ func ResourceInstance() *schema.Resource {
93
92
Type : schema .TypeFloat ,
94
93
Computed : true ,
95
94
},
95
+ "ip_address_type" : {
96
+ Type : schema .TypeString ,
97
+ Optional : true ,
98
+ Default : "dualstack" ,
99
+ },
96
100
"ipv6_address" : {
97
101
Type : schema .TypeString ,
98
102
Computed : true ,
@@ -144,10 +148,15 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
144
148
if v , ok := d .GetOk ("key_pair_name" ); ok {
145
149
req .KeyPairName = aws .String (v .(string ))
146
150
}
151
+
147
152
if v , ok := d .GetOk ("user_data" ); ok {
148
153
req .UserData = aws .String (v .(string ))
149
154
}
150
155
156
+ if v , ok := d .GetOk ("ip_address_type" ); ok {
157
+ req .IpAddressType = aws .String (v .(string ))
158
+ }
159
+
151
160
if len (tags ) > 0 {
152
161
req .Tags = Tags (tags .IgnoreAWS ())
153
162
}
@@ -164,16 +173,8 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
164
173
op := resp .Operations [0 ]
165
174
d .SetId (d .Get ("name" ).(string ))
166
175
167
- stateConf := & resource.StateChangeConf {
168
- Pending : []string {"Started" },
169
- Target : []string {"Completed" , "Succeeded" },
170
- Refresh : resourceOperationRefreshFunc (op .Id , meta ),
171
- Timeout : 10 * time .Minute ,
172
- Delay : 5 * time .Second ,
173
- MinTimeout : 3 * time .Second ,
174
- }
176
+ err = waitOperation (conn , op .Id )
175
177
176
- _ , err = stateConf .WaitForState ()
177
178
if err != nil {
178
179
// We don't return an error here because the Create call succeeded
179
180
log .Printf ("[ERR] Error waiting for instance (%s) to become ready: %s" , d .Id (), err )
@@ -230,6 +231,7 @@ func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
230
231
}
231
232
232
233
d .Set ("ipv6_addresses" , aws .StringValueSlice (i .Ipv6Addresses ))
234
+ d .Set ("ip_address_type" , i .IpAddressType )
233
235
d .Set ("is_static_ip" , i .IsStaticIp )
234
236
d .Set ("private_ip_address" , i .PrivateIpAddress )
235
237
d .Set ("public_ip_address" , i .PublicIpAddress )
@@ -260,16 +262,8 @@ func resourceInstanceDelete(d *schema.ResourceData, meta interface{}) error {
260
262
261
263
op := resp .Operations [0 ]
262
264
263
- stateConf := & resource.StateChangeConf {
264
- Pending : []string {"Started" },
265
- Target : []string {"Completed" , "Succeeded" },
266
- Refresh : resourceOperationRefreshFunc (op .Id , meta ),
267
- Timeout : 10 * time .Minute ,
268
- Delay : 5 * time .Second ,
269
- MinTimeout : 3 * time .Second ,
270
- }
265
+ err = waitOperation (conn , op .Id )
271
266
272
- _ , err = stateConf .WaitForState ()
273
267
if err != nil {
274
268
return fmt .Errorf (
275
269
"Error waiting for instance (%s) to become destroyed: %s" ,
@@ -282,42 +276,36 @@ func resourceInstanceDelete(d *schema.ResourceData, meta interface{}) error {
282
276
func resourceInstanceUpdate (d * schema.ResourceData , meta interface {}) error {
283
277
conn := meta .(* conns.AWSClient ).LightsailConn
284
278
285
- if d .HasChange ("tags_all" ) {
286
- o , n := d .GetChange ("tags_all" )
279
+ if d .HasChange ("ip_address_type" ) {
280
+ resp , err := conn .SetIpAddressType (& lightsail.SetIpAddressTypeInput {
281
+ ResourceName : aws .String (d .Id ()),
282
+ ResourceType : aws .String ("Instance" ),
283
+ IpAddressType : aws .String (d .Get ("ip_address_type" ).(string )),
284
+ })
287
285
288
- if err := UpdateTags ( conn , d . Id (), o , n ); err != nil {
289
- return fmt . Errorf ( "error updating Lightsail Instance (%s) tags: %s" , d . Id (), err )
286
+ if err != nil {
287
+ return err
290
288
}
291
- }
292
289
293
- return resourceInstanceRead (d , meta )
294
- }
290
+ if len (resp .Operations ) == 0 {
291
+ return fmt .Errorf ("No operations found for CreateInstance request" )
292
+ }
295
293
296
- // method to check the status of an Operation, which is returned from
297
- // Create/Delete methods.
298
- // Status's are an aws.OperationStatus enum:
299
- // - NotStarted
300
- // - Started
301
- // - Failed
302
- // - Completed
303
- // - Succeeded (not documented?)
304
- func resourceOperationRefreshFunc (
305
- oid * string , meta interface {}) resource.StateRefreshFunc {
306
- return func () (interface {}, string , error ) {
307
- conn := meta .(* conns.AWSClient ).LightsailConn
308
- log .Printf ("[DEBUG] Checking if Lightsail Operation (%s) is Completed" , * oid )
309
- o , err := conn .GetOperation (& lightsail.GetOperationInput {
310
- OperationId : oid ,
311
- })
294
+ op := resp .Operations [0 ]
295
+
296
+ err = waitOperation (conn , op .Id )
312
297
if err != nil {
313
- return o , "FAILED" , err
298
+ return err
314
299
}
300
+ }
315
301
316
- if o .Operation == nil {
317
- return nil , "Failed" , fmt .Errorf ("Error retrieving Operation info for operation (%s)" , * oid )
318
- }
302
+ if d .HasChange ("tags_all" ) {
303
+ o , n := d .GetChange ("tags_all" )
319
304
320
- log .Printf ("[DEBUG] Lightsail Operation (%s) is currently %q" , * oid , * o .Operation .Status )
321
- return o , * o .Operation .Status , nil
305
+ if err := UpdateTags (conn , d .Id (), o , n ); err != nil {
306
+ return fmt .Errorf ("error updating Lightsail Instance (%s) tags: %s" , d .Id (), err )
307
+ }
322
308
}
309
+
310
+ return resourceInstanceRead (d , meta )
323
311
}
0 commit comments