Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: aws_lightsail_disk_attachment crash on failure #28593

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/28593.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lightsail_disk_attachment: Resolves a panic when an attachment fails and attempts to display the error returned by AWS.
```
12 changes: 6 additions & 6 deletions internal/service/lightsail/disk_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ func resourceDiskAttachmentCreate(ctx context.Context, d *schema.ResourceData, m
out, err := conn.AttachDiskWithContext(ctx, &in)

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeAttachDisk, ResDiskAttachment, d.Get("name").(string), err)
return create.DiagError(names.Lightsail, lightsail.OperationTypeAttachDisk, ResDiskAttachment, d.Get("disk_name").(string), err)
}

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

op := out.Operations[0]

err = waitOperation(conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeAttachDisk, ResDiskAttachment, d.Get("name").(string), errors.New("Error waiting for Attach Disk request operation"))
return create.DiagError(names.Lightsail, lightsail.OperationTypeAttachDisk, ResDiskAttachment, d.Get("disk_name").(string), errors.New("Error waiting for Attach Disk request operation"))
}

// Generate an ID
Expand Down Expand Up @@ -144,18 +144,18 @@ func resourceDiskAttachmentDelete(ctx context.Context, d *schema.ResourceData, m
})

if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDetachDisk, ResDiskAttachment, d.Get("name").(string), err)
return create.DiagError(names.Lightsail, lightsail.OperationTypeDetachDisk, ResDiskAttachment, d.Get("disk_name").(string), err)
}

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

op := out.Operations[0]

err = waitOperation(conn, op.Id)
if err != nil {
return create.DiagError(names.Lightsail, lightsail.OperationTypeDetachDisk, ResDiskAttachment, d.Get("name").(string), errors.New("Error waiting for Detach Disk request operation"))
return create.DiagError(names.Lightsail, lightsail.OperationTypeDetachDisk, ResDiskAttachment, d.Get("disk_name").(string), errors.New("Error waiting for Detach Disk request operation"))
}

iStateOut, err = waitInstanceStateWithContext(ctx, conn, &iName)
Expand Down
8 changes: 7 additions & 1 deletion internal/service/lightsail/disk_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/service/lightsail"
Expand All @@ -24,6 +25,7 @@ func TestAccLightsailDiskAttachment_basic(t *testing.T) {
dName := sdkacctest.RandomWithPrefix("tf-acc-test")
liName := sdkacctest.RandomWithPrefix("tf-acc-test")
diskPath := "/dev/xvdf"
diskPathBad := "/jenkins-home"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -44,6 +46,10 @@ func TestAccLightsailDiskAttachment_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "instance_name", liName),
),
},
{
Config: testAccDiskAttachmentConfig_basic(dName, liName, diskPathBad),
ExpectError: regexp.MustCompile(`The disk path is invalid. You must specify a valid disk path.`),
},
},
})
}
Expand Down Expand Up @@ -149,7 +155,7 @@ resource "aws_lightsail_disk" "test" {
resource "aws_lightsail_instance" "test" {
name = %[2]q
availability_zone = data.aws_availability_zones.available.names[0]
blueprint_id = "amazon_linux"
blueprint_id = "amazon_linux_2"
bundle_id = "nano_1_0"
}

Expand Down