-
Notifications
You must be signed in to change notification settings - Fork 151
/
plugin_provider.go
317 lines (285 loc) · 9.8 KB
/
plugin_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"fmt"
"os"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-provider-tfe/internal/client"
)
type pluginProviderServer struct {
providerSchema *tfprotov5.Schema
providerMetaSchema *tfprotov5.Schema
resourceSchemas map[string]*tfprotov5.Schema
dataSourceSchemas map[string]*tfprotov5.Schema
tfeClient *tfe.Client
organization string
resourceRouter
dataSourceRouter map[string]func(ConfiguredClient) tfprotov5.DataSourceServer
}
type errUnsupportedDataSource string
func (e errUnsupportedDataSource) Error() string {
return "unsupported data source: " + string(e)
}
type errUnsupportedResource string
func (e errUnsupportedResource) Error() string {
return "unsupported resource: " + string(e)
}
type providerMeta struct {
token string
hostname string
sslSkipVerify bool
organization string
}
func (p *pluginProviderServer) GetMetadata(ctx context.Context, req *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) {
return &tfprotov5.GetMetadataResponse{
ServerCapabilities: &tfprotov5.ServerCapabilities{
GetProviderSchemaOptional: true,
},
DataSources: []tfprotov5.DataSourceMetadata{
{
TypeName: "tfe_outputs",
},
},
}, nil
}
func (p *pluginProviderServer) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
return &tfprotov5.GetProviderSchemaResponse{
Provider: p.providerSchema,
ProviderMeta: p.providerMetaSchema,
ResourceSchemas: p.resourceSchemas,
DataSourceSchemas: p.dataSourceSchemas,
}, nil
}
func (p *pluginProviderServer) PrepareProviderConfig(ctx context.Context, req *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) {
return nil, nil
}
func (p *pluginProviderServer) ConfigureProvider(ctx context.Context, req *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
resp := &tfprotov5.ConfigureProviderResponse{
Diagnostics: []*tfprotov5.Diagnostic{},
}
meta, err := retrieveProviderMeta(req)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Error retrieving provider meta values for internal provider.",
Detail: fmt.Sprintf("This should never happen; please report it to https://github.com/hashicorp/terraform-provider-tfe/issues\n\nThe error received was: %q", err.Error()),
})
return resp, nil
}
tfeClient, err := client.GetClient(meta.hostname, meta.token, meta.sslSkipVerify)
if err != nil {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Error getting client",
Detail: fmt.Sprintf("Error getting client: %v", err),
})
return resp, nil
}
if meta.organization == "" {
meta.organization = os.Getenv("TFE_ORGANIZATION")
}
p.tfeClient = tfeClient
p.organization = meta.organization
return resp, nil
}
func (p *pluginProviderServer) StopProvider(ctx context.Context, req *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) {
return &tfprotov5.StopProviderResponse{}, nil
}
func (p *pluginProviderServer) ValidateDataSourceConfig(ctx context.Context, req *tfprotov5.ValidateDataSourceConfigRequest) (*tfprotov5.ValidateDataSourceConfigResponse, error) {
ds, ok := p.dataSourceRouter[req.TypeName]
if !ok {
return nil, errUnsupportedDataSource(req.TypeName)
}
return ds(ConfiguredClient{p.tfeClient, p.organization}).ValidateDataSourceConfig(ctx, req)
}
func (p *pluginProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error) {
ds, ok := p.dataSourceRouter[req.TypeName]
if !ok {
return nil, errUnsupportedDataSource(req.TypeName)
}
return ds(ConfiguredClient{p.tfeClient, p.organization}).ReadDataSource(ctx, req)
}
type resourceRouter map[string]tfprotov5.ResourceServer
func (r resourceRouter) ValidateResourceTypeConfig(ctx context.Context, req *tfprotov5.ValidateResourceTypeConfigRequest) (*tfprotov5.ValidateResourceTypeConfigResponse, error) {
res, ok := r[req.TypeName]
if !ok {
return nil, errUnsupportedResource(req.TypeName)
}
return res.ValidateResourceTypeConfig(ctx, req)
}
func (r resourceRouter) UpgradeResourceState(ctx context.Context, req *tfprotov5.UpgradeResourceStateRequest) (*tfprotov5.UpgradeResourceStateResponse, error) {
res, ok := r[req.TypeName]
if !ok {
return nil, errUnsupportedResource(req.TypeName)
}
return res.UpgradeResourceState(ctx, req)
}
func (r resourceRouter) ReadResource(ctx context.Context, req *tfprotov5.ReadResourceRequest) (*tfprotov5.ReadResourceResponse, error) {
res, ok := r[req.TypeName]
if !ok {
return nil, errUnsupportedResource(req.TypeName)
}
return res.ReadResource(ctx, req)
}
func (r resourceRouter) PlanResourceChange(ctx context.Context, req *tfprotov5.PlanResourceChangeRequest) (*tfprotov5.PlanResourceChangeResponse, error) {
res, ok := r[req.TypeName]
if !ok {
return nil, errUnsupportedResource(req.TypeName)
}
return res.PlanResourceChange(ctx, req)
}
func (r resourceRouter) ApplyResourceChange(ctx context.Context, req *tfprotov5.ApplyResourceChangeRequest) (*tfprotov5.ApplyResourceChangeResponse, error) {
res, ok := r[req.TypeName]
if !ok {
return nil, errUnsupportedResource(req.TypeName)
}
return res.ApplyResourceChange(ctx, req)
}
func (r resourceRouter) ImportResourceState(ctx context.Context, req *tfprotov5.ImportResourceStateRequest) (*tfprotov5.ImportResourceStateResponse, error) {
res, ok := r[req.TypeName]
if !ok {
return nil, errUnsupportedResource(req.TypeName)
}
return res.ImportResourceState(ctx, req)
}
// PluginProviderServer returns the implementation of an interface for a lower
// level usage of the Provider to Terraform protocol.
// This relies on the terraform-plugin-go library, which provides low level
// bindings for the Terraform plugin protocol.
func PluginProviderServer() tfprotov5.ProviderServer {
return &pluginProviderServer{
providerSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "hostname",
Type: tftypes.String,
Description: descriptions["hostname"],
Optional: true,
},
{
Name: "token",
Type: tftypes.String,
Description: descriptions["token"],
Optional: true,
},
{
Name: "ssl_skip_verify",
Type: tftypes.Bool,
Description: descriptions["ssl_skip_verify"],
Optional: true,
},
{
Name: "organization",
Type: tftypes.String,
Description: descriptions["organization"],
Optional: true,
},
},
},
},
dataSourceSchemas: map[string]*tfprotov5.Schema{
"tfe_outputs": {
Version: 1,
Block: &tfprotov5.SchemaBlock{
Version: 1,
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "id",
Type: tftypes.String,
Computed: true,
},
{
Name: "workspace",
Type: tftypes.String,
Description: "The workspace to fetch the remote state from.",
DescriptionKind: tfprotov5.StringKindPlain,
Required: true,
},
{
Name: "organization",
Type: tftypes.String,
Description: "The organization to fetch the remote state from.",
DescriptionKind: tfprotov5.StringKindPlain,
Optional: true,
},
{
Name: "values",
Type: tftypes.DynamicPseudoType,
Optional: true,
Computed: true,
Sensitive: true,
},
{
Name: "nonsensitive_values",
Type: tftypes.DynamicPseudoType,
Computed: true,
Sensitive: false,
},
},
},
},
},
dataSourceRouter: map[string]func(ConfiguredClient) tfprotov5.DataSourceServer{
"tfe_outputs": newDataSourceOutputs,
},
}
}
func retrieveProviderMeta(req *tfprotov5.ConfigureProviderRequest) (providerMeta, error) {
meta := providerMeta{}
config := req.Config
val, err := config.Unmarshal(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"hostname": tftypes.String,
"token": tftypes.String,
"ssl_skip_verify": tftypes.Bool,
"organization": tftypes.String,
}})
if err != nil {
return meta, fmt.Errorf("could not unmarshal ConfigureProviderRequest %w", err)
}
var hostname string
var token string
var sslSkipVerify bool
var organization string
var valMap map[string]tftypes.Value
err = val.As(&valMap)
if err != nil {
return meta, fmt.Errorf("could not set the schema attributes to map %w", err)
}
if !valMap["hostname"].IsNull() {
err = valMap["hostname"].As(&hostname)
if err != nil {
return meta, fmt.Errorf("could not set the hostname value to string %w", err)
}
}
if !valMap["token"].IsNull() {
err = valMap["token"].As(&token)
if err != nil {
return meta, fmt.Errorf("could not set the token value to string %w", err)
}
}
if !valMap["ssl_skip_verify"].IsNull() {
err = valMap["ssl_skip_verify"].As(&sslSkipVerify)
if err != nil {
return meta, fmt.Errorf("could not set the ssl_skip_verify value to boolean %w", err)
}
} else {
sslSkipVerify = defaultSSLSkipVerify
}
if !valMap["organization"].IsNull() {
err = valMap["organization"].As(&organization)
if err != nil {
return meta, fmt.Errorf("failed to set the organization value to string: %w", err)
}
}
meta.hostname = hostname
meta.token = token
meta.sslSkipVerify = sslSkipVerify
meta.organization = organization
return meta, nil
}