/
data_source_datastore_parameters.go
169 lines (139 loc) · 5.4 KB
/
data_source_datastore_parameters.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
package db
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-log/tflog"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/clients"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/services/db/v1/datastores"
)
var (
_ datasource.DataSource = &DatastoreParametersDataSource{}
_ datasource.DataSourceWithConfigure = &DatastoreParametersDataSource{}
)
func NewDatastoreParametersDataSource() datasource.DataSource {
return &DatastoreParametersDataSource{}
}
type DatastoreParametersDataSource struct {
config clients.Config
}
type DatastoreParametersDataSourceModel struct {
ID types.String `tfsdk:"id"`
Region types.String `tfsdk:"region"`
DatastoreName types.String `tfsdk:"datastore_name"`
DatastoreVersionID types.String `tfsdk:"datastore_version_id"`
Parameters []DatastoreParameterModel `tfsdk:"parameters"`
}
type DatastoreParameterModel struct {
Max types.Float64 `tfsdk:"max"`
Min types.Float64 `tfsdk:"min"`
Name types.String `tfsdk:"name"`
RestartRequried types.Bool `tfsdk:"restart_required"`
Type types.String `tfsdk:"type"`
}
func (d *DatastoreParametersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "vkcs_db_datastore_parameters"
}
func (d *DatastoreParametersDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "ID of the resource.",
},
"region": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The region to obtain the service client. If omitted, the `region` argument of the provider is used.",
},
"datastore_name": schema.StringAttribute{
Required: true,
Description: "Name of the data store.",
},
"datastore_version_id": schema.StringAttribute{
Required: true,
Description: "ID of the version of the data store.",
},
"parameters": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"min": schema.Float64Attribute{
Computed: true,
Description: "Minimum value of a configuration parameter.",
},
"max": schema.Float64Attribute{
Computed: true,
Description: "Maximum value of a configuration parameter.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name of a configuration parameter.",
},
"restart_required": schema.BoolAttribute{
Computed: true,
Description: "This attribute indicates whether a restart required when a parameter is set.",
},
"type": schema.StringAttribute{
Computed: true,
Description: "Type of a configuration parameter.",
},
},
},
Description: "Configuration parameters supported for the datastore.",
},
},
Description: "Use this data source to get configuration parameters supported for a VKCS datastore.",
}
}
func (d *DatastoreParametersDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.config = req.ProviderData.(clients.Config)
}
func (d *DatastoreParametersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data DatastoreParametersDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
region := data.Region.ValueString()
if region == "" {
region = d.config.GetRegion()
}
client, err := d.config.DatabaseV1Client(region)
if err != nil {
resp.Diagnostics.AddError("Error creating VKCS Databases API client", err.Error())
return
}
dsName, dsVersionID := data.DatastoreName.ValueString(), data.DatastoreVersionID.ValueString()
ctx = tflog.SetField(ctx, "datastore_name", dsName)
ctx = tflog.SetField(ctx, "datastore_version_id", dsVersionID)
tflog.Debug(ctx, "Calling Databases API to list parameters for the datastore")
params, err := datastores.ListParameters(client, dsName, dsVersionID).Extract()
if err != nil {
resp.Diagnostics.AddError("Error calling VKCS Databases API", err.Error())
return
}
tflog.Debug(ctx, "Called Databases API to list parameters for the datastore", map[string]interface{}{"parameters": fmt.Sprintf("%#v", params)})
data.ID = types.StringValue(fmt.Sprintf("%s/%s/params", dsName, dsVersionID))
data.Region = types.StringValue(region)
data.Parameters = flattenDatastoreParameters(params)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func flattenDatastoreParameters(params []datastores.Param) (r []DatastoreParameterModel) {
for _, p := range params {
r = append(r, DatastoreParameterModel{
Max: types.Float64Value(p.MaxValue),
Min: types.Float64Value(p.MinValue),
Name: types.StringValue(p.Name),
RestartRequried: types.BoolValue(p.RestartRequried),
Type: types.StringValue(p.Type),
})
}
return
}