/
provider.go
509 lines (436 loc) · 16.1 KB
/
provider.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package schema
import (
"context"
"errors"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
const uaEnvVar = "TF_APPEND_USER_AGENT"
var ReservedProviderFields = []string{
"alias",
"version",
}
// StopContext returns a context safe for global use that will cancel
// when Terraform requests a stop. This function should only be called
// within a ConfigureContextFunc, passing in the request scoped context
// received in that method.
//
// Deprecated: The use of a global context is discouraged. Please use the new
// context aware CRUD methods.
func StopContext(ctx context.Context) (context.Context, bool) {
stopContext, ok := ctx.Value(StopContextKey).(context.Context)
return stopContext, ok
}
// Provider represents a resource provider in Terraform, and properly
// implements all of the ResourceProvider API.
//
// By defining a schema for the configuration of the provider, the
// map of supporting resources, and a configuration function, the schema
// framework takes over and handles all the provider operations for you.
//
// After defining the provider structure, it is unlikely that you'll require any
// of the methods on Provider itself.
type Provider struct {
// Schema is the schema for the configuration of this provider. If this
// provider has no configuration, this can be omitted.
//
// The keys of this map are the configuration keys, and the value is
// the schema describing the value of the configuration.
Schema map[string]*Schema
// ResourcesMap is the list of available resources that this provider
// can manage, along with their Resource structure defining their
// own schemas and CRUD operations.
//
// Provider automatically handles routing operations such as Apply,
// Diff, etc. to the proper resource.
ResourcesMap map[string]*Resource
// DataSourcesMap is the collection of available data sources that
// this provider implements, with a Resource instance defining
// the schema and Read operation of each.
//
// Resource instances for data sources must have a Read function
// and must *not* implement Create, Update or Delete.
DataSourcesMap map[string]*Resource
// ProviderMetaSchema is the schema for the configuration of the meta
// information for this provider. If this provider has no meta info,
// this can be omitted. This functionality is currently experimental
// and subject to change or break without warning; it should only be
// used by providers that are collaborating on its use with the
// Terraform team.
ProviderMetaSchema map[string]*Schema
// ConfigureFunc is a function for configuring the provider. If the
// provider doesn't need to be configured, this can be omitted.
//
// Deprecated: Please use ConfigureContextFunc instead.
ConfigureFunc ConfigureFunc
// ConfigureContextFunc is a function for configuring the provider. If the
// provider doesn't need to be configured, this can be omitted. This function
// receives a context.Context that will cancel when Terraform sends a
// cancellation signal. This function can yield Diagnostics.
ConfigureContextFunc ConfigureContextFunc
// configured is enabled after a Configure() call
configured bool
meta interface{}
TerraformVersion string
}
// ConfigureFunc is the function used to configure a Provider.
//
// Deprecated: Please use ConfigureContextFunc
type ConfigureFunc func(*ResourceData) (interface{}, error)
// ConfigureContextFunc is the function used to configure a Provider.
//
// The interface{} value returned by this function is stored and passed into
// the subsequent resources as the meta parameter. This return value is
// usually used to pass along a configured API client, a configuration
// structure, etc.
type ConfigureContextFunc func(context.Context, *ResourceData) (interface{}, diag.Diagnostics)
// InternalValidate should be called to validate the structure
// of the provider.
//
// This should be called in a unit test for any provider to verify
// before release that a provider is properly configured for use with
// this library.
func (p *Provider) InternalValidate() error {
if p == nil {
return errors.New("provider is nil")
}
if p.ConfigureFunc != nil && p.ConfigureContextFunc != nil {
return errors.New("ConfigureFunc and ConfigureContextFunc must not both be set")
}
var validationErrors error
sm := schemaMap(p.Schema)
if err := sm.InternalValidate(sm); err != nil {
validationErrors = multierror.Append(validationErrors, err)
}
// Provider-specific checks
for k := range sm {
if isReservedProviderFieldName(k) {
return fmt.Errorf("%s is a reserved field name for a provider", k)
}
}
for k, r := range p.ResourcesMap {
if err := r.InternalValidate(nil, true); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
}
}
for k, r := range p.DataSourcesMap {
if err := r.InternalValidate(nil, false); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
}
}
return validationErrors
}
func isReservedProviderFieldName(name string) bool {
for _, reservedName := range ReservedProviderFields {
if name == reservedName {
return true
}
}
return false
}
// Meta returns the metadata associated with this provider that was
// returned by the Configure call. It will be nil until Configure is called.
func (p *Provider) Meta() interface{} {
return p.meta
}
// SetMeta can be used to forcefully set the Meta object of the provider.
// Note that if Configure is called the return value will override anything
// set here.
func (p *Provider) SetMeta(v interface{}) {
p.meta = v
}
// GetSchema returns the config schema for the main provider
// configuration, as would appear in a "provider" block in the
// configuration files.
//
// Currently not all providers support schema. Callers must therefore
// first call Resources and DataSources and ensure that at least one
// resource or data source has the SchemaAvailable flag set.
func (p *Provider) GetSchema(req *terraform.ProviderSchemaRequest) (*terraform.ProviderSchema, error) {
resourceTypes := map[string]*configschema.Block{}
dataSources := map[string]*configschema.Block{}
for _, name := range req.ResourceTypes {
if r, exists := p.ResourcesMap[name]; exists {
resourceTypes[name] = r.CoreConfigSchema()
}
}
for _, name := range req.DataSources {
if r, exists := p.DataSourcesMap[name]; exists {
dataSources[name] = r.CoreConfigSchema()
}
}
return &terraform.ProviderSchema{
Provider: schemaMap(p.Schema).CoreConfigSchema(),
ResourceTypes: resourceTypes,
DataSources: dataSources,
}, nil
}
// Validate is called once at the beginning with the raw configuration
// (no interpolation done) and can return diagnostics
//
// This is called once with the provider configuration only. It may not
// be called at all if no provider configuration is given.
//
// This should not assume that any values of the configurations are valid.
// The primary use case of this call is to check that required keys are
// set.
func (p *Provider) Validate(c *terraform.ResourceConfig) diag.Diagnostics {
if err := p.InternalValidate(); err != nil {
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: "InternalValidate",
Detail: fmt.Sprintf("Internal validation of the provider failed! This is always a bug\n"+
"with the provider itself, and not a user issue. Please report\n"+
"this bug:\n\n%s", err),
},
}
}
return schemaMap(p.Schema).Validate(c)
}
// ValidateResource is called once at the beginning with the raw
// configuration (no interpolation done) and can return diagnostics.
//
// This is called once per resource.
//
// This should not assume any of the values in the resource configuration
// are valid since it is possible they have to be interpolated still.
// The primary use case of this call is to check that the required keys
// are set and that the general structure is correct.
func (p *Provider) ValidateResource(
t string, c *terraform.ResourceConfig) diag.Diagnostics {
r, ok := p.ResourcesMap[t]
if !ok {
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf("Provider doesn't support resource: %s", t),
},
}
}
return r.Validate(c)
}
// Configure configures the provider itself with the configuration
// given. This is useful for setting things like access keys.
//
// This won't be called at all if no provider configuration is given.
func (p *Provider) Configure(ctx context.Context, c *terraform.ResourceConfig) diag.Diagnostics {
// No configuration
if p.ConfigureFunc == nil && p.ConfigureContextFunc == nil {
return nil
}
if p.configured {
logging.HelperSchemaWarn(ctx, "Previously configured provider being re-configured. This can cause issues in concurrent testing if the configurations are not equal.")
}
sm := schemaMap(p.Schema)
// Get a ResourceData for this configuration. To do this, we actually
// generate an intermediary "diff" although that is never exposed.
diff, err := sm.Diff(ctx, nil, c, nil, p.meta, true)
if err != nil {
return diag.FromErr(err)
}
data, err := sm.Data(nil, diff)
if err != nil {
return diag.FromErr(err)
}
if p.ConfigureFunc != nil {
meta, err := p.ConfigureFunc(data)
if err != nil {
return diag.FromErr(err)
}
p.meta = meta
}
var diags diag.Diagnostics
if p.ConfigureContextFunc != nil {
meta, configureDiags := p.ConfigureContextFunc(ctx, data)
diags = append(diags, configureDiags...)
if diags.HasError() {
return diags
}
p.meta = meta
}
p.configured = true
return diags
}
// Resources returns all the available resource types that this provider
// knows how to manage.
func (p *Provider) Resources() []terraform.ResourceType {
keys := make([]string, 0, len(p.ResourcesMap))
for k := range p.ResourcesMap {
keys = append(keys, k)
}
sort.Strings(keys)
result := make([]terraform.ResourceType, 0, len(keys))
for _, k := range keys {
resource := p.ResourcesMap[k]
// This isn't really possible (it'd fail InternalValidate), but
// we do it anyways to avoid a panic.
if resource == nil {
resource = &Resource{}
}
result = append(result, terraform.ResourceType{
Name: k,
Importable: resource.Importer != nil,
// Indicates that a provider is compiled against a new enough
// version of core to support the GetSchema method.
SchemaAvailable: true,
})
}
return result
}
// ImportState requests that the given resource be imported.
//
// The returned InstanceState only requires ID be set. Importing
// will always call Refresh after the state to complete it.
//
// IMPORTANT: InstanceState doesn't have the resource type attached
// to it. A type must be specified on the state via the Ephemeral
// field on the state.
//
// This function can return multiple states. Normally, an import
// will map 1:1 to a physical resource. However, some resources map
// to multiple. For example, an AWS security group may contain many rules.
// Each rule is represented by a separate resource in Terraform,
// therefore multiple states are returned.
func (p *Provider) ImportState(
ctx context.Context,
info *terraform.InstanceInfo,
id string) ([]*terraform.InstanceState, error) {
// Find the resource
r, ok := p.ResourcesMap[info.Type]
if !ok {
return nil, fmt.Errorf("unknown resource type: %s", info.Type)
}
// If it doesn't support import, error
if r.Importer == nil {
return nil, fmt.Errorf("resource %s doesn't support import", info.Type)
}
// Create the data
data := r.Data(nil)
data.SetId(id)
data.SetType(info.Type)
// Call the import function
results := []*ResourceData{data}
if r.Importer.State != nil || r.Importer.StateContext != nil {
var err error
logging.HelperSchemaTrace(ctx, "Calling downstream")
if r.Importer.StateContext != nil {
results, err = r.Importer.StateContext(ctx, data, p.meta)
} else {
results, err = r.Importer.State(data, p.meta)
}
logging.HelperSchemaTrace(ctx, "Called downstream")
if err != nil {
return nil, err
}
}
// Convert the results to InstanceState values and return it
states := make([]*terraform.InstanceState, len(results))
for i, r := range results {
if r == nil {
return nil, fmt.Errorf("The provider returned a missing resource during ImportResourceState. " +
"This is generally a bug in the resource implementation for import. " +
"Resource import code should return an error for missing resources and skip returning a missing or empty ResourceData. " +
"Please report this to the provider developers.")
}
if r.Id() == "" {
return nil, fmt.Errorf("The provider returned a resource missing an identifier during ImportResourceState. " +
"This is generally a bug in the resource implementation for import. " +
"Resource import code should not call d.SetId(\"\") or create an empty ResourceData. " +
"If the resource is missing, instead return an error. " +
"Please report this to the provider developers.")
}
states[i] = r.State()
}
// Verify that all are non-nil. If there are any nil the error
// isn't obvious so we circumvent that with a friendlier error.
for _, s := range states {
if s == nil {
return nil, fmt.Errorf("The provider returned a missing resource during ImportResourceState. " +
"This is generally a bug in the resource implementation for import. " +
"Resource import code should return an error for missing resources. " +
"Please report this to the provider developers.")
}
}
return states, nil
}
// ValidateDataSource is called once at the beginning with the raw
// configuration (no interpolation done) and can return diagnostics.
//
// This is called once per data source instance.
//
// This should not assume any of the values in the resource configuration
// are valid since it is possible they have to be interpolated still.
// The primary use case of this call is to check that the required keys
// are set and that the general structure is correct.
func (p *Provider) ValidateDataSource(
t string, c *terraform.ResourceConfig) diag.Diagnostics {
r, ok := p.DataSourcesMap[t]
if !ok {
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf("Provider doesn't support data source: %s", t),
},
}
}
return r.Validate(c)
}
// DataSources returns all of the available data sources that this
// provider implements.
func (p *Provider) DataSources() []terraform.DataSource {
keys := make([]string, 0, len(p.DataSourcesMap))
for k := range p.DataSourcesMap {
keys = append(keys, k)
}
sort.Strings(keys)
result := make([]terraform.DataSource, 0, len(keys))
for _, k := range keys {
result = append(result, terraform.DataSource{
Name: k,
// Indicates that a provider is compiled against a new enough
// version of core to support the GetSchema method.
SchemaAvailable: true,
})
}
return result
}
// UserAgent returns a string suitable for use in the User-Agent header of
// requests generated by the provider. The generated string contains the
// version of Terraform, the Plugin SDK, and the provider used to generate the
// request. `name` should be the hyphen-separated reporting name of the
// provider, and `version` should be the version of the provider.
//
// If TF_APPEND_USER_AGENT is set, its value will be appended to the returned
// string.
func (p *Provider) UserAgent(name, version string) string {
ua := fmt.Sprintf("Terraform/%s (+https://www.terraform.io) Terraform-Plugin-SDK/%s", p.TerraformVersion, meta.SDKVersionString())
if name != "" {
ua += " " + name
if version != "" {
ua += "/" + version
}
}
if add := os.Getenv(uaEnvVar); add != "" {
add = strings.TrimSpace(add)
if len(add) > 0 {
ua += " " + add
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
}
}
return ua
}
// GRPCProvider returns a gRPC server, for use with terraform-plugin-mux.
func (p *Provider) GRPCProvider() tfprotov5.ProviderServer {
return NewGRPCProviderServer(p)
}