Skip to content

Commit

Permalink
Merge pull request #30056 from brittandeyoung/td-lightsail_expandOper…
Browse files Browse the repository at this point in the history
…ations

BugFix and Cleanup:  Implement new Lightsail `expandOperations` across all Lightsail resources. Additionally fix 3 reported Lightsail Bugs.
  • Loading branch information
ewbankkit authored Mar 16, 2023
2 parents 2cdaef3 + 2cc1646 commit 11d3319
Show file tree
Hide file tree
Showing 35 changed files with 478 additions and 463 deletions.
11 changes: 11 additions & 0 deletions .changelog/30056.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
resource/aws_lightsail_domain_entry: Moved the error handling of an improperly formatted ID to be before attempting to access the id_parts. This will cause a proper empty resource message instead of a panic when ID is not properly formed.
```

```release-note:bug
resource/aws_lightsail_instance: Added a check to ensure that the availability_zone value is within the current region of the provider.
```

```release-note:bug
resource/aws_lightsail_domain_entry: Allow for the domain entry to begin with an underscore.
```
34 changes: 11 additions & 23 deletions internal/service/lightsail/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lightsail

import (
"context"
"errors"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -90,18 +89,14 @@ func resourceBucketCreate(ctx context.Context, d *schema.ResourceData, meta inte
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateBucket, ResBucket, d.Get("name").(string), err)
}

if len(out.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateBucket, ResBucket, d.Get("name").(string), errors.New("No operations found for Create Bucket request"))
}

op := out.Operations[0]
id := d.Get("name").(string)
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeCreateBucket, ResBucket, id)

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateBucket, ResBucket, d.Get("name").(string), errors.New("Error waiting for Create Bucket request operation"))
if diag != nil {
return diag
}

d.SetId(d.Get("name").(string))
d.SetId(id)

return resourceBucketRead(ctx, d, meta)
}
Expand Down Expand Up @@ -176,15 +171,10 @@ func resourceBucketUpdate(ctx context.Context, d *schema.ResourceData, meta inte
return create.DiagError(names.Lightsail, lightsail.OperationTypeUpdateBucket, ResBucket, d.Get("name").(string), err)
}

if len(out.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeUpdateBucket, ResBucket, d.Get("name").(string), errors.New("No operations found for request"))
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeUpdateBucket, ResBucket, d.Get("name").(string))

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeUpdateBucket, ResBucket, d.Get("name").(string), errors.New("Error waiting for request operation"))
if diag != nil {
return diag
}
}

Expand All @@ -205,12 +195,10 @@ func resourceBucketDelete(ctx context.Context, d *schema.ResourceData, meta inte
return create.DiagError(names.CE, create.ErrActionDeleting, ResBucket, d.Id(), err)
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeDeleteBucket, ResBucket, d.Id())

err = waitOperation(ctx, conn, op.Id)

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDeleteCertificate, ResBucket, d.Id(), err)
if diag != nil {
return diag
}

return nil
Expand Down
20 changes: 6 additions & 14 deletions internal/service/lightsail/bucket_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lightsail

import (
"context"
"errors"
"regexp"
"time"

Expand Down Expand Up @@ -73,15 +72,10 @@ func resourceBucketAccessKeyCreate(ctx context.Context, d *schema.ResourceData,
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateBucketAccessKey, ResBucketAccessKey, d.Get("bucket_name").(string), err)
}

if len(out.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateBucketAccessKey, ResBucketAccessKey, d.Get("bucket_name").(string), errors.New("No operations found for request"))
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeCreateBucketAccessKey, ResBucketAccessKey, d.Get("bucket_name").(string))

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateBucketAccessKey, ResBucketAccessKey, d.Get("bucket_name").(string), errors.New("Error waiting for request operation"))
if diag != nil {
return diag
}

idParts := []string{d.Get("bucket_name").(string), *out.AccessKey.AccessKeyId}
Expand Down Expand Up @@ -141,12 +135,10 @@ func resourceBucketAccessKeyDelete(ctx context.Context, d *schema.ResourceData,
return create.DiagError(names.Lightsail, create.ErrActionDeleting, ResBucketAccessKey, d.Id(), err)
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeDeleteBucketAccessKey, ResBucketAccessKey, d.Id())

err = waitOperation(ctx, conn, op.Id)

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDeleteCertificate, ResBucketAccessKey, d.Id(), err)
if diag != nil {
return diag
}

return nil
Expand Down
20 changes: 6 additions & 14 deletions internal/service/lightsail/bucket_resource_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lightsail

import (
"context"
"errors"
"regexp"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -64,15 +63,10 @@ func resourceBucketResourceAccessCreate(ctx context.Context, d *schema.ResourceD
return create.DiagError(names.Lightsail, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Get("bucket_name").(string), err)
}

if len(out.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Get("bucket_name").(string), errors.New("No operations found for request"))
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Get("bucket_name").(string))

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Get("bucket_name").(string), errors.New("Error waiting for request operation"))
if diag != nil {
return diag
}

idParts := []string{d.Get("bucket_name").(string), d.Get("resource_name").(string)}
Expand Down Expand Up @@ -136,12 +130,10 @@ func resourceBucketResourceAccessDelete(ctx context.Context, d *schema.ResourceD
return create.DiagError(names.Lightsail, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Id(), err)
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Id())

err = waitOperation(ctx, conn, op.Id)

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeSetResourceAccessForBucket, ResBucketResourceAccess, d.Id(), err)
if diag != nil {
return diag
}

return nil
Expand Down
23 changes: 8 additions & 15 deletions internal/service/lightsail/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lightsail

import (
"context"
"errors"
"fmt"
"regexp"
"time"
Expand Down Expand Up @@ -152,18 +151,14 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateCertificate, ResCertificate, d.Get("name").(string), err)
}

if len(resp.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateCertificate, ResCertificate, d.Get("name").(string), errors.New("No operations found for CreateCertificate request"))
}

op := resp.Operations[0]
id := d.Get("name").(string)
diag := expandOperations(ctx, conn, resp.Operations, lightsail.OperationTypeCreateCertificate, ResCertificate, id)

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateCertificate, ResCertificate, d.Get("name").(string), errors.New("Error waiting for Create Certificate request operation"))
if diag != nil {
return diag
}

d.SetId(d.Get("name").(string))
d.SetId(id)

return resourceCertificateRead(ctx, d, meta)
}
Expand Down Expand Up @@ -243,12 +238,10 @@ func resourceCertificateDelete(ctx context.Context, d *schema.ResourceData, meta
return create.DiagError(names.CE, create.ErrActionDeleting, ResCertificate, d.Id(), err)
}

op := resp.Operations[0]
diag := expandOperations(ctx, conn, resp.Operations, lightsail.OperationTypeDeleteCertificate, ResCertificate, d.Id())

err = waitOperation(ctx, conn, op.Id)

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDeleteCertificate, ResCertificate, d.Id(), err)
if diag != nil {
return diag
}

return nil
Expand Down
26 changes: 17 additions & 9 deletions internal/service/lightsail/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,14 @@ func resourceDatabaseCreate(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf("creating Lightsail Relational Database (%s): %s", relationalDatabaseName, err)
}

d.SetId(relationalDatabaseName)
diagError := expandOperations(ctx, conn, output.Operations, lightsail.OperationTypeCreateRelationalDatabase, ResNameDatabase, relationalDatabaseName)

if err := waitOperationWithContext(ctx, conn, output.Operations[0].Id); err != nil {
return diag.Errorf("waiting for Lightsail Relational Database (%s) create: %s", d.Id(), err)
if diagError != nil {
return diagError
}

d.SetId(relationalDatabaseName)

// Backup Retention is not a value you can pass on creation and defaults to true.
// Forcing an update of the value after creation if the backup_retention_enabled value is false.
if !d.Get("backup_retention_enabled").(bool) {
Expand All @@ -248,8 +250,10 @@ func resourceDatabaseCreate(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf("updating Lightsail Relational Database (%s) backup retention: %s", d.Id(), err)
}

if err := waitOperationWithContext(ctx, conn, output.Operations[0].Id); err != nil {
return diag.Errorf("waiting for Lightsail Relational Database (%s) update: %s", d.Id(), err)
diagError := expandOperations(ctx, conn, output.Operations, lightsail.OperationTypeUpdateRelationalDatabase, ResNameDatabase, relationalDatabaseName)

if diagError != nil {
return diagError
}

if err := waitDatabaseBackupRetentionModified(ctx, conn, aws.String(d.Id()), false); err != nil {
Expand Down Expand Up @@ -366,8 +370,10 @@ func resourceDatabaseUpdate(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf("updating Lightsail Relational Database (%s): %s", d.Id(), err)
}

if err := waitOperationWithContext(ctx, conn, output.Operations[0].Id); err != nil {
return diag.Errorf("waiting for Lightsail Relational Database (%s) update: %s", d.Id(), err)
diagError := expandOperations(ctx, conn, output.Operations, lightsail.OperationTypeUpdateRelationalDatabase, ResNameDatabase, d.Id())

if diagError != nil {
return diagError
}

if d.HasChange("backup_retention_enabled") {
Expand Down Expand Up @@ -428,8 +434,10 @@ func resourceDatabaseDelete(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf("deleting Lightsail Relational Database (%s): %s", d.Id(), err)
}

if err := waitOperationWithContext(ctx, conn, output.Operations[0].Id); err != nil {
return diag.Errorf("waiting for Lightsail Relational Database (%s) delete: %s", d.Id(), err)
diagError := expandOperations(ctx, conn, output.Operations, lightsail.OperationTypeDeleteRelationalDatabase, ResNameDatabase, d.Id())

if diagError != nil {
return diagError
}

return nil
Expand Down
31 changes: 11 additions & 20 deletions internal/service/lightsail/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lightsail

import (
"context"
"errors"
"regexp"
"time"

Expand Down Expand Up @@ -75,11 +74,12 @@ func resourceDiskCreate(ctx context.Context, d *schema.ResourceData, meta interf
conn := meta.(*conns.AWSClient).LightsailConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))
id := d.Get("name").(string)

in := lightsail.CreateDiskInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
SizeInGb: aws.Int64(int64(d.Get("size_in_gb").(int))),
DiskName: aws.String(d.Get("name").(string)),
DiskName: aws.String(id),
}

if len(tags) > 0 {
Expand All @@ -89,21 +89,17 @@ func resourceDiskCreate(ctx context.Context, d *schema.ResourceData, meta interf
out, err := conn.CreateDiskWithContext(ctx, &in)

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateDisk, ResDisk, d.Get("name").(string), err)
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateDisk, ResDisk, id, err)
}

if len(out.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateDisk, ResDisk, d.Get("name").(string), errors.New("No operations found for Create Disk request"))
}

op := out.Operations[0]
d.SetId(d.Get("name").(string))
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeCreateDisk, ResDisk, id)

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeCreateDisk, ResDisk, d.Get("name").(string), errors.New("Error waiting for Create Disk request operation"))
if diag != nil {
return diag
}

d.SetId(id)

return resourceDiskRead(ctx, d, meta)
}

Expand Down Expand Up @@ -178,15 +174,10 @@ func resourceDiskDelete(ctx context.Context, d *schema.ResourceData, meta interf
return create.DiagError(names.Lightsail, lightsail.OperationTypeDeleteDisk, ResDisk, d.Get("name").(string), err)
}

if len(out.Operations) == 0 {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDeleteDisk, ResDisk, d.Get("name").(string), errors.New("No operations found for Delete Disk request"))
}

op := out.Operations[0]
diag := expandOperations(ctx, conn, out.Operations, lightsail.OperationTypeDeleteDisk, ResDisk, d.Id())

err = waitOperation(ctx, conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDeleteDisk, ResDisk, d.Get("name").(string), errors.New("Error waiting for Delete Disk request operation"))
if diag != nil {
return diag
}

return nil
Expand Down
Loading

0 comments on commit 11d3319

Please sign in to comment.