forked from vmware/terraform-provider-vcfa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
370 lines (325 loc) · 15.5 KB
/
provider.go
File metadata and controls
370 lines (325 loc) · 15.5 KB
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
package vcfa
import (
"context"
"fmt"
"os"
"regexp"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/go-vcloud-director/v3/util"
)
// BuildVersion holds version which is meant to be injected at build time using ldflags
// (e.g. 'go build -ldflags="-X 'github.com/vmware/terraform-provider-vcfa/vcfa.BuildVersion=v1.0.0'"')
var BuildVersion = "unset"
// DataSources is a public function which allows filtering and access all defined data sources
// When 'nameRegexp' is not empty - it will return only those matching the regexp
// When 'includeDeprecated' is false - it will skip out the resources which have a DeprecationMessage set
func DataSources(nameRegexp string, includeDeprecated bool) (map[string]*schema.Resource, error) {
return vcfaSchemaFilter(globalDataSourceMap, nameRegexp, includeDeprecated)
}
// Resources is a public function which allows filtering and access all defined resources
// When 'nameRegexp' is not empty - it will return only those matching the regexp
// When 'includeDeprecated' is false - it will skip out the resources which have a DeprecationMessage set
func Resources(nameRegexp string, includeDeprecated bool) (map[string]*schema.Resource, error) {
return vcfaSchemaFilter(globalResourceMap, nameRegexp, includeDeprecated)
}
var globalDataSourceMap = map[string]*schema.Resource{
"vcfa_tm_version": datasourceVcfaTmVersion(), // 1.0
"vcfa_vcenter": datasourceVcfaVcenter(), // 1.0
"vcfa_org": datasourceVcfaOrg(), // 1.0
"vcfa_nsx_manager": datasourceVcfaNsxManager(), // 1.0
"vcfa_supervisor": datasourceVcfaSupervisor(), // 1.0
"vcfa_supervisor_zone": datasourceVcfaSupervisorZone(), // 1.0
"vcfa_region": datasourceVcfaRegion(), // 1.0
"vcfa_ip_space": datasourceVcfaIpSpace(), // 1.0
"vcfa_region_zone": datasourceVcfaRegionZone(), // 1.0
"vcfa_org_vdc": datasourceVcfaOrgVdc(), // 1.0
"vcfa_region_storage_policy": datasourceVcfaRegionStoragePolicy(), // 1.0
"vcfa_storage_class": datasourceVcfaStorageClass(), // 1.0
"vcfa_content_library": datasourceVcfaContentLibrary(), // 1.0
"vcfa_content_library_item": datasourceVcfaContentLibraryItem(), // 1.0
"vcfa_tier0_gateway": datasourceVcfaTier0Gateway(), // 1.0
"vcfa_provider_gateway": datasourceVcfaProviderGateway(), // 1.0
"vcfa_edge_cluster": datasourceVcfaEdgeCluster(), // 1.0
"vcfa_edge_cluster_qos": datasourceVcfaEdgeClusterQos(), // 1.0
"vcfa_org_oidc": datasourceVcfaOrgOidc(), // 1.0
"vcfa_org_networking": datasourceVcfaOrgNetworking(), // 1.0
"vcfa_org_regional_networking": datasourceVcfaOrgRegionalNetworking(), // 1.0
"vcfa_org_regional_networking_vpc_qos": datasourceVcfaOrgRegionalNetworkingVpcQos(), // 1.0
"vcfa_right": datasourceVcfaRight(), // 1.0
"vcfa_rights_bundle": datasourceVcfaRightsBundle(),
}
var globalResourceMap = map[string]*schema.Resource{
"vcfa_vcenter": resourceVcfaVcenter(), // 1.0
"vcfa_org": resourceVcfaOrg(), // 1.0
"vcfa_nsx_manager": resourceVcfaNsxManager(), // 1.0
"vcfa_region": resourceVcfaRegion(), // 1.0
"vcfa_ip_space": resourceVcfaIpSpace(), // 1.0
"vcfa_org_vdc": resourceVcfaOrgVdc(), // 1.0
"vcfa_content_library": resourceVcfaContentLibrary(), // 1.0
"vcfa_content_library_item": resourceVcfaContentLibraryItem(), // 1.0
"vcfa_provider_gateway": resourceVcfaProviderGateway(), // 1.0
"vcfa_edge_cluster_qos": resourceVcfaEdgeClusterQos(), // 1.0
"vcfa_org_oidc": resourceVcfaOrgOidc(), // 1.0
"vcfa_org_networking": resourceVcfaOrgNetworking(), // 1.0
"vcfa_org_regional_networking": resourceVcfaOrgRegionalNetworking(), // 1.0
"vcfa_org_regional_networking_vpc_qos": resourceVcfaOrgRegionalNetworkingVpcQos(), // 1.0
"vcfa_rights_bundle": resourceVcfaRightsBundle(), // 1.0
}
// Provider returns a terraform.ResourceProvider.
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
// TODO: VCFA: Revisit and review the existing options
"user": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_USER", nil),
Description: "The user name for VCFA API operations.",
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_PASSWORD", nil),
Description: "The user password for VCFA API operations.",
},
"auth_type": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_AUTH_TYPE", "integrated"),
Description: "'integrated', 'token', 'api_token', 'api_token_file' and 'service_account_token_file' are supported. 'integrated' is default.",
ValidateFunc: validation.StringInSlice([]string{"integrated", "token", "api_token", "api_token_file", "service_account_token_file"}, false),
},
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_TOKEN", nil),
Description: "The token used instead of username/password for VCFA API operations.",
},
"api_token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_API_TOKEN", nil),
Description: "The API token used instead of username/password for VCFA API operations. (Requires VCFA 10.3.1+)",
},
"api_token_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_API_TOKEN_FILE", nil),
Description: "The API token file instead of username/password for VCFA API operations. (Requires VCFA 10.3.1+)",
},
"allow_api_token_file": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Set this to true if you understand the security risks of using API token files and would like to suppress the warnings",
},
"service_account_token_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_SA_TOKEN_FILE", nil),
Description: "The Service Account API token file instead of username/password for VCFA API operations. (Requires VCFA 9.0+)",
},
"allow_service_account_token_file": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Set this to true if you understand the security risks of using Service Account token files and would like to suppress the warnings",
},
"sysorg": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_SYS_ORG", nil),
Description: "The VCFA Org for user authentication",
},
"org": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_ORG", nil),
Description: "The VCFA Org for API operations",
},
"vdc": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_VDC", nil),
Description: "The VDC for API operations",
},
"url": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_URL", nil),
Description: "The VCFA url for VCFA API operations.",
},
"allow_unverified_ssl": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_ALLOW_UNVERIFIED_SSL", false),
Description: "If set, VCFAClient will permit unverifiable SSL certificates.",
},
"logging": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_API_LOGGING", false),
Description: "If set, it will enable logging of API requests and responses",
},
"logging_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_API_LOGGING_FILE", "go-vcloud-director.log"),
Description: "Defines the full name of the logging file for API calls (requires 'logging')",
},
"import_separator": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCFA_IMPORT_SEPARATOR", "."),
Description: "Defines the import separation string to be used with 'terraform import'",
},
},
ResourcesMap: globalResourceMap,
DataSourcesMap: globalDataSourceMap,
ConfigureContextFunc: providerConfigure,
}
}
func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
if err := validateProviderSchema(d); err != nil {
return nil, diag.Errorf("[provider validation] :%s", err)
}
// If sysOrg is defined, we use it for authentication.
// Otherwise, we use the default org defined for regular usage
connectOrg := d.Get("sysorg").(string)
if connectOrg == "" {
connectOrg = d.Get("org").(string)
}
config := Config{
User: d.Get("user").(string),
Password: d.Get("password").(string),
Token: d.Get("token").(string),
ApiToken: d.Get("api_token").(string),
ApiTokenFile: d.Get("api_token_file").(string),
AllowApiTokenFile: d.Get("allow_api_token_file").(bool),
ServiceAccountTokenFile: d.Get("service_account_token_file").(string),
AllowSATokenFile: d.Get("allow_service_account_token_file").(bool),
SysOrg: connectOrg, // Connection org
Org: d.Get("org").(string), // Default org for operations
Vdc: d.Get("vdc").(string), // Default vdc
Href: d.Get("url").(string),
InsecureFlag: d.Get("allow_unverified_ssl").(bool),
}
// auth_type dependent configuration
authType := d.Get("auth_type").(string)
switch authType {
case "token":
if config.Token == "" {
return nil, diag.Errorf("empty token detected with 'auth_type' == 'token'")
}
case "api_token":
if config.ApiToken == "" {
return nil, diag.Errorf("empty API token detected with 'auth_type' == 'api_token'")
}
case "service_account_token_file":
if config.ServiceAccountTokenFile == "" {
return nil, diag.Errorf("service account token file not provided with 'auth_type' == 'service_account_token_file'")
}
case "api_token_file":
if config.ApiTokenFile == "" {
return nil, diag.Errorf("api token file not provided with 'auth_type' == 'service_account_token_file'")
}
default:
if config.ApiToken != "" || config.Token != "" {
return nil, diag.Errorf("to use a token, the appropriate 'auth_type' (either 'token' or 'api_token') must be set")
}
}
if config.ApiToken != "" && config.Token != "" {
return nil, diag.Errorf("only one of 'token' or 'api_token' should be set")
}
var providerDiagnostics diag.Diagnostics
if config.ServiceAccountTokenFile != "" && !config.AllowSATokenFile {
providerDiagnostics = append(providerDiagnostics, diag.Diagnostic{
Severity: diag.Warning,
Summary: "The file " + config.ServiceAccountTokenFile + " should be considered sensitive information.",
Detail: "The file " + config.ServiceAccountTokenFile + " containing the initial service account API " +
"HAS BEEN UPDATED with a freshly generated token. The initial token was invalidated and the " +
"token currently in the file will be invalidated at the next usage. In the meantime, it is " +
"usable by anyone to run operations to the current VCFA. As such, it should be considered SENSITIVE INFORMATION. " +
"If you would like to remove this warning, add\n\n" + " allow_service_account_token_file = true\n\nto the provider settings.",
})
}
if config.ApiTokenFile != "" && !config.AllowApiTokenFile {
providerDiagnostics = append(providerDiagnostics, diag.Diagnostic{
Severity: diag.Warning,
Summary: "The file " + config.ServiceAccountTokenFile + " should be considered sensitive information.",
Detail: "The file " + config.ServiceAccountTokenFile + " contains the API token which can be used by anyone " +
"to run operations to the current VCFA. AS such, it should be considered SENSITIVE INFORMATION. " +
"If you would like to remove this warning, add\n\n" + " allow_api_token_file = true\n\nto the provider settings.",
})
}
// If the provider includes logging directives,
// it will activate logging from upstream go-vcloud-director
logging := d.Get("logging").(bool)
// Logging is disabled by default.
// If enabled, we set the log file name and invoke the upstream logging set-up
if logging {
loggingFile := d.Get("logging_file").(string)
if loggingFile != "" {
util.EnableLogging = true
util.ApiLogFileName = loggingFile
util.InitLogging()
}
}
separator := os.Getenv("VCFA_IMPORT_SEPARATOR")
if separator != "" {
ImportSeparator = separator
} else {
ImportSeparator = d.Get("import_separator").(string)
}
vcdClient, err := config.Client()
if err != nil {
return nil, diag.FromErr(err)
}
return vcdClient, providerDiagnostics
}
// vcfaSchemaFilter is a function which allows to filters and export type 'map[string]*schema.Resource' which may hold
// Terraform's native resource or data source list
// When 'nameRegexp' is not empty - it will return only those matching the regexp
// When 'includeDeprecated' is false - it will skip out the resources which have a DeprecationMessage set
func vcfaSchemaFilter(schemaMap map[string]*schema.Resource, nameRegexp string, includeDeprecated bool) (map[string]*schema.Resource, error) {
var (
err error
re *regexp.Regexp
)
filteredResources := make(map[string]*schema.Resource)
// validate regex if it was provided
if nameRegexp != "" {
re, err = regexp.Compile(nameRegexp)
if err != nil {
return nil, fmt.Errorf("unable to compile regexp: %s", err)
}
}
// copy the map with filtering out unwanted object
for resourceName, schemaResource := range schemaMap {
// Skip deprecated resources if it was requested so
if !includeDeprecated && schemaResource.DeprecationMessage != "" {
continue
}
// If regex was defined - try to filter based on it
if re != nil {
// if it does not match regex - skip it
doesNotmatchRegex := !re.MatchString(resourceName)
if doesNotmatchRegex {
continue
}
}
filteredResources[resourceName] = schemaResource
}
return filteredResources, nil
}
func validateProviderSchema(d *schema.ResourceData) error {
// Validate org and sys org
sysOrg := d.Get("sysorg").(string)
org := d.Get("org").(string)
if sysOrg == "" && org == "" {
return fmt.Errorf(`both "org" and "sysorg" properties are empty`)
}
return nil
}