From 770f3605d0f5608c634e742f997354c8b1b859bb Mon Sep 17 00:00:00 2001 From: Stuart Auld Date: Wed, 31 Jan 2018 18:24:16 +1100 Subject: [PATCH 1/5] add a data source for elasticbeanstalk hosted zones --- ...ource_aws_elastic_beanstalk_hosted_zone.go | 63 +++++++++++++++++++ ..._aws_elastic_beanstalk_hosted_zone_test.go | 41 ++++++++++++ aws/provider.go | 49 ++++++++------- website/aws.erb | 3 + ...lastic_beanstalk_hosted_zone.html.markdown | 29 +++++++++ 5 files changed, 161 insertions(+), 24 deletions(-) create mode 100644 aws/data_source_aws_elastic_beanstalk_hosted_zone.go create mode 100644 aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go create mode 100644 website/docs/d/elastic_beanstalk_hosted_zone.html.markdown diff --git a/aws/data_source_aws_elastic_beanstalk_hosted_zone.go b/aws/data_source_aws_elastic_beanstalk_hosted_zone.go new file mode 100644 index 00000000000..6ed6c9c2f6f --- /dev/null +++ b/aws/data_source_aws_elastic_beanstalk_hosted_zone.go @@ -0,0 +1,63 @@ +package aws + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +const unsupportedElasticBeanstalkRegion = "UnsupportedRegion" + +// See # http://docs.aws.amazon.com/general/latest/gr/rande.html#elasticbeanstalk_region +var elasticBeanstalkHostedZoneIds = map[string]string{ + "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] + + 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 +} diff --git a/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go b/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go new file mode 100644 index 00000000000..60a2fd4e4e1 --- /dev/null +++ b/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go @@ -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, + 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" +} +` diff --git a/aws/provider.go b/aws/provider.go index 4291b167d20..cfee7e35b8f 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/website/aws.erb b/website/aws.erb index ae9d15d7924..3fead1f0048 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -95,6 +95,9 @@ > aws_eip + > + aws_elastic_beanstalk_hosted_zone + > aws_elastic_beanstalk_solution_stack diff --git a/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown b/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown new file mode 100644 index 00000000000..f5e7c2f6adb --- /dev/null +++ b/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown @@ -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. + +## 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" From 38e5f8710a1294a406786bfeb5780590f0c0c8ef Mon Sep 17 00:00:00 2001 From: Stuart Auld Date: Tue, 6 Feb 2018 07:34:07 +1100 Subject: [PATCH 2/5] remove unsupported region support, lexically sort region map --- ...ource_aws_elastic_beanstalk_hosted_zone.go | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/aws/data_source_aws_elastic_beanstalk_hosted_zone.go b/aws/data_source_aws_elastic_beanstalk_hosted_zone.go index 6ed6c9c2f6f..078f4968cc4 100644 --- a/aws/data_source_aws_elastic_beanstalk_hosted_zone.go +++ b/aws/data_source_aws_elastic_beanstalk_hosted_zone.go @@ -6,26 +6,23 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -const unsupportedElasticBeanstalkRegion = "UnsupportedRegion" - // See # http://docs.aws.amazon.com/general/latest/gr/rande.html#elasticbeanstalk_region var elasticBeanstalkHostedZoneIds = map[string]string{ - "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, + "ap-northeast-2": "Z3JE5OI70TWKCP", + "ap-south-1": "Z18NTBI3Y7N9TZ", + "ca-central-1": "ZJFCZL7SSZB5I", "eu-central-1": "Z1FRNW7UH4DEZJ", "eu-west-1": "Z2NYPWQ7DFZAZH", "eu-west-2": "Z1GKAAAUGATPF1", "eu-west-3": "Z5WN6GAYWG5OB", "sa-east-1": "Z10X7K2B4QSOFV", + "us-east-1": "Z117KPS5GTRQ2G", + "us-east-2": "Z14LCN19Q5QHIC", + "us-west-1": "Z1LQECGX5PH1X", + "us-west-2": "Z38NKT9BP95V3O", } func dataSourceAwsElasticBeanstalkHostedZone() *schema.Resource { @@ -49,12 +46,8 @@ func dataSourceAwsElasticBeanstalkHostedZoneRead(d *schema.ResourceData, meta in zoneID := elasticBeanstalkHostedZoneIds[region] - if zoneID == unsupportedElasticBeanstalkRegion { - return fmt.Errorf("Unsupported region (%q)", region) - } - if zoneID == "" { - return fmt.Errorf("Unknown region (%q)", region) + return fmt.Errorf("Unknown region or elasticbeanstalk not supported (%q)", region) } d.SetId(zoneID) From f5c1a8cce6da196b284e9851327e2f793bb30483 Mon Sep 17 00:00:00 2001 From: Stuart Auld Date: Tue, 6 Feb 2018 07:44:00 +1100 Subject: [PATCH 3/5] Elastic Beanstalk Hosted Zone docs update --- .github/CONTRIBUTING.md | 9 +++++---- .../docs/d/elastic_beanstalk_hosted_zone.html.markdown | 4 +--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 71370a7da86..ee52a7b407b 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -264,6 +264,7 @@ manually sourced values from documentation. - [ ] Check [CloudTrail Supported Regions docs](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-supported-regions.html) and add AWS Account ID if available to `aws/data_source_aws_cloudtrail_service_account.go` - [ ] Check [Elastic Load Balancing Access Logs docs](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-access-logs.html#attach-bucket-policy) and add Elastic Load Balancing Account ID if available to `aws/data_source_aws_elb_service_account.go` - [ ] Check [Redshift Database Audit Logging docs](https://docs.aws.amazon.com/redshift/latest/mgmt/db-auditing.html) and add AWS Account ID if available to `aws/data_source_aws_redshift_service_account.go` + - [ ] Check [Regions and Endpoints Elastic Beanstalk](https://docs.aws.amazon.com/general/latest/gr/rande.html#elasticbeanstalk_region) and add Route53 Hosted Zone ID if available to `aws/data_source_aws_elastic_beanstalk_hosted_zone.go`] #### Terraform Schema and Code Idiosyncracies @@ -279,17 +280,17 @@ and style - [ ] __`Computed`__: The `Computed` attribute is generally used in isolation for any IDs or anything not defined in the config and returned by the API. - [ ] __`Computed` with `Optional`__: The `Computed` attribute is generally used - in conjunction with `Optional` when the API automatically sets unpredictable - default value or when the value is generally not static and depends on other + in conjunction with `Optional` when the API automatically sets unpredictable + default value or when the value is generally not static and depends on other attributes. - - [ ] __Spelling__: When referencing reosources in the AWS API, use spelling which + - [ ] __Spelling__: When referencing resources in the AWS API, use spelling which matches that of official AWS documentation. In all other cases, use American spelling for variables, functions, and constants. - [ ] __Removed Resources__: If a resource is removed from AWS outside of Terraform (e.g. via different tool, API or web UI), make sure to catch this case. Print a `[WARN]` log message, and use `d.SetId("")` to remove the resource from state inside `Read()`. - + ### Writing Acceptance Tests diff --git a/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown b/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown index f5e7c2f6adb..c2c94791c6f 100644 --- a/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown +++ b/website/docs/d/elastic_beanstalk_hosted_zone.html.markdown @@ -8,7 +8,7 @@ description: |- # Data Source: aws_elastic_beanstalk_hosted_zone -Use this data source to get the ID of an elastic beanstalk hosted zone. +Use this data source to get the ID of an [elastic beanstalk hosted zone](http://docs.aws.amazon.com/general/latest/gr/rande.html#elasticbeanstalk_region). ## Example Usage @@ -25,5 +25,3 @@ data "aws_elastic_beanstalk_hosted_zone" "current" {} * `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" From 67d8858d274e2b18777f4eb5ca60fa3dd67ab015 Mon Sep 17 00:00:00 2001 From: Stuart Auld Date: Tue, 6 Feb 2018 07:54:31 +1100 Subject: [PATCH 4/5] added tests for eu-west-1 and ss-pluto-1 --- ..._aws_elastic_beanstalk_hosted_zone_test.go | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go b/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go index 60a2fd4e4e1..3d8169fea59 100644 --- a/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go +++ b/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go @@ -1,6 +1,8 @@ package aws import ( + "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" @@ -18,11 +20,21 @@ func TestAccAwsDataSourceElasticBeanstalkHostedZone(t *testing.T) { ), }, { - Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_sydney, + Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion("ap-southeast-2"), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.sydney", "id", "Z2PCDNR3VC2G1N"), + resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.test", "id", "Z2PCDNR3VC2G1N"), ), }, + { + Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion("eu-west-1"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.test", "id", "Z2NYPWQ7DFZAZH"), + ), + }, + { + Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion("ss-pluto-1"), + ExpectError: regexp.MustCompile("Unknown region or elasticbeanstalk not supported"), + }, }, }) } @@ -34,8 +46,10 @@ provider "aws" { data "aws_elastic_beanstalk_hosted_zone" "current" {} ` -const testAccCheckAwsElasticBeanstalkHostedZoneDataSource_sydney = ` -data "aws_elastic_beanstalk_hosted_zone" "sydney" { - region = "ap-southeast-2" +func testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion(r string) string { + return fmt.Sprintf(` +data "aws_elastic_beanstalk_hosted_zone" "test" { + region = "%s" +} +`, r) } -` From 2dfb5529b63862b1a35d2c474751fb7b7de7e24a Mon Sep 17 00:00:00 2001 From: Stuart Auld Date: Tue, 6 Feb 2018 07:59:08 +1100 Subject: [PATCH 5/5] PR feedback --- aws/data_source_aws_elastic_beanstalk_hosted_zone.go | 6 +++--- aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/data_source_aws_elastic_beanstalk_hosted_zone.go b/aws/data_source_aws_elastic_beanstalk_hosted_zone.go index 078f4968cc4..d0f209eae6b 100644 --- a/aws/data_source_aws_elastic_beanstalk_hosted_zone.go +++ b/aws/data_source_aws_elastic_beanstalk_hosted_zone.go @@ -44,10 +44,10 @@ func dataSourceAwsElasticBeanstalkHostedZoneRead(d *schema.ResourceData, meta in region = v.(string) } - zoneID := elasticBeanstalkHostedZoneIds[region] + zoneID, ok := elasticBeanstalkHostedZoneIds[region] - if zoneID == "" { - return fmt.Errorf("Unknown region or elasticbeanstalk not supported (%q)", region) + if !ok { + return fmt.Errorf("Unsupported region: %s", region) } d.SetId(zoneID) diff --git a/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go b/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go index 3d8169fea59..23703a6ca84 100644 --- a/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go +++ b/aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go @@ -33,7 +33,7 @@ func TestAccAwsDataSourceElasticBeanstalkHostedZone(t *testing.T) { }, { Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion("ss-pluto-1"), - ExpectError: regexp.MustCompile("Unknown region or elasticbeanstalk not supported"), + ExpectError: regexp.MustCompile("Unsupported region"), }, }, })