Skip to content

Commit

Permalink
Merge pull request #67 from ddelnano/ddelnano/add-tags-to-vms
Browse files Browse the repository at this point in the history
Allow support for adding tags to VMs
  • Loading branch information
ddelnano authored Apr 15, 2023
2 parents f6a317c + de1a62d commit 83b1052
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions builder/xenserver/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,17 @@ func ConnectNetwork(c *Connection, networkRef xenapi.NetworkRef, vmRef xenapi.VM
return &vif, nil
}

func AddVMTags(c *Connection, vmRef xenapi.VMRef, tags []string) error {
for _, tag := range tags {
log.Printf("Adding tag %s to VM %s\n", tag, vmRef)
err := c.GetClient().VM.AddTags(c.session, vmRef, tag)
if err != nil {
return err
}
}
return nil
}

// Setters

func (self *VM) SetIsATemplate(is_a_template bool) (err error) {
Expand Down
1 change: 1 addition & 0 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type CommonConfig struct {
FloppyFiles []string `mapstructure:"floppy_files"`
NetworkNames []string `mapstructure:"network_names"`
ExportNetworkNames []string `mapstructure:"export_network_names"`
VMTags []string `mapstructure:"vm_tags"`

HostPortMin uint `mapstructure:"host_port_min"`
HostPortMax uint `mapstructure:"host_port_max"`
Expand Down
1 change: 1 addition & 0 deletions builder/xenserver/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
DiskSize uint `mapstructure:"disk_size"`
CloneTemplate string `mapstructure:"clone_template"`
VMOtherConfig map[string]string `mapstructure:"vm_other_config"`
VMTags []string `mapstructure:"vm_tags"`

ISOChecksum string `mapstructure:"iso_checksum"`
ISOUrls []string `mapstructure:"iso_urls"`
Expand Down
2 changes: 2 additions & 0 deletions builder/xenserver/common/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions builder/xenserver/common/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ func (self *StepCreateInstance) Run(ctx context.Context, state multistep.StateBa
}
}

err = AddVMTags(c, instance, config.VMTags)
if err != nil {
ui.Error(fmt.Sprintf("Failed to add tags: %s", err.Error()))
return multistep.ActionHalt
}

instanceId, err := c.GetClient().VM.GetUUID(c.GetSessionRef(), instance)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
Expand Down
6 changes: 6 additions & 0 deletions builder/xenserver/xva/step_import_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBa
return multistep.ActionHalt
}

err = xscommon.AddVMTags(c, instance, config.VMTags)
if err != nil {
ui.Error(fmt.Sprintf("Failed to add tags: %s", err.Error()))
return multistep.ActionHalt
}

ui.Say(fmt.Sprintf("Imported instance '%s'", instanceId))

return multistep.ActionContinue
Expand Down
2 changes: 2 additions & 0 deletions docs/builders/iso/xenserver-iso.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ each category, the available options are alphabetized and described.
* `vm_memory` (integer) - The size, in megabytes, of the amount of memory to
allocate for the VM. By default, this is 1024 (1 GB).

* `vm_tags` (array of strings) - A list of tags to add to the VM

## Differences with other Packer builders

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

0 comments on commit 83b1052

Please sign in to comment.