Skip to content

Commit

Permalink
feat: set template name
Browse files Browse the repository at this point in the history
Adds the ability to set the name of the of the template in the `vsphere-template` post-processor. If `template_name` is not provided, the name of the source virtual machine will be used.

Ref: #397

Signed-off-by: Ryan Johnson <[email protected]
  • Loading branch information
tenthirtyam committed Apr 30, 2024
1 parent de0c305 commit 81d2fb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ Optional:
- `datacenter` (string) - Specifies the name of the datacenter to use.
Required when the vCenter Server instance endpoint has more than one datacenter.

- `template_name` (string) - Specifies the name of the template.
If not specified, the name of the virtual machine will be used.

- `folder` (string) - Specifies the name of the virtual machine folder path where the template will be created.

- `snapshot_enable` (bool) - Specifies whether to create a snapshot before marking as a template. Defaults to `false`.\
- `snapshot_enable` (bool) - Specifies whether to create a snapshot before marking as a template. Defaults to `false`.

- `snapshot_name` (string) - Specifies the name of the snapshot. Required when `snapshot_enable` is `true`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
- `datacenter` (string) - Specifies the name of the datacenter to use.
Required when the vCenter Server instance endpoint has more than one datacenter.

- `template_name` (string) - Specifies the name of the template.
If not specified, the name of the virtual machine will be used.

- `folder` (string) - Specifies the name of the virtual machine folder path where the template will be created.

- `snapshot_enable` (bool) - Specifies whether to create a snapshot before marking as a template. Defaults to `false`.\
- `snapshot_enable` (bool) - Specifies whether to create a snapshot before marking as a template. Defaults to `false`.

- `snapshot_name` (string) - Specifies the name of the snapshot. Required when `snapshot_enable` is `true`.

Expand Down
7 changes: 5 additions & 2 deletions post-processor/vsphere-template/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ type Config struct {
// Specifies the name of the datacenter to use.
// Required when the vCenter Server instance endpoint has more than one datacenter.
Datacenter string `mapstructure:"datacenter"`
// Specifies the name of the template.
// If not specified, the name of the virtual machine will be used.
TemplateName string `mapstructure:"template_name"`
// Specifies the name of the virtual machine folder path where the template will be created.
Folder string `mapstructure:"folder"`
// Specifies whether to create a snapshot before marking as a template. Defaults to `false`.\
// Specifies whether to create a snapshot before marking as a template. Defaults to `false`.
SnapshotEnable bool `mapstructure:"snapshot_enable"`
// Specifies the name of the snapshot. Required when `snapshot_enable` is `true`.
SnapshotName string `mapstructure:"snapshot_name"`
Expand Down Expand Up @@ -122,7 +125,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
// Check if the artifact is supported by the post-processor.
if _, ok := builtins[artifact.BuilderId()]; !ok {
return nil, false, false, fmt.Errorf(
"error: unsupported artifact type %s. supported types: vmware-iso (ESXi) or vSphere post-processor", artifact.BuilderId())
"error: unsupported artifact type %s. supported types: vsphere-iso exported OVF or vSphere post-processor", artifact.BuilderId())
}

f := artifact.State(ArtifactConfFormat)
Expand Down
2 changes: 2 additions & 0 deletions post-processor/vsphere-template/post-processor.hcl2spec.go

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

12 changes: 9 additions & 3 deletions post-processor/vsphere-template/step_mark_as_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

type stepMarkAsTemplate struct {
VMName string
TemplateName string
RemoteFolder string
ReregisterVM config.Trilean
}
Expand All @@ -44,6 +45,7 @@ func NewStepMarkAsTemplate(artifact packersdk.Artifact, p *PostProcessor) *stepM

return &stepMarkAsTemplate{
VMName: vmname,
TemplateName: p.config.TemplateName,
RemoteFolder: remoteFolder,
ReregisterVM: p.config.ReregisterVM,
}
Expand Down Expand Up @@ -74,8 +76,6 @@ func (s *stepMarkAsTemplate) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionContinue
}

ui.Message("Registering virtual machine as a template...")

dsPath, err := datastorePath(vm)
if err != nil {
state.Put("error", err)
Expand All @@ -102,7 +102,13 @@ func (s *stepMarkAsTemplate) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionHalt
}

task, err := folder.RegisterVM(context.Background(), dsPath.String(), s.VMName, true, nil, host)
artifactName := s.VMName
if s.TemplateName != "" {
artifactName = s.TemplateName
}
ui.Message("Registering virtual machine as a template: " + artifactName)

task, err := folder.RegisterVM(context.Background(), dsPath.String(), artifactName, true, nil, host)
if err != nil {
state.Put("error", err)
ui.Error("RegisterVM:" + err.Error())
Expand Down

0 comments on commit 81d2fb7

Please sign in to comment.