Allow tagging VMs
This commit is contained in:
parent
45ee5b90be
commit
4200795cf0
@ -624,6 +624,17 @@ func ConnectNetwork(c *Connection, networkRef xenapi.NetworkRef, vmRef xenapi.VM
|
|||||||
return &vif, nil
|
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
|
// Setters
|
||||||
|
|
||||||
func (self *VM) SetIsATemplate(is_a_template bool) (err error) {
|
func (self *VM) SetIsATemplate(is_a_template bool) (err error) {
|
||||||
|
@ -24,6 +24,7 @@ type CommonConfig struct {
|
|||||||
FloppyFiles []string `mapstructure:"floppy_files"`
|
FloppyFiles []string `mapstructure:"floppy_files"`
|
||||||
NetworkNames []string `mapstructure:"network_names"`
|
NetworkNames []string `mapstructure:"network_names"`
|
||||||
ExportNetworkNames []string `mapstructure:"export_network_names"`
|
ExportNetworkNames []string `mapstructure:"export_network_names"`
|
||||||
|
VMTags []string `mapstructure:"vm_tags"`
|
||||||
|
|
||||||
HostPortMin uint `mapstructure:"host_port_min"`
|
HostPortMin uint `mapstructure:"host_port_min"`
|
||||||
HostPortMax uint `mapstructure:"host_port_max"`
|
HostPortMax uint `mapstructure:"host_port_max"`
|
||||||
@ -133,6 +134,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
|
|||||||
c.IPGetter = "auto"
|
c.IPGetter = "auto"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.VMTags == nil {
|
||||||
|
c.VMTags = make([]string, 0)
|
||||||
|
}
|
||||||
|
|
||||||
// Validation
|
// Validation
|
||||||
|
|
||||||
if c.Username == "" {
|
if c.Username == "" {
|
||||||
|
@ -20,6 +20,7 @@ type Config struct {
|
|||||||
DiskSize uint `mapstructure:"disk_size"`
|
DiskSize uint `mapstructure:"disk_size"`
|
||||||
CloneTemplate string `mapstructure:"clone_template"`
|
CloneTemplate string `mapstructure:"clone_template"`
|
||||||
VMOtherConfig map[string]string `mapstructure:"vm_other_config"`
|
VMOtherConfig map[string]string `mapstructure:"vm_other_config"`
|
||||||
|
VMTags []string `mapstructure:"vm_tags"`
|
||||||
|
|
||||||
ISOChecksum string `mapstructure:"iso_checksum"`
|
ISOChecksum string `mapstructure:"iso_checksum"`
|
||||||
ISOChecksumType string `mapstructure:"iso_checksum_type"`
|
ISOChecksumType string `mapstructure:"iso_checksum_type"`
|
||||||
|
@ -107,6 +107,7 @@ type FlatConfig struct {
|
|||||||
PlatformArgs map[string]string `mapstructure:"platform_args" cty:"platform_args" hcl:"platform_args"`
|
PlatformArgs map[string]string `mapstructure:"platform_args" cty:"platform_args" hcl:"platform_args"`
|
||||||
RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"`
|
RawInstallTimeout *string `mapstructure:"install_timeout" cty:"install_timeout" hcl:"install_timeout"`
|
||||||
SourcePath *string `mapstructure:"source_path" cty:"source_path" hcl:"source_path"`
|
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.
|
// 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},
|
"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},
|
"install_timeout": &hcldec.AttrSpec{Name: "install_timeout", Type: cty.String, Required: false},
|
||||||
"source_path": &hcldec.AttrSpec{Name: "source_path", 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
|
return s
|
||||||
}
|
}
|
||||||
|
@ -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)
|
instanceId, err := c.GetClient().VM.GetUUID(c.GetSessionRef(), instance)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
|
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
|
||||||
|
@ -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)
|
instanceId, err := c.GetClient().VM.GetUUID(c.GetSessionRef(), instance)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
|
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
|
||||||
|
@ -86,6 +86,12 @@ func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBa
|
|||||||
return multistep.ActionHalt
|
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))
|
ui.Say(fmt.Sprintf("Imported instance '%s'", instanceId))
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
|
Loading…
Reference in New Issue
Block a user