internal/vmware: add support for the GOVC_FOLDER option

When importing the ova it also creates a VM, and users don't always have
permission to register in the default folder.
This commit is contained in:
Sanne Raymaekers 2023-05-08 16:06:35 +02:00
parent ce52f658d1
commit 8a8607cdf6
4 changed files with 24 additions and 12 deletions

View file

@ -437,6 +437,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
Cluster: targetOptions.Cluster,
Datacenter: targetOptions.Datacenter,
Datastore: targetOptions.Datastore,
Folder: targetOptions.Folder,
}
tempDirectory, err := os.MkdirTemp(impl.Output, job.Id().String()+"-vmware-*")

View file

@ -9,6 +9,7 @@ type VMWareTargetOptions struct {
Datacenter string `json:"datacenter"`
Cluster string `json:"cluster"`
Datastore string `json:"datastore"`
Folder string `json:"folder"`
}
func (VMWareTargetOptions) isTargetOptions() {}

View file

@ -14,6 +14,22 @@ type Credentials struct {
Datacenter string
Cluster string
Datastore string
Folder string
}
func commonOptions(creds Credentials) []string {
args := []string{
fmt.Sprintf("-u=%s:%s@%s", creds.Username, creds.Password, creds.Host),
"-k=true",
fmt.Sprintf("-pool=%s/Resources", creds.Cluster),
fmt.Sprintf("-dc=%s", creds.Datacenter),
fmt.Sprintf("-ds=%s", creds.Datastore),
}
if creds.Folder != "" {
args = append(args, fmt.Sprintf("-folder=%s", creds.Folder))
}
return args
}
// ImportVmdk is a function that uploads a stream optimized vmdk image to vSphere
@ -21,13 +37,9 @@ type Credentials struct {
func ImportVmdk(creds Credentials, imagePath string) error {
args := []string{
"import.vmdk",
fmt.Sprintf("-u=%s:%s@%s", creds.Username, creds.Password, creds.Host),
"-k=true",
fmt.Sprintf("-pool=%s/Resources", creds.Cluster),
fmt.Sprintf("-dc=%s", creds.Datacenter),
fmt.Sprintf("-ds=%s", creds.Datastore),
imagePath,
}
args = append(args, commonOptions(creds)...)
args = append(args, imagePath)
retcode := cli.Run(args)
if retcode != 0 {
@ -39,14 +51,10 @@ func ImportVmdk(creds Credentials, imagePath string) error {
func ImportOva(creds Credentials, imagePath, targetName string) error {
args := []string{
"import.ova",
fmt.Sprintf("-u=%s:%s@%s", creds.Username, creds.Password, creds.Host),
"-k=true",
fmt.Sprintf("-pool=%s/Resources", creds.Cluster),
fmt.Sprintf("-dc=%s", creds.Datacenter),
fmt.Sprintf("-ds=%s", creds.Datastore),
fmt.Sprintf("-name=%s", targetName),
imagePath,
}
args = append(args, commonOptions(creds)...)
args = append(args, imagePath)
retcode := cli.Run(args)
if retcode != 0 {

View file

@ -81,6 +81,7 @@ type vmwareUploadSettings struct {
Datacenter string `json:"datacenter"`
Cluster string `json:"cluster"`
Datastore string `json:"datastore"`
Folder string `json:"folder"`
}
func (vmwareUploadSettings) isUploadSettings() {}
@ -340,6 +341,7 @@ func uploadRequestToTarget(u uploadRequest, imageType distro.ImageType) *target.
Cluster: options.Cluster,
Datacenter: options.Datacenter,
Datastore: options.Datastore,
Folder: options.Folder,
}
case *ociUploadSettings:
t.Name = target.TargetNameOCI