Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple categories #116

Merged
merged 1 commit into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions builder/nutanix/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,ClusterConfig,VmConfig,VmDisk,VmNIC
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,Category,ClusterConfig,VmConfig,VmDisk,VmNIC

package nutanix

Expand Down Expand Up @@ -36,14 +36,18 @@ type Config struct {
VmConfig `mapstructure:",squash"`
ForceDeregister bool `mapstructure:"force_deregister" json:"force_deregister" required:"false"`
ImageDescription string `mapstructure:"image_description" json:"image_description" required:"false"`
ImageCategoryKey string `mapstructure:"image_category_key" json:"image_category_key" required:"false"`
ImageCategoryValue string `mapstructure:"image_category_value" json:"image_category_value" required:"false"`
ImageCategories []Category `mapstructure:"image_categories" required:"false"`
ImageDelete bool `mapstructure:"image_delete" json:"image_delete" required:"false"`
WaitTimeout time.Duration `mapstructure:"ip_wait_timeout" json:"ip_wait_timeout" required:"false"`

ctx interpolate.Context
}

type Category struct {
Key string `mapstructure:"key" json:"key" required:"false"`
Value string `mapstructure:"value" json:"value" required:"false"`
}

type ClusterConfig struct {
Username string `mapstructure:"nutanix_username" required:"false"`
Password string `mapstructure:"nutanix_password" required:"false"`
Expand All @@ -65,19 +69,18 @@ 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"`
VmCategoryKey string `mapstructure:"vm_category_key" json:"vm_category_key" required:"false"`
VmCategoryValue string `mapstructure:"vm_category_value" json:"vm_category_value" 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"`
VMCategories []Category `mapstructure:"vm_categories" required:"false"`
}

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

// if c.VmConfig.SourceImageName == "" {
// errs = packersdk.MultiErrorAppend(errs, errors.New("Missing source_image_name"))
// }

if c.VmConfig.VMName == "" {
p := fmt.Sprintf("Packer-%s", random.String(random.PossibleAlphaNumUpper, 8))
log.Println("No vmname assigned, setting to " + p)
Expand All @@ -156,25 +155,29 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}

// Validate if both Image Category key and value are given in same time
if c.ImageCategoryKey != "" && c.ImageCategoryValue == "" {
log.Println("Nutanix Image Category value missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing image_category_value"))
}
for _, imageCategory := range c.ImageCategories {
if imageCategory.Key != "" && imageCategory.Value == "" {
log.Println("Nutanix Image Category value missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing value for image category %s", imageCategory.Key))
}

if c.ImageCategoryKey == "" && c.ImageCategoryValue != "" {
log.Println("Nutanix Image Category key missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing image_category_key"))
if imageCategory.Key == "" && imageCategory.Value != "" {
log.Println("Nutanix Image Category name missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing name for an image categories with value %s", imageCategory.Value))
}
}

// 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"))
}
for _, vmCategory := range c.VMCategories {
if vmCategory.Key != "" && vmCategory.Value == "" {
log.Println("Nutanix VM Category value missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing value for vm category %s", vmCategory.Key))
}

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 vmCategory.Key == "" && vmCategory.Value != "" {
log.Println("Nutanix VM Category name missing from configuration")
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("missing name for a vm categories with value %s", vmCategory.Value))
}
}

if c.CommConfig.SSHPort == 0 {
Expand Down
87 changes: 53 additions & 34 deletions builder/nutanix/config.hcl2spec.go

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

16 changes: 10 additions & 6 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Driver interface {
UploadImage(string, string, string, VmConfig) (*nutanixImage, error)
DeleteImage(string) error
GetImage(string) (*nutanixImage, error)
SaveVMDisk(string, string, string) (*nutanixImage, error)
SaveVMDisk(string, []Category) (*nutanixImage, error)
WaitForShutdown(string, <-chan struct{}) bool
}

Expand Down Expand Up @@ -426,9 +426,11 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig) (*v3.VMIntentInput, error) {
}
}

if vm.VmCategoryKey != "" && vm.VmCategoryValue != "" {
if len(vm.VMCategories) != 0 {
c := make(map[string]string)
c[vm.VmCategoryKey] = vm.VmCategoryValue
for _, category := range vm.VMCategories {
c[category.Key] = category.Value
}
req.Metadata.Categories = c
}

Expand Down Expand Up @@ -732,7 +734,7 @@ func (d *NutanixDriver) PowerOff(vmUUID string) error {
log.Printf("PowerOff task: %s", taskUUID)
return nil
}
func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageCategoryKey string, imageCategoryValue string) (*nutanixImage, error) {
func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageCategories []Category) (*nutanixImage, error) {

configCreds := client.Credentials{
URL: fmt.Sprintf("%s:%d", d.ClusterConfig.Endpoint, d.ClusterConfig.Port),
Expand Down Expand Up @@ -795,9 +797,11 @@ func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageCategoryKey string, ima
},
}

if imageCategoryKey != "" && imageCategoryValue != "" {
if len(imageCategories) != 0 {
c := make(map[string]string)
c[imageCategoryKey] = imageCategoryValue
for _, category := range imageCategories {
c[category.Key] = category.Value
}
req.Metadata.Categories = c
}

Expand Down
2 changes: 1 addition & 1 deletion builder/nutanix/step_copy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
return multistep.ActionHalt
}

imageResponse, err := d.SaveVMDisk(diskToCopy, s.Config.ImageCategoryKey, s.Config.ImageCategoryValue)
imageResponse, err := d.SaveVMDisk(diskToCopy, s.Config.ImageCategories)
if err != nil {
ui.Error("Image creation failed: " + err.Error())
state.Put("error", err)
Expand Down
Loading