-
Notifications
You must be signed in to change notification settings - Fork 11
/
list.go
79 lines (67 loc) · 2.12 KB
/
list.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
package sqlUser
import (
"context"
"fmt"
"github.com/PGSSoft/terraform-provider-mssql/internal/core/datasource"
common2 "github.com/PGSSoft/terraform-provider-mssql/internal/services/common"
"github.com/PGSSoft/terraform-provider-mssql/internal/sql"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type listDataSourceData struct {
Id types.String `tfsdk:"id"`
DatabaseId types.String `tfsdk:"database_id"`
Users []resourceData `tfsdk:"users"`
}
type listDataSource struct{}
func (l *listDataSource) GetName() string {
return "sql_users"
}
func (l *listDataSource) GetSchema(context.Context) tfsdk.Schema {
attrs := map[string]tfsdk.Attribute{}
for n, attr := range attributes {
attr.Computed = true
attrs[n] = attr
}
return tfsdk.Schema{
Description: "Obtains information about all SQL users found in a database",
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
Description: "ID of the resource, equals to database ID",
},
"database_id": func() tfsdk.Attribute {
attr := attributes["database_id"]
attr.Optional = true
attr.MarkdownDescription += " Defaults to ID of `master`."
return attr
}(),
"users": {
Description: "Set of SQL user objects",
Attributes: tfsdk.SetNestedAttributes(attrs),
Computed: true,
},
},
}
}
func (l *listDataSource) Read(ctx context.Context, req datasource.ReadRequest[listDataSourceData], resp *datasource.ReadResponse[listDataSourceData]) {
var db sql.Database
var dbId sql.DatabaseId
req.
Then(func() { db = common2.GetResourceDb(ctx, req.Conn, req.Config.DatabaseId.Value) }).
Then(func() { dbId = db.GetId(ctx) }).
Then(func() {
state := listDataSourceData{
DatabaseId: types.String{Value: fmt.Sprint(dbId)},
}
state.Id = state.DatabaseId
for id, user := range sql.GetUsers(ctx, db) {
s := user.GetSettings(ctx)
if s.Type == sql.USER_TYPE_SQL {
state.Users = append(state.Users, resourceData{}.withIds(dbId, id).withSettings(s))
}
}
resp.SetState(state)
})
}