-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource.go
179 lines (154 loc) · 6.42 KB
/
resource.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
package sqlLogin
import (
"context"
"fmt"
"github.com/PGSSoft/terraform-provider-mssql/internal/core/resource"
common2 "github.com/PGSSoft/terraform-provider-mssql/internal/services/common"
"strconv"
"github.com/PGSSoft/terraform-provider-mssql/internal/sql"
"github.com/PGSSoft/terraform-provider-mssql/internal/utils"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type resourceData struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Password types.String `tfsdk:"password"`
MustChangePassword types.Bool `tfsdk:"must_change_password"`
DefaultDatabaseId types.String `tfsdk:"default_database_id"`
DefaultLanguage types.String `tfsdk:"default_language"`
CheckPasswordExpiration types.Bool `tfsdk:"check_password_expiration"`
CheckPasswordPolicy types.Bool `tfsdk:"check_password_policy"`
}
func (d resourceData) toSettings(ctx context.Context) sql.SqlLoginSettings {
var dbId int
if !d.DefaultDatabaseId.Null && !d.DefaultDatabaseId.Unknown {
if id, err := strconv.Atoi(d.DefaultDatabaseId.Value); err == nil {
dbId = id
} else {
utils.AddError(ctx, "Failed to parse DB id", err)
}
}
return sql.SqlLoginSettings{
Name: d.Name.Value,
Password: d.Password.Value,
MustChangePassword: d.MustChangePassword.Value,
DefaultDatabaseId: sql.DatabaseId(dbId),
DefaultLanguage: d.DefaultLanguage.Value,
CheckPasswordExpiration: d.CheckPasswordExpiration.Value,
CheckPasswordPolicy: d.CheckPasswordPolicy.Value || d.CheckPasswordPolicy.Null || d.CheckPasswordPolicy.Unknown,
}
}
func (d resourceData) withSettings(settings sql.SqlLoginSettings, isAzure bool) resourceData {
d.Name = types.String{Value: settings.Name}
if isAzure {
return d
}
if common2.IsAttrSet(d.MustChangePassword) {
d.MustChangePassword.Value = settings.MustChangePassword
}
if common2.IsAttrSet(d.DefaultDatabaseId) {
d.DefaultDatabaseId = types.String{Value: fmt.Sprint(settings.DefaultDatabaseId)}
}
if common2.IsAttrSet(d.DefaultLanguage) {
d.DefaultLanguage = types.String{Value: settings.DefaultLanguage}
}
if common2.IsAttrSet(d.CheckPasswordExpiration) {
d.CheckPasswordExpiration = types.Bool{Value: settings.CheckPasswordExpiration}
}
if common2.IsAttrSet(d.CheckPasswordPolicy) {
d.CheckPasswordPolicy = types.Bool{Value: settings.CheckPasswordPolicy}
}
return d
}
type res struct{}
func (r *res) GetName() string {
return "sql_login"
}
func (r *res) GetSchema(context.Context) tfsdk.Schema {
const azureSQLNote = "\n\n-> **Note** In case of Azure SQL, which does not support this feature, the flag will be ignored. "
return tfsdk.Schema{
Description: "Manages single login.",
Attributes: map[string]tfsdk.Attribute{
"id": common2.ToResourceId(attributes["id"]),
"name": common2.ToRequired(attributes["name"]),
"password": {
MarkdownDescription: "Password for the login. Must follow strong password policies defined for SQL server. " +
"Passwords are case-sensitive, length must be 8-128 chars, can include all characters except `'` or `name`.\n\n" +
"~> **Note** Password will be stored in the raw state as plain-text. [Read more about sensitive data in state](https://www.terraform.io/language/state/sensitive-data).",
Type: types.StringType,
Required: true,
Sensitive: true,
},
"must_change_password": func() tfsdk.Attribute {
attr := attributes["must_change_password"]
attr.Optional = true
attr.MarkdownDescription += " Defaults to `false`. \n\n" +
"-> **Note** After password is changed, this flag is being reset to `false`, which will show as changes in Terraform plan. " +
"Use `ignore_changes` block to prevent this behavior." + azureSQLNote
return attr
}(),
"default_database_id": func() tfsdk.Attribute {
attr := attributes["default_database_id"]
attr.Optional = true
attr.MarkdownDescription += " Defaults to ID of `master`." + azureSQLNote
return attr
}(),
"default_language": func() tfsdk.Attribute {
attr := attributes["default_language"]
attr.Optional = true
attr.Description += " Defaults to current default language of the server. " +
"If the default language of the server is later changed, the default language of the login remains unchanged." + azureSQLNote
return attr
}(),
"check_password_expiration": func() tfsdk.Attribute {
attr := attributes["check_password_expiration"]
attr.Optional = true
attr.MarkdownDescription += " Defaults to `false`." + azureSQLNote
return attr
}(),
"check_password_policy": func() tfsdk.Attribute {
attr := attributes["check_password_policy"]
attr.Optional = true
attr.MarkdownDescription += " Defaults to `true`." + azureSQLNote
return attr
}(),
},
}
}
func (r *res) Create(ctx context.Context, req resource.CreateRequest[resourceData], resp *resource.CreateResponse[resourceData]) {
var login sql.SqlLogin
req.
Then(func() { login = sql.CreateSqlLogin(ctx, req.Conn, req.Plan.toSettings(ctx)) }).
Then(func() {
resp.State = req.Plan.withSettings(login.GetSettings(ctx), req.Conn.IsAzure(ctx))
resp.State.Id = types.String{Value: string(login.GetId(ctx))}
})
}
func (r *res) Read(ctx context.Context, req resource.ReadRequest[resourceData], resp *resource.ReadResponse[resourceData]) {
var (
login sql.SqlLogin
exists bool
)
req.
Then(func() { login = sql.GetSqlLogin(ctx, req.Conn, sql.LoginId(req.State.Id.Value)) }).
Then(func() { exists = login.Exists(ctx) }).
Then(func() {
if exists {
resp.SetState(req.State.withSettings(login.GetSettings(ctx), req.Conn.IsAzure(ctx)))
}
})
}
func (r *res) Update(ctx context.Context, req resource.UpdateRequest[resourceData], resp *resource.UpdateResponse[resourceData]) {
var login sql.SqlLogin
req.
Then(func() { login = sql.GetSqlLogin(ctx, req.Conn, sql.LoginId(req.Plan.Id.Value)) }).
Then(func() { login.UpdateSettings(ctx, req.Plan.toSettings(ctx)) }).
Then(func() { resp.State = req.Plan.withSettings(login.GetSettings(ctx), req.Conn.IsAzure(ctx)) })
}
func (r *res) Delete(ctx context.Context, req resource.DeleteRequest[resourceData], _ *resource.DeleteResponse[resourceData]) {
var login sql.SqlLogin
req.
Then(func() { login = sql.GetSqlLogin(ctx, req.Conn, sql.LoginId(req.State.Id.Value)) }).
Then(func() { login.Drop(ctx) })
}