/
provider.go
183 lines (155 loc) · 5.21 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
package provider
import (
"context"
"github.com/PGSSoft/terraform-provider-mssql/internal/core"
"github.com/PGSSoft/terraform-provider-mssql/internal/sql"
"github.com/PGSSoft/terraform-provider-mssql/internal/utils"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/pkg/errors"
)
// To ensure provider fully satisfies framework interfaces
var (
_ provider.ProviderWithMetadata = &mssqlProvider{}
_ provider.ProviderWithValidateConfig = &mssqlProvider{}
)
const (
VersionDev = "dev"
VersionTest = "test"
)
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &mssqlProvider{
Version: version,
}
}
}
type mssqlProvider struct {
Version string
Db sql.Connection
}
func (p *mssqlProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "mssql"
resp.Version = p.Version
}
func (p *mssqlProvider) Configure(_ context.Context, request provider.ConfigureRequest, response *provider.ConfigureResponse) {
resCtx := core.ResourceContext{}
if p.Version == VersionTest {
resCtx.ConnFactory = func(ctx context.Context) sql.Connection {
return p.Db
}
} else {
resCtx.ConnFactory = func(ctx context.Context) sql.Connection {
var data providerData
d := request.Config.Get(ctx, &data)
if utils.AppendDiagnostics(ctx, d...); utils.HasError(ctx) {
return nil
}
connDetails, d := data.asConnectionDetails(ctx)
if utils.AppendDiagnostics(ctx, d...); utils.HasError(ctx) {
return nil
}
conn, d := connDetails.Open(ctx)
utils.AppendDiagnostics(ctx, d...)
return conn
}
}
response.DataSourceData = resCtx
response.ResourceData = resCtx
}
func (p *mssqlProvider) Resources(context.Context) []func() resource.Resource {
var ctors []func() resource.Resource
for _, svc := range Services() {
for _, svcRes := range svc.Resources() {
ctor := svcRes
ctors = append(ctors, func() resource.Resource { return ctor() })
}
}
return ctors
}
func (p *mssqlProvider) DataSources(context.Context) []func() datasource.DataSource {
var ctors []func() datasource.DataSource
for _, svc := range Services() {
for _, svcDataSource := range svc.DataSources() {
ctor := svcDataSource
ctors = append(ctors, func() datasource.DataSource { return ctor() })
}
}
return ctors
}
func (p *mssqlProvider) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
if p.Version == VersionTest {
return tfsdk.Schema{}, nil
}
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"hostname": {
Description: "FQDN or IP address of the SQL endpoint. Can be also set using `MSSQL_HOSTNAME` environment variable.",
Type: types.StringType,
Optional: true,
},
"port": {
MarkdownDescription: "TCP port of SQL endpoint. Defaults to `1433`. Can be also set using `MSSQL_PORT` environment variable.",
Type: types.Int64Type,
Optional: true,
},
"sql_auth": {
Description: "When provided, SQL authentication will be used when connecting.",
Optional: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"username": {
Description: "User name for SQL authentication.",
Type: types.StringType,
Required: true,
},
"password": {
Description: "Password for SQL authentication.",
Type: types.StringType,
Required: true,
Sensitive: true,
},
}),
},
"azure_auth": {
Description: "When provided, Azure AD authentication will be used when connecting.",
Optional: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"client_id": {
Description: "Service Principal client (application) ID. When omitted, default, chained set of credentials will be used.",
Type: types.StringType,
Optional: true,
},
"client_secret": {
Description: "Service Principal secret. When omitted, default, chained set of credentials will be used.",
Type: types.StringType,
Sensitive: true,
Optional: true,
},
"tenant_id": {
Description: "Azure AD tenant ID. Required only if Azure SQL Server's tenant is different than Service Principal's.",
Type: types.StringType,
Optional: true,
},
}),
},
},
}, nil
}
func (p *mssqlProvider) ValidateConfig(ctx context.Context, request provider.ValidateConfigRequest, response *provider.ValidateConfigResponse) {
if p.Version == VersionTest {
return
}
var data providerData
ctx = utils.WithDiagnostics(ctx, &response.Diagnostics)
utils.StopOnError(ctx).
Then(func() { data = utils.GetData[providerData](ctx, request.Config) }).
Then(func() {
if data.AzureAuth == nil && data.SqlAuth == nil {
utils.AddError(ctx, "Missing SQL authentication config", errors.New("One of authentication methods must be provided: sql_auth, azure_auth"))
}
})
}