/
base.go
63 lines (54 loc) · 1.93 KB
/
base.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
package serverRole
import (
"context"
"fmt"
common2 "github.com/PGSSoft/terraform-provider-mssql/internal/services/common"
"github.com/PGSSoft/terraform-provider-mssql/internal/sql"
"github.com/PGSSoft/terraform-provider-mssql/internal/utils"
"github.com/PGSSoft/terraform-provider-mssql/internal/validators"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"strconv"
)
var attributes = map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "Role principal ID.",
Type: types.StringType,
},
"name": {
MarkdownDescription: fmt.Sprintf("Role name. %s and cannot be longer than 128 chars.", common2.RegularIdentifiersDoc),
Type: types.StringType,
Validators: validators.UserNameValidators,
},
"owner_id": {
MarkdownDescription: "ID of another server role or login owning this role. Can be retrieved using `mssql_server_role` or `mssql_sql_login`.",
Type: types.StringType,
},
}
type resourceData struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
OwnerId types.String `tfsdk:"owner_id"`
}
func (d resourceData) withSettings(settings sql.ServerRoleSettings) resourceData {
d.Name = types.StringValue(settings.Name)
d.OwnerId = types.StringValue(fmt.Sprint(settings.OwnerId))
return d
}
func (d resourceData) toSettings(ctx context.Context) sql.ServerRoleSettings {
settings := sql.ServerRoleSettings{
Name: d.Name.ValueString(),
OwnerId: sql.EmptyServerPrincipalId,
}
if common2.IsAttrSet(d.OwnerId) {
id, err := strconv.Atoi(d.OwnerId.ValueString())
utils.AddError(ctx, "Failed to parse owner ID", err)
settings.OwnerId = sql.GenericServerPrincipalId(id)
}
return settings
}
func parseId(ctx context.Context, id types.String) sql.ServerRoleId {
intId, err := strconv.Atoi(id.ValueString())
utils.AddError(ctx, "Failed to parse ID", err)
return sql.ServerRoleId(intId)
}