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 889d4c5 commit a119b6d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 75 deletions.
58 changes: 2 additions & 56 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 All @@ -49,7 +41,7 @@ func dxVirtualInterfaceDelete(d *schema.ResourceData, meta interface{}) error {
VirtualInterfaceId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "DirectConnectClientException", "does not exist") {
if isAWSErr(err, directconnect.ErrCodeClientException, "does not exist") {
return nil
}
return fmt.Errorf("Error deleting Direct Connect virtual interface: %s", 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
}
42 changes: 37 additions & 5 deletions aws/resource_aws_dx_hosted_public_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 resourceAwsDxHostedPublicVirtualInterface() *schema.Resource {
Read: resourceAwsDxHostedPublicVirtualInterfaceRead,
Delete: resourceAwsDxHostedPublicVirtualInterfaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsDxHostedPublicVirtualInterfaceImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -117,13 +118,13 @@ func resourceAwsDxHostedPublicVirtualInterfaceCreate(d *schema.ResourceData, met
AddressFamily: aws.String(addressFamily),
},
}
if v, ok := d.GetOk("bgp_auth_key"); ok {
if v, ok := d.GetOk("bgp_auth_key"); ok && v.(string) != "" {
req.NewPublicVirtualInterfaceAllocation.AuthKey = aws.String(v.(string))
}
if caOk {
if caOk && caRaw.(string) != "" {
req.NewPublicVirtualInterfaceAllocation.CustomerAddress = aws.String(caRaw.(string))
}
if aaOk {
if aaOk && aaRaw.(string) != "" {
req.NewPublicVirtualInterfaceAllocation.AmazonAddress = aws.String(aaRaw.(string))
}
if v, ok := d.GetOk("route_filter_prefixes"); ok {
Expand All @@ -137,6 +138,14 @@ func resourceAwsDxHostedPublicVirtualInterfaceCreate(d *schema.ResourceData, met
}

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 := dxHostedPublicVirtualInterfaceWaitUntilAvailable(d, conn); err != nil {
return err
Expand All @@ -158,14 +167,37 @@ func resourceAwsDxHostedPublicVirtualInterfaceRead(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("route_filter_prefixes", flattenDxRouteFilterPrefixes(vif.RouteFilterPrefixes))
d.Set("owner_account_id", vif.OwnerAccount)
return dxPublicVirtualInterfaceAttributes(d, meta, vif)

return nil
}

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

func resourceAwsDxHostedPublicVirtualInterfaceImport(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 dxHostedPublicVirtualInterfaceWaitUntilAvailable(d *schema.ResourceData, conn *directconnect.DirectConnect) error {
return dxVirtualInterfaceWaitUntilAvailable(
d,
Expand Down
27 changes: 23 additions & 4 deletions aws/resource_aws_dx_hosted_public_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 resourceAwsDxHostedPublicVirtualInterfaceAccepter() *schema.Resource {
Update: resourceAwsDxHostedPublicVirtualInterfaceAccepterUpdate,
Delete: resourceAwsDxHostedPublicVirtualInterfaceAccepterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsDxHostedPublicVirtualInterfaceAccepterImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -55,6 +56,14 @@ func resourceAwsDxHostedPublicVirtualInterfaceAccepterCreate(d *schema.ResourceD
}

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 := dxHostedPublicVirtualInterfaceAccepterWaitUntilAvailable(d, conn); err != nil {
return err
Expand All @@ -76,9 +85,6 @@ func resourceAwsDxHostedPublicVirtualInterfaceAccepterRead(d *schema.ResourceDat
return nil
}

if err := dxVirtualInterfaceArnAttribute(d, meta); err != nil {
return err
}
d.Set("virtual_interface_id", vif.VirtualInterfaceId)
if err := getTagsDX(conn, d, d.Get("arn").(string)); err != nil {
return err
Expand All @@ -99,6 +105,19 @@ func resourceAwsDxHostedPublicVirtualInterfaceAccepterDelete(d *schema.ResourceD
return dxVirtualInterfaceDelete(d, meta)
}

func resourceAwsDxHostedPublicVirtualInterfaceAccepterImport(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 dxHostedPublicVirtualInterfaceAccepterWaitUntilAvailable(d *schema.ResourceData, conn *directconnect.DirectConnect) error {
return dxVirtualInterfaceWaitUntilAvailable(
d,
Expand Down
11 changes: 6 additions & 5 deletions aws/resource_aws_dx_hosted_public_virtual_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ func TestAccAwsDxHostedPublicVirtualInterface_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: testAccCheckAwsDxHostedPublicVirtualInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDxHostedPublicVirtualInterfaceConfig_basic(connectionId, ownerAccountId, vifName),
Config: testAccDxHostedPublicVirtualInterfaceConfig_basic(connectionId, ownerAccountId, vifName, bgpAsn),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxHostedPublicVirtualInterfaceExists("aws_dx_hosted_public_virtual_interface.foo"),
resource.TestCheckResourceAttr("aws_dx_hosted_public_virtual_interface.foo", "name", vifName),
Expand Down Expand Up @@ -83,7 +84,7 @@ func testAccCheckAwsDxHostedPublicVirtualInterfaceExists(name string) resource.T
}
}

func testAccDxHostedPublicVirtualInterfaceConfig_basic(cid, ownerAcctId, n string) string {
func testAccDxHostedPublicVirtualInterfaceConfig_basic(cid, ownerAcctId, n string, bgpAsn int) string {
return fmt.Sprintf(`
resource "aws_dx_hosted_public_virtual_interface" "foo" {
connection_id = "%s"
Expand All @@ -92,7 +93,7 @@ resource "aws_dx_hosted_public_virtual_interface" "foo" {
name = "%s"
vlan = 4094
address_family = "ipv4"
bgp_asn = 65352
bgp_asn = %d
customer_address = "175.45.176.1/30"
amazon_address = "175.45.176.2/30"
Expand All @@ -101,5 +102,5 @@ resource "aws_dx_hosted_public_virtual_interface" "foo" {
"175.45.176.0/22"
]
}
`, cid, ownerAcctId, n)
`, cid, ownerAcctId, n, bgpAsn)
}
10 changes: 5 additions & 5 deletions website/docs/r/dx_hosted_public_virtual_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ resource "aws_dx_hosted_public_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.
* `route_filter_prefixes` - (Required) A list of routes to be advertised to the AWS network in this region.
* `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.
* `route_filter_prefixes` - (Required) A list of routes to be advertised to the AWS network in this region.

## Attributes Reference

Expand Down

0 comments on commit a119b6d

Please sign in to comment.