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

feat: implement apprunner_hosted_zone_id #36288

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/36288.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_apprunner_hosted_zone_id
```
1 change: 1 addition & 0 deletions docs/add-a-new-region.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ Some data sources include static values specific to regions that are not availab
- Check [Amazon Simple Storage Service endpoints and quotas](https://docs.aws.amazon.com/general/latest/gr/s3.html#s3_website_region_endpoints) and add Route53 Hosted Zone ID if available to [`internal/service/s3/hosted_zones.go`](https://github.com/hashicorp/terraform-provider-aws/tree/main/internal/service/s3/hosted_zones.go)
- Check [AWS Elastic Beanstalk endpoints and quotas](https://docs.aws.amazon.com/general/latest/gr/elasticbeanstalk.html) and add Route53 Hosted Zone ID if available to [`internal/service/elasticbeanstalk/hosted_zone_data_source.go`](https://github.com/hashicorp/terraform-provider-aws/tree/main/internal/service/elasticbeanstalk/hosted_zone_data_source.go)
- Check [SageMaker docs](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-algo-docker-registry-paths.html) and add AWS Account IDs if available to [`internal/service/sagemaker/prebuilt_ecr_image_data_source.go`](https://github.com/hashicorp/terraform-provider-aws/tree/main/internal/service/sagemaker/prebuilt_ecr_image_data_source.go)
- Check [App Runner docs](https://docs.aws.amazon.com/general/latest/gr/apprunner.html#apprunner_region) and add Route53 Hosted Zone ID if available to [`internal/service/apprunner/hosted_zone_id_data_source.go`](https://github.com/hashicorp/terraform-provider-aws/tree/main/internal/service/apprunner/hosted_zone_id_data_source.go)
88 changes: 88 additions & 0 deletions internal/service/apprunner/hosted_zone_id_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package apprunner

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/names"
)

// See https://docs.aws.amazon.com/general/latest/gr/apprunner.html

var hostedZoneIDPerRegionMap = map[string]string{
endpoints.UsEast2RegionID: "Z0224347AD7KVHMLOX31",
endpoints.UsEast1RegionID: "Z01915732ZBZKC8D32TPT",
endpoints.UsWest2RegionID: "Z02243383FTQ64HJ5772Q",
endpoints.ApSouth1RegionID: "Z00855883LBHKTIC4ODF2",
endpoints.ApSoutheast1RegionID: "Z09819469CZ3KQ8PWMCL",
endpoints.ApSoutheast2RegionID: "Z03657752RA8799S0TI5I",
endpoints.ApNortheast1RegionID: "Z08491812XW6IPYLR6CCA",
endpoints.EuCentral1RegionID: "Z0334911C2FDI2Q9M4FZ",
endpoints.EuWest1RegionID: "Z087551914Z2PCAU0QHMW",
endpoints.EuWest2RegionID: "Z098228427VC6B3IX76ON",
endpoints.EuWest3RegionID: "Z087117439MBKHYM69QS6",
}

// @FrameworkDataSource(name="Hosted Zone ID")
func newHostedZoneIDDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
return &hostedZoneIDDataSource{}, nil
}

type hostedZoneIDDataSource struct {
framework.DataSourceWithConfigure
}

func (d *hostedZoneIDDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) {
response.TypeName = "aws_apprunner_hosted_zone_id"
}

func (d *hostedZoneIDDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrID: framework.IDAttribute(),
"region": schema.StringAttribute{
Optional: true,
Computed: true,
},
},
}
}

func (d *hostedZoneIDDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var data hostedZoneIDDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

var region string
if data.Region.IsNull() {
region = d.Meta().Region
} else {
region = data.Region.ValueString()
}

if zoneID, ok := hostedZoneIDPerRegionMap[region]; ok {
data.ID = types.StringValue(zoneID)
data.Region = types.StringValue(region)
} else {
response.Diagnostics.AddError("unsupported AWS Region", fmt.Sprintf("region %s is currently not supported", region))

return
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

type hostedZoneIDDataSourceModel struct {
ID types.String `tfsdk:"id"`
Region types.String `tfsdk:"region"`
}
54 changes: 54 additions & 0 deletions internal/service/apprunner/hosted_zone_id_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package apprunner_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccAppRunnerHostedZoneIDDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
datasourceName := "data.aws_apprunner_hosted_zone_id.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.AppRunner)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.AppRunner),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccHostedZoneIDDataSourceConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(datasourceName, "id"),
),
},
{
Config: testAccHostedZoneIDDataSourceConfig_explicitRegion(names.APNortheast1RegionID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "id", "Z08491812XW6IPYLR6CCA"),
),
},
},
})
}

const testAccHostedZoneIDDataSourceConfig_basic = `
data "aws_apprunner_hosted_zone_id" "test" {}
`

func testAccHostedZoneIDDataSourceConfig_explicitRegion(region string) string {
return fmt.Sprintf(`
data "aws_apprunner_hosted_zone_id" "test" {
region = %[1]q
}
`, region)
}
7 changes: 6 additions & 1 deletion internal/service/apprunner/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions website/docs/d/apprunner_hosted_zone_id.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "App Runner"
layout: "aws"
page_title: "AWS: aws_apprunner_hosted_zone_id"
description: |-
Get AWS App Runner Hosted Zone Id
---

# Data Source: aws_apprunner_hosted_zone_id

Use this data source to get the HostedZoneId of an AWS App Runner service deployed
in a given region for the purpose of using it in an AWS Route53 Alias record.

## Example Usage

```terraform
data "aws_apprunner_hosted_zone_id" "main" {}

resource "aws_route53_record" "www" {
zone_id = aws_route53_zone.primary.zone_id
name = "example.com"
type = "A"

alias {
name = aws_apprunner_custom_domain_association.main.dns_target
zone_id = data.aws_apprunner_hosted_zone_id.main.id
evaluate_target_health = true
}
}
```

## Argument Reference

* `region` - (Optional) Name of the region whose AWS App Runner service HostedZoneId is desired.
Defaults to the region from the AWS provider configuration.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `id` - ID of the AWS App Runner service HostedZoneId in the selected region.
Loading