Skip to content

Commit

Permalink
implement vm category (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxtof authored Feb 23, 2023
1 parent 1cdcd56 commit 3c1c5e9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 deletions.
35 changes: 24 additions & 11 deletions builder/nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ type VmNIC struct {
SubnetUUID string `mapstructure:"subnet_uuid" json:"subnet_uuid" required:"false"`
}
type VmConfig struct {
VMName string `mapstructure:"vm_name" json:"vm_name" required:"false"`
OSType string `mapstructure:"os_type" json:"os_type" required:"true"`
BootType string `mapstructure:"boot_type" json:"boot_type" required:"false"`
VmDisks []VmDisk `mapstructure:"vm_disks"`
VmNICs []VmNIC `mapstructure:"vm_nics"`
ImageName string `mapstructure:"image_name" json:"image_name" required:"false"`
ClusterUUID string `mapstructure:"cluster_uuid" json:"cluster_uuid" required:"false"`
ClusterName string `mapstructure:"cluster_name" json:"cluster_name" required:"false"`
CPU int64 `mapstructure:"cpu" json:"cpu" required:"false"`
MemoryMB int64 `mapstructure:"memory_mb" json:"memory_mb" required:"false"`
UserData string `mapstructure:"user_data" json:"user_data" required:"false"`
VMName string `mapstructure:"vm_name" json:"vm_name" required:"false"`
OSType string `mapstructure:"os_type" json:"os_type" required:"true"`
BootType string `mapstructure:"boot_type" json:"boot_type" required:"false"`
VmDisks []VmDisk `mapstructure:"vm_disks"`
VmNICs []VmNIC `mapstructure:"vm_nics"`
ImageName string `mapstructure:"image_name" json:"image_name" required:"false"`
ClusterUUID string `mapstructure:"cluster_uuid" json:"cluster_uuid" required:"false"`
ClusterName string `mapstructure:"cluster_name" json:"cluster_name" required:"false"`
CPU int64 `mapstructure:"cpu" json:"cpu" required:"false"`
MemoryMB int64 `mapstructure:"memory_mb" json:"memory_mb" required:"false"`
UserData string `mapstructure:"user_data" json:"user_data" required:"false"`
VmCategoryKey string `mapstructure:"vm_category_key" json:"vm_category_key" required:"false"`
VmCategoryValue string `mapstructure:"vm_category_value" json:"vm_category_value" required:"false"`
}

func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
Expand Down Expand Up @@ -164,6 +166,17 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing image_category_key"))
}

// Validate if both VM Category key and value are given in same time
if c.VmCategoryKey != "" && c.VmCategoryValue == "" {
log.Println("Nutanix VM Category value missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing vm_category_value"))
}

if c.VmCategoryKey == "" && c.VmCategoryValue != "" {
log.Println("Nutanix VM Category key missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing vm_category_key"))
}

if c.CommConfig.SSHPort == 0 {
log.Println("SSHPort not set, defaulting to 22")
c.CommConfig.SSHPort = 22
Expand Down
52 changes: 30 additions & 22 deletions builder/nutanix/config.hcl2spec.go

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

6 changes: 6 additions & 0 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig) (*v3.VMIntentInput, error) {
}
}

if vm.VmCategoryKey != "" && vm.VmCategoryValue != "" {
c := make(map[string]string)
c[vm.VmCategoryKey] = vm.VmCategoryValue
req.Metadata.Categories = c
}

return req, nil

}
Expand Down
3 changes: 3 additions & 0 deletions docs/builders/nutanix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ These parameters allow to define information about platform and temporary VM use
- `boot_type` (string) - Type of boot used on the temporary VM ("legacy" or "UEFI").
- `ip_wait_timeout` (duration string | ex: "0h42m0s") - Amount of time to wait for VM's IP, similar to 'ssh_timeout'. Defaults to 15m (15 minutes). See the Golang [ParseDuration](https://golang.org/pkg/time/#ParseDuration) documentation for full details.


## Output configuration
These parameters allow to configure everything around image creation, from the temporary VM connection to the final image definition.

Expand All @@ -45,6 +46,8 @@ These parameters allow to configure everything around image creation, from the t
- `shutdown_command` (string) - Command line to shutdown your temporary VM.
- `shutdown_timeout` (string) - Timeout for VM shutdown (format : 2m).
- `communicator` (string) - Protocol used for Packer connection (ex "winrm" or "ssh"). Default is : "ssh".
- `vm_category_key` (string) - Name of the category to assign to the vm.
- `vm_category_value` (string) - Value of the category to assign to the vm.

### Dedicated to Linux
- `user_data` (string) - cloud-init content base64 coded.
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/centos-img/source.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ source "nutanix" "centos" {
}

vm_name = "e2e-packer-${var.test}-${formatdate("MDYYhms", timestamp())}"
vm_category_key = "Environment"
vm_category_value = "Testing"

image_name = "e2e-packer-${var.test}-${formatdate("MDYYhms", timestamp())}"
image_delete = true
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/centos-iso/source.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ source "nutanix" "centos" {
cd_label = "OEMDRV"

vm_name = "e2e-packer-${var.test}-${formatdate("MDYYhms", timestamp())}"
vm_category_key = "Environment"
vm_category_value = "Testing"

image_name = "e2e-packer-${var.test}-${formatdate("MDYYhms", timestamp())}"
image_delete = true
Expand Down

0 comments on commit 3c1c5e9

Please sign in to comment.