-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
model.go
229 lines (186 loc) · 6.65 KB
/
model.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package apigatewayv2
import (
"context"
"fmt"
"log"
"strings"
"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/apigatewayv2"
awstypes "github.com/aws/aws-sdk-go-v2/service/apigatewayv2/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
// @SDKResource("aws_apigatewayv2_model", name="Model")
func resourceModel() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceModelCreate,
ReadWithoutTimeout: resourceModelRead,
UpdateWithoutTimeout: resourceModelUpdate,
DeleteWithoutTimeout: resourceModelDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceModelImport,
},
Schema: map[string]*schema.Schema{
"api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
names.AttrContentType: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 256),
},
names.AttrDescription: {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
names.AttrName: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 128),
validation.StringMatch(regexache.MustCompile(`^[0-9A-Za-z]+$`), "must be alphanumeric"),
),
},
names.AttrSchema: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(0, 32768),
validation.StringIsJSON,
),
DiffSuppressFunc: verify.SuppressEquivalentJSONDiffs,
DiffSuppressOnRefresh: true,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
},
}
}
func resourceModelCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).APIGatewayV2Client(ctx)
name := d.Get(names.AttrName).(string)
input := &apigatewayv2.CreateModelInput{
ApiId: aws.String(d.Get("api_id").(string)),
ContentType: aws.String(d.Get(names.AttrContentType).(string)),
Name: aws.String(name),
Schema: aws.String(d.Get(names.AttrSchema).(string)),
}
if v, ok := d.GetOk(names.AttrDescription); ok {
input.Description = aws.String(v.(string))
}
output, err := conn.CreateModel(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating API Gateway v2 Model (%s): %s", name, err)
}
d.SetId(aws.ToString(output.ModelId))
return append(diags, resourceModelRead(ctx, d, meta)...)
}
func resourceModelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).APIGatewayV2Client(ctx)
output, err := findModelByTwoPartKey(ctx, conn, d.Get("api_id").(string), d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] API Gateway v2 Model (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading API Gateway v2 Model (%s): %s", d.Id(), err)
}
d.Set(names.AttrContentType, output.ContentType)
d.Set(names.AttrDescription, output.Description)
d.Set(names.AttrName, output.Name)
d.Set(names.AttrSchema, output.Schema)
return diags
}
func resourceModelUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).APIGatewayV2Client(ctx)
input := &apigatewayv2.UpdateModelInput{
ApiId: aws.String(d.Get("api_id").(string)),
ModelId: aws.String(d.Id()),
}
if d.HasChange(names.AttrContentType) {
input.ContentType = aws.String(d.Get(names.AttrContentType).(string))
}
if d.HasChange(names.AttrDescription) {
input.Description = aws.String(d.Get(names.AttrDescription).(string))
}
if d.HasChange(names.AttrName) {
input.Name = aws.String(d.Get(names.AttrName).(string))
}
if d.HasChange(names.AttrSchema) {
input.Schema = aws.String(d.Get(names.AttrSchema).(string))
}
_, err := conn.UpdateModel(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating API Gateway v2 Model (%s): %s", d.Id(), err)
}
return append(diags, resourceModelRead(ctx, d, meta)...)
}
func resourceModelDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).APIGatewayV2Client(ctx)
log.Printf("[DEBUG] Deleting API Gateway v2 Model: %s", d.Id())
_, err := conn.DeleteModel(ctx, &apigatewayv2.DeleteModelInput{
ApiId: aws.String(d.Get("api_id").(string)),
ModelId: aws.String(d.Id()),
})
if errs.IsA[*awstypes.NotFoundException](err) {
return diags
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting API Gateway v2 Model (%s): %s", d.Id(), err)
}
return diags
}
func resourceModelImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("wrong format of import ID (%s), use: 'api-id/model-id'", d.Id())
}
d.SetId(parts[1])
d.Set("api_id", parts[0])
return []*schema.ResourceData{d}, nil
}
func findModelByTwoPartKey(ctx context.Context, conn *apigatewayv2.Client, apiID, modelID string) (*apigatewayv2.GetModelOutput, error) {
input := &apigatewayv2.GetModelInput{
ApiId: aws.String(apiID),
ModelId: aws.String(modelID),
}
return findModel(ctx, conn, input)
}
func findModel(ctx context.Context, conn *apigatewayv2.Client, input *apigatewayv2.GetModelInput) (*apigatewayv2.GetModelOutput, error) {
output, err := conn.GetModel(ctx, input)
if errs.IsA[*awstypes.NotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}
if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}
return output, nil
}