forked from hashicorp/terraform-provider-aws
/
resource_aws_neptune_cluster_snapshot.go
218 lines (190 loc) · 6.49 KB
/
resource_aws_neptune_cluster_snapshot.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/neptune"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsNeptuneClusterSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceAwsNeptuneClusterSnapshotCreate,
Read: resourceAwsNeptuneClusterSnapshotRead,
Delete: resourceAwsNeptuneClusterSnapshotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"db_cluster_snapshot_identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"db_cluster_identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"allocated_storage": {
Type: schema.TypeInt,
Computed: true,
},
"availability_zones": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"db_cluster_snapshot_arn": {
Type: schema.TypeString,
Computed: true,
},
"engine": {
Type: schema.TypeString,
Computed: true,
},
"engine_version": {
Type: schema.TypeString,
Computed: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"license_model": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"source_db_cluster_snapshot_arn": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_type": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"storage_encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsNeptuneClusterSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).neptuneconn
input := &neptune.CreateDBClusterSnapshotInput{
DBClusterIdentifier: aws.String(d.Get("db_cluster_identifier").(string)),
DBClusterSnapshotIdentifier: aws.String(d.Get("db_cluster_snapshot_identifier").(string)),
}
log.Printf("[DEBUG] Creating Neptune DB Cluster Snapshot: %s", input)
_, err := conn.CreateDBClusterSnapshot(input)
if err != nil {
return fmt.Errorf("error creating Neptune DB Cluster Snapshot: %s", err)
}
d.SetId(d.Get("db_cluster_snapshot_identifier").(string))
stateConf := &resource.StateChangeConf{
Pending: []string{"creating"},
Target: []string{"available"},
Refresh: resourceAwsNeptuneClusterSnapshotStateRefreshFunc(d.Id(), conn),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 10 * time.Second,
Delay: 5 * time.Second,
}
// Wait, catching any errors
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for Neptune DB Cluster Snapshot %q to create: %s", d.Id(), err)
}
return resourceAwsNeptuneClusterSnapshotRead(d, meta)
}
func resourceAwsNeptuneClusterSnapshotRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).neptuneconn
input := &neptune.DescribeDBClusterSnapshotsInput{
DBClusterSnapshotIdentifier: aws.String(d.Id()),
}
log.Printf("[DEBUG] Reading Neptune DB Cluster Snapshot: %s", input)
output, err := conn.DescribeDBClusterSnapshots(input)
if err != nil {
if isAWSErr(err, neptune.ErrCodeDBClusterSnapshotNotFoundFault, "") {
log.Printf("[WARN] Neptune DB Cluster Snapshot %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading Neptune DB Cluster Snapshot %q: %s", d.Id(), err)
}
if output == nil || len(output.DBClusterSnapshots) == 0 || output.DBClusterSnapshots[0] == nil || aws.StringValue(output.DBClusterSnapshots[0].DBClusterSnapshotIdentifier) != d.Id() {
log.Printf("[WARN] Neptune DB Cluster Snapshot %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
snapshot := output.DBClusterSnapshots[0]
d.Set("allocated_storage", snapshot.AllocatedStorage)
if err := d.Set("availability_zones", flattenStringList(snapshot.AvailabilityZones)); err != nil {
return fmt.Errorf("error setting availability_zones: %s", err)
}
d.Set("db_cluster_identifier", snapshot.DBClusterIdentifier)
d.Set("db_cluster_snapshot_arn", snapshot.DBClusterSnapshotArn)
d.Set("db_cluster_snapshot_identifier", snapshot.DBClusterSnapshotIdentifier)
d.Set("engine_version", snapshot.EngineVersion)
d.Set("engine", snapshot.Engine)
d.Set("kms_key_id", snapshot.KmsKeyId)
d.Set("license_model", snapshot.LicenseModel)
d.Set("port", snapshot.Port)
d.Set("snapshot_type", snapshot.SnapshotType)
d.Set("source_db_cluster_snapshot_arn", snapshot.SourceDBClusterSnapshotArn)
d.Set("status", snapshot.Status)
d.Set("storage_encrypted", snapshot.StorageEncrypted)
d.Set("vpc_id", snapshot.VpcId)
return nil
}
func resourceAwsNeptuneClusterSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).neptuneconn
input := &neptune.DeleteDBClusterSnapshotInput{
DBClusterSnapshotIdentifier: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting Neptune DB Cluster Snapshot: %s", input)
_, err := conn.DeleteDBClusterSnapshot(input)
if err != nil {
if isAWSErr(err, neptune.ErrCodeDBClusterSnapshotNotFoundFault, "") {
return nil
}
return fmt.Errorf("error deleting Neptune DB Cluster Snapshot %q: %s", d.Id(), err)
}
return nil
}
func resourceAwsNeptuneClusterSnapshotStateRefreshFunc(dbClusterSnapshotIdentifier string, conn *neptune.Neptune) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &neptune.DescribeDBClusterSnapshotsInput{
DBClusterSnapshotIdentifier: aws.String(dbClusterSnapshotIdentifier),
}
log.Printf("[DEBUG] Reading Neptune DB Cluster Snapshot: %s", input)
output, err := conn.DescribeDBClusterSnapshots(input)
if err != nil {
if isAWSErr(err, neptune.ErrCodeDBClusterSnapshotNotFoundFault, "") {
return nil, "", nil
}
return nil, "", fmt.Errorf("Error retrieving DB Cluster Snapshots: %s", err)
}
if output == nil || len(output.DBClusterSnapshots) == 0 || output.DBClusterSnapshots[0] == nil {
return nil, "", fmt.Errorf("No snapshots returned for %s", dbClusterSnapshotIdentifier)
}
snapshot := output.DBClusterSnapshots[0]
return output, aws.StringValue(snapshot.Status), nil
}
}