Skip to content

Commit

Permalink
Changes after code review on hashicorp#3253.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kit Ewbank authored and Kit Ewbank committed Jun 22, 2018
1 parent 025d799 commit 4af87fd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 77 deletions.
56 changes: 1 addition & 55 deletions aws/dx_vif.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -27,14 +26,7 @@ func dxVirtualInterfaceRead(id string, conn *directconnect.DirectConnect) (*dire
func dxVirtualInterfaceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
if err := setTagsDX(conn, d, arn); err != nil {
if err := setTagsDX(conn, d, d.Get("arn").(string)); err != nil {
return err
}

Expand Down Expand Up @@ -120,49 +112,3 @@ func dxVirtualInterfaceWaitUntilAvailable(d *schema.ResourceData, conn *directco

return nil
}

// Attributes common to public VIFs and creator side of hosted public VIFs.
func dxPublicVirtualInterfaceAttributes(d *schema.ResourceData, meta interface{}, vif *directconnect.VirtualInterface) error {
if err := dxVirtualInterfaceAttributes(d, meta, vif); err != nil {
return err
}
d.Set("route_filter_prefixes", flattenDxRouteFilterPrefixes(vif.RouteFilterPrefixes))

return nil
}

// Attributes common to private VIFs and creator side of hosted private VIFs.
func dxPrivateVirtualInterfaceAttributes(d *schema.ResourceData, meta interface{}, vif *directconnect.VirtualInterface) error {
return dxVirtualInterfaceAttributes(d, meta, vif)
}

// Attributes common to public/private VIFs and creator side of hosted public/private VIFs.
func dxVirtualInterfaceAttributes(d *schema.ResourceData, meta interface{}, vif *directconnect.VirtualInterface) error {
if err := dxVirtualInterfaceArnAttribute(d, meta); err != nil {
return err
}

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)

return nil
}

func dxVirtualInterfaceArnAttribute(d *schema.ResourceData, meta interface{}) error {
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
d.Set("arn", arn)

return nil
}
41 changes: 36 additions & 5 deletions aws/resource_aws_dx_hosted_private_virtual_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"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"
Expand All @@ -17,7 +18,7 @@ func resourceAwsDxHostedPrivateVirtualInterface() *schema.Resource {
Read: resourceAwsDxHostedPrivateVirtualInterfaceRead,
Delete: resourceAwsDxHostedPrivateVirtualInterfaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsDxHostedPrivateVirtualInterfaceImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -98,13 +99,13 @@ func resourceAwsDxHostedPrivateVirtualInterfaceCreate(d *schema.ResourceData, me
AddressFamily: aws.String(d.Get("address_family").(string)),
},
}
if v, ok := d.GetOk("bgp_auth_key"); ok {
if v, ok := d.GetOk("bgp_auth_key"); ok && v.(string) != "" {
req.NewPrivateVirtualInterfaceAllocation.AuthKey = aws.String(v.(string))
}
if v, ok := d.GetOk("customer_address"); ok {
if v, ok := d.GetOk("customer_address"); ok && v.(string) != "" {
req.NewPrivateVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string))
}
if v, ok := d.GetOk("amazon_address"); ok {
if v, ok := d.GetOk("amazon_address"); ok && v.(string) != "" {
req.NewPrivateVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string))
}

Expand All @@ -115,6 +116,14 @@ func resourceAwsDxHostedPrivateVirtualInterfaceCreate(d *schema.ResourceData, me
}

d.SetId(aws.StringValue(resp.VirtualInterfaceId))
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
d.Set("arn", arn)

if err := dxHostedPrivateVirtualInterfaceWaitUntilAvailable(d, conn); err != nil {
return err
Expand All @@ -136,14 +145,36 @@ func resourceAwsDxHostedPrivateVirtualInterfaceRead(d *schema.ResourceData, meta
return nil
}

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("owner_account_id", vif.OwnerAccount)
return dxPrivateVirtualInterfaceAttributes(d, meta, vif)

return nil
}

func resourceAwsDxHostedPrivateVirtualInterfaceDelete(d *schema.ResourceData, meta interface{}) error {
return dxVirtualInterfaceDelete(d, meta)
}

func resourceAwsDxHostedPrivateVirtualInterfaceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
d.Set("arn", arn)

return []*schema.ResourceData{d}, nil
}

func dxHostedPrivateVirtualInterfaceWaitUntilAvailable(d *schema.ResourceData, conn *directconnect.DirectConnect) error {
return dxVirtualInterfaceWaitUntilAvailable(
d,
Expand Down
31 changes: 25 additions & 6 deletions aws/resource_aws_dx_hosted_private_virtual_interface_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/schema"
)
Expand All @@ -17,7 +18,7 @@ func resourceAwsDxHostedPrivateVirtualInterfaceAccepter() *schema.Resource {
Update: resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate,
Delete: resourceAwsDxHostedPrivateVirtualInterfaceAccepterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsDxHostedPrivateVirtualInterfaceAccepterImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -66,10 +67,10 @@ func resourceAwsDxHostedPrivateVirtualInterfaceAccepterCreate(d *schema.Resource
req := &directconnect.ConfirmPrivateVirtualInterfaceInput{
VirtualInterfaceId: aws.String(vifId),
}
if vgwOk {
if vgwOk && vgwIdRaw.(string) != "" {
req.VirtualGatewayId = aws.String(vgwIdRaw.(string))
}
if dxgwOk {
if dxgwOk && dxgwIdRaw.(string) != "" {
req.DirectConnectGatewayId = aws.String(dxgwIdRaw.(string))
}

Expand All @@ -80,6 +81,14 @@ func resourceAwsDxHostedPrivateVirtualInterfaceAccepterCreate(d *schema.Resource
}

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

if err := dxHostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(d, conn); err != nil {
return err
Expand All @@ -101,9 +110,6 @@ func resourceAwsDxHostedPrivateVirtualInterfaceAccepterRead(d *schema.ResourceDa
return nil
}

if err := dxVirtualInterfaceArnAttribute(d, meta); err != nil {
return err
}
d.Set("virtual_interface_id", vif.VirtualInterfaceId)
d.Set("vpn_gateway_id", vif.VirtualGatewayId)
d.Set("dx_gateway_id", vif.DirectConnectGatewayId)
Expand All @@ -126,6 +132,19 @@ func resourceAwsDxHostedPrivateVirtualInterfaceAccepterDelete(d *schema.Resource
return dxVirtualInterfaceDelete(d, meta)
}

func resourceAwsDxHostedPrivateVirtualInterfaceAccepterImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
d.Set("arn", arn)

return []*schema.ResourceData{d}, nil
}

func dxHostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(d *schema.ResourceData, conn *directconnect.DirectConnect) error {
return dxVirtualInterfaceWaitUntilAvailable(
d,
Expand Down
13 changes: 7 additions & 6 deletions aws/resource_aws_dx_hosted_private_virtual_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ func TestAccAwsDxHostedPrivateVirtualInterface_basic(t *testing.T) {
if ownerAccountId == "" {
t.Skipf("Environment variable %s is not set", key)
}
vifName := fmt.Sprintf("tf-dx-vif-%s", acctest.RandString(5))
vifName := fmt.Sprintf("terraform-testacc-dxvif-%s", acctest.RandString(5))
bgpAsn := randIntRange(64512, 65534)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDxHostedPrivateVirtualInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDxHostedPrivateVirtualInterfaceConfig_basic(connectionId, ownerAccountId, vifName),
Config: testAccDxHostedPrivateVirtualInterfaceConfig_basic(connectionId, ownerAccountId, vifName, bgpAsn),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxHostedPrivateVirtualInterfaceExists("aws_dx_hosted_private_virtual_interface.foo"),
resource.TestCheckResourceAttr("aws_dx_hosted_private_virtual_interface.foo", "name", vifName),
Expand Down Expand Up @@ -65,7 +66,7 @@ func testAccCheckAwsDxHostedPrivateVirtualInterfaceDestroy(s *terraform.State) e
}
for _, v := range resp.VirtualInterfaces {
if *v.VirtualInterfaceId == rs.Primary.ID && !(*v.VirtualInterfaceState == directconnect.VirtualInterfaceStateDeleted) {
return fmt.Errorf("[DESTROY ERROR] Dx Public VIF (%s) not deleted", rs.Primary.ID)
return fmt.Errorf("[DESTROY ERROR] Dx Private VIF (%s) not deleted", rs.Primary.ID)
}
}
}
Expand All @@ -83,7 +84,7 @@ func testAccCheckAwsDxHostedPrivateVirtualInterfaceExists(name string) resource.
}
}

func testAccDxHostedPrivateVirtualInterfaceConfig_basic(cid, ownerAcctId, n string) string {
func testAccDxHostedPrivateVirtualInterfaceConfig_basic(cid, ownerAcctId, n string, bgpAsn int) string {
return fmt.Sprintf(`
resource "aws_dx_hosted_private_virtual_interface" "foo" {
connection_id = "%s"
Expand All @@ -92,7 +93,7 @@ resource "aws_dx_hosted_private_virtual_interface" "foo" {
name = "%s"
vlan = 4094
address_family = "ipv4"
bgp_asn = 65352
bgp_asn = %d
}
`, cid, ownerAcctId, n)
`, cid, ownerAcctId, n, bgpAsn)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ resource "aws_dx_hosted_private_virtual_interface" "foo" {

The following arguments are supported:

* `address_family` - (Required) The address family for the BGP peer. `ipv4 ` or `ipv6`.
* `bgp_asn` - (Required) The autonomous system (AS) number for Border Gateway Protocol (BGP) configuration.
* `connection_id` - (Required) The ID of the Direct Connect connection (or LAG) on which to create the virtual interface.
* `owner_account_id` - (Required) The AWS account that will own the new virtual interface.
* `name` - (Required) The name for the virtual interface.
* `owner_account_id` - (Required) The AWS account that will own the new virtual interface.
* `vlan` - (Required) The VLAN ID.
* `bgp_asn` - (Required) The autonomous system (AS) number for Border Gateway Protocol (BGP) configuration.
* `amazon_address` - (Optional) The IPv4 CIDR address to use to send traffic to Amazon. Required for IPv4 BGP peers.
* `bgp_auth_key` - (Optional) The authentication key for BGP configuration.
* `address_family` - (Required) The address family for the BGP peer. `ipv4 ` or `ipv6`.
* `customer_address` - (Optional) The IPv4 CIDR destination address to which Amazon should send traffic. Required for IPv4 BGP peers.
* `amazon_address` - (Optional) The IPv4 CIDR address to use to send traffic to Amazon. Required for IPv4 BGP peers.

## Attributes Reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ resource "aws_dx_hosted_private_virtual_interface_accepter" "accepter" {
The following arguments are supported:

* `virtual_interface_id` - (Required) The ID of the Direct Connect virtual interface to accept.
* `vpn_gateway_id` - (Optional) The ID of the [virtual private gateway](vpn_gateway.html) to which to connect the virtual interface.
* `dx_gateway_id` - (Optional) The ID of the Direct Connect gateway to which to connect the virtual interface.
* `tags` - (Optional) A mapping of tags to assign to the resource.
* `vpn_gateway_id` - (Optional) The ID of the [virtual private gateway](vpn_gateway.html) to which to connect the virtual interface.

## Attributes Reference

Expand Down

0 comments on commit 4af87fd

Please sign in to comment.