/
data_source_datastores.go
149 lines (122 loc) · 4.16 KB
/
data_source_datastores.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
package db
import (
"context"
"sort"
"github.com/gophercloud/utils/terraform/hashcode"
"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 = &DatastoresDataSource{}
_ datasource.DataSourceWithConfigure = &DatastoresDataSource{}
)
func NewDatastoresDataSource() datasource.DataSource {
return &DatastoresDataSource{}
}
type DatastoresDataSource struct {
config clients.Config
}
type DatastoresDataSourceModel struct {
ID types.String `tfsdk:"id"`
Region types.String `tfsdk:"region"`
Datastores []DatastoreModel `tfsdk:"datastores"`
}
type DatastoreModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func (d *DatastoresDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "vkcs_db_datastores"
}
func (d *DatastoresDataSource) 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.",
},
"datastores": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of a datastore.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name of a datastore.",
},
},
},
Description: "List of datastores within VKCS.",
},
},
Description: "Use this data source to get a list of datastores from VKCS.",
}
}
func (d *DatastoresDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.config = req.ProviderData.(clients.Config)
}
func (d *DatastoresDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data DatastoresDataSourceModel
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
}
tflog.Debug(ctx, "Calling Databases API to list all datastores")
allPages, err := datastores.List(client).AllPages()
if err != nil {
resp.Diagnostics.AddError("Error calling VKCS Databases API", err.Error())
return
}
allDatastores, err := datastores.ExtractDatastores(allPages)
if err != nil {
resp.Diagnostics.AddError("Error reading VKCS Databases API response", err.Error())
return
}
tflog.Debug(ctx, "Called Databases API to list all datastores")
flattenedDatastores := flattenDatastores(allDatastores)
sort.SliceStable(flattenedDatastores, func(i, j int) bool {
return flattenedDatastores[i].Name.ValueString() < flattenedDatastores[j].Name.ValueString()
})
var names []string
for _, d := range flattenedDatastores {
names = append(names, d.Name.ValueString())
}
data.ID = types.StringValue(hashcode.Strings(names))
data.Region = types.StringValue(region)
data.Datastores = flattenedDatastores
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func flattenDatastores(datastores []datastores.Datastore) (r []DatastoreModel) {
for _, d := range datastores {
r = append(r, DatastoreModel{
ID: types.StringValue(d.ID),
Name: types.StringValue(d.Name),
})
}
return
}