generated from hashicorp/terraform-provider-scaffolding-framework
/
provider.go
186 lines (157 loc) · 6.33 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
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/nsbno/terraform-provider-vy/internal/central_cognito"
"github.com/nsbno/terraform-provider-vy/internal/enroll_account"
"github.com/nsbno/terraform-provider-vy/internal/version_handler"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ provider.Provider = &VyProvider{}
// VyProvider satisfies the tfsdk.Provider interface and usually is included
// with all Resource and DataSource implementations.
type VyProvider struct {
// version is set to the VyProvider version on release, "dev" when the
// VyProvider is built and ran locally, and "test" when running acceptance
// testing.
version string
config *VyProviderConfiguration
}
type VyProviderConfiguration struct {
Environment string
CognitoClient *central_cognito.Client
EnrollAccountClient *enroll_account.Client
VersionHandlerClient *version_handler.Client
}
// VyProviderModel can be used to store data from the Terraform configuration.
type VyProviderModel struct {
CentralCognitoBaseUrl types.String `tfsdk:"central_cognito_base_url"`
EnrollAccountBaseUrl types.String `tfsdk:"enroll_account_base_url"`
Environment types.String `tfsdk:"environment"`
DeploymentServiceEnvironment types.String `tfsdk:"deployment_service_environment"`
}
func (p VyProvider) Metadata(ctx context.Context, request provider.MetadataRequest, response *provider.MetadataResponse) {
response.TypeName = "vy"
response.Version = p.version
}
func (p VyProvider) Schema(ctx context.Context, request provider.SchemaRequest, response *provider.SchemaResponse) {
response.Schema = schema.Schema{
MarkdownDescription: "A VyProvider for interracting with Vy's internal services.",
Attributes: map[string]schema.Attribute{
"central_cognito_base_url": schema.StringAttribute{
MarkdownDescription: "The base url for the central-cognito service",
Optional: true,
},
"enroll_account_base_url": schema.StringAttribute{
MarkdownDescription: "The base url for the deployment enrollment service",
Optional: true,
},
"environment": schema.StringAttribute{
MarkdownDescription: "The environment to provision in",
Required: true,
},
"deployment_service_environment": schema.StringAttribute{
MarkdownDescription: "The environment of the deployment service (this should be left blank unless you're testing the deployment service)",
Optional: true,
},
},
}
}
func createUrlFromEnvironment(baseUrl string, urlPrefix string, environment string) string {
if environment == "prod" {
return fmt.Sprintf("%s.%s", urlPrefix, baseUrl)
} else {
return fmt.Sprintf("%s.%s.%s", urlPrefix, environment, baseUrl)
}
}
func (p VyProvider) Configure(ctx context.Context, request provider.ConfigureRequest, response *provider.ConfigureResponse) {
var data VyProviderModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}
cognito_domain := "cognito.vydev.io"
if !data.CentralCognitoBaseUrl.IsNull() {
cognito_domain = data.CentralCognitoBaseUrl.ValueString()
}
enroll_account_domain := "vydeployment.vydev.io"
if !data.EnrollAccountBaseUrl.IsNull() {
enroll_account_domain = data.EnrollAccountBaseUrl.ValueString()
}
deployment_service_environment := "prod"
if !data.DeploymentServiceEnvironment.IsNull() {
response.Diagnostics.AddWarning(
"Non-prod deployment service environment",
"You have selected a non-prod deployment service environment. This should only be done while testing the deployment service.",
)
deployment_service_environment = data.DeploymentServiceEnvironment.ValueString()
}
cognito_client := ¢ral_cognito.Client{
BaseUrl: createUrlFromEnvironment(cognito_domain, "delegated", data.Environment.ValueString()),
}
enroll_client := &enroll_account.Client{
BaseUrl: createUrlFromEnvironment(enroll_account_domain, "enroll", deployment_service_environment),
}
version_client := &version_handler.Client{
BaseUrl: createUrlFromEnvironment(enroll_account_domain, "version-handler", deployment_service_environment),
}
config := &VyProviderConfiguration{
Environment: data.Environment.ValueString(),
CognitoClient: cognito_client,
EnrollAccountClient: enroll_client,
VersionHandlerClient: version_client,
}
p.config = config
response.ResourceData = config
response.DataSourceData = config
}
func (p VyProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewResourceServerResource,
NewAppClientResource,
NewDeploymentAccountResource,
NewEnvironmentAccountResource,
}
}
func (p VyProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewCognitoInfoDataSource,
NewArtifactVersionDataSource,
}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &VyProvider{
version: version,
}
}
}
// convertProviderType is a helper function for NewResource and NewDataSource
// implementations to associate the concrete provider type. Alternatively,
// this helper can be skipped and the provider type can be directly type
// asserted (e.g. provider: in.(*provider)), however using this can prevent
// potential panics.
func convertProviderType(in provider.Provider) (*VyProvider, diag.Diagnostics) {
var diags diag.Diagnostics
p, ok := in.(*VyProvider)
if !ok {
diags.AddError(
"Unexpected Provider Instance Type",
fmt.Sprintf("While creating the data source or resource, an unexpected provider type (%T) was received. This is always a bug in the provider code and should be reported to the provider developers.", p),
)
return &VyProvider{}, diags
}
if p == nil {
diags.AddError(
"Unexpected Provider Instance Type",
"While creating the data source or resource, an unexpected empty provider instance was received. This is always a bug in the provider code and should be reported to the provider developers.",
)
return &VyProvider{}, diags
}
return p, diags
}