Skip to content

Commit

Permalink
Merge branch 'ewbankkit-issue-3251'
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Jun 25, 2018
2 parents 0676933 + a755534 commit cfbb2ab
Show file tree
Hide file tree
Showing 7 changed files with 991 additions and 368 deletions.
738 changes: 370 additions & 368 deletions aws/provider.go

Large diffs are not rendered by default.

190 changes: 190 additions & 0 deletions aws/resource_aws_dx_hosted_private_virtual_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package aws

import (
"fmt"
"log"
"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"
)

func resourceAwsDxHostedPrivateVirtualInterface() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDxHostedPrivateVirtualInterfaceCreate,
Read: resourceAwsDxHostedPrivateVirtualInterfaceRead,
Delete: resourceAwsDxHostedPrivateVirtualInterfaceDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsDxHostedPrivateVirtualInterfaceImport,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"connection_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vlan": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 4094),
},
"bgp_asn": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"bgp_auth_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"address_family": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{directconnect.AddressFamilyIpv4, directconnect.AddressFamilyIpv6}, false),
},
"customer_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"amazon_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"owner_account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAwsAccountId,
},
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}

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

req := &directconnect.AllocatePrivateVirtualInterfaceInput{
ConnectionId: aws.String(d.Get("connection_id").(string)),
OwnerAccount: aws.String(d.Get("owner_account_id").(string)),
NewPrivateVirtualInterfaceAllocation: &directconnect.NewPrivateVirtualInterfaceAllocation{
VirtualInterfaceName: aws.String(d.Get("name").(string)),
Vlan: aws.Int64(int64(d.Get("vlan").(int))),
Asn: aws.Int64(int64(d.Get("bgp_asn").(int))),
AddressFamily: aws.String(d.Get("address_family").(string)),
},
}
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 && v.(string) != "" {
req.NewPrivateVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string))
}
if v, ok := d.GetOk("amazon_address"); ok && v.(string) != "" {
req.NewPrivateVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating Direct Connect hosted private virtual interface: %#v", req)
resp, err := conn.AllocatePrivateVirtualInterface(req)
if err != nil {
return fmt.Errorf("Error creating Direct Connect hosted private virtual interface: %s", err.Error())
}

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
}

return resourceAwsDxHostedPrivateVirtualInterfaceRead(d, meta)
}

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

vif, err := dxVirtualInterfaceRead(d.Id(), conn)
if err != nil {
return err
}
if vif == nil {
log.Printf("[WARN] Direct Connect virtual interface (%s) not found, removing from state", d.Id())
d.SetId("")
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 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,
conn,
[]string{
directconnect.VirtualInterfaceStatePending,
},
[]string{
directconnect.VirtualInterfaceStateAvailable,
directconnect.VirtualInterfaceStateConfirming,
directconnect.VirtualInterfaceStateDown,
})
}
168 changes: 168 additions & 0 deletions aws/resource_aws_dx_hosted_private_virtual_interface_accepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package aws

import (
"fmt"
"log"
"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"
)

func resourceAwsDxHostedPrivateVirtualInterfaceAccepter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDxHostedPrivateVirtualInterfaceAccepterCreate,
Read: resourceAwsDxHostedPrivateVirtualInterfaceAccepterRead,
Update: resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate,
Delete: resourceAwsDxHostedPrivateVirtualInterfaceAccepterDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsDxHostedPrivateVirtualInterfaceAccepterImport,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"virtual_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vpn_gateway_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"dx_gateway_id"},
},
"dx_gateway_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"vpn_gateway_id"},
},
"tags": tagsSchema(),
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}

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

vgwIdRaw, vgwOk := d.GetOk("vpn_gateway_id")
dxgwIdRaw, dxgwOk := d.GetOk("dx_gateway_id")
if vgwOk == dxgwOk {
return fmt.Errorf(
"One of ['vpn_gateway_id', 'dx_gateway_id'] must be set to create a Direct Connect private virtual interface accepter")
}

vifId := d.Get("virtual_interface_id").(string)
req := &directconnect.ConfirmPrivateVirtualInterfaceInput{
VirtualInterfaceId: aws.String(vifId),
}
if vgwOk && vgwIdRaw.(string) != "" {
req.VirtualGatewayId = aws.String(vgwIdRaw.(string))
}
if dxgwOk && dxgwIdRaw.(string) != "" {
req.DirectConnectGatewayId = aws.String(dxgwIdRaw.(string))
}

log.Printf("[DEBUG] Accepting Direct Connect hosted private virtual interface: %#v", req)
_, err := conn.ConfirmPrivateVirtualInterface(req)
if err != nil {
return fmt.Errorf("Error accepting Direct Connect hosted private virtual interface: %s", err.Error())
}

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
}

return resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate(d, meta)
}

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

vif, err := dxVirtualInterfaceRead(d.Id(), conn)
if err != nil {
return err
}
if vif == nil {
log.Printf("[WARN] Direct Connect virtual interface (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
vifState := aws.StringValue(vif.VirtualInterfaceState)
if vifState != directconnect.VirtualInterfaceStateAvailable &&
vifState != directconnect.VirtualInterfaceStateDown {
log.Printf("[WARN] Direct Connect virtual interface (%s) is '%s', removing from state", vifState, d.Id())
d.SetId("")
return nil
}

d.Set("virtual_interface_id", vif.VirtualInterfaceId)
d.Set("vpn_gateway_id", vif.VirtualGatewayId)
d.Set("dx_gateway_id", vif.DirectConnectGatewayId)
if err := getTagsDX(conn, d, d.Get("arn").(string)); err != nil {
return err
}

return nil
}

func resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate(d *schema.ResourceData, meta interface{}) error {
if err := dxVirtualInterfaceUpdate(d, meta); err != nil {
return err
}

return resourceAwsDxHostedPrivateVirtualInterfaceAccepterRead(d, meta)
}

func resourceAwsDxHostedPrivateVirtualInterfaceAccepterDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Will not delete Direct Connect virtual interface. Terraform will remove this resource from the state file, however resources may remain.")
return nil
}

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,
conn,
[]string{
directconnect.VirtualInterfaceStateConfirming,
directconnect.VirtualInterfaceStatePending,
},
[]string{
directconnect.VirtualInterfaceStateAvailable,
directconnect.VirtualInterfaceStateDown,
})
}
Loading

0 comments on commit cfbb2ab

Please sign in to comment.