/
provider.go
135 lines (110 loc) · 3.81 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
package provider
import (
"context"
"os"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"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-plugin-log/tflog"
"github.com/hmada15/terraform-provider-webdock/api"
)
var _ provider.Provider = &webdockProvider{}
// webdockProvider is the provider implementation.
type (
webdockProvider struct {
version string
}
webdockProviderModel struct {
Token types.String `tfsdk:"token"`
}
)
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &webdockProvider{
version: version,
}
}
}
// Metadata returns the provider type name.
func (p *webdockProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "webdock"
resp.Version = p.version
}
// Schema defines the provider-level schema for configuration data.
func (p *webdockProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"token": schema.StringAttribute{
Required: true,
Sensitive: true,
Description: "Webdock token",
},
},
}
}
func (p *webdockProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Info(ctx, "Configuring Token client")
// Retrieve provider data from configuration
var config webdockProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if config.Token.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Unknown Token API Token",
"The provider cannot create the Token API client as there is an unknown configuration value for the Token API token. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the WEBDOCK_TOKEN environment variable.",
)
}
if resp.Diagnostics.HasError() {
return
}
token := os.Getenv("WEBDOCK_TOKEN")
if !config.Token.IsNull() {
token = config.Token.ValueString()
}
// If any of the expected configurations are missing, return
// errors with provider-specific guidance.
if token == "" {
resp.Diagnostics.AddAttributeError(
path.Root("token"),
"Missing webdock API Token",
"The provider cannot create the webdock API client as there is a missing or empty value for the webdock API token. "+
"Set the token value in the configuration or use the WEBDOCK_TOKEN environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if resp.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "webdock_token", token)
tflog.Debug(ctx, "Creating Token client")
// Create a new Token client using the configuration values
client := api.NewClient(token)
// Make the Token client available during DataSource and Resource
resp.DataSourceData = client
resp.ResourceData = client
tflog.Info(ctx, "Configured webdock client", map[string]any{"success": true})
}
// DataSources defines the data sources implemented in the provider.
func (p *webdockProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewServerDataSource,
NewLocationDataSource,
NewProfileDataSource,
NewImagesDataSource,
}
}
// Resources defines the resources implemented in the provider.
func (p *webdockProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewServerResource,
NewPublicKeyResource,
}
}