This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
/
provider.go
157 lines (133 loc) · 4.51 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
/*
* Copyright (c) Matthew Hartstonge <matt@mykro.co.nz>
* SPDX-License-Identifier: MPL-2.0
*/
package provider
import (
// Standard Library Imports
"context"
"net/url"
"os"
// External Imports
"github.com/hashicorp/terraform-plugin-framework/datasource"
"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"
// Internal Imports
faClient "github.com/matthewhartstonge/terraform-provider-fusionauth/internal/client"
)
const (
envApiToken = "FUSIONAUTH_API_TOKEN"
envEndpoint = "FUSIONAUTH_ENDPOINT"
envTenant = "FUSIONAUTH_TENANT"
)
// Ensure FusionAuthProvider satisfies various provider interfaces.
var _ provider.Provider = &FusionAuthProvider{}
// FusionAuthProvider defines the provider implementation.
type FusionAuthProvider struct {
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
version string
}
// FusionAuthProviderModel describes the provider data model.
type FusionAuthProviderModel struct {
ApiToken types.String `tfsdk:"api_token"`
Endpoint types.String `tfsdk:"endpoint"`
Tenant types.String `tfsdk:"tenant"`
}
func (p *FusionAuthProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "fusionauth"
resp.Version = p.version
}
func (p *FusionAuthProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"api_token": schema.StringAttribute{
MarkdownDescription: "FusionAuth API Token. Can also be set with the `" + envApiToken + "` environment variable.",
Optional: true,
},
"endpoint": schema.StringAttribute{
MarkdownDescription: "Address of the FusionAuth instance to use. Can also be set with the `" + envEndpoint + "` environment variable.",
Optional: true,
},
"tenant": schema.StringAttribute{
MarkdownDescription: "FusionAuth tenant to scope requests to. Can also be set with the `" + envTenant + "` environment variable.",
Optional: true,
},
},
}
}
func (p *FusionAuthProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
// Check environment variables
apiToken := os.Getenv(envApiToken)
endpoint := os.Getenv(envEndpoint)
tenant := os.Getenv(envTenant)
var data FusionAuthProviderModel
// Read configuration data into model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Configuration values are now available.
if data.ApiToken.String() != "" {
apiToken = data.ApiToken.String()
}
if apiToken == "" {
resp.Diagnostics.AddError(
"Missing API Token Configuration",
"While configuring the provider, the API token was not found in "+
"the "+envApiToken+" environment variable or provider "+
"configuration block api_token attribute.",
)
}
if data.Endpoint.String() != "" {
endpoint = data.Endpoint.String()
}
if endpoint == "" {
resp.Diagnostics.AddError(
"Missing Endpoint Configuration",
"While configuring the provider, the API endpoint was not found in "+
"the "+envEndpoint+" environment variable or provider "+
"configuration block endpoint attribute.",
)
}
if data.Tenant.String() != "" {
tenant = data.Tenant.String()
}
baseURL, err := url.Parse(endpoint)
if err != nil {
resp.Diagnostics.AddError(
"Unable to parse FusionAuth API Endpoint",
"While configuring the provider, the API endpoint '"+endpoint+"'was unable "+
"to be parsed as a URL.",
)
}
// Finalized validating config, return errors
if resp.Diagnostics.HasError() {
return
}
// client configuration for data sources and resources
client := faClient.New(baseURL, apiToken, tenant)
// Bind in the client data
resp.DataSourceData = client
resp.ResourceData = client
}
func (p *FusionAuthProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewExampleResource,
}
}
func (p *FusionAuthProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewExampleDataSource,
}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &FusionAuthProvider{
version: version,
}
}
}