/
provider.go
204 lines (171 loc) · 6.57 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package adcm
import (
"context"
"os"
adcmClient "github.com/giggsoff/terraform-provider-adcm/client"
"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 the implementation satisfies the expected interfaces
var (
_ provider.Provider = &adcmProvider{}
)
// New is a helper function to simplify provider server and testing implementation.
func New() provider.Provider {
return &adcmProvider{}
}
// adcmProvider is the provider implementation.
type adcmProvider struct{}
type adcmProviderModel struct {
Url types.String `tfsdk:"url"`
Login types.String `tfsdk:"login"`
Password types.String `tfsdk:"password"`
}
func (a adcmProvider) Metadata(_ context.Context, _ provider.MetadataRequest, response *provider.MetadataResponse) {
response.TypeName = "adcm"
}
func (a adcmProvider) Schema(_ context.Context, _ provider.SchemaRequest, response *provider.SchemaResponse) {
response.Schema = schema.Schema{
Description: "Interact with ADCM.",
Attributes: map[string]schema.Attribute{
"url": schema.StringAttribute{
Description: "URI for ADCM API. May also be provided via ADCM_URL environment variable.",
Optional: true,
},
"login": schema.StringAttribute{
Description: "Login for ADCM API. May also be provided via ADCM_LOGIN environment variable.",
Optional: true,
},
"password": schema.StringAttribute{
Description: "Password for ADCM API. May also be provided via ADCM_PASSWORD environment variable.",
Optional: true,
Sensitive: true,
},
},
}
}
func (a adcmProvider) Configure(ctx context.Context, request provider.ConfigureRequest, response *provider.ConfigureResponse) {
tflog.Info(ctx, "Configuring ADCM client")
// Retrieve provider data from configuration
var config adcmProviderModel
diags := request.Config.Get(ctx, &config)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
// If practitioner provided a configuration value for any of the
// attributes, it must be a known value.
if config.Url.IsUnknown() {
response.Diagnostics.AddAttributeError(
path.Root("url"),
"Unknown ADCM API Url",
"The provider cannot create the ADCM API client as there is an unknown configuration value for the ADCM API host. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the ADCM_URL environment variable.",
)
}
if config.Login.IsUnknown() {
response.Diagnostics.AddAttributeError(
path.Root("username"),
"Unknown ADCM API login",
"The provider cannot create the ADCM API client as there is an unknown configuration value for the ADCM API login. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the ADCM_LOGIN environment variable.",
)
}
if config.Password.IsUnknown() {
response.Diagnostics.AddAttributeError(
path.Root("password"),
"Unknown ADCM API Password",
"The provider cannot create the ADCM API client as there is an unknown configuration value for the ADCM API password. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the ADCM_PASSWORD environment variable.",
)
}
if response.Diagnostics.HasError() {
return
}
// Default values to environment variables, but override
// with Terraform configuration value if set.
url := os.Getenv("ADCM_URL")
login := os.Getenv("ADCM_LOGIN")
password := os.Getenv("ADCM_PASSWORD")
if !config.Url.IsNull() {
url = config.Url.ValueString()
}
if !config.Login.IsNull() {
login = config.Login.ValueString()
}
if !config.Password.IsNull() {
password = config.Password.ValueString()
}
// If any of the expected configurations are missing, return
// errors with provider-specific guidance.
if url == "" {
response.Diagnostics.AddAttributeError(
path.Root("url"),
"Missing ADCM API url",
"The provider cannot create the ADCM API client as there is a missing or empty value for the ADCM API url. "+
"Set the host value in the configuration or use the ADCM_URL environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if login == "" {
response.Diagnostics.AddAttributeError(
path.Root("login"),
"Missing ADCM API login",
"The provider cannot create the ADCM API client as there is a missing or empty value for the ADCM API login. "+
"Set the username value in the configuration or use the ADCM_LOGIN environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if password == "" {
response.Diagnostics.AddAttributeError(
path.Root("password"),
"Missing ADCM API password",
"The provider cannot create the ADCM API client as there is a missing or empty value for the ADCM API password. "+
"Set the password value in the configuration or use the ADCM_PASSWORD environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if response.Diagnostics.HasError() {
return
}
ctx = tflog.SetField(ctx, "adcm_url", url)
ctx = tflog.SetField(ctx, "adcm_login", login)
ctx = tflog.SetField(ctx, "adcm_password", password)
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "adcm_password")
tflog.Debug(ctx, "Creating ADCM client")
// Create a new ADCM client using the configuration values
client, err := adcmClient.NewClient(&url, &login, &password)
if err != nil {
response.Diagnostics.AddError(
"Unable to Create ADCM API Client",
"An unexpected error occurred when creating the ADCM API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"ADCM Client Error: "+err.Error(),
)
return
}
// Make the ADCM client available during DataSource and Resource
// type Configure methods.
response.DataSourceData = client
response.ResourceData = client
tflog.Info(ctx, "Configured ADCM client", map[string]any{"success": true})
}
func (a adcmProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewBundleDataSource,
NewProviderDataSource,
}
}
func (a adcmProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewHostResource,
NewClusterResource,
NewBundleResource,
NewProviderResource,
}
}