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

Add Outpost Local Gateway support #12764

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

import (
"fmt"
"log"

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

func dataSourceAwsLocalGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLocalGatewayRead,

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

"outpost_arn": {
Type: schema.TypeString,
Computed: true,
},

"filter": ec2CustomFiltersSchema(),

"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"tags": tagsSchemaComputed(),

"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsLocalGatewayRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeLocalGatewaysInput{}

var id string
if cid, ok := d.GetOk("id"); ok {
id = cid.(string)
}

if id != "" {
req.LocalGatewayIds = []*string{aws.String(id)}
}

req.Filters = buildEC2AttributeFilterList(
map[string]string{
"state": d.Get("state").(string),
},
)

if tags, tagsOk := d.GetOk("tags"); tagsOk {
req.Filters = append(req.Filters, buildEC2TagFilterList(
keyvaluetags.New(tags.(map[string]interface{})).Ec2Tags(),
)...)
}

req.Filters = append(req.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)
if len(req.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
req.Filters = nil
}

log.Printf("[DEBUG] Reading AWS LOCAL GATEWAY: %s", req)
resp, err := conn.DescribeLocalGateways(req)
if err != nil {
return err
}
if resp == nil || len(resp.LocalGateways) == 0 {
return fmt.Errorf("no matching Local Gateway found")
}
if len(resp.LocalGateways) > 1 {
return fmt.Errorf("multiple Local Gateways matched; use additional constraints to reduce matches to a single Local Gateway")
}

localGateway := resp.LocalGateways[0]

d.SetId(aws.StringValue(localGateway.LocalGatewayId))
d.Set("outpost_arn", localGateway.OutpostArn)
d.Set("state", localGateway.State)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(localGateway.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("owner_id", localGateway.OwnerId)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "outposts",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("outpost/%s", d.Id()),
}.String()
d.Set("arn", arn)

return nil
}
45 changes: 45 additions & 0 deletions aws/data_source_aws_local_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aws

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceAwsLocalGateway_basic(t *testing.T) {
dsResourceName := "data.aws_local_gateway.by_id"

localGatewayId := os.Getenv("AWS_LOCAL_GATEWAY_ID")
if localGatewayId == "" {
t.Skip(
"Environment variable AWS_LOCAL_GATEWAY_ID is not set. " +
"This environment variable must be set to the ID of " +
"a deployed Local Gateway to enable this test.")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsLocalGatewayConfig(localGatewayId),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dsResourceName, "id", localGatewayId),
testAccCheckResourceAttrAccountID(dsResourceName, "owner_id"),
resource.TestCheckResourceAttrSet(dsResourceName, "state"),
resource.TestCheckResourceAttrSet(dsResourceName, "outpost_arn"),
),
},
},
})
}

func testAccDataSourceAwsLocalGatewayConfig(localGatewayId string) string {
return fmt.Sprintf(`
data "aws_local_gateway" "by_id" {
id = "%s"
}
`, localGatewayId)
}
75 changes: 75 additions & 0 deletions aws/data_source_aws_local_gateways.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package aws

import (
"fmt"
"log"
"time"

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

func dataSourceAwsLocalGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLocalGatewaysRead,
Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),

"tags": tagsSchemaComputed(),

"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceAwsLocalGatewaysRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeLocalGatewaysInput{}

if tags, tagsOk := d.GetOk("tags"); tagsOk {
req.Filters = append(req.Filters, buildEC2TagFilterList(
keyvaluetags.New(tags.(map[string]interface{})).Ec2Tags(),
)...)
}

if filters, filtersOk := d.GetOk("filter"); filtersOk {
req.Filters = append(req.Filters, buildEC2CustomFilterList(
filters.(*schema.Set),
)...)
}
if len(req.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
req.Filters = nil
}

log.Printf("[DEBUG] DescribeLocalGateways %s\n", req)
resp, err := conn.DescribeLocalGateways(req)
if err != nil {
return err
}

if resp == nil || len(resp.LocalGateways) == 0 {
return fmt.Errorf("no matching Local Gateways found")
}

localgateways := make([]string, 0)

for _, localgateway := range resp.LocalGateways {
localgateways = append(localgateways, aws.StringValue(localgateway.LocalGatewayId))
}

d.SetId(time.Now().UTC().String())
if err := d.Set("ids", localgateways); err != nil {
return fmt.Errorf("Error setting local gateway ids: %s", err)
}

return nil
}
39 changes: 39 additions & 0 deletions aws/data_source_aws_local_gateways_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aws

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"testing"
)

func TestAccDataSourceAwsLocalGateways_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsLocalGatewaysConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLocalGatewaysDataSourceExists("data.aws_local_gateways.all"),
),
},
},
})
}

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

if rs.Primary.ID == "" {
return fmt.Errorf("aws_local_gateways data source ID not set")
}
return nil
}
}

const testAccDataSourceAwsLocalGatewaysConfig = `data "aws_local_gateways" "all" {}`
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ func Provider() terraform.ResourceProvider {
"aws_mq_broker": dataSourceAwsMqBroker(),
"aws_msk_cluster": dataSourceAwsMskCluster(),
"aws_msk_configuration": dataSourceAwsMskConfiguration(),
"aws_local_gateways": dataSourceAwsLocalGateways(),
"aws_local_gateway": dataSourceAwsLocalGateway(),
"aws_nat_gateway": dataSourceAwsNatGateway(),
"aws_network_acls": dataSourceAwsNetworkAcls(),
"aws_network_interface": dataSourceAwsNetworkInterface(),
Expand Down
6 changes: 6 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,12 @@
<li>
<a href="/docs/providers/aws/d/internet_gateway.html">aws_internet_gateway</a>
</li>
<li>
<a href="/docs/providers/aws/d/local_gateway.html">aws_local_gateway</a>
</li>
<li>
<a href="/docs/providers/aws/d/local_gateways.html">aws_local_gateways</a>
</li>
<li>
<a href="/docs/providers/aws/d/nat_gateway.html">aws_nat_gateway</a>
</li>
Expand Down
61 changes: 61 additions & 0 deletions website/docs/d/local_gateway.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
subcategory: "VPC"
layout: "aws"
page_title: "AWS: aws_local_gateway"
description: |-
Provides details about a specific Local Gateway
---

# Data Source: aws_local_gateway

`aws_local_gateway` provides details about a specific Local Gateway.

## Example Usage

The following example shows how one might accept a local gateway id as a variable.

```hcl
variable "local_gateway_id" {}

data "aws_local_gateway" "selected" {
id = "${var.local_gateway_id}"
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available
Local Gateways in the current region. The given filters must match exactly one
Local Gateway whose data will be exported as attributes.

* `filter` - (Optional) Custom filter block as described below.

* `id` - (Optional) The id of the specific Local Gateway to retrieve.

* `state` - (Optional) The current state of the desired Local Gateway.
Can be either `"pending"` or `"available"`.

* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
a pair on the desired Local Gateway.

More complex filters can be expressed using one or more `filter` sub-blocks,
which take the following arguments:

* `name` - (Required) The name of the field to filter by, as defined by
[the underlying AWS API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeLocalGateways.html).

* `values` - (Required) Set of values that are accepted for the given field.
A Local Gateway will be selected if any one of the given values matches.

## Attributes Reference

All of the argument attributes except `filter` blocks are also exported as
result attributes. This data source will complete the data by populating
any fields that are not included in the configuration with the data for
the selected Local Gateway.

The following attribute is additionally exported:

* `outpost_arn` - Amazon Resource Name (ARN) of Local Gateway
* `owner_id` - The ID of the AWS account that owns the Local Gateway.
* `state` - The State of the local gateway.
Loading