forked from hashicorp/terraform-provider-aws
/
resource_aws_vpc_endpoint_connection_notification.go
155 lines (131 loc) · 5.19 KB
/
resource_aws_vpc_endpoint_connection_notification.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsVpcEndpointConnectionNotification() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVpcEndpointConnectionNotificationCreate,
Read: resourceAwsVpcEndpointConnectionNotificationRead,
Update: resourceAwsVpcEndpointConnectionNotificationUpdate,
Delete: resourceAwsVpcEndpointConnectionNotificationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"vpc_endpoint_service_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"vpc_endpoint_id"},
},
"vpc_endpoint_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"vpc_endpoint_service_id"},
},
"connection_notification_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"connection_events": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"notification_type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsVpcEndpointConnectionNotificationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.CreateVpcEndpointConnectionNotificationInput{
ConnectionNotificationArn: aws.String(d.Get("connection_notification_arn").(string)),
ConnectionEvents: expandStringSet(d.Get("connection_events").(*schema.Set)),
}
if v, ok := d.GetOk("vpc_endpoint_service_id"); ok {
req.ServiceId = aws.String(v.(string))
} else if v, ok := d.GetOk("vpc_endpoint_id"); ok {
req.VpcEndpointId = aws.String(v.(string))
} else {
return fmt.Errorf(
"One of ['vpc_endpoint_service_id', 'vpc_endpoint_id'] must be set to create a VPC Endpoint connection notification")
}
log.Printf("[DEBUG] Creating VPC Endpoint connection notification: %#v", req)
resp, err := conn.CreateVpcEndpointConnectionNotification(req)
if err != nil {
return fmt.Errorf("Error creating VPC Endpoint connection notification: %s", err.Error())
}
d.SetId(aws.StringValue(resp.ConnectionNotification.ConnectionNotificationId))
return resourceAwsVpcEndpointConnectionNotificationRead(d, meta)
}
func resourceAwsVpcEndpointConnectionNotificationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
resp, err := conn.DescribeVpcEndpointConnectionNotifications(&ec2.DescribeVpcEndpointConnectionNotificationsInput{
ConnectionNotificationId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "InvalidConnectionNotification", "") {
log.Printf("[WARN] VPC Endpoint connection notification (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading VPC Endpoint connection notification: %s", err.Error())
}
return vpcEndpointConnectionNotificationAttributes(d, resp.ConnectionNotificationSet[0])
}
func resourceAwsVpcEndpointConnectionNotificationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.ModifyVpcEndpointConnectionNotificationInput{
ConnectionNotificationId: aws.String(d.Id()),
}
if d.HasChange("connection_notification_arn") {
req.ConnectionNotificationArn = aws.String(d.Get("connection_notification_arn").(string))
}
if d.HasChange("connection_events") {
req.ConnectionEvents = expandStringSet(d.Get("connection_events").(*schema.Set))
}
log.Printf("[DEBUG] Updating VPC Endpoint connection notification: %#v", req)
if _, err := conn.ModifyVpcEndpointConnectionNotification(req); err != nil {
return fmt.Errorf("Error updating VPC Endpoint connection notification: %s", err.Error())
}
return resourceAwsVpcEndpointConnectionNotificationRead(d, meta)
}
func resourceAwsVpcEndpointConnectionNotificationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
log.Printf("[DEBUG] Deleting VPC Endpoint connection notification: %s", d.Id())
_, err := conn.DeleteVpcEndpointConnectionNotifications(&ec2.DeleteVpcEndpointConnectionNotificationsInput{
ConnectionNotificationIds: aws.StringSlice([]string{d.Id()}),
})
if err != nil {
if isAWSErr(err, "InvalidConnectionNotification", "") {
log.Printf("[DEBUG] VPC Endpoint connection notification %s is already gone", d.Id())
} else {
return fmt.Errorf("Error deleting VPC Endpoint connection notification: %s", err.Error())
}
}
return nil
}
func vpcEndpointConnectionNotificationAttributes(d *schema.ResourceData, cn *ec2.ConnectionNotification) error {
d.Set("vpc_endpoint_service_id", cn.ServiceId)
d.Set("vpc_endpoint_id", cn.VpcEndpointId)
d.Set("connection_notification_arn", cn.ConnectionNotificationArn)
d.Set("connection_events", flattenStringList(cn.ConnectionEvents))
d.Set("state", cn.ConnectionNotificationState)
d.Set("notification_type", cn.ConnectionNotificationType)
return nil
}