/
provider.go
185 lines (160 loc) · 5.79 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
package provider
import (
"context"
"os"
qlikcloud "github.com/daniepett/qlik-cloud-client-go"
"github.com/daniepett/terraform-provider-qlik/pkg/datasources"
"github.com/daniepett/terraform-provider-qlik/pkg/resources"
"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 = &qlikProvider{}
)
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &qlikProvider{
version: version,
}
}
}
type qlikProvider struct {
version string
}
type qlikProviderModel struct {
TenantID types.String `tfsdk:"tenant_id"`
Region types.String `tfsdk:"region"`
ClientID types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
}
func (p *qlikProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "qlik"
resp.Version = p.version
}
func (p *qlikProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"tenant_id": schema.StringAttribute{
Required: true,
},
"region": schema.StringAttribute{
Required: true,
},
"client_id": schema.StringAttribute{
Optional: true,
},
"client_secret": schema.StringAttribute{
Optional: true,
Sensitive: true,
},
},
}
}
func (p *qlikProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
// Retrieve provider data from configuration
var config qlikProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Default values to environment variables, but override
// with Terraform configuration value if set.
client_id := os.Getenv("QLIK_CLIENT_ID")
client_secret := os.Getenv("QLIK_CLIENT_SECRET")
tenant_id := os.Getenv("QLIK_TENANT_ID")
region := os.Getenv("QLIK_REGION")
if !config.TenantID.IsNull() {
tenant_id = config.TenantID.ValueString()
}
if !config.Region.IsNull() {
region = config.Region.ValueString()
}
if !config.ClientID.IsNull() {
client_id = config.ClientID.ValueString()
}
if !config.ClientSecret.IsNull() {
client_secret = config.ClientSecret.ValueString()
}
// If any of the expected configurations are missing, return
// errors with provider-specific guidance.
if tenant_id == "" {
resp.Diagnostics.AddAttributeError(
path.Root("tenant_id"),
"Missing Qlik Cloud Tenant ID",
"The provider cannot create the Qlik Cloud API client as there is a missing or empty value for the Qlik Cloud Tenant ID. "+
"Set the tenant_id value in the configuration or use the QLIK_CLOUD_TENANT_ID environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if region == "" {
resp.Diagnostics.AddAttributeError(
path.Root("region"),
"Missing Qlik Cloud Region",
"The provider cannot create the Qlik Cloud API client as there is a missing or empty value for the Qlik Cloud region. "+
"Set the region value in the configuration or use the QLIK_CLOUD_REGION environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if client_id == "" {
resp.Diagnostics.AddAttributeError(
path.Root("client_id"),
"Missing Qlik Cloud Client ID",
"The provider cannot create the Qlik Cloud API client as there is a missing or empty value for the Qlik Cloud Client ID. "+
"Set the client_id value in the configuration or use the QLIK_CLOUD_CLIENT_ID environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if client_secret == "" {
resp.Diagnostics.AddAttributeError(
path.Root("client_secret"),
"Missing Qlik Cloud Client Secret",
"The provider cannot create the Qlik Cloud API client as there is a missing or empty value for the Qlik Cloud Client Secret. "+
"Set the client_secret value in the configuration or use the QLIK_CLOUD_CLIENT_SECRET environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
if resp.Diagnostics.HasError() {
return
}
// Create a new Qlik Cloud client using the configuration values
client, err := qlikcloud.NewClient(&tenant_id, ®ion, &client_id, &client_secret)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Qlik Cloud Client",
"An unexpected error occurred when creating the Qlik Cloud client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"Qlik Cloud Client Error: "+err.Error(),
)
return
}
// Make the Qlik client available during DataSource and Resource
// type Configure methods.
resp.DataSourceData = client
resp.ResourceData = client
}
// DataSources defines the data sources implemented in the provider.
func (p *qlikProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
datasources.NewSpacesDataSource,
datasources.NewSpaceDataSource,
datasources.NewDataGatewayDataSource,
datasources.NewDataConnectionsDataSource,
datasources.NewSourceEntitiesDataSource,
}
}
// Resources defines the resources implemented in the provider.
func (p *qlikProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
resources.NewSpaceResource,
resources.NewDataConnectionResource,
resources.NewDataProjectResource,
resources.NewDataAppResource,
resources.NewDataAppSourceSelectionResource,
}
}