-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
find.go
148 lines (114 loc) · 3.29 KB
/
find.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
package kms
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)
func FindAliasByName(conn *kms.KMS, name string) (*kms.AliasListEntry, error) {
input := &kms.ListAliasesInput{}
var output *kms.AliasListEntry
err := conn.ListAliasesPages(input, func(page *kms.ListAliasesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
for _, alias := range page.Aliases {
if aws.StringValue(alias.AliasName) == name {
output = alias
return false
}
}
return !lastPage
})
if err != nil {
return nil, err
}
if output == nil {
return nil, &resource.NotFoundError{}
}
return output, nil
}
func FindCustomKeyStoreByID(ctx context.Context, conn *kms.KMS, in *kms.DescribeCustomKeyStoresInput) (*kms.CustomKeyStoresListEntry, error) {
out, err := conn.DescribeCustomKeyStoresWithContext(ctx, in)
if tfawserr.ErrCodeEquals(err, kms.ErrCodeCustomKeyStoreNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: in,
}
}
if err != nil {
return nil, err
}
if out == nil || out.CustomKeyStores[0] == nil {
return nil, tfresource.NewEmptyResultError(in)
}
return out.CustomKeyStores[0], nil
}
func FindKeyByID(conn *kms.KMS, id string) (*kms.KeyMetadata, error) {
input := &kms.DescribeKeyInput{
KeyId: aws.String(id),
}
output, err := conn.DescribeKey(input)
if tfawserr.ErrCodeEquals(err, kms.ErrCodeNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}
if output == nil || output.KeyMetadata == nil {
return nil, tfresource.NewEmptyResultError(input)
}
keyMetadata := output.KeyMetadata
// Once the CMK is in the pending (replica) deletion state Terraform considers it logically deleted.
if state := aws.StringValue(keyMetadata.KeyState); state == kms.KeyStatePendingDeletion || state == kms.KeyStatePendingReplicaDeletion {
return nil, &resource.NotFoundError{
Message: state,
LastRequest: input,
}
}
return keyMetadata, nil
}
func FindKeyPolicyByKeyIDAndPolicyName(conn *kms.KMS, keyID, policyName string) (*string, error) {
input := &kms.GetKeyPolicyInput{
KeyId: aws.String(keyID),
PolicyName: aws.String(policyName),
}
output, err := conn.GetKeyPolicy(input)
if tfawserr.ErrCodeEquals(err, kms.ErrCodeNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}
if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}
return output.Policy, nil
}
func FindKeyRotationEnabledByKeyID(conn *kms.KMS, keyID string) (*bool, error) {
input := &kms.GetKeyRotationStatusInput{
KeyId: aws.String(keyID),
}
output, err := conn.GetKeyRotationStatus(input)
if tfawserr.ErrCodeEquals(err, kms.ErrCodeNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}
if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}
return output.KeyRotationEnabled, nil
}