Skip to content

Commit

Permalink
Merge pull request #19647 from kumadee/add-data-globalaccelerator-acc…
Browse files Browse the repository at this point in the history
…elerator

d/aws_globalaccelerator_accelerator: Add data globalaccelerator accelerator
  • Loading branch information
ewbankkit committed Jun 9, 2021
2 parents eddcaff + e316d59 commit 3e886e7
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/19647.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_globalaccelerator_accelerator
```
153 changes: 153 additions & 0 deletions aws/data_source_aws_globalaccelerator_accelerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/globalaccelerator"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/globalaccelerator/finder"
)

func dataSourceAwsGlobalAcceleratorAccelerator() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsGlobalAcceleratorAcceleratorRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ip_address_type": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"dns_name": {
Type: schema.TypeString,
Computed: true,
},
"hosted_zone_id": {
Type: schema.TypeString,
Computed: true,
},
"ip_sets": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ip_family": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"attributes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flow_logs_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"flow_logs_s3_bucket": {
Type: schema.TypeString,
Computed: true,
},
"flow_logs_s3_prefix": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsGlobalAcceleratorAcceleratorRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).globalacceleratorconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

var results []*globalaccelerator.Accelerator

err := conn.ListAcceleratorsPages(&globalaccelerator.ListAcceleratorsInput{}, func(page *globalaccelerator.ListAcceleratorsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, l := range page.Accelerators {
if l == nil {
continue
}

if v, ok := d.GetOk("arn"); ok && v.(string) != aws.StringValue(l.AcceleratorArn) {
continue
}

if v, ok := d.GetOk("name"); ok && v.(string) != aws.StringValue(l.Name) {
continue
}

results = append(results, l)
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error reading AWS Global Accelerator: %w", err)
}

if len(results) != 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(results))
}

accelerator := results[0]
d.SetId(aws.StringValue(accelerator.AcceleratorArn))
d.Set("arn", accelerator.AcceleratorArn)
d.Set("enabled", accelerator.Enabled)
d.Set("dns_name", accelerator.DnsName)
d.Set("hosted_zone_id", globalAcceleratorRoute53ZoneID)
d.Set("name", accelerator.Name)
d.Set("ip_address_type", accelerator.IpAddressType)
d.Set("ip_sets", flattenGlobalAcceleratorIpSets(accelerator.IpSets))

acceleratorAttributes, err := finder.AcceleratorAttributesByARN(conn, d.Id())
if err != nil {
return fmt.Errorf("error reading Global Accelerator Accelerator (%s) attributes: %w", d.Id(), err)
}

if err := d.Set("attributes", []interface{}{flattenGlobalAcceleratorAcceleratorAttributes(acceleratorAttributes)}); err != nil {
return fmt.Errorf("error setting attributes: %w", err)
}

tags, err := keyvaluetags.GlobalacceleratorListTags(conn, d.Id())
if err != nil {
return fmt.Errorf("error listing tags for Global Accelerator Accelerator (%s): %w", d.Id(), err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}
return nil
}
79 changes: 79 additions & 0 deletions aws/data_source_aws_globalaccelerator_accelerator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/globalaccelerator"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSDataGlobalAcceleratorAccelerator_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_globalaccelerator_accelerator.test"
dataSourceName := "data.aws_globalaccelerator_accelerator.test_by_arn"
dataSourceName2 := "data.aws_globalaccelerator_accelerator.test_by_name"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckGlobalAccelerator(t) },
ErrorCheck: testAccErrorCheck(t, globalaccelerator.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSGlobalAcceleratorAcceleratorConfigWithDataSource(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "attributes.#", resourceName, "attributes.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "attributes.0.flow_logs_enabled", resourceName, "attributes.0.flow_logs_enabled"),
resource.TestCheckResourceAttrPair(dataSourceName, "attributes.0.flow_logs_s3_bucket", resourceName, "attributes.0.flow_logs_s3_bucket"),
resource.TestCheckResourceAttrPair(dataSourceName, "attributes.0.flow_logs_s3_prefix", resourceName, "attributes.0.flow_logs_s3_prefix"),
resource.TestCheckResourceAttrPair(dataSourceName, "dns_name", resourceName, "dns_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "enabled", resourceName, "enabled"),
resource.TestCheckResourceAttrPair(dataSourceName, "hosted_zone_id", resourceName, "hosted_zone_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_address_type", resourceName, "ip_address_type"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_sets.#", resourceName, "ip_sets.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_sets.0.ip_addresses.#", resourceName, "ip_sets.0.ip_addresses.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_sets.0.ip_addresses.0", resourceName, "ip_sets.0.ip_addresses.0"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_sets.0.ip_addresses.1", resourceName, "ip_sets.0.ip_addresses.1"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_sets.0.ip_family", resourceName, "ip_sets.0.ip_family"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName2, "attributes.#", resourceName, "attributes.#"),
resource.TestCheckResourceAttrPair(dataSourceName2, "attributes.0.flow_logs_enabled", resourceName, "attributes.0.flow_logs_enabled"),
resource.TestCheckResourceAttrPair(dataSourceName2, "attributes.0.flow_logs_s3_bucket", resourceName, "attributes.0.flow_logs_s3_bucket"),
resource.TestCheckResourceAttrPair(dataSourceName2, "attributes.0.flow_logs_s3_prefix", resourceName, "attributes.0.flow_logs_s3_prefix"),
resource.TestCheckResourceAttrPair(dataSourceName2, "dns_name", resourceName, "dns_name"),
resource.TestCheckResourceAttrPair(dataSourceName2, "enabled", resourceName, "enabled"),
resource.TestCheckResourceAttrPair(dataSourceName2, "hosted_zone_id", resourceName, "hosted_zone_id"),
resource.TestCheckResourceAttrPair(dataSourceName2, "ip_address_type", resourceName, "ip_address_type"),
resource.TestCheckResourceAttrPair(dataSourceName2, "ip_sets.#", resourceName, "ip_sets.#"),
resource.TestCheckResourceAttrPair(dataSourceName2, "ip_sets.0.ip_addresses.#", resourceName, "ip_sets.0.ip_addresses.#"),
resource.TestCheckResourceAttrPair(dataSourceName2, "ip_sets.0.ip_addresses.0", resourceName, "ip_sets.0.ip_addresses.0"),
resource.TestCheckResourceAttrPair(dataSourceName2, "ip_sets.0.ip_addresses.1", resourceName, "ip_sets.0.ip_addresses.1"),
resource.TestCheckResourceAttrPair(dataSourceName2, "ip_sets.0.ip_family", resourceName, "ip_sets.0.ip_family"),
resource.TestCheckResourceAttrPair(dataSourceName2, "name", resourceName, "name"),
),
},
},
})
}

func testAccAWSGlobalAcceleratorAcceleratorConfigWithDataSource(rName string) string {
return fmt.Sprintf(`
resource "aws_globalaccelerator_accelerator" "test" {
name = %[1]q
attributes {
flow_logs_enabled = false
flow_logs_s3_bucket = ""
flow_logs_s3_prefix = "flow-logs/globalaccelerator/"
}
}
data "aws_globalaccelerator_accelerator" "test_by_arn" {
arn = aws_globalaccelerator_accelerator.test.id
}
data "aws_globalaccelerator_accelerator" "test_by_name" {
name = aws_globalaccelerator_accelerator.test.name
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func Provider() *schema.Provider {
"aws_elasticache_replication_group": dataSourceAwsElasticacheReplicationGroup(),
"aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
"aws_globalaccelerator_accelerator": dataSourceAwsGlobalAcceleratorAccelerator(),
"aws_glue_connection": dataSourceAwsGlueConnection(),
"aws_glue_data_catalog_encryption_settings": dataSourceAwsGlueDataCatalogEncryptionSettings(),
"aws_glue_script": dataSourceAwsGlueScript(),
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/globalaccelerator_accelerator.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
subcategory: "Global Accelerator"
layout: "aws"
page_title: "AWS: aws_globalaccelerator_accelerator"
description: |-
Provides a Global Accelerator accelerator data source.
---

# Data Source: aws_globalaccelerator_accelerator

Provides information about a Global Accelerator accelerator.

## Example Usage

```terraform
variable "accelerator_arn" {
type = string
default = ""
}
variable "accelerator_name" {
type = string
default = ""
}
data "aws_globalaccelerator_accelerator" "example" {
arn = var.accelerator_arn
name = var.accelerator_name
}
```

## Argument Reference

The following arguments are supported:

* `arn` - (Optional) The full ARN of the Global Accelerator.
* `name` - (Optional) The unique name of the Global Accelerator.

~> **NOTE**: When both `arn` and `name` are specified, `arn` takes precedence.

## Attributes Reference

website/docs/r/globalaccelerator_accelerator.markdown
See the [`aws_globalaccelerator_accelerator` resource](/docs/providers/aws/r/globalaccelerator_accelerator.html) for details on the
returned attributes - they are identical.

0 comments on commit 3e886e7

Please sign in to comment.