forked from hashicorp/terraform-provider-aws
/
resource_aws_api_gateway_client_certificate.go
125 lines (106 loc) · 3.62 KB
/
resource_aws_api_gateway_client_certificate.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
package aws
import (
"fmt"
"log"
"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/schema"
)
func resourceAwsApiGatewayClientCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayClientCertificateCreate,
Read: resourceAwsApiGatewayClientCertificateRead,
Update: resourceAwsApiGatewayClientCertificateUpdate,
Delete: resourceAwsApiGatewayClientCertificateDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Computed: true,
},
"pem_encoded_certificate": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsApiGatewayClientCertificateCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
input := apigateway.GenerateClientCertificateInput{}
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}
log.Printf("[DEBUG] Generating API Gateway Client Certificate: %s", input)
out, err := conn.GenerateClientCertificate(&input)
if err != nil {
return fmt.Errorf("Failed to generate client certificate: %s", err)
}
d.SetId(*out.ClientCertificateId)
return resourceAwsApiGatewayClientCertificateRead(d, meta)
}
func resourceAwsApiGatewayClientCertificateRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
input := apigateway.GetClientCertificateInput{
ClientCertificateId: aws.String(d.Id()),
}
out, err := conn.GetClientCertificate(&input)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] API Gateway Client Certificate %s not found, removing", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[DEBUG] Received API Gateway Client Certificate: %s", out)
d.Set("description", out.Description)
d.Set("created_date", out.CreatedDate.String())
d.Set("expiration_date", out.ExpirationDate.String())
d.Set("pem_encoded_certificate", out.PemEncodedCertificate)
return nil
}
func resourceAwsApiGatewayClientCertificateUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("description") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/description"),
Value: aws.String(d.Get("description").(string)),
})
}
input := apigateway.UpdateClientCertificateInput{
ClientCertificateId: aws.String(d.Id()),
PatchOperations: operations,
}
log.Printf("[DEBUG] Updating API Gateway Client Certificate: %s", input)
_, err := conn.UpdateClientCertificate(&input)
if err != nil {
return fmt.Errorf("Updating API Gateway Client Certificate failed: %s", err)
}
return resourceAwsApiGatewayClientCertificateRead(d, meta)
}
func resourceAwsApiGatewayClientCertificateDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Client Certificate: %s", d.Id())
input := apigateway.DeleteClientCertificateInput{
ClientCertificateId: aws.String(d.Id()),
}
_, err := conn.DeleteClientCertificate(&input)
if err != nil {
return fmt.Errorf("Deleting API Gateway Client Certificate failed: %s", err)
}
return nil
}