diff --git a/aws/dx_vif.go b/aws/dx_vif.go index 92ee5358cc73..ea659c7b6c99 100644 --- a/aws/dx_vif.go +++ b/aws/dx_vif.go @@ -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" @@ -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 } @@ -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) @@ -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 -} diff --git a/aws/resource_aws_dx_public_virtual_interface.go b/aws/resource_aws_dx_public_virtual_interface.go index 88cfd30a5c1c..c12ba2502f39 100644 --- a/aws/resource_aws_dx_public_virtual_interface.go +++ b/aws/resource_aws_dx_public_virtual_interface.go @@ -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" @@ -18,7 +19,7 @@ func resourceAwsDxPublicVirtualInterface() *schema.Resource { Update: resourceAwsDxPublicVirtualInterfaceUpdate, Delete: resourceAwsDxPublicVirtualInterfaceDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceAwsDxPublicVirtualInterfaceImport, }, CustomizeDiff: resourceAwsDxPublicVirtualInterfaceCustomizeDiff, @@ -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 { @@ -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 @@ -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 } @@ -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. diff --git a/aws/resource_aws_dx_public_virtual_interface_test.go b/aws/resource_aws_dx_public_virtual_interface_test.go index 28e94108674a..e498651cbb85 100644 --- a/aws/resource_aws_dx_public_virtual_interface_test.go +++ b/aws/resource_aws_dx_public_virtual_interface_test.go @@ -18,7 +18,8 @@ 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) }, @@ -26,7 +27,7 @@ func TestAccAwsDxPublicVirtualInterface_basic(t *testing.T) { 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), @@ -34,7 +35,7 @@ func TestAccAwsDxPublicVirtualInterface_basic(t *testing.T) { ), }, { - 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), @@ -88,7 +89,7 @@ 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" @@ -96,7 +97,7 @@ resource "aws_dx_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" @@ -105,10 +106,10 @@ 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" @@ -116,7 +117,7 @@ resource "aws_dx_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" @@ -129,5 +130,5 @@ resource "aws_dx_public_virtual_interface" "foo" { Environment = "test" } } -`, cid, n) +`, cid, n, bgpAsn) } diff --git a/website/docs/r/dx_public_virtual_interface.html.markdown b/website/docs/r/dx_public_virtual_interface.html.markdown index 05d611d40bb8..c8d3705613ae 100644 --- a/website/docs/r/dx_public_virtual_interface.html.markdown +++ b/website/docs/r/dx_public_virtual_interface.html.markdown @@ -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.