Allow tagging VMs

This commit is contained in:
Heinrich Kruger 2022-05-26 17:28:05 +01:00
parent 45ee5b90be
commit 4200795cf0
No known key found for this signature in database
GPG Key ID: 05A6F0214B1C419D
7 changed files with 37 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"`
@ -133,6 +134,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
c.IPGetter = "auto"
}
if c.VMTags == nil {
c.VMTags = make([]string, 0)
}
// Validation
if c.Username == "" {

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"`
ISOChecksumType string `mapstructure:"iso_checksum_type"`

View File

@ -107,6 +107,7 @@ type FlatConfig struct {
PlatformArgs map[string]string `mapstructure:"platform_args" cty:"platform_args" hcl:"platform_args"`
RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"`
SourcePath *string `mapstructure:"source_path" cty:"source_path" hcl:"source_path"`
VMTags []string `mapstructure:"vm_tags" cty:"vm_tags" hcl:"vm_tags"`
}
// FlatMapstructure returns a new FlatConfig.
@ -218,6 +219,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"platform_args": &hcldec.AttrSpec{Name: "platform_args", Type: cty.Map(cty.String), Required: false},
"install_timeout": &hcldec.AttrSpec{Name: "install_timeout", Type: cty.String, Required: false},
"source_path": &hcldec.AttrSpec{Name: "source_path", Type: cty.String, Required: false},
"vm_tags": &hcldec.AttrSpec{Name: "vm_tags", Type: cty.List(cty.String), Required: false},
}
return s
}

View File

@ -205,6 +205,12 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
}
}
err = xscommon.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

@ -159,6 +159,12 @@ func (self *stepCreateFromTemplate) Run(ctx context.Context, state multistep.Sta
}
}
err = xscommon.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