Allow tagging VMs

(cherry picked from commit 4200795cf0)
This commit is contained in:
Heinrich Kruger 2022-05-26 17:28:05 +01:00 committed by Dom Del Nano
parent f6a317cb41
commit 5c090071a9
5 changed files with 25 additions and 0 deletions

View File

@ -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) {

View File

@ -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"`

View File

@ -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"`

View File

@ -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()))

View File

@ -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