forked from hashicorp/terraform-provider-aws
/
resource_aws_api_gateway_usage_plan_key.go
115 lines (94 loc) · 2.9 KB
/
resource_aws_api_gateway_usage_plan_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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsApiGatewayUsagePlanKey() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayUsagePlanKeyCreate,
Read: resourceAwsApiGatewayUsagePlanKeyRead,
Delete: resourceAwsApiGatewayUsagePlanKeyDelete,
Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"usage_plan_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsApiGatewayUsagePlanKeyCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Print("[DEBUG] Creating API Gateway Usage Plan Key")
params := &apigateway.CreateUsagePlanKeyInput{
KeyId: aws.String(d.Get("key_id").(string)),
KeyType: aws.String(d.Get("key_type").(string)),
UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
}
up, err := conn.CreateUsagePlanKey(params)
if err != nil {
return fmt.Errorf("Error creating API Gateway Usage Plan Key: %s", err)
}
d.SetId(*up.Id)
return resourceAwsApiGatewayUsagePlanKeyRead(d, meta)
}
func resourceAwsApiGatewayUsagePlanKeyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Usage Plan Key: %s", d.Id())
up, err := conn.GetUsagePlanKey(&apigateway.GetUsagePlanKeyInput{
UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
KeyId: aws.String(d.Get("key_id").(string)),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] API Gateway Usage Plan Key (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("name", up.Name)
d.Set("value", up.Value)
return nil
}
func resourceAwsApiGatewayUsagePlanKeyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Usage Plan Key: %s", d.Id())
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteUsagePlanKey(&apigateway.DeleteUsagePlanKeyInput{
UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
KeyId: aws.String(d.Get("key_id").(string)),
})
if err == nil {
return nil
}
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
return nil
}
return resource.NonRetryableError(err)
})
}