-
Notifications
You must be signed in to change notification settings - Fork 132
/
provider_new.go
174 lines (151 loc) · 5.69 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
package ovh
import (
"context"
"os"
"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"],
},
},
}
}
// 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 resp.Diagnostics.HasError() {
return
}
endpoint := os.Getenv("OVH_ENDPOINT")
if !config.Endpoint.IsNull() {
endpoint = config.Endpoint.ValueString()
}
clientConfig := Config{
Endpoint: endpoint,
lockAuth: &sync.Mutex{},
}
// First try to read configuration from config file in home directory
if err := readOVHConfigurationFile(&clientConfig); err != nil {
resp.Diagnostics.AddError("failed to read OVH API configuration file", err.Error())
return
}
// Check if env variables are set
if appKey := os.Getenv("OVH_APPLICATION_KEY"); appKey != "" {
clientConfig.ApplicationKey = appKey
}
if appSecret := os.Getenv("OVH_APPLICATION_SECRET"); appSecret != "" {
clientConfig.ApplicationSecret = appSecret
}
if consumerKey := os.Getenv("OVH_CONSUMER_KEY"); consumerKey != "" {
clientConfig.ConsumerKey = consumerKey
}
// Check if API variables has been set directly in the configuration
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 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 nil
}
// Resources defines the resources implemented in the provider.
func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
return nil
}
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"`
}