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/aws_dx_private_virtual_interface: Adding new data source for Direct Connect private virtual interfaces #7042

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
127 changes: 127 additions & 0 deletions aws/data_source_aws_dx_private_virtual_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func dataSourceAwsDxPrivateVirtualInterface() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDxPrivateVirtualInterfaceRead,

Schema: map[string]*schema.Schema{
"virtual_interface_id": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"connection_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"vpn_gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"dx_gateway_id"},
},
"dx_gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"vpn_gateway_id"},
},
"vlan": {
Type: schema.TypeInt,
Computed: true,
ValidateFunc: validation.IntBetween(1, 4094),
},
"bgp_asn": {
Type: schema.TypeInt,
Computed: true,
},
"bgp_auth_key": {
Type: schema.TypeString,
Optional: true,
slapula marked this conversation as resolved.
Show resolved Hide resolved
Computed: true,
},
"address_family": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{directconnect.AddressFamilyIpv4, directconnect.AddressFamilyIpv6}, false),
},
"customer_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"amazon_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"mtu": {
Type: schema.TypeInt,
Default: 1500,
Computed: true,
ValidateFunc: validateIntegerInSlice([]int{1500, 9001}),
},
"jumbo_frame_capable": {
Type: schema.TypeBool,
Computed: true,
},
"tags": tagsSchema(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to tagsSchemaComputed.

},
}
}

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

vif, err := dxVirtualInterfaceRead(d.Get("virtual_interface_id").(string), conn)
if err != nil {
return err
}
if vif == nil {
log.Printf("[WARN] Direct Connect virtual interface (%s) not found", d.Get("virtual_interface_id").(string))
slapula marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Get("virtual_interface_id")),
}.String()

d.Set("arn", arn)
slapula marked this conversation as resolved.
Show resolved Hide resolved
d.Set("connection_id", vif.ConnectionId)
d.Set("name", vif.VirtualInterfaceName)
d.Set("vlan", vif.Vlan)
d.Set("bgp_asn", vif.Asn)
d.Set("bgp_auth_key", vif.AuthKey)
d.Set("address_family", vif.AddressFamily)
d.Set("customer_address", vif.CustomerAddress)
d.Set("amazon_address", vif.AmazonAddress)
d.Set("vpn_gateway_id", vif.VirtualGatewayId)
d.Set("dx_gateway_id", vif.DirectConnectGatewayId)
d.Set("mtu", vif.Mtu)
d.Set("jumbo_frame_capable", vif.JumboFrameCapable)

err1 := getTagsDX(conn, d, arn)

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

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsDxPrivateVirtualInterface_basic(t *testing.T) {
key := "DX_CONNECTION_ID"
connectionID := os.Getenv(key)
if connectionID == "" {
t.Skipf("Environment variable %s is not set", key)
}
vifName := fmt.Sprintf("terraform-testacc-dxvif-%s", acctest.RandString(5))
bgpAsn := randIntRange(64512, 65534)
vlan := randIntRange(2049, 4094)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDxPrivateVirtualInterfaceConfigBasic(connectionID, vifName, bgpAsn, vlan),
Check: resource.ComposeTestCheckFunc(
slapula marked this conversation as resolved.
Show resolved Hide resolved
testAccCheckDataSourceAwsDxPrivateVirtualInterfaceExists("data.aws_dx_private_virtual_interface.foo"),
resource.TestCheckResourceAttr("data.aws_dx_private_virtual_interface.foo", "name", vifName),
resource.TestCheckResourceAttr("data.aws_dx_private_virtual_interface.foo", "tags.%", "0"),
),
},
},
})
}

func testAccCheckDataSourceAwsDxPrivateVirtualInterfaceExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

return nil
}
}

func testAccDxPrivateVirtualInterfaceConfigBasic(cid, n string, bgpAsn, vlan int) string {
return fmt.Sprintf(`
resource "aws_vpn_gateway" "foo" {
tags = {
Name = "%s"
}
}

resource "aws_dx_private_virtual_interface" "foo" {
connection_id = "%s"

vpn_gateway_id = "${aws_vpn_gateway.foo.id}"
name = "%s"
vlan = %d
address_family = "ipv4"
bgp_asn = %d
}

data "aws_dx_private_virtual_interface" "foo" {
virtual_interface_id = "${aws_dx_private_virtual_interface.foo.id}"
}
`, n, cid, n, vlan, bgpAsn)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func Provider() terraform.ResourceProvider {
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_dx_gateway": dataSourceAwsDxGateway(),
"aws_dx_private_virtual_interface": dataSourceAwsDxPrivateVirtualInterface(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
<li<%= sidebar_current("docs-aws-datasource-dx-gateway") %>>
<a href="/docs/providers/aws/d/dx_gateway.html">aws_dx_gateway</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-dx-private-virtual-interface") %>>
<a href="/docs/providers/aws/d/dx_private_virtual_interface.html">aws_dx_private_virtual_interface</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-dynamodb-table") %>>
<a href="/docs/providers/aws/d/dynamodb_table.html">aws_dynamodb_table</a>
</li>
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/dx_private_virtual_interface.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: "aws"
page_title: "AWS: aws_dx_private_virtual_interface"
sidebar_current: "docs-aws-datasource-dx-private-virtual-interface"
description: |-
Retrieve information about a Direct Connect private virtual interface resource.
---

# Data Source: aws_dx_private_virtual_interface

Retrieve information about a Direct Connect private virtual interface resource.

## Example Usage

```hcl
data "aws_dx_private_virtual_interface" "example" {
virtual_interface_id = "${aws_dx_private_virtual_interface.example.id}"
}
```

## Argument Reference

The following arguments are supported:

* `virtual_interface_id` - (Required) The ID of the virtual interface.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The ARN of the virtual interface.
* `jumbo_frame_capable` - Indicates whether jumbo frames (9001 MTU) are supported.
* `address_family` - The address family for the BGP peer. `ipv4 ` or `ipv6`.
* `bgp_asn` - The autonomous system (AS) number for Border Gateway Protocol (BGP) configuration.
* `connection_id` - The ID of the Direct Connect connection (or LAG) on which to create the virtual interface.
* `name` - The name for the virtual interface.
* `vlan` - The VLAN ID.
* `amazon_address` - The IPv4 CIDR address to use to send traffic to Amazon. Required for IPv4 BGP peers.
* `mtu` - The maximum transmission unit (MTU) is the size, in bytes, of the largest permissible packet that can be passed over the connection.
The MTU of a virtual private interface can be either `1500` or `9001` (jumbo frames). Default is `1500`.
* `bgp_auth_key` - The authentication key for BGP configuration.
* `customer_address` - The IPv4 CIDR destination address to which Amazon should send traffic. Required for IPv4 BGP peers.
* `dx_gateway_id` - The ID of the Direct Connect gateway to which to connect the virtual interface.
* `tags` - A mapping of tags to assign to the resource.
* `vpn_gateway_id` - The ID of the [virtual private gateway](vpn_gateway.html) to which to connect the virtual interface.