-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
cluster_snapshot.go
178 lines (147 loc) · 5.7 KB
/
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package redshift
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
// @SDKResource("aws_redshift_cluster_snapshot", name="Cluster Snapshot")
// @Tags(identifierAttribute="arn")
func ResourceClusterSnapshot() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterSnapshotCreate,
ReadWithoutTimeout: resourceClusterSnapshotRead,
UpdateWithoutTimeout: resourceClusterSnapshotUpdate,
DeleteWithoutTimeout: resourceClusterSnapshotDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"manual_snapshot_retention_period": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
"owner_account": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
CustomizeDiff: verify.SetTagsDiff,
}
}
func resourceClusterSnapshotCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).RedshiftConn(ctx)
input := redshift.CreateClusterSnapshotInput{
SnapshotIdentifier: aws.String(d.Get("snapshot_identifier").(string)),
ClusterIdentifier: aws.String(d.Get("cluster_identifier").(string)),
Tags: getTagsIn(ctx),
}
if v, ok := d.GetOk("manual_snapshot_retention_period"); ok {
input.ManualSnapshotRetentionPeriod = aws.Int64(int64(v.(int)))
}
output, err := conn.CreateClusterSnapshotWithContext(ctx, &input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Redshift Cluster Snapshot : %s", err)
}
d.SetId(aws.StringValue(output.Snapshot.SnapshotIdentifier))
if _, err := waitClusterSnapshotCreated(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for Redshift Cluster Snapshot (%s) create: %s", d.Id(), err)
}
return append(diags, resourceClusterSnapshotRead(ctx, d, meta)...)
}
func resourceClusterSnapshotRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).RedshiftConn(ctx)
snapshot, err := FindClusterSnapshotByID(ctx, conn, d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Redshift Cluster Snapshot (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Redshift Cluster Snapshot (%s): %s", d.Id(), err)
}
arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "redshift",
Region: meta.(*conns.AWSClient).Region,
AccountID: meta.(*conns.AWSClient).AccountID,
Resource: fmt.Sprintf("snapshot:%s/%s", aws.StringValue(snapshot.ClusterIdentifier), d.Id()),
}.String()
d.Set("arn", arn)
d.Set("cluster_identifier", snapshot.ClusterIdentifier)
d.Set("kms_key_id", snapshot.KmsKeyId)
d.Set("manual_snapshot_retention_period", snapshot.ManualSnapshotRetentionPeriod)
d.Set("owner_account", snapshot.OwnerAccount)
d.Set("snapshot_identifier", snapshot.SnapshotIdentifier)
setTagsOut(ctx, snapshot.Tags)
return diags
}
func resourceClusterSnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).RedshiftConn(ctx)
if d.HasChangesExcept("tags", "tags_all") {
input := &redshift.ModifyClusterSnapshotInput{
ManualSnapshotRetentionPeriod: aws.Int64(int64(d.Get("manual_snapshot_retention_period").(int))),
SnapshotIdentifier: aws.String(d.Id()),
}
_, err := conn.ModifyClusterSnapshotWithContext(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Redshift Cluster Snapshot (%s): %s", d.Id(), err)
}
}
return append(diags, resourceClusterSnapshotRead(ctx, d, meta)...)
}
func resourceClusterSnapshotDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).RedshiftConn(ctx)
log.Printf("[DEBUG] Deleting Redshift Cluster Snapshot: %s", d.Id())
_, err := conn.DeleteClusterSnapshotWithContext(ctx, &redshift.DeleteClusterSnapshotInput{
SnapshotIdentifier: aws.String(d.Id()),
})
if tfawserr.ErrCodeEquals(err, redshift.ErrCodeClusterSnapshotNotFoundFault) {
return diags
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting Redshift Cluster Snapshot (%s): %s", d.Id(), err)
}
if _, err := waitClusterSnapshotDeleted(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for Redshift Cluster Snapshot (%s) delete: %s", d.Id(), err)
}
return diags
}