Skip to content

Commit 2599b9a

Browse files
committed
add: name and tag fields into output
After some testing in lab I decided that some additional info about found VM will be useful (to add source image name into notes or copy tags from source).
1 parent 4246f9b commit 2599b9a

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

.web-docs/components/data-source/template/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ When datasource cannot return only one (zero or >1) guest identifiers it will re
7878

7979
- `vm_id` (uint) - Identifier of the found guest.
8080

81+
- `vm_name` (string) - Name of the found guest.
82+
83+
- `vm_tags` (string) - Tags of the found guest separated with semicolon.
84+
8185
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/proxmox/data.go; -->
8286

8387

8488
## Example Usage
8589

86-
This is a very basic example which connects to localhost, finds the latest guest
87-
which name matches the regex `image-.*` and which type is `template`. The ID is
88-
then printed to console as output variable.
90+
This is a very basic example which connects to local PVE host, finds the latest
91+
guest which name matches the regex `image-.*` and which type is `template`. The
92+
ID is then printed to console as output variable.
8993

9094
```hcl
9195
data "proxmox-template" "default" {
@@ -117,5 +121,4 @@ build {
117121
]
118122
}
119123
}
120-
121124
```

datasource/proxmox/data.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ type Datasource struct {
9191
type DatasourceOutput struct {
9292
// Identifier of the found guest.
9393
VmId uint `mapstructure:"vm_id"`
94+
// Name of the found guest.
95+
VmName string `mapstructure:"vm_name"`
96+
// Tags of the found guest separated with semicolon.
97+
VmTags string `mapstructure:"vm_tags"`
9498
}
9599

96100
type vmConfig map[string]interface{}
@@ -162,6 +166,7 @@ func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
162166
func (d *Datasource) Execute() (cty.Value, error) {
163167
// This value of VM ID the function should return
164168
var vmId uint
169+
var vmName, vmTags string
165170

166171
client, err := newProxmoxClient(d.config)
167172
if err != nil {
@@ -190,15 +195,21 @@ func (d *Datasource) Execute() (cty.Value, error) {
190195
}
191196

192197
vmId = latestConfig["vmid"].(uint)
198+
vmName = latestConfig["name"].(string)
199+
vmTags = latestConfig["tags"].(string)
193200
} else {
194201
if len(filteredVms) > 1 {
195202
return cty.NullVal(cty.EmptyObject), errors.New("more than one guest passed filters, cannot return vm_id")
196203
}
197204
vmId = filteredVms[0].Id
205+
vmName = filteredVms[0].Name
206+
vmTags = joinTags(filteredVms[0].Tags, ";")
198207
}
199208

200209
output := DatasourceOutput{
201-
VmId: vmId,
210+
VmId: vmId,
211+
VmName: vmName,
212+
VmTags: vmTags,
202213
}
203214
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
204215
}
@@ -391,3 +402,11 @@ func parseMetaField(field string) (int, error) {
391402
}
392403
return value, nil
393404
}
405+
406+
func joinTags(tags []proxmox.Tag, separator string) string {
407+
tagsAsStrings := make([]string, len(tags))
408+
for i, tag := range tags {
409+
tagsAsStrings[i] = string(tag)
410+
}
411+
return strings.Join(tagsAsStrings, separator)
412+
}

datasource/proxmox/data.hcl2spec.go

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datasource/proxmox/data_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func TestExecute(t *testing.T) {
166166
}
167167
if err == nil {
168168
vmIdInt64, _ := result.GetAttr("vm_id").AsBigFloat().Int64()
169+
vmName := result.GetAttr("vm_name").AsString()
170+
vmTags := result.GetAttr("vm_tags").AsString()
171+
t.Logf("Returned: vmId=%d, vmName=%s, vmTags=%s", vmIdInt64, vmName, vmTags)
169172
require.Equal(t, dsTestConfig.expectedVmId, vmIdInt64)
170173
}
171174
})

docs-partials/datasource/proxmox/DatasourceOutput.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
- `vm_id` (uint) - Identifier of the found guest.
44

5+
- `vm_name` (string) - Name of the found guest.
6+
7+
- `vm_tags` (string) - Tags of the found guest separated with semicolon.
8+
59
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/proxmox/data.go; -->

0 commit comments

Comments
 (0)