Skip to content

Commit 83b1052

Browse files
authored
Merge pull request #67 from ddelnano/ddelnano/add-tags-to-vms
Allow support for adding tags to VMs
2 parents f6a317c + de1a62d commit 83b1052

7 files changed

Lines changed: 29 additions & 0 deletions

File tree

builder/xenserver/common/client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,17 @@ func ConnectNetwork(c *Connection, networkRef xenapi.NetworkRef, vmRef xenapi.VM
624624
return &vif, nil
625625
}
626626

627+
func AddVMTags(c *Connection, vmRef xenapi.VMRef, tags []string) error {
628+
for _, tag := range tags {
629+
log.Printf("Adding tag %s to VM %s\n", tag, vmRef)
630+
err := c.GetClient().VM.AddTags(c.session, vmRef, tag)
631+
if err != nil {
632+
return err
633+
}
634+
}
635+
return nil
636+
}
637+
627638
// Setters
628639

629640
func (self *VM) SetIsATemplate(is_a_template bool) (err error) {

builder/xenserver/common/common_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type CommonConfig struct {
2424
FloppyFiles []string `mapstructure:"floppy_files"`
2525
NetworkNames []string `mapstructure:"network_names"`
2626
ExportNetworkNames []string `mapstructure:"export_network_names"`
27+
VMTags []string `mapstructure:"vm_tags"`
2728

2829
HostPortMin uint `mapstructure:"host_port_min"`
2930
HostPortMax uint `mapstructure:"host_port_max"`

builder/xenserver/common/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Config struct {
2020
DiskSize uint `mapstructure:"disk_size"`
2121
CloneTemplate string `mapstructure:"clone_template"`
2222
VMOtherConfig map[string]string `mapstructure:"vm_other_config"`
23+
VMTags []string `mapstructure:"vm_tags"`
2324

2425
ISOChecksum string `mapstructure:"iso_checksum"`
2526
ISOUrls []string `mapstructure:"iso_urls"`

builder/xenserver/common/config.hcl2spec.go

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

builder/xenserver/common/step_create_instance.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ func (self *StepCreateInstance) Run(ctx context.Context, state multistep.StateBa
216216
}
217217
}
218218

219+
err = AddVMTags(c, instance, config.VMTags)
220+
if err != nil {
221+
ui.Error(fmt.Sprintf("Failed to add tags: %s", err.Error()))
222+
return multistep.ActionHalt
223+
}
224+
219225
instanceId, err := c.GetClient().VM.GetUUID(c.GetSessionRef(), instance)
220226
if err != nil {
221227
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))

builder/xenserver/xva/step_import_instance.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBa
8686
return multistep.ActionHalt
8787
}
8888

89+
err = xscommon.AddVMTags(c, instance, config.VMTags)
90+
if err != nil {
91+
ui.Error(fmt.Sprintf("Failed to add tags: %s", err.Error()))
92+
return multistep.ActionHalt
93+
}
94+
8995
ui.Say(fmt.Sprintf("Imported instance '%s'", instanceId))
9096

9197
return multistep.ActionContinue

docs/builders/iso/xenserver-iso.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ each category, the available options are alphabetized and described.
207207
* `vm_memory` (integer) - The size, in megabytes, of the amount of memory to
208208
allocate for the VM. By default, this is 1024 (1 GB).
209209

210+
* `vm_tags` (array of strings) - A list of tags to add to the VM
211+
210212
## Differences with other Packer builders
211213

212214
Currently the XenServer builder has some quirks when compared with other Packer builders.

0 commit comments

Comments
 (0)