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

resource/aws_ec2_capacity_reservation: Fix to set ARN from its response and add owner_id attribute #17129

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 6 additions & 11 deletions aws/resource_aws_ec2_capacity_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -97,6 +96,10 @@ func resourceAwsEc2CapacityReservation() *schema.Resource {
Required: true,
ForceNew: true,
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"tenancy": {
Type: schema.TypeString,
Expand Down Expand Up @@ -205,22 +208,14 @@ func resourceAwsEc2CapacityReservationRead(d *schema.ResourceData, meta interfac
d.Set("instance_match_criteria", reservation.InstanceMatchCriteria)
d.Set("instance_platform", reservation.InstancePlatform)
d.Set("instance_type", reservation.InstanceType)
d.Set("owner_id", reservation.OwnerId)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(reservation.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("tenancy", reservation.Tenancy)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("capacity-reservation/%s", d.Id()),
}.String()
Comment on lines -215 to -221
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When removing this type of manual ARN building, we should double check that it is not in the website/docs/index.html file skip_requesting_account_id list of resources that are documented as potentially not working. 👍


d.Set("arn", arn)
d.Set("arn", reservation.CapacityReservationArn)

return nil
}
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_ec2_capacity_reservation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestAccAWSEc2CapacityReservation_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "instance_match_criteria", "open"),
resource.TestCheckResourceAttr(resourceName, "instance_platform", "Linux/UNIX"),
resource.TestCheckResourceAttr(resourceName, "instance_type", "t2.micro"),
resource.TestCheckResourceAttrSet(resourceName, "owner_id"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can verify that the owner_id is set to the current AWS account ID.

Suggested change
resource.TestCheckResourceAttrSet(resourceName, "owner_id"),
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know that kind of test helper exists. Thank you!

resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "tenancy", "default"),
),
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ec2_capacity_reservation.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - The Capacity Reservation ID.
* `owner_id` - The ID of the AWS account that owns the Capacity Reservation.
* `arn` - The ARN of the Capacity Reservation.

## Import
Expand Down