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

Add arn attribute to aws_ebs_volume resource and datasource #2271

Merged
merged 2 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions aws/data_source_aws_ebs_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"sort"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -22,6 +23,10 @@ func dataSourceAwsEbsVolume() *schema.Resource {
Default: false,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -98,7 +103,7 @@ func dataSourceAwsEbsVolumeRead(d *schema.ResourceData, meta interface{}) error
}

log.Printf("[DEBUG] aws_ebs_volume - Single Volume found: %s", *volume.VolumeId)
return volumeDescriptionAttributes(d, volume)
return volumeDescriptionAttributes(d, meta, volume)
}

type volumeSort []*ec2.Volume
Expand All @@ -117,9 +122,19 @@ func mostRecentVolume(volumes []*ec2.Volume) *ec2.Volume {
return sortedVolumes[len(sortedVolumes)-1]
}

func volumeDescriptionAttributes(d *schema.ResourceData, volume *ec2.Volume) error {
func volumeDescriptionAttributes(d *schema.ResourceData, meta interface{}, volume *ec2.Volume) error {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need the whole meta here 🤔 , do you mind "reducing it" to client *AWSClient?

d.SetId(*volume.VolumeId)
d.Set("volume_id", volume.VolumeId)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "ec2",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("volume/%s", d.Id()),
}
d.Set("arn", arn.String())

d.Set("availability_zone", volume.AvailabilityZone)
d.Set("encrypted", volume.Encrypted)
d.Set("iops", volume.Iops)
Expand Down
1 change: 1 addition & 0 deletions aws/data_source_aws_ebs_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAccAWSEbsVolumeDataSource_basic(t *testing.T) {
Config: testAccCheckAwsEbsVolumeDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsEbsVolumeDataSourceID("data.aws_ebs_volume.ebs_volume"),
resource.TestCheckResourceAttrSet("data.aws_ebs_volume.ebs_volume", "arn"),
resource.TestCheckResourceAttr("data.aws_ebs_volume.ebs_volume", "size", "40"),
resource.TestCheckResourceAttr("data.aws_ebs_volume.ebs_volume", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_ebs_volume.ebs_volume", "tags.Name", "External Volume"),
Expand Down
19 changes: 17 additions & 2 deletions aws/resource_aws_ebs_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"

Expand All @@ -25,6 +26,11 @@ func resourceAwsEbsVolume() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Optional: true,
Copy link
Member

Choose a reason for hiding this comment

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

Is there any particular reason why this field should also be optional?

Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -238,7 +244,7 @@ func resourceAwsEbsVolumeRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error reading EC2 volume %s: %s", d.Id(), err)
}

return readVolume(d, response.Volumes[0])
return readVolume(d, meta, response.Volumes[0])
}

func resourceAwsEbsVolumeDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -267,9 +273,18 @@ func resourceAwsEbsVolumeDelete(d *schema.ResourceData, meta interface{}) error

}

func readVolume(d *schema.ResourceData, volume *ec2.Volume) error {
func readVolume(d *schema.ResourceData, meta interface{}, volume *ec2.Volume) error {
Copy link
Member

Choose a reason for hiding this comment

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

As above, do you mind reducing the interface?

d.SetId(*volume.VolumeId)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "ec2",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("volume/%s", d.Id()),
}
d.Set("arn", arn.String())

d.Set("availability_zone", *volume.AvailabilityZone)
if volume.Encrypted != nil {
d.Set("encrypted", *volume.Encrypted)
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_ebs_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccAWSEBSVolume_basic(t *testing.T) {
Config: testAccAwsEbsVolumeConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckVolumeExists("aws_ebs_volume.test", &v),
resource.TestCheckResourceAttrSet("aws_ebs_volume.test", "arn"),
),
},
},
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/ebs_volume.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following attributes are exported:

* `id` - The volume ID (e.g. vol-59fcb34e).
* `volume_id` - The volume ID (e.g. vol-59fcb34e).
* `arn` - The volume ARN (e.g. arn:aws:ec2:us-east-1:0123456789012:volume/vol-59fcb34e).
* `availability_zone` - The AZ where the EBS volume exists.
* `encrypted` - Whether the disk is encrypted.
* `iops` - The amount of IOPS for the disk.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ebs_volume.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The following arguments are supported:
The following attributes are exported:

* `id` - The volume ID (e.g. vol-59fcb34e).
* `arn` - The volume ARN (e.g. arn:aws:ec2:us-east-1:0123456789012:volume/vol-59fcb34e).


## Import
Expand Down