-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
data_source_aws_kms_key.go
111 lines (107 loc) · 2.81 KB
/
data_source_aws_kms_key.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
package aws
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsKmsKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsKmsKeyRead,
Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateKmsKey,
},
"grant_tokens": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"aws_account_id": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"deletion_date": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"expiration_model": {
Type: schema.TypeString,
Computed: true,
},
"key_manager": {
Type: schema.TypeString,
Computed: true,
},
"key_state": {
Type: schema.TypeString,
Computed: true,
},
"key_usage": {
Type: schema.TypeString,
Computed: true,
},
"origin": {
Type: schema.TypeString,
Computed: true,
},
"valid_to": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsKmsKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kmsconn
keyId := d.Get("key_id")
var grantTokens []*string
if v, ok := d.GetOk("grant_tokens"); ok {
grantTokens = aws.StringSlice(v.([]string))
}
input := &kms.DescribeKeyInput{
KeyId: aws.String(keyId.(string)),
GrantTokens: grantTokens,
}
output, err := conn.DescribeKey(input)
if err != nil {
return fmt.Errorf("error while describing key [%s]: %s", keyId, err)
}
d.SetId(aws.StringValue(output.KeyMetadata.KeyId))
d.Set("arn", output.KeyMetadata.Arn)
d.Set("aws_account_id", output.KeyMetadata.AWSAccountId)
d.Set("creation_date", aws.TimeValue(output.KeyMetadata.CreationDate).Format(time.RFC3339))
if output.KeyMetadata.DeletionDate != nil {
d.Set("deletion_date", aws.TimeValue(output.KeyMetadata.DeletionDate).Format(time.RFC3339))
}
d.Set("description", output.KeyMetadata.Description)
d.Set("enabled", output.KeyMetadata.Enabled)
d.Set("expiration_model", output.KeyMetadata.ExpirationModel)
d.Set("key_manager", output.KeyMetadata.KeyManager)
d.Set("key_state", output.KeyMetadata.KeyState)
d.Set("key_usage", output.KeyMetadata.KeyUsage)
d.Set("origin", output.KeyMetadata.Origin)
if output.KeyMetadata.ValidTo != nil {
d.Set("valid_to", aws.TimeValue(output.KeyMetadata.ValidTo).Format(time.RFC3339))
}
return nil
}