-
Notifications
You must be signed in to change notification settings - Fork 132
/
provider_new.go
207 lines (185 loc) · 7 KB
/
provider_new.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
205
206
207
package ovh
import (
"context"
"sync"
"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"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ provider.Provider = &OvhProvider{}
)
// OvhProvider is the provider implementation.
type OvhProvider 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 *OvhProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "ovh"
resp.Version = p.version
}
// Schema defines the provider-level schema for configuration data.
func (p *OvhProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
Optional: true,
Description: descriptions["endpoint"],
},
"application_key": schema.StringAttribute{
Optional: true,
Description: descriptions["application_key"],
},
"application_secret": schema.StringAttribute{
Optional: true,
Description: descriptions["application_secret"],
},
"consumer_key": schema.StringAttribute{
Optional: true,
Description: descriptions["consumer_key"],
},
"client_id": schema.StringAttribute{
Optional: true,
Description: descriptions["client_id"],
},
"client_secret": schema.StringAttribute{
Optional: true,
Description: descriptions["client_secret"],
},
},
}
}
// Configure prepares a HashiCups API client for data sources and resources.
func (p *OvhProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var config ovhProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if config.Endpoint.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("endpoint"),
"Unknown OVH API endpoint",
"The provider cannot create the OVH API client as there is a missing or empty value for the API endpoint."+
"Set the endpoint value in the configuration and ensure the value is not empty.",
)
}
if config.ApplicationKey.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("application_key"),
"Unknown OVH API application_key",
"The provider cannot create the OVH API client as there is a missing or empty value for the API application key."+
"Set the application key value in the configuration or use the OVH_APPLICATION_KEY environment variable."+
"If either is already set, ensure the value is not empty.",
)
}
if config.ApplicationSecret.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("application_secret"),
"Unknown OVH API application_secret",
"The provider cannot create the OVH API client as there is a missing or empty value for the API application secret."+
"Set the application secret value in the configuration or use the OVH_APPLICATION_SECRET environment variable."+
"If either is already set, ensure the value is not empty.",
)
}
if config.ConsumerKey.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("consumer_key"),
"Unknown OVH API consumer_key",
"The provider cannot create the OVH API client as there is a missing or empty value for the API consumer key."+
"Set the consumer key value in the configuration or use the OVH_CONSUMER_KEY environment variable."+
"If either is already set, ensure the value is not empty.",
)
}
if config.ClientID.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("client_id"),
"Unknown OVH API oAuth2 client ID",
"The provider cannot create the OVH API client as there is a missing or empty value for the API oAuth2 client id."+
"Set the client id value in the configuration or use the OVH_CLIENT_ID environment variable."+
"If either is already set, ensure the value is not empty.",
)
}
if config.ClientSecret.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("client_secret"),
"Unknown OVH API oAuth2 client secret",
"The provider cannot create the OVH API client as there is a missing or empty value for the API oAuth2 client secret."+
"Set the client secret value in the configuration or use the OVH_CLIENT_SECRET environment variable."+
"If either is already set, ensure the value is not empty.",
)
}
if resp.Diagnostics.HasError() {
return
}
clientConfig := Config{
lockAuth: &sync.Mutex{},
}
// Check if API variables has been set directly in the configuration
if !config.Endpoint.IsNull() {
clientConfig.Endpoint = config.Endpoint.ValueString()
}
if !config.ApplicationKey.IsNull() {
clientConfig.ApplicationKey = config.ApplicationKey.ValueString()
}
if !config.ApplicationSecret.IsNull() {
clientConfig.ApplicationSecret = config.ApplicationSecret.ValueString()
}
if !config.ConsumerKey.IsNull() {
clientConfig.ConsumerKey = config.ConsumerKey.ValueString()
}
if !config.ClientID.IsNull() {
clientConfig.ClientID = config.ClientID.ValueString()
}
if !config.ClientSecret.IsNull() {
clientConfig.ClientSecret = config.ClientSecret.ValueString()
}
if err := clientConfig.loadAndValidate(); err != nil {
resp.Diagnostics.AddError(err.Error(), "failed to init OVH API client")
return
}
resp.DataSourceData = &clientConfig
resp.ResourceData = &clientConfig
}
// DataSources defines the data sources implemented in the provider.
func (p *OvhProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewCloudProjectDatabaseIPRestrictionsDataSource,
NewCloudProjectDataSource,
NewCloudProjectsDataSource,
NewDedicatedServerSpecificationsHardwareDataSource,
NewDedicatedServerSpecificationsNetworkDataSource,
NewDomainZoneDnssecDataSource,
NewIpFirewallDataSource,
NewIpFirewallRuleDataSource,
NewIpMitigationDataSource,
}
}
// Resources defines the resources implemented in the provider.
func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewCloudProjectAlertingResource,
NewDomainZoneDnssecResource,
NewIpFirewallResource,
NewIpFirewallRuleResource,
NewIploadbalancingUdpFrontendResource,
NewIpMitigationResource,
NewVpsResource,
}
}
type ovhProviderModel struct {
Endpoint types.String `tfsdk:"endpoint"`
ApplicationKey types.String `tfsdk:"application_key"`
ApplicationSecret types.String `tfsdk:"application_secret"`
ConsumerKey types.String `tfsdk:"consumer_key"`
ClientID types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
}