/
base.go
211 lines (167 loc) · 7.19 KB
/
base.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package framework
import (
"context"
"time"
"github.com/cloudbuzzbyone/terraform-provider-appflow/internal/conns"
"github.com/cloudbuzzbyone/terraform-provider-appflow/internal/framework/flex"
tftags "github.com/cloudbuzzbyone/terraform-provider-appflow/internal/tags"
"github.com/cloudbuzzbyone/terraform-provider-appflow/names"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type withMeta struct {
meta *conns.AWSClient
}
func (w *withMeta) Meta() *conns.AWSClient {
return w.meta
}
type withMigratedFromPluginSDK struct {
migrated bool
}
// SetMigratedFromPluginSDK sets whether or not the resource (or data source) has been migrated from terraform-plugin-sdk.
func (w *withMigratedFromPluginSDK) SetMigratedFromPluginSDK(migrated bool) {
w.migrated = migrated
}
// MigratedFromPluginSDK returns whether or not the resource (or data source) has been migrated from terraform-plugin-sdk.
func (w *withMigratedFromPluginSDK) MigratedFromPluginSDK() bool {
return w.migrated
}
// ResourceWithConfigure is a structure to be embedded within a Resource that implements the ResourceWithConfigure interface.
type ResourceWithConfigure struct {
withMeta
withMigratedFromPluginSDK
}
// Configure enables provider-level data or clients to be set in the
// provider-defined Resource type.
func (r *ResourceWithConfigure) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) {
if v, ok := request.ProviderData.(*conns.AWSClient); ok {
r.meta = v
}
}
// SetTagsAll calculates the new value for the `tags_all` attribute.
func (r *ResourceWithConfigure) SetTagsAll(ctx context.Context, request resource.ModifyPlanRequest, response *resource.ModifyPlanResponse) {
// If the entire plan is null, the resource is planned for destruction.
if request.Plan.Raw.IsNull() {
return
}
defaultTagsConfig := r.Meta().DefaultTagsConfig
ignoreTagsConfig := r.Meta().IgnoreTagsConfig
var planTags types.Map
response.Diagnostics.Append(request.Plan.GetAttribute(ctx, path.Root(names.AttrTags), &planTags)...)
if response.Diagnostics.HasError() {
return
}
if !planTags.IsUnknown() {
if !mapHasUnknownElements(planTags) {
resourceTags := tftags.New(ctx, planTags)
allTags := defaultTagsConfig.MergeTags(resourceTags).IgnoreConfig(ignoreTagsConfig)
response.Diagnostics.Append(response.Plan.SetAttribute(ctx, path.Root(names.AttrTagsAll), flex.FlattenFrameworkStringValueMapLegacy(ctx, allTags.Map()))...)
} else {
response.Diagnostics.Append(response.Plan.SetAttribute(ctx, path.Root(names.AttrTagsAll), tftags.Unknown)...)
}
} else {
response.Diagnostics.Append(response.Plan.SetAttribute(ctx, path.Root(names.AttrTagsAll), tftags.Unknown)...)
}
}
// WithImportByID is intended to be embedded in resources which import state via the "id" attribute.
// See https://developer.hashicorp.com/terraform/plugin/framework/resources/import.
type WithImportByID struct{}
func (w *WithImportByID) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root(names.AttrID), request, response)
}
// DataSourceWithConfigure is a structure to be embedded within a DataSource that implements the DataSourceWithConfigure interface.
type DataSourceWithConfigure struct {
withMeta
withMigratedFromPluginSDK
}
// Configure enables provider-level data or clients to be set in the
// provider-defined DataSource type. It is separately executed for each
// ReadDataSource RPC.
func (d *DataSourceWithConfigure) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
if v, ok := request.ProviderData.(*conns.AWSClient); ok {
d.meta = v
}
}
// WithTimeouts is intended to be embedded in resources which use the special "timeouts" nested block.
// See https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts.
type WithTimeouts struct {
defaultCreateTimeout, defaultReadTimeout, defaultUpdateTimeout, defaultDeleteTimeout time.Duration
}
// SetDefaultCreateTimeout sets the resource's default Create timeout value.
func (w *WithTimeouts) SetDefaultCreateTimeout(timeout time.Duration) {
w.defaultCreateTimeout = timeout
}
// SetDefaultReadTimeout sets the resource's default Read timeout value.
func (w *WithTimeouts) SetDefaultReadTimeout(timeout time.Duration) {
w.defaultReadTimeout = timeout
}
// SetDefaultUpdateTimeout sets the resource's default Update timeout value.
func (w *WithTimeouts) SetDefaultUpdateTimeout(timeout time.Duration) {
w.defaultUpdateTimeout = timeout
}
// SetDefaultDeleteTimeout sets the resource's default Delete timeout value.
func (w *WithTimeouts) SetDefaultDeleteTimeout(timeout time.Duration) {
w.defaultDeleteTimeout = timeout
}
// CreateTimeout returns any configured Create timeout value or the default value.
func (w *WithTimeouts) CreateTimeout(ctx context.Context, timeouts timeouts.Value) time.Duration {
timeout, diags := timeouts.Create(ctx, w.defaultCreateTimeout)
if errors := diags.Errors(); len(errors) > 0 {
tflog.Warn(ctx, "reading configured Create timeout", map[string]interface{}{
"summary": errors[0].Summary(),
"detail": errors[0].Detail(),
})
return w.defaultCreateTimeout
}
return timeout
}
// ReadTimeout returns any configured Read timeout value or the default value.
func (w *WithTimeouts) ReadTimeout(ctx context.Context, timeouts timeouts.Value) time.Duration {
timeout, diags := timeouts.Read(ctx, w.defaultReadTimeout)
if errors := diags.Errors(); len(errors) > 0 {
tflog.Warn(ctx, "reading configured Read timeout", map[string]interface{}{
"summary": errors[0].Summary(),
"detail": errors[0].Detail(),
})
return w.defaultReadTimeout
}
return timeout
}
// UpdateTimeout returns any configured Update timeout value or the default value.
func (w *WithTimeouts) UpdateTimeout(ctx context.Context, timeouts timeouts.Value) time.Duration {
timeout, diags := timeouts.Update(ctx, w.defaultUpdateTimeout)
if errors := diags.Errors(); len(errors) > 0 {
tflog.Warn(ctx, "reading configured Update timeout", map[string]interface{}{
"summary": errors[0].Summary(),
"detail": errors[0].Detail(),
})
return w.defaultUpdateTimeout
}
return timeout
}
// DeleteTimeout returns any configured Delete timeout value or the default value.
func (w *WithTimeouts) DeleteTimeout(ctx context.Context, timeouts timeouts.Value) time.Duration {
timeout, diags := timeouts.Delete(ctx, w.defaultDeleteTimeout)
if errors := diags.Errors(); len(errors) > 0 {
tflog.Warn(ctx, "reading configured Delete timeout", map[string]interface{}{
"summary": errors[0].Summary(),
"detail": errors[0].Detail(),
})
return w.defaultDeleteTimeout
}
return timeout
}
func mapHasUnknownElements(m types.Map) bool {
for _, v := range m.Elements() {
if v.IsUnknown() {
return true
}
}
return false
}