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/Adds a url_suffix attribute to data_source_aws_partition #5602

Merged
merged 3 commits into from
Aug 7, 2019
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
33 changes: 33 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type AWSClient struct {
redshiftconn *redshift.Redshift
r53conn *route53.Route53
partition string
dnsSuffix string
accountid string
supportedplatforms []string
region string
Expand Down Expand Up @@ -458,6 +459,20 @@ func (c *Config) Client() (interface{}, error) {
}
}

// Infer URL Suffix from STS Endpoint
resolver := endpoints.DefaultResolver()
endpoint, err := resolver.EndpointFor(endpoints.StatesServiceID, client.region)
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment notes inferring this information from the STS endpoint, however its using the states service.

That said, the AWS Go SDK documentation lists the XXXServiceID constants as deprecated: https://docs.aws.amazon.com/sdk-for-go/api/aws/endpoints/#pkg-constants

Deprecated: Use client package's EndpointsID value instead of these ServiceIDs. These IDs are not maintained, and are out of date.

This should be swapped out according to that deprecation.

Ideally though, we should fetch this information directly from the AWS Go SDK endpoints information first, then only fallback to parsing an endpoint should that fail since endpoint hostnames may not always follow a consistent implementation. I have submitted an upstream feature request in the AWS to directly provide the DNS suffix information, which can help prevent us from hardcoding these cases in the future: aws/aws-sdk-go#2710

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one I'm not sure when I'll have time to research and figure out the alternative. If anyone wants to take it, please be my guest!

if err != nil {
return nil, fmt.Errorf("Failed to resolve endpoint. Errors: %s", err)
}

dnsSuffix, err := parseDNSSuffixFromEndpoint(endpoint.URL, client.region)
if err != nil {
return nil, fmt.Errorf("Failed to resolve URL Suffix. Errors: %s", err)
}

client.dnsSuffix = aws.StringValue(dnsSuffix)

client.ec2conn = ec2.New(awsEc2Sess)

if !c.SkipGetEC2Platforms {
Expand Down Expand Up @@ -610,6 +625,24 @@ func (c *Config) Client() (interface{}, error) {
return &client, nil
}

func parseDNSSuffixFromEndpoint(endpoint string, region string) (*string, error) {
e := strings.Split(endpoint, ".")

// Expect endpoints with at least three elements
// {service}.{optional region}.{one or more zones}.{root}
if len(e) < 2 {
return nil, fmt.Errorf("Could not determine URL Suffix from endpoint '%s'", endpoint)
}

// An endpoint may have the region in the second element
if e[1] == region {
return aws.String(strings.Join(e[2:], ".")), nil
}

// Without a region, the URL Suffix is everything past the first element
return aws.String(strings.Join(e[1:], ".")), nil
}

func hasEc2Classic(platforms []string) bool {
for _, p := range platforms {
if p == "EC2" {
Expand Down
7 changes: 7 additions & 0 deletions aws/data_source_aws_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func dataSourceAwsPartition() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"dns_suffix": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -29,5 +33,8 @@ func dataSourceAwsPartitionRead(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Setting AWS Partition to %s.", client.partition)
d.Set("partition", meta.(*AWSClient).partition)

log.Printf("[DEBUG] Setting AWS URL Suffix to %s.", client.dnsSuffix)
d.Set("dns_suffix", meta.(*AWSClient).dnsSuffix)

return nil
}
21 changes: 21 additions & 0 deletions aws/data_source_aws_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAccAWSPartition_basic(t *testing.T) {
Config: testAccCheckAwsPartitionConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsPartition("data.aws_partition.current"),
testAccCheckAwsDnsSuffix("data.aws_partition.current"),
),
},
},
Expand All @@ -39,6 +40,26 @@ func testAccCheckAwsPartition(n string) resource.TestCheckFunc {
}
}

func testAccCheckAwsDnsSuffix(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find resource: %s", n)
}

expected := testAccProvider.Meta().(*AWSClient).dnsSuffix
if rs.Primary.Attributes["dns_suffix"] != expected {
return fmt.Errorf("Incorrect DNS Suffix: expected %q, got %q", expected, rs.Primary.Attributes["dns_suffix"])
}

if rs.Primary.Attributes["dns_suffix"] == "" {
return fmt.Errorf("DNS Suffix expected to not be nil")
}

return nil
}
}

const testAccCheckAwsPartitionConfig_basic = `
data "aws_partition" "current" { }
`
6 changes: 4 additions & 2 deletions website/docs/d/partition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ description: |-

# Data Source: aws_partition

Use this data source to lookup current AWS partition in which Terraform is working
Use this data source to lookup information about the current AWS partition in
which Terraform is working.

## Example Usage

Expand Down Expand Up @@ -36,4 +37,5 @@ There are no arguments available for this data source.

## Attributes Reference

`partition` is set to the identifier of the current partition.
* `partition` is set to the identifier of the current partition.
* `dns_suffix` is set to the base DNS domain name for the current partition (e.g. `amazonaws.com` in AWS Commercial, `amazonaws.com.cn` in AWS China).