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 9fde05d commit adb1caa
Show file tree
Hide file tree
Showing 4 changed files with 50 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: 35 additions & 7 deletions aws/resource_aws_dx_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 @@ -18,7 +19,7 @@ func resourceAwsDxPublicVirtualInterface() *schema.Resource {
Update: resourceAwsDxPublicVirtualInterfaceUpdate,
Delete: resourceAwsDxPublicVirtualInterfaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsDxPublicVirtualInterfaceImport,
},
CustomizeDiff: resourceAwsDxPublicVirtualInterfaceCustomizeDiff,

Expand Down Expand Up @@ -101,13 +102,13 @@ func resourceAwsDxPublicVirtualInterfaceCreate(d *schema.ResourceData, meta inte
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.NewPublicVirtualInterface.AuthKey = aws.String(v.(string))
}
if v, ok := d.GetOk("customer_address"); ok {
if v, ok := d.GetOk("customer_address"); ok && v.(string) != "" {
req.NewPublicVirtualInterface.CustomerAddress = aws.String(v.(string))
}
if v, ok := d.GetOk("amazon_address"); ok {
if v, ok := d.GetOk("amazon_address"); ok && v.(string) != "" {
req.NewPublicVirtualInterface.AmazonAddress = aws.String(v.(string))
}
if v, ok := d.GetOk("route_filter_prefixes"); ok {
Expand All @@ -121,6 +122,14 @@ func resourceAwsDxPublicVirtualInterfaceCreate(d *schema.ResourceData, meta inte
}

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 := dxPublicVirtualInterfaceWaitUntilAvailable(d, conn); err != nil {
return err
Expand All @@ -142,9 +151,15 @@ func resourceAwsDxPublicVirtualInterfaceRead(d *schema.ResourceData, meta interf
return nil
}

if err := dxPublicVirtualInterfaceAttributes(d, meta, vif); 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)
d.Set("route_filter_prefixes", flattenDxRouteFilterPrefixes(vif.RouteFilterPrefixes))
if err := getTagsDX(conn, d, d.Get("arn").(string)); err != nil {
return err
}
Expand All @@ -164,6 +179,19 @@ func resourceAwsDxPublicVirtualInterfaceDelete(d *schema.ResourceData, meta inte
return dxVirtualInterfaceDelete(d, meta)
}

func resourceAwsDxPublicVirtualInterfaceImport(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 resourceAwsDxPublicVirtualInterfaceCustomizeDiff(diff *schema.ResourceDiff, meta interface{}) error {
if diff.Id() == "" {
// New resource.
Expand Down
19 changes: 10 additions & 9 deletions aws/resource_aws_dx_public_virtual_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ func TestAccAwsDxPublicVirtualInterface_basic(t *testing.T) {
if connectionId == "" {
t.Skipf("Environment variable %s is not set", key)
}
vifName := fmt.Sprintf("terraform-testacc-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: testAccCheckAwsDxPublicVirtualInterfaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDxPublicVirtualInterfaceConfig_noTags(connectionId, vifName),
Config: testAccDxPublicVirtualInterfaceConfig_noTags(connectionId, vifName, bgpAsn),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxPublicVirtualInterfaceExists("aws_dx_public_virtual_interface.foo"),
resource.TestCheckResourceAttr("aws_dx_public_virtual_interface.foo", "name", vifName),
resource.TestCheckResourceAttr("aws_dx_public_virtual_interface.foo", "tags.%", "0"),
),
},
{
Config: testAccDxPublicVirtualInterfaceConfig_tags(connectionId, vifName),
Config: testAccDxPublicVirtualInterfaceConfig_tags(connectionId, vifName, bgpAsn),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxPublicVirtualInterfaceExists("aws_dx_public_virtual_interface.foo"),
resource.TestCheckResourceAttr("aws_dx_public_virtual_interface.foo", "name", vifName),
Expand Down Expand Up @@ -88,15 +89,15 @@ func testAccCheckAwsDxPublicVirtualInterfaceExists(name string) resource.TestChe
}
}

func testAccDxPublicVirtualInterfaceConfig_noTags(cid, n string) string {
func testAccDxPublicVirtualInterfaceConfig_noTags(cid, n string, bgpAsn int) string {
return fmt.Sprintf(`
resource "aws_dx_public_virtual_interface" "foo" {
connection_id = "%s"
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 @@ -105,18 +106,18 @@ resource "aws_dx_public_virtual_interface" "foo" {
"175.45.176.0/22"
]
}
`, cid, n)
`, cid, n, bgpAsn)
}

func testAccDxPublicVirtualInterfaceConfig_tags(cid, n string) string {
func testAccDxPublicVirtualInterfaceConfig_tags(cid, n string, bgpAsn int) string {
return fmt.Sprintf(`
resource "aws_dx_public_virtual_interface" "foo" {
connection_id = "%s"
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 @@ -129,5 +130,5 @@ resource "aws_dx_public_virtual_interface" "foo" {
Environment = "test"
}
}
`, cid, n)
`, cid, n, bgpAsn)
}
6 changes: 3 additions & 3 deletions website/docs/r/dx_public_virtual_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ resource "aws_dx_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.
* `name` - (Required) The name for the 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.
* `route_filter_prefixes` - (Required) A list of routes to be advertised to the AWS network in this region.
* `tags` - (Optional) A mapping of tags to assign to the resource.

Expand Down

0 comments on commit adb1caa

Please sign in to comment.