Skip to content

Commit 5c5a701

Browse files
author
awlsring
committed
feat: single template data source
1 parent 201fb16 commit 5c5a701

File tree

6 files changed

+363
-44
lines changed

6 files changed

+363
-44
lines changed

internal/service/templates.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package service
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/awlsring/proxmox-go/proxmox"
8+
)
9+
10+
func (c *Proxmox) ListTemplates(ctx context.Context, node string) ([]proxmox.VirtualMachineSummary, error) {
11+
request := c.client.ListVirtualMachines(ctx, node)
12+
resp, _, err := c.client.ListVirtualMachinesExecute(request)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
templateSummaries := []proxmox.VirtualMachineSummary{}
18+
for _, vmSummary := range resp.Data {
19+
if vmSummary.HasTemplate() {
20+
if *vmSummary.Template == 1 {
21+
templateSummaries = append(templateSummaries, vmSummary)
22+
}
23+
}
24+
}
25+
26+
return templateSummaries, nil
27+
}
28+
29+
func (c *Proxmox) DescribeTemplate(ctx context.Context, node string, vmId int) (*VirtualMachine, error) {
30+
templates, err := c.ListTemplates(ctx, node)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
for _, vm := range templates {
36+
id := int(vm.Vmid)
37+
38+
if id == vmId {
39+
cf, err := c.DescribeVirtualMachine(ctx, node, id)
40+
if err != nil {
41+
return nil, err
42+
}
43+
return cf, nil
44+
}
45+
}
46+
47+
return nil, fmt.Errorf("template not found: %v", vmId)
48+
}
49+
50+
func (c *Proxmox) DescribeTemplateFromName(ctx context.Context, node string, name string) (*VirtualMachine, error) {
51+
templates, err := c.ListTemplates(ctx, node)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
for _, vm := range templates {
57+
id := int(vm.Vmid)
58+
59+
if vm.HasName() {
60+
if *vm.Name == name {
61+
cf, err := c.DescribeVirtualMachine(ctx, node, id)
62+
if err != nil {
63+
return nil, err
64+
}
65+
return cf, nil
66+
}
67+
}
68+
}
69+
70+
return nil, fmt.Errorf("template not found: %s", name)
71+
}
72+
73+
func (c *Proxmox) DescribeTemplates(ctx context.Context, node string) ([]*VirtualMachine, error) {
74+
templates, err := c.ListTemplates(ctx, node)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
var templateList []*VirtualMachine
80+
for _, vm := range templates {
81+
id := int(vm.Vmid)
82+
cf, err := c.DescribeVirtualMachine(ctx, node, id)
83+
if err != nil {
84+
return nil, err
85+
}
86+
templateList = append(templateList, cf)
87+
}
88+
89+
return templateList, nil
90+
}

internal/service/vm.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@ import (
1010
"github.com/awlsring/terraform-provider-proxmox/internal/service/vm"
1111
)
1212

13-
func (c *Proxmox) ListTemplates(ctx context.Context, node string) ([]proxmox.VirtualMachineSummary, error) {
14-
request := c.client.ListVirtualMachines(ctx, node)
15-
resp, _, err := c.client.ListVirtualMachinesExecute(request)
16-
if err != nil {
17-
return nil, err
18-
}
19-
20-
templateSummaries := []proxmox.VirtualMachineSummary{}
21-
for _, vmSummary := range resp.Data {
22-
if vmSummary.HasTemplate() {
23-
if *vmSummary.Template == 1 {
24-
templateSummaries = append(templateSummaries, vmSummary)
25-
}
26-
}
27-
}
28-
29-
return templateSummaries, nil
30-
}
31-
3213
func (c *Proxmox) GetVirtualMachineConfiguration(ctx context.Context, node string, vmId int) (*proxmox.VirtualMachineConfigurationSummary, error) {
3314
vmIdStr := strconv.Itoa(vmId)
3415
request := c.client.GetVirtualMachineConfiguration(ctx, node, vmIdStr)
@@ -40,25 +21,6 @@ func (c *Proxmox) GetVirtualMachineConfiguration(ctx context.Context, node strin
4021
return &resp.Data, nil
4122
}
4223

43-
func (c *Proxmox) DescribeTemplates(ctx context.Context, node string) ([]*VirtualMachine, error) {
44-
templates, err := c.ListTemplates(ctx, node)
45-
if err != nil {
46-
return nil, err
47-
}
48-
49-
var templateList []*VirtualMachine
50-
for _, vm := range templates {
51-
id := int(vm.Vmid)
52-
cf, err := c.DescribeVirtualMachine(ctx, node, id)
53-
if err != nil {
54-
return nil, err
55-
}
56-
templateList = append(templateList, cf)
57-
}
58-
59-
return templateList, nil
60-
}
61-
6224
func (c *Proxmox) DeleteVirtualMachine(ctx context.Context, node string, vmid int) error {
6325
vmId := strconv.Itoa(vmid)
6426
request := c.client.DeleteVirtualMachine(ctx, node, vmId)

proxmox/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ func (p *ProxmoxProvider) DataSources(ctx context.Context) []func() datasource.D
209209
bridges.DataSource,
210210
resource_pools.DataSource,
211211
vms.DataSource,
212-
templates.DataSource,
212+
templates.DataSourceMulti,
213+
templates.DataSourceSingle,
213214
zfs_pool.DataSource,
214215
lvmthin.DataSource,
215216
lvm.DataSource,

proxmox/qemu/templates/data_source.go renamed to proxmox/qemu/templates/data_source_multi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
_ datasource.DataSourceWithConfigure = &templatesDataSource{}
1818
)
1919

20-
func DataSource() datasource.DataSource {
20+
func DataSourceMulti() datasource.DataSource {
2121
return &templatesDataSource{}
2222
}
2323

@@ -48,7 +48,7 @@ func (d *templatesDataSource) Schema(_ context.Context, _ datasource.SchemaReque
4848
resp.Schema = schema.Schema{
4949
Attributes: map[string]schema.Attribute{
5050
"filters": filter.Schema(),
51-
"templates": DataSourceSchema,
51+
"templates": TemplateMultiDataSourceSchema,
5252
},
5353
}
5454
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package templates
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/awlsring/terraform-provider-proxmox/internal/service"
8+
qt "github.com/awlsring/terraform-provider-proxmox/proxmox/qemu/types"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/path"
12+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
14+
)
15+
16+
var (
17+
_ datasource.DataSource = &templateDataSource{}
18+
_ datasource.DataSourceWithConfigure = &templateDataSource{}
19+
_ datasource.DataSourceWithValidateConfig = &templateDataSource{}
20+
)
21+
22+
func DataSourceSingle() datasource.DataSource {
23+
return &templateDataSource{}
24+
}
25+
26+
type templateDataSource struct {
27+
client *service.Proxmox
28+
}
29+
30+
func (d *templateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
31+
resp.TypeName = req.ProviderTypeName + "_template"
32+
}
33+
34+
func (d *templateDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
35+
if req.ProviderData == nil {
36+
return
37+
}
38+
39+
d.client = req.ProviderData.(*service.Proxmox)
40+
}
41+
42+
func (d *templateDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
43+
tflog.Debug(ctx, "validate config template")
44+
idValue := basetypes.Int64Value{}
45+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("id"), &idValue)...)
46+
47+
nameValue := basetypes.StringValue{}
48+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &nameValue)...)
49+
if resp.Diagnostics.HasError() {
50+
return
51+
}
52+
53+
if nameValue.IsNull() && idValue.IsNull() {
54+
resp.Diagnostics.AddError(
55+
"Invalid template parameters",
56+
"Either name or id must be specified",
57+
)
58+
return
59+
}
60+
}
61+
62+
func (d *templateDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
63+
resp.Schema = schema.Schema{
64+
Attributes: TemplateSingleDataSourceSchema,
65+
}
66+
}
67+
68+
func (d *templateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
69+
tflog.Debug(ctx, "Read template")
70+
nodeValue := basetypes.StringValue{}
71+
72+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("node"), &nodeValue)...)
73+
node := nodeValue.ValueString()
74+
75+
idValue := basetypes.Int64Value{}
76+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("id"), &idValue)...)
77+
78+
nameValue := basetypes.StringValue{}
79+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &nameValue)...)
80+
if resp.Diagnostics.HasError() {
81+
return
82+
}
83+
84+
var vm *service.VirtualMachine
85+
var err error
86+
if !idValue.IsNull() {
87+
tflog.Debug(ctx, fmt.Sprintf("Getting template by id %v", idValue.ValueInt64()))
88+
id := int(idValue.ValueInt64())
89+
90+
vm, err = d.client.DescribeTemplate(ctx, node, id)
91+
if err != nil {
92+
resp.Diagnostics.AddError(
93+
"Unable to get templates",
94+
"An error was encountered retrieving templates.\n"+
95+
err.Error(),
96+
)
97+
return
98+
}
99+
} else if !nameValue.IsNull() {
100+
tflog.Debug(ctx, fmt.Sprintf("Getting template by name %v", nameValue.ValueString()))
101+
name := nameValue.ValueString()
102+
103+
vm, err = d.client.DescribeTemplateFromName(ctx, node, name)
104+
if err != nil {
105+
resp.Diagnostics.AddError(
106+
"Unable to get templates",
107+
"An error was encountered retrieving templates.\n"+
108+
err.Error(),
109+
)
110+
return
111+
}
112+
} else {
113+
resp.Diagnostics.AddError(
114+
"Unable to get templates",
115+
"An error was encountered retrieving templates.\n"+
116+
"Either id or name must be set",
117+
)
118+
return
119+
}
120+
121+
tflog.Debug(ctx, fmt.Sprintf("Converting template %v to model", vm.VmId))
122+
model := qt.VMToModel(ctx, vm)
123+
124+
diags := resp.State.Set(ctx, &model)
125+
resp.Diagnostics.Append(diags...)
126+
if resp.Diagnostics.HasError() {
127+
return
128+
}
129+
}

0 commit comments

Comments
 (0)