debian-forge-composer/internal/upload/vmware/vmware.go
Sanne Raymaekers 8a8607cdf6 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.
2023-05-25 10:14:32 +02:00

65 lines
1.5 KiB
Go

package vmware
import (
"fmt"
"github.com/vmware/govmomi/govc/cli"
_ "github.com/vmware/govmomi/govc/importx"
)
type Credentials struct {
Host string
Username string
Password string
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
// uploaded image will be present in a directory of the same name
func ImportVmdk(creds Credentials, imagePath string) error {
args := []string{
"import.vmdk",
}
args = append(args, commonOptions(creds)...)
args = append(args, imagePath)
retcode := cli.Run(args)
if retcode != 0 {
return fmt.Errorf("importing %s into vSphere failed", imagePath)
}
return nil
}
func ImportOva(creds Credentials, imagePath, targetName string) error {
args := []string{
"import.ova",
fmt.Sprintf("-name=%s", targetName),
}
args = append(args, commonOptions(creds)...)
args = append(args, imagePath)
retcode := cli.Run(args)
if retcode != 0 {
return fmt.Errorf("importing %s into vSphere failed", imagePath)
}
return nil
}