Skip to content

Commit

Permalink
refactor: tools upload flavors
Browse files Browse the repository at this point in the history
Refactors to use `allowedToolsFlavorValues` for tools upload flavor options based on constants.

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed Aug 6, 2024
1 parent 593c198 commit 8e71e70
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 59 deletions.
30 changes: 16 additions & 14 deletions .web-docs/components/builder/iso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,22 @@ JSON Example:

<!-- Code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; DO NOT EDIT MANUALLY -->

- `tools_upload_flavor` (string) - The flavor of the VMware Tools ISO to
upload into the VM. Valid values are darwin, linux, and windows. By
default, this is empty, which means VMware tools won't be uploaded.

- `tools_upload_path` (string) - The path in the VM to upload the VMware tools. This only takes effect if
`tools_upload_flavor` is non-empty. This is a [configuration
template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
`Flavor`, which will be the value of `tools_upload_flavor`. By default
the upload path is set to `{{.Flavor}}.iso`. This setting is not used
when `remote_type` is `esx5`.

- `tools_source_path` (string) - The path on your local machine to fetch the vmware tools from. If this
is not set but the tools_upload_flavor is set, then Packer will try to
load the VMware tools from the VMware installation directory.
- `tools_upload_flavor` (string) - The flavor of VMware tools to upload into the virtual machine based on
the guest operating system. Allowed values are `darwin` (macOS), `linux`,
and `windows`. Default is empty and no version will be uploaded.

- `tools_upload_path` (string) - The path in the virtual machine to upload the VMware Tools. This only
takes effect if `tools_upload_flavor` is non-empty. This is a
[configuration template](/packer/docs/templates/legacy_json_templates/engine)
that has a single valid variable, `Flavor`, which will be the value of
`tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.

~> **Note:** This setting is not used when `remote_type` is `esx5`.

- `tools_source_path` (string) - The local path on your machine to the VMware Tools ISO file.

~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
will load the VMware Tools from the product installation directory.

<!-- End of code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; -->

Expand Down
30 changes: 16 additions & 14 deletions .web-docs/components/builder/vmx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ JSON Example:

<!-- Code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; DO NOT EDIT MANUALLY -->

- `tools_upload_flavor` (string) - The flavor of the VMware Tools ISO to
upload into the VM. Valid values are darwin, linux, and windows. By
default, this is empty, which means VMware tools won't be uploaded.

- `tools_upload_path` (string) - The path in the VM to upload the VMware tools. This only takes effect if
`tools_upload_flavor` is non-empty. This is a [configuration
template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
`Flavor`, which will be the value of `tools_upload_flavor`. By default
the upload path is set to `{{.Flavor}}.iso`. This setting is not used
when `remote_type` is `esx5`.

- `tools_source_path` (string) - The path on your local machine to fetch the vmware tools from. If this
is not set but the tools_upload_flavor is set, then Packer will try to
load the VMware tools from the VMware installation directory.
- `tools_upload_flavor` (string) - The flavor of VMware tools to upload into the virtual machine based on
the guest operating system. Allowed values are `darwin` (macOS), `linux`,
and `windows`. Default is empty and no version will be uploaded.

- `tools_upload_path` (string) - The path in the virtual machine to upload the VMware Tools. This only
takes effect if `tools_upload_flavor` is non-empty. This is a
[configuration template](/packer/docs/templates/legacy_json_templates/engine)
that has a single valid variable, `Flavor`, which will be the value of
`tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.

~> **Note:** This setting is not used when `remote_type` is `esx5`.

- `tools_source_path` (string) - The local path on your machine to the VMware Tools ISO file.

~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
will load the VMware Tools from the product installation directory.

<!-- End of code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; -->

Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/common/step_upload_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *StepUploadTools) Run(ctx context.Context, state multistep.StateBag) mul
tools_source := state.Get("tools_upload_source").(string)
ui := state.Get("ui").(packersdk.Ui)

ui.Say(fmt.Sprintf("Uploading the '%s' VMware Tools", c.ToolsUploadFlavor))
ui.Say(fmt.Sprintf("Uploading VMware Tools (%s)...", c.ToolsUploadFlavor))
f, err := os.Open(tools_source)
if err != nil {
state.Put("error", fmt.Errorf("error opening VMware Tools ISO: %s", err))
Expand Down
55 changes: 38 additions & 17 deletions builder/vmware/common/tools_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,59 @@ package common

import (
"fmt"
"slices"
"strings"

"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)

// Set the allowed values for the `ToolsUploadFlavor`.
const (
ToolsFlavorMacOS = "darwin"
ToolsFlavorLinux = "linux"
ToolsFlavorWindows = "windows"
)

// allowedToolsFlavorValues is a list of allowed values for the
// `ToolsUploadFlavor`.
var allowedToolsFlavorValues = []string{ToolsFlavorMacOS, ToolsFlavorLinux, ToolsFlavorWindows}

type ToolsConfig struct {
// The flavor of the VMware Tools ISO to
// upload into the VM. Valid values are darwin, linux, and windows. By
// default, this is empty, which means VMware tools won't be uploaded.
// The flavor of VMware tools to upload into the virtual machine based on
// the guest operating system. Allowed values are `darwin` (macOS), `linux`,
// and `windows`. Default is empty and no version will be uploaded.
ToolsUploadFlavor string `mapstructure:"tools_upload_flavor" required:"false"`
// The path in the VM to upload the VMware tools. This only takes effect if
// `tools_upload_flavor` is non-empty. This is a [configuration
// template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
// `Flavor`, which will be the value of `tools_upload_flavor`. By default
// the upload path is set to `{{.Flavor}}.iso`. This setting is not used
// when `remote_type` is `esx5`.
// The path in the virtual machine to upload the VMware Tools. This only
// takes effect if `tools_upload_flavor` is non-empty. This is a
// [configuration template](/packer/docs/templates/legacy_json_templates/engine)
// that has a single valid variable, `Flavor`, which will be the value of
// `tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.
//
// ~> **Note:** This setting is not used when `remote_type` is `esx5`.
ToolsUploadPath string `mapstructure:"tools_upload_path" required:"false"`
// The path on your local machine to fetch the vmware tools from. If this
// is not set but the tools_upload_flavor is set, then Packer will try to
// load the VMware tools from the VMware installation directory.
// The local path on your machine to the VMware Tools ISO file.
//
// ~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
// will load the VMware Tools from the product installation directory.
ToolsSourcePath string `mapstructure:"tools_source_path" required:"false"`
}

func (c *ToolsConfig) Prepare(ctx *interpolate.Context) []error {
errs := []error{}

if c.ToolsUploadPath == "" {
if c.ToolsSourcePath != "" && c.ToolsUploadFlavor == "" {
errs = append(errs, fmt.Errorf("provide either "+
"'tools_upload_flavor' or 'tools_upload_path' with "+
"'tools_source_path'"))
errs = append(errs, fmt.Errorf("provide either 'tools_upload_flavor' or 'tools_upload_path' with 'tools_source_path'"))
} else if c.ToolsUploadFlavor != "" && !slices.Contains(allowedToolsFlavorValues, c.ToolsUploadFlavor) {
errMsg := fmt.Sprintf("invalid 'tools_upload_flavor' specified: %s; must be one of %s", c.ToolsUploadFlavor, strings.Join(allowedToolsFlavorValues, ", "))
errs = append(errs, fmt.Errorf(errMsg))
} else {
c.ToolsUploadPath = fmt.Sprintf("%s.iso", c.ToolsUploadFlavor)
}
c.ToolsUploadPath = "{{ .Flavor }}.iso"
}

if c.ToolsSourcePath == "" {
c.ToolsUploadPath = "{{ .Flavor }}.iso"
}
}
return errs
}
2 changes: 1 addition & 1 deletion builder/vmware/iso/testdata/minimal.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"tools_upload_flavor": "linux"
}
]
}
}
26 changes: 14 additions & 12 deletions docs-partials/builder/vmware/common/ToolsConfig-not-required.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<!-- Code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; DO NOT EDIT MANUALLY -->

- `tools_upload_flavor` (string) - The flavor of the VMware Tools ISO to
upload into the VM. Valid values are darwin, linux, and windows. By
default, this is empty, which means VMware tools won't be uploaded.
- `tools_upload_flavor` (string) - The flavor of VMware tools to upload into the virtual machine based on
the guest operating system. Allowed values are `darwin` (macOS), `linux`,
and `windows`. Default is empty and no version will be uploaded.

- `tools_upload_path` (string) - The path in the VM to upload the VMware tools. This only takes effect if
`tools_upload_flavor` is non-empty. This is a [configuration
template](/packer/docs/templates/legacy_json_templates/engine) that has a single valid variable:
`Flavor`, which will be the value of `tools_upload_flavor`. By default
the upload path is set to `{{.Flavor}}.iso`. This setting is not used
when `remote_type` is `esx5`.
- `tools_upload_path` (string) - The path in the virtual machine to upload the VMware Tools. This only
takes effect if `tools_upload_flavor` is non-empty. This is a
[configuration template](/packer/docs/templates/legacy_json_templates/engine)
that has a single valid variable, `Flavor`, which will be the value of
`tools_upload_flavor` when the upload path is set to `{{.Flavor}}.iso`.

~> **Note:** This setting is not used when `remote_type` is `esx5`.

- `tools_source_path` (string) - The path on your local machine to fetch the vmware tools from. If this
is not set but the tools_upload_flavor is set, then Packer will try to
load the VMware tools from the VMware installation directory.
- `tools_source_path` (string) - The local path on your machine to the VMware Tools ISO file.

~> **Note:** If not set, but the `tools_upload_flavor` is set, the plugin
will load the VMware Tools from the product installation directory.

<!-- End of code generated from the comments of the ToolsConfig struct in builder/vmware/common/tools_config.go; -->

0 comments on commit 8e71e70

Please sign in to comment.