/
data_source.go
100 lines (87 loc) · 2.81 KB
/
data_source.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
package yandex_mdb_mongodb_user
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
provider_config "github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/provider-config"
"github.com/yandex-cloud/terraform-provider-yandex/yandex-framework/utils"
)
type bindingDataSource struct {
providerConfig *provider_config.Config
}
func NewDataSource() datasource.DataSource {
return &bindingDataSource{}
}
func (d *bindingDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_mdb_mongodb_user"
}
func (d *bindingDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
providerConfig, ok := req.ProviderData.(*provider_config.Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected DataSource Configure Type",
fmt.Sprintf("Expected *provider_config.Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.providerConfig = providerConfig
}
func (d *bindingDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"cluster_id": schema.StringAttribute{
Required: true,
},
"name": schema.StringAttribute{
Required: true,
},
"password": schema.StringAttribute{
Computed: true,
Sensitive: true,
},
},
Blocks: map[string]schema.Block{
"permission": schema.SetNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"database_name": schema.StringAttribute{
Required: true,
},
"roles": schema.SetAttribute{
Optional: true,
ElementType: basetypes.StringType{},
},
},
},
},
},
}
}
func (d *bindingDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state User
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
cid := state.ClusterID.ValueString()
userName := state.Name.ValueString()
user := readUser(ctx, d.providerConfig.SDK, &resp.Diagnostics, cid, userName)
if resp.Diagnostics.HasError() {
return
}
state.Id = types.StringValue(utils.ConstructResourceId(cid, userName))
resp.Diagnostics.Append(userToState(user, &state)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}