/
resource_aws_appsync_api_key.go
184 lines (159 loc) · 4.3 KB
/
resource_aws_appsync_api_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
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
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appsync"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsAppsyncApiKey() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAppsyncApiKeyCreate,
Read: resourceAwsAppsyncApiKeyRead,
Update: resourceAwsAppsyncApiKeyUpdate,
Delete: resourceAwsAppsyncApiKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},
"api_id": {
Type: schema.TypeString,
Required: true,
},
"expires": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// Ignore unsetting value
if old != "" && new == "" {
return true
}
return false
},
ValidateFunc: validation.ValidateRFC3339TimeString,
},
"key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func resourceAwsAppsyncApiKeyCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appsyncconn
apiID := d.Get("api_id").(string)
params := &appsync.CreateApiKeyInput{
ApiId: aws.String(apiID),
Description: aws.String(d.Get("description").(string)),
}
if v, ok := d.GetOk("expires"); ok {
t, _ := time.Parse(time.RFC3339, v.(string))
params.Expires = aws.Int64(t.Unix())
}
resp, err := conn.CreateApiKey(params)
if err != nil {
return fmt.Errorf("error creating Appsync API Key: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s", apiID, aws.StringValue(resp.ApiKey.Id)))
return resourceAwsAppsyncApiKeyRead(d, meta)
}
func resourceAwsAppsyncApiKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appsyncconn
apiID, keyID, err := decodeAppSyncApiKeyId(d.Id())
if err != nil {
return err
}
key, err := getAppsyncApiKey(apiID, keyID, conn)
if err != nil {
return fmt.Errorf("error getting Appsync API Key %q: %s", d.Id(), err)
}
if key == nil {
log.Printf("[WARN] AppSync API Key %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("api_id", apiID)
d.Set("key", key.Id)
d.Set("description", key.Description)
d.Set("expires", time.Unix(aws.Int64Value(key.Expires), 0).UTC().Format(time.RFC3339))
return nil
}
func resourceAwsAppsyncApiKeyUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appsyncconn
apiID, keyID, err := decodeAppSyncApiKeyId(d.Id())
if err != nil {
return err
}
params := &appsync.UpdateApiKeyInput{
ApiId: aws.String(apiID),
Id: aws.String(keyID),
}
if d.HasChange("description") {
params.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("expires") {
t, _ := time.Parse(time.RFC3339, d.Get("expires").(string))
params.Expires = aws.Int64(t.Unix())
}
_, err = conn.UpdateApiKey(params)
if err != nil {
return err
}
return resourceAwsAppsyncApiKeyRead(d, meta)
}
func resourceAwsAppsyncApiKeyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appsyncconn
apiID, keyID, err := decodeAppSyncApiKeyId(d.Id())
if err != nil {
return err
}
input := &appsync.DeleteApiKeyInput{
ApiId: aws.String(apiID),
Id: aws.String(keyID),
}
_, err = conn.DeleteApiKey(input)
if err != nil {
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") {
return nil
}
return err
}
return nil
}
func decodeAppSyncApiKeyId(id string) (string, string, error) {
parts := strings.Split(id, ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected format of ID (%q), expected API-ID:API-KEY-ID", id)
}
return parts[0], parts[1], nil
}
func getAppsyncApiKey(apiID, keyID string, conn *appsync.AppSync) (*appsync.ApiKey, error) {
input := &appsync.ListApiKeysInput{
ApiId: aws.String(apiID),
}
for {
resp, err := conn.ListApiKeys(input)
if err != nil {
return nil, err
}
for _, apiKey := range resp.ApiKeys {
if aws.StringValue(apiKey.Id) == keyID {
return apiKey, nil
}
}
if resp.NextToken == nil {
break
}
input.NextToken = resp.NextToken
}
return nil, nil
}