-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
188 lines (159 loc) · 6.34 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
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/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"
)
// Ensure K8sProvider satisfies various provider interfaces.
var _ provider.Provider = &K8sProvider{}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &K8sProvider{
version: version,
}
}
}
// K8sProvider defines the provider implementation.
type K8sProvider 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
}
// Metadata returns the provider type name.
func (p *K8sProvider) Metadata(ctx context.Context, request provider.MetadataRequest, response *provider.MetadataResponse) {
response.TypeName = "custom-resource"
response.Version = p.version
}
// Schema defines the provider-level schema for configuration data.
func (p *K8sProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"host": schema.StringAttribute{
MarkdownDescription: "The address of the API server host to connect to.",
Required: true,
},
"token": schema.StringAttribute{
MarkdownDescription: "The bearer token to use in order to authenticate against the API server; if not specified, a username and password combination should be provided instead.",
Optional: true,
Sensitive: true,
},
"username": schema.StringAttribute{
MarkdownDescription: "The username to use, in combination with a password, in order to authenticate against the API server. It a bearer token has been provided, it will take precedence over username and password basic authentication",
Optional: true,
},
"password": schema.StringAttribute{
MarkdownDescription: "The password to use, in combination with a username, in order to authenticate against the API server. It a bearer token has been provided, it will take precedence over username and password basic authentication",
Optional: true,
Sensitive: true,
},
},
}
}
// K8sProviderModel describes the provider data model.
type K8sProviderModel struct {
Host types.String `tfsdk:"host"`
Token types.String `tfsdk:"token"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
}
type K8sProviderConfiguration struct {
Host string
Token string
Username string
Password string
}
// Configure prepares a CRD API client for data sources and resources.
func (p *K8sProvider) Configure(ctx context.Context, request provider.ConfigureRequest, response *provider.ConfigureResponse) {
var model K8sProviderModel
tflog.Trace(ctx, "configuring Kubernetes Custom Resource Definition client (kubectl)...")
response.Diagnostics.Append(request.Config.Get(ctx, &model)...)
if response.Diagnostics.HasError() {
return
}
if model.Host.IsUnknown() {
response.Diagnostics.AddAttributeError(
path.Root("host"),
"Unknown Kubernetes API Server host",
"The provider cannot create the Kubernetes API client as there is an unknown configuration value for the API Server endpoint. "+
"Set the value statically in the configuration, or use the K8S_HOST environment variable.",
)
}
if model.Token.IsUnknown() && (model.Username.IsUnknown() || model.Password.IsUnknown()) {
response.Diagnostics.AddError(
"Unknown Kubernetes API Server credentials",
"The provider cannot create the Kubernetes API client as there are no valid credentials specified. "+
"Statically set the value of either the token, or the username and password combination, in the configuration, or use the K8S_TOKEN, K8S_USERNAME and K8S_PASSWORD environment variables.",
)
}
if response.Diagnostics.HasError() {
return
}
host := os.Getenv("K8S_HOST")
token := os.Getenv("K8S_TOKEN")
username := os.Getenv("K8S_USERNAME")
password := os.Getenv("K8S_PASSWORD")
if !model.Host.IsNull() {
host = model.Host.ValueString()
}
if !model.Token.IsNull() {
token = model.Token.ValueString()
}
if !model.Username.IsNull() {
username = model.Username.ValueString()
}
if !model.Password.IsNull() {
password = model.Password.ValueString()
}
if host == "" {
response.Diagnostics.AddAttributeError(
path.Root("host"),
"Missing Kubernetes API Server host",
"The provider cannot create the Kubernetes API client as there is a missing or empty value for the API Server host. "+
"Set the endpoint value in the configuration or use the K8S_ENDPOINT environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if token == "" && (username == "" || password == "") {
response.Diagnostics.AddError(
"Missing Kubernetes API Server credentials",
"The provider cannot create the Kubernetes API client as there is a missing or empty value for the API Server credentials. "+
"Set the token value, or a valid username/password combination in the configuration or use the K8S_TOKEN, "+
"K8S_USERNAME and K8S_PASSWORD environment variables. "+
"If either is already set, ensure the values are not empty.",
)
}
if response.Diagnostics.HasError() {
return
}
// Example client configuration for model sources and resources
configuration := K8sProviderConfiguration{
Host: host,
Token: token,
Username: username,
Password: password,
}
response.DataSourceData = &configuration
response.ResourceData = &configuration
}
// Resources defines the resources implemented in the provider.
func (p *K8sProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewCustomResourceDefinition,
NewCustomResourceInstance,
}
}
// DataSources defines the data sources implemented in the provider.
func (p *K8sProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewExampleDataSource,
}
}