forked from hashicorp/terraform-provider-aws
/
resource_aws_dx_connection_association.go
94 lines (80 loc) · 2.32 KB
/
resource_aws_dx_connection_association.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package aws
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsDxConnectionAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDxConnectionAssociationCreate,
Read: resourceAwsDxConnectionAssociationRead,
Delete: resourceAwsDxConnectionAssociationDelete,
Schema: map[string]*schema.Schema{
"connection_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"lag_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsDxConnectionAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
input := &directconnect.AssociateConnectionWithLagInput{
ConnectionId: aws.String(d.Get("connection_id").(string)),
LagId: aws.String(d.Get("lag_id").(string)),
}
resp, err := conn.AssociateConnectionWithLag(input)
if err != nil {
return err
}
d.SetId(*resp.ConnectionId)
return nil
}
func resourceAwsDxConnectionAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
input := &directconnect.DescribeConnectionsInput{
ConnectionId: aws.String(d.Id()),
}
resp, err := conn.DescribeConnections(input)
if err != nil {
return err
}
if len(resp.Connections) < 1 {
d.SetId("")
return nil
}
if len(resp.Connections) != 1 {
return fmt.Errorf("Found %d DX connections for %s, expected 1", len(resp.Connections), d.Id())
}
if *resp.Connections[0].LagId != d.Get("lag_id").(string) {
d.SetId("")
return nil
}
return nil
}
func resourceAwsDxConnectionAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
input := &directconnect.DisassociateConnectionFromLagInput{
ConnectionId: aws.String(d.Id()),
LagId: aws.String(d.Get("lag_id").(string)),
}
return resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.DisassociateConnectionFromLag(input)
if err != nil {
if isAWSErr(err, directconnect.ErrCodeClientException, "is in a transitioning state.") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
}