/
data_source_multi.go
99 lines (82 loc) · 2.77 KB
/
data_source_multi.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
package templates
import (
"context"
"fmt"
"github.com/awlsring/terraform-provider-proxmox/internal/service"
"github.com/awlsring/terraform-provider-proxmox/proxmox/filters"
qt "github.com/awlsring/terraform-provider-proxmox/proxmox/qemu/types"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var (
_ datasource.DataSource = &templatesDataSource{}
_ datasource.DataSourceWithConfigure = &templatesDataSource{}
)
func DataSourceMulti() datasource.DataSource {
return &templatesDataSource{}
}
type templatesDataSource struct {
client *service.Proxmox
}
type templatesDataSourceModel struct {
Templates qt.VirtualMachineDataSourceSetValue `tfsdk:"templates"`
Filters []filters.FilterModel `tfsdk:"filters"`
}
func (d *templatesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_templates"
}
func (d *templatesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*service.Proxmox)
}
var filter = filters.FilterConfig{"node"}
func (d *templatesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"filters": filter.Schema(),
"templates": TemplateMultiDataSourceSchema,
},
}
}
func (d *templatesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state templatesDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
nodes := filters.DetermineNode(d.client, state.Filters)
for _, node := range nodes {
vms, err := d.client.DescribeTemplates(ctx, node)
if err != nil {
resp.Diagnostics.AddError(
"Unable to get templates",
"An error was encountered retrieving templates.\n"+
err.Error(),
)
return
}
vmModels := []qt.VirtualMachineDataSourceModel{}
for _, vm := range vms {
tflog.Debug(ctx, fmt.Sprintf("Converting template %v to model", vm.VmId))
model := qt.VMToModel(ctx, vm)
vmModels = append(vmModels, *model)
}
state.Templates, err = qt.VirtualMachineDataSourceSetValueFrom(ctx, vmModels)
if err != nil {
resp.Diagnostics.AddError(
"Unable to convert virtual machines to state",
"An error was encountered converting virtual machines to state.\n"+
err.Error(),
)
return
}
}
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}