-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathprovider_next.go
More file actions
188 lines (166 loc) · 6.43 KB
/
provider_next.go
File metadata and controls
188 lines (166 loc) · 6.43 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"os"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-tfe/internal/client"
)
// frameworkProvider is a type that implements the terraform-plugin-framework
// provider.Provider interface. Someday, this will probably encompass the entire
// behavior of the tfe provider. Today, it is a small but growing subset.
type frameworkProvider struct {
defaultOrgName *string
}
// Compile-time interface check
var _ provider.Provider = &frameworkProvider{}
var _ provider.ProviderWithEphemeralResources = &frameworkProvider{}
// FrameworkProviderConfig is a helper type for extracting the provider
// configuration from the provider block.
type FrameworkProviderConfig struct {
Hostname types.String `tfsdk:"hostname"`
Token types.String `tfsdk:"token"`
Organization types.String `tfsdk:"organization"`
SSLSkipVerify types.Bool `tfsdk:"ssl_skip_verify"`
}
// NewFrameworkProvider is a helper function for initializing the portion of
// the tfe provider implemented via the terraform-plugin-framework.
func NewFrameworkProvider() provider.Provider {
return &frameworkProvider{}
}
// NewFrameworkProviderWithDefaultOrg is a helper function for
// initializing a framework provider with a default organization name.
func NewFrameworkProviderWithDefaultOrg(defaultOrgName string) provider.Provider {
return &frameworkProvider{defaultOrgName: &defaultOrgName}
}
// Metadata (a Provider interface function) lets the provider identify itself.
// Resources and data sources can access this information from their request
// objects.
func (p *frameworkProvider) Metadata(_ context.Context, _ provider.MetadataRequest, res *provider.MetadataResponse) {
res.TypeName = "tfe"
}
// Schema (a Provider interface function) returns the schema for the Terraform
// block that configures the provider itself.
func (p *frameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest, res *provider.SchemaResponse) {
res.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"hostname": schema.StringAttribute{
Description: descriptions["hostname"],
Optional: true,
},
"token": schema.StringAttribute{
Optional: true,
Description: descriptions["token"],
// TODO: should be sensitive, but that's a breaking change.
},
"organization": schema.StringAttribute{
Description: descriptions["organization"],
Optional: true,
},
"ssl_skip_verify": schema.BoolAttribute{
Description: descriptions["ssl_skip_verify"],
Optional: true,
},
},
}
}
// Configure (a Provider interface function) sets up the HCP Terraform client per the
// specified provider configuration block and env vars.
func (p *frameworkProvider) Configure(ctx context.Context, req provider.ConfigureRequest, res *provider.ConfigureResponse) {
var data FrameworkProviderConfig
diags := req.Config.Get(ctx, &data)
res.Diagnostics.Append(diags...)
if res.Diagnostics.HasError() {
return
}
// TODO: add .IsUnknown() error handling in the case where user passed
// unresolvable references for provider config values, c.f. what hashicups
// does around
// https://github.com/hashicorp/terraform-provider-hashicups-pf/blob/main/hashicups/provider.go#L79
// Read default organization from environment if it wasn't set in the
// config. All other env defaults are handled by getClient().
if data.Organization.IsNull() {
// Falling back to Getenv will collapse the new type system's handling
// of null/unknown into a plain zero-value, but that's OK at this point.
data.Organization = types.StringValue(os.Getenv("TFE_ORGANIZATION"))
// Override if a default was passed to NewFrameworkProviderWithDefaultOrg.
// This is primarily for acceptance testing purposes.
if p.defaultOrgName != nil {
data.Organization = types.StringValue(*p.defaultOrgName)
}
}
tfeClient, err := client.GetClient(data.Hostname.ValueString(), data.Token.ValueString(), data.SSLSkipVerify.ValueBool())
if err != nil {
res.Diagnostics.AddError("Failed to initialize HTTP client", err.Error())
return
}
configuredClient := ConfiguredClient{
Client: tfeClient,
Organization: data.Organization.ValueString(),
}
res.DataSourceData = configuredClient
res.ResourceData = configuredClient
res.EphemeralResourceData = configuredClient
}
func (p *frameworkProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewHYOKCustomerKeyVersionDataSource,
NewHYOKEncryptedDataKeyDataSource,
NewNoCodeModuleDataSource,
NewOrganizationRunTaskDataSource,
NewOrganizationRunTaskGlobalSettingsDataSource,
NewOutputsDataSource,
NewProjectDataSource,
NewProjectsDataSource,
NewRegistryGPGKeyDataSource,
NewRegistryGPGKeysDataSource,
NewRegistryModuleDataSource,
NewRegistryProviderDataSource,
NewRegistryProvidersDataSource,
NewSAMLSettingsDataSource,
NewVariablesDataSource,
NewWorkspaceRunTaskDataSource,
}
}
func (p *frameworkProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewAuditTrailTokenResource,
NewDataRetentionPolicyResource,
NewOrganizationDefaultSettings,
NewOrganizationRunTaskGlobalSettingsResource,
NewOrganizationRunTaskResource,
NewPolicySetParameterResource,
NewProjectResource,
NewRegistryGPGKeyResource,
NewRegistryProviderResource,
NewResourceVariable,
NewResourceWorkspaceSettings,
NewSAMLSettingsResource,
NewSSHKey,
NewStackResource,
NewTeamNotificationConfigurationResource,
NewTestVariableResource,
NewWorkspaceRunTaskResource,
NewNotificationConfigurationResource,
NewTeamTokenResource,
NewProjectSettingsResource,
NewTerraformVersionResource,
NewOPAVersionResource,
NewsentinelVersionResource,
}
}
func (p *frameworkProvider) EphemeralResources(ctx context.Context) []func() ephemeral.EphemeralResource {
return []func() ephemeral.EphemeralResource{
NewAgentTokenEphemeralResource,
NewOrganizationTokenEphemeralResource,
NewOutputsEphemeralResource,
NewTeamTokenEphemeralResource,
NewAuditTrailTokenEphemeralResource,
}
}