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

d/elastic_beanstalk_hosted_zones - new data source #3208

Merged
merged 5 commits into from
Feb 5, 2018
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
63 changes: 63 additions & 0 deletions aws/data_source_aws_elastic_beanstalk_hosted_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package aws

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

const unsupportedElasticBeanstalkRegion = "UnsupportedRegion"
Copy link
Contributor

Choose a reason for hiding this comment

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

We can skip something like this. 😄 I'll explain below


// See # http://docs.aws.amazon.com/general/latest/gr/rande.html#elasticbeanstalk_region
var elasticBeanstalkHostedZoneIds = map[string]string{
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick: can this map be arranged alphabetically please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do. My approach was based on mirroring the ordering in the AWS docs, but given the low-frequency at which updates will be required I prefer alphabetical too.

"us-east-2": "Z14LCN19Q5QHIC",
"us-east-1": "Z117KPS5GTRQ2G",
"us-west-1": "Z1LQECGX5PH1X",
"us-west-2": "Z38NKT9BP95V3O",
"ca-central-1": "ZJFCZL7SSZB5I",
"ap-south-1": "Z18NTBI3Y7N9TZ",
"ap-northeast-2": "Z3JE5OI70TWKCP",
"ap-southeast-1": "Z16FZ9L249IFLT",
"ap-southeast-2": "Z2PCDNR3VC2G1N",
"ap-northeast-1": "Z1R25G3KIG2GBW",
"cn-northwest-1": unsupportedElasticBeanstalkRegion,
"eu-central-1": "Z1FRNW7UH4DEZJ",
"eu-west-1": "Z2NYPWQ7DFZAZH",
"eu-west-2": "Z1GKAAAUGATPF1",
"eu-west-3": "Z5WN6GAYWG5OB",
"sa-east-1": "Z10X7K2B4QSOFV",
}

func dataSourceAwsElasticBeanstalkHostedZone() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsElasticBeanstalkHostedZoneRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func dataSourceAwsElasticBeanstalkHostedZoneRead(d *schema.ResourceData, meta interface{}) error {
region := meta.(*AWSClient).region
if v, ok := d.GetOk("region"); ok {
region = v.(string)
}

zoneID := elasticBeanstalkHostedZoneIds[region]
Copy link
Contributor

Choose a reason for hiding this comment

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

While we appreciate the distinction between unknown and unsupported regions at the current moment this is being developed, I think we should simplify this so we do not need to maintain the difference in the future. Recently we switched the provider so AWS SDK updates with new regions will automatically validate them. We can add manually checking the Regions and Endpoints documentation for this EB Hosted Zone data source to the CONTRIBUTING.md New Regions section of our documentation.

zoneID, ok := elasticBeanstalkHostedZoneIds[region]
if !ok {
  return fmt.Errorf("Unsupported region: %s", region)
}


if zoneID == unsupportedElasticBeanstalkRegion {
return fmt.Errorf("Unsupported region (%q)", region)
}

if zoneID == "" {
return fmt.Errorf("Unknown region (%q)", region)
}

d.SetId(zoneID)
d.Set("region", region)
return nil
}
41 changes: 41 additions & 0 deletions aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAwsDataSourceElasticBeanstalkHostedZone(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_currentRegion,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a check for an unsupported region please?

{
  Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_region("does-not-exist"),
  ExpectError: regexp.MustCompile(`Unsupported region`),
},

Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.current", "id", "Z2PCDNR3VC2G1N"),
),
},
{
Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_sydney,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.sydney", "id", "Z2PCDNR3VC2G1N"),
),
},
},
})
}

const testAccCheckAwsElasticBeanstalkHostedZoneDataSource_currentRegion = `
provider "aws" {
region = "ap-southeast-2"
}
data "aws_elastic_beanstalk_hosted_zone" "current" {}
`

const testAccCheckAwsElasticBeanstalkHostedZoneDataSource_sydney = `
data "aws_elastic_beanstalk_hosted_zone" "sydney" {
region = "ap-southeast-2"
}
`
49 changes: 25 additions & 24 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,31 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"aws_acm_certificate": dataSourceAwsAcmCertificate(),
"aws_ami": dataSourceAwsAmi(),
"aws_ami_ids": dataSourceAwsAmiIds(),
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
"aws_billing_service_account": dataSourceAwsBillingServiceAccount(),
"aws_caller_identity": dataSourceAwsCallerIdentity(),
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
"aws_ebs_volume": dataSourceAwsEbsVolume(),
"aws_ecr_repository": dataSourceAwsEcrRepository(),
"aws_ecs_cluster": dataSourceAwsEcsCluster(),
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
"aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(),
"aws_efs_file_system": dataSourceAwsEfsFileSystem(),
"aws_efs_mount_target": dataSourceAwsEfsMountTarget(),
"aws_eip": dataSourceAwsEip(),
"aws_acm_certificate": dataSourceAwsAcmCertificate(),
"aws_ami": dataSourceAwsAmi(),
"aws_ami_ids": dataSourceAwsAmiIds(),
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
"aws_billing_service_account": dataSourceAwsBillingServiceAccount(),
"aws_caller_identity": dataSourceAwsCallerIdentity(),
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
"aws_ebs_volume": dataSourceAwsEbsVolume(),
"aws_ecr_repository": dataSourceAwsEcrRepository(),
"aws_ecs_cluster": dataSourceAwsEcsCluster(),
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
"aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(),
"aws_efs_file_system": dataSourceAwsEfsFileSystem(),
"aws_efs_mount_target": dataSourceAwsEfsMountTarget(),
"aws_eip": dataSourceAwsEip(),
"aws_elastic_beanstalk_hosted_zone": dataSourceAwsElasticBeanstalkHostedZone(),
"aws_elastic_beanstalk_solution_stack": dataSourceAwsElasticBeanstalkSolutionStack(),
"aws_elasticache_cluster": dataSourceAwsElastiCacheCluster(),
"aws_elb": dataSourceAwsElb(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<li<%= sidebar_current("docs-aws-datasource-eip") %>>
<a href="/docs/providers/aws/d/eip.html">aws_eip</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-elastic-beanstalk-hosted-zone") %>>
<a href="/docs/providers/aws/d/elastic_beanstalk_hosted_zone.html">aws_elastic_beanstalk_hosted_zone</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-elastic-beanstalk-solution-stack") %>>
<a href="/docs/providers/aws/d/elastic_beanstalk_solution_stack.html">aws_elastic_beanstalk_solution_stack</a>
</li>
Expand Down
29 changes: 29 additions & 0 deletions website/docs/d/elastic_beanstalk_hosted_zone.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: "aws"
page_title: "AWS: aws_elastic_beanstalk_hosted_zone"
sidebar_current: "docs-aws-datasource-elastic-beanstalk-hosted-zone"
description: |-
Get an elastic beanstalk hosted zone.
---

# Data Source: aws_elastic_beanstalk_hosted_zone

Use this data source to get the ID of an elastic beanstalk hosted zone.
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to use the link at the bottom? If not, could you use it please? 😄


## Example Usage

```hcl
data "aws_elastic_beanstalk_hosted_zone" "current" {}
```

## Argument Reference

* `region` - (Optional) The region you'd like the zone for. By default, fetches the current region.

## Attributes Reference

* `id` - The ID of the hosted zone.

* `region` - The region of the hosted zone.

[elastic-beanstalk-hosted-zones]: http://docs.aws.amazon.com/general/latest/gr/rande.html#elasticbeanstalk_region "AWS Elastic Beanstalk Hosted Zones documentation"