From 32019fa0085023a9251b391670e7455458b6551c Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 29 Jun 2016 17:57:36 +0100 Subject: [PATCH] provider/aws: Add dns_name to aws_efs_mount_target --- .../aws/resource_aws_efs_mount_target.go | 29 +++++++++++++++++++ .../aws/resource_aws_efs_mount_target_test.go | 16 ++++++++++ .../aws/r/efs_mount_target.html.markdown | 3 ++ 3 files changed, 48 insertions(+) diff --git a/builtin/providers/aws/resource_aws_efs_mount_target.go b/builtin/providers/aws/resource_aws_efs_mount_target.go index f1d03398c..de15ac760 100644 --- a/builtin/providers/aws/resource_aws_efs_mount_target.go +++ b/builtin/providers/aws/resource_aws_efs_mount_target.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/efs" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -51,6 +52,10 @@ func resourceAwsEfsMountTarget() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "dns_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -163,9 +168,33 @@ func resourceAwsEfsMountTargetRead(d *schema.ResourceData, meta interface{}) err d.Set("security_groups", schema.NewSet(schema.HashString, flattenStringList(sgResp.SecurityGroups))) + // DNS name per http://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html + az, err := getAzFromSubnetId(*mt.SubnetId, meta.(*AWSClient).ec2conn) + if err != nil { + return fmt.Errorf("Failed getting AZ from subnet ID (%s): %s", *mt.SubnetId, err) + } + region := meta.(*AWSClient).region + d.Set("dns_name", fmt.Sprintf("%s.%s.efs.%s.amazonaws.com", az, *mt.FileSystemId, region)) + return nil } +func getAzFromSubnetId(subnetId string, conn *ec2.EC2) (string, error) { + input := ec2.DescribeSubnetsInput{ + SubnetIds: []*string{aws.String(subnetId)}, + } + out, err := conn.DescribeSubnets(&input) + if err != nil { + return "", err + } + + if len(out.Subnets) != 1 { + return "", fmt.Errorf("Expected exactly 1 subnet returned for %q", subnetId) + } + + return *out.Subnets[0].AvailabilityZone, nil +} + func resourceAwsEfsMountTargetDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn diff --git a/builtin/providers/aws/resource_aws_efs_mount_target_test.go b/builtin/providers/aws/resource_aws_efs_mount_target_test.go index bd773bc9a..e93357b33 100644 --- a/builtin/providers/aws/resource_aws_efs_mount_target_test.go +++ b/builtin/providers/aws/resource_aws_efs_mount_target_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -24,6 +25,11 @@ func TestAccAWSEFSMountTarget_basic(t *testing.T) { testAccCheckEfsMountTarget( "aws_efs_mount_target.alpha", ), + resource.TestMatchResourceAttr( + "aws_efs_mount_target.alpha", + "dns_name", + regexp.MustCompile("^us-west-2a.[^.]+.efs.us-west-2.amazonaws.com$"), + ), ), }, resource.TestStep{ @@ -32,9 +38,19 @@ func TestAccAWSEFSMountTarget_basic(t *testing.T) { testAccCheckEfsMountTarget( "aws_efs_mount_target.alpha", ), + resource.TestMatchResourceAttr( + "aws_efs_mount_target.alpha", + "dns_name", + regexp.MustCompile("^us-west-2a.[^.]+.efs.us-west-2.amazonaws.com$"), + ), testAccCheckEfsMountTarget( "aws_efs_mount_target.beta", ), + resource.TestMatchResourceAttr( + "aws_efs_mount_target.beta", + "dns_name", + regexp.MustCompile("^us-west-2b.[^.]+.efs.us-west-2.amazonaws.com$"), + ), ), }, }, diff --git a/website/source/docs/providers/aws/r/efs_mount_target.html.markdown b/website/source/docs/providers/aws/r/efs_mount_target.html.markdown index 724316c5e..b54ff9730 100644 --- a/website/source/docs/providers/aws/r/efs_mount_target.html.markdown +++ b/website/source/docs/providers/aws/r/efs_mount_target.html.markdown @@ -41,7 +41,10 @@ The following arguments are supported: ## Attributes Reference +~> **Note:** The `dns_name` attribute is only useful if the mount target is in a VPC with `enable_dns_hostnames = true`. + The following attributes are exported: * `id` - The ID of the mount target +* `dns_name` - The DNS name for the given subnet/AZ per [documented convention](http://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html) * `network_interface_id` - The ID of the network interface that Amazon EFS created when it created the mount target.