forked from nutanix-cloud-native/packer-plugin-nutanix
/
config.go
148 lines (123 loc) · 4.9 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,ClusterConfig,VmConfig,VmDisk,VmNIC
package nutanix
import (
//"errors"
"fmt"
"log"
"time"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/random"
"github.com/hashicorp/packer-plugin-sdk/shutdowncommand"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
CommConfig communicator.Config `mapstructure:",squash"`
commonsteps.CDConfig `mapstructure:",squash"`
shutdowncommand.ShutdownConfig `mapstructure:",squash"`
ClusterConfig `mapstructure:",squash"`
VmConfig `mapstructure:",squash"`
ctx interpolate.Context
}
type ClusterConfig struct {
Username string `mapstructure:"nutanix_username" required:"false"`
Password string `mapstructure:"nutanix_password" required:"false"`
Insecure bool `mapstructure:"nutanix_insecure" required:"false"`
Endpoint string `mapstructure:"nutanix_endpoint" required:"true"`
Port int32 `mapstructure:"nutanix_port" required:"false"`
}
type VmDisk struct {
ImageType string `mapstructure:"image_type" json:"image_type" required:"false"`
SourceImageName string `mapstructure:"source_image_name" json:"source_image_name" required:"false"`
SourceImageUUID string `mapstructure:"source_image_uuid" json:"source_image_uuid" required:"false"`
DiskSizeGB int64 `mapstructure:"disk_size_gb" json:"disk_size_gb" required:"false"`
}
type VmNIC struct {
SubnetName string `mapstructure:"subnet_name" json:"subnet_name" required:"false"`
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"`
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 int32 `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"`
}
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
err := config.Decode(c, &config.DecodeOpts{
PluginType: BuilderId,
Interpolate: true,
InterpolateContext: &c.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"boot_command",
},
},
}, raws...)
if err != nil {
return nil, err
}
// Accumulate any errors and warnings
var errs *packersdk.MultiError
warnings := make([]string, 0)
// if c.VmConfig.DiskSizeGB == 0 {
// c.VmConfig.DiskSizeGB = 40
// }
if c.CommConfig.Type == "" {
if c.CommConfig.SSHUsername != "" {
log.Println("No Communicator Type assigned but SSH Creds available, setting to 'SSH'")
c.CommConfig.Type = "ssh"
} else {
log.Println("No Communicator Type set, setting to 'none'")
c.CommConfig.Type = "none"
}
}
if c.MemoryMB == 0 {
log.Println("No VM Memory configured, defaulting to '4096'")
c.MemoryMB = 4096
}
if c.ClusterConfig.Port == 0 {
log.Println("No Nutanix Port configured, defaulting to '9440'")
c.ClusterConfig.Port = 9440
}
// Validate Cluster Username
if c.ClusterConfig.Username == "" {
log.Println("Nutanix Username missing from configuration")
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)
c.VmConfig.VMName = p
}
if c.VmConfig.ImageName == "" {
log.Println("No image_name assigned, setting to vm_name")
c.VmConfig.ImageName = c.VmConfig.VMName
}
if c.CommConfig.SSHPort == 0 {
log.Println("SSHPort not set, defaulting to 22")
c.CommConfig.SSHPort = 22
}
if c.CommConfig.SSHTimeout == 0 {
log.Println("SSHTimeout not set, defaulting to 20min")
c.CommConfig.SSHTimeout = 20 * time.Minute
}
errs = packersdk.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
errs = packersdk.MultiErrorAppend(errs, c.CDConfig.Prepare(&c.ctx)...)
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
}
return warnings, nil
}