Skip to content

Commit

Permalink
Merge branch 'main' into feat/hashicorp#1967
Browse files Browse the repository at this point in the history
  • Loading branch information
zxinyu08 authored Nov 10, 2023
2 parents b83712b + c644fcd commit c7e2fc4
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/acceptance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
echo "VSPHERE_REST_SESSION_PATH=$(pwd)/rest_sessions" >> $GITHUB_ENV
- name: Set up Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_wrapper: false

Expand All @@ -63,7 +63,7 @@ jobs:
go-version-file: '.go-version'

- name: Set up Node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
with:
node-version: 18

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/issue_greeting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
vars: |
author: ${{ github.actor }}
- name: Create Comment
uses: peter-evans/create-or-update-comment@c6c9a1a66007646a28c153e2a8580a5bad27bcfa # v3.0.2
uses: peter-evans/create-or-update-comment@23ff15729ef2fc348714a3bb66d2f655ca9066f2 # v3.1.0
with:
issue-number: '${{ github.event.issue.number }}'
body: '${{ steps.template.outputs.result }}'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

## 2.6.0 (Unreleased)

BUG FIXES:

* `resource/virtual_machine`: Resolves upload error when deploying an OVF/OVA directly to an ESXi host. ([#1813](https://github.com/terraform-providers/terraform-provider-vsphere/pull/1813))

FEATURES:

* `resource/compute_cluster`: Adds support for vSAN Express Storage Architecture in vSphere 8.0. ([#1874](https://github.com/terraform-providers/terraform-provider-vsphere/pull/1874))
* `resource/compute_cluster`: Adds support for vSAN stretched clusters. ([#1885](https://github.com/hashicorp/terraform-provider-vsphere/pull/1885/))

## 2.5.1 (October 12, 2023)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ require (
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/grpc v1.57.1 h1:upNTNqv0ES+2ZOOqACwVtS3Il8M12/+Hz41RCPzAjQg=
google.golang.org/grpc v1.57.1/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand Down
49 changes: 25 additions & 24 deletions vsphere/internal/helper/ovfdeploy/ovf_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (pr *ProgressReader) Read(p []byte) (n int, err error) {
return
}

func DeployOvfAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecResult, resourcePoolObj *object.ResourcePool,
func DeployOvfAndGetResult(client *govmomi.Client, ovfCreateImportSpecResult *types.OvfCreateImportSpecResult, resourcePoolObj *object.ResourcePool,
folder *object.Folder, host *object.HostSystem, filePath string, deployOva bool, fromLocal bool, allowUnverifiedSSL bool) error {

var currBytesRead int64
Expand Down Expand Up @@ -100,15 +100,15 @@ func DeployOvfAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecR
}
if !deployOva {
if fromLocal {
err = uploadDisksFromLocal(filePath, ovfFileItem, deviceObj, &currBytesRead)
err = uploadDisksFromLocal(client, filePath, ovfFileItem, deviceObj, &currBytesRead)
} else {
err = uploadDisksFromURL(filePath, ovfFileItem, deviceObj, &currBytesRead, allowUnverifiedSSL)
err = uploadDisksFromURL(client, filePath, ovfFileItem, deviceObj, &currBytesRead, allowUnverifiedSSL)
}
} else {
if fromLocal {
err = uploadOvaDisksFromLocal(filePath, ovfFileItem, deviceObj, &currBytesRead)
err = uploadOvaDisksFromLocal(client, filePath, ovfFileItem, deviceObj, &currBytesRead)
} else {
err = uploadOvaDisksFromURL(filePath, ovfFileItem, deviceObj, &currBytesRead, allowUnverifiedSSL)
err = uploadOvaDisksFromURL(client, filePath, ovfFileItem, deviceObj, &currBytesRead, allowUnverifiedSSL)
}
}
if err != nil {
Expand All @@ -125,12 +125,13 @@ func DeployOvfAndGetResult(ovfCreateImportSpecResult *types.OvfCreateImportSpecR
return nfcLease.Complete(context.Background())
}

func upload(ctx context.Context, item types.OvfFileItem, f io.Reader, url string, size int64, totalBytesRead *int64) error {
u, err := soap.ParseURL(url)
func upload(ctx context.Context, client *govmomi.Client, item types.OvfFileItem, f io.Reader, rawUrl string, size int64, totalBytesRead *int64) error {
u, err := client.Client.ParseURL(rawUrl)
if err != nil {
return err
}
c := soap.NewClient(u, true)
url := u.String()
c := client.Client.Client

param := soap.Upload{
ContentLength: size,
Expand Down Expand Up @@ -185,7 +186,7 @@ func upload(ctx context.Context, item types.OvfFileItem, f io.Reader, url string
return err
}

func uploadDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
func uploadDisksFromLocal(client *govmomi.Client, filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
absoluteFilePath := ""
if strings.Contains(filePath, string(os.PathSeparator)) {
absoluteFilePath = string(filePath[0 : strings.LastIndex(filePath, string(os.PathSeparator))+1])
Expand All @@ -196,7 +197,7 @@ func uploadDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, device
if err != nil {
return err
}
err = upload(context.Background(), ovfFileItem, file, deviceObj.Url, ovfFileItem.Size, currBytesRead)
err = upload(context.Background(), client, ovfFileItem, file, deviceObj.Url, ovfFileItem.Size, currBytesRead)
if err != nil {
return fmt.Errorf("error while uploading the file %s %s", vmdkFilePath, err)
}
Expand All @@ -207,27 +208,27 @@ func uploadDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, device
return nil
}

func uploadDisksFromURL(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64,
func uploadDisksFromURL(client *govmomi.Client, filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64,
allowUnverifiedSSL bool) error {
absoluteFilePath := ""
if strings.Contains(filePath, "/") {
absoluteFilePath = string(filePath[0 : strings.LastIndex(filePath, "/")+1])
}
vmdkFilePath := absoluteFilePath + ovfFileItem.Path
client := getClient(allowUnverifiedSSL)
resp, err := client.Get(vmdkFilePath)
httpClient := getClient(allowUnverifiedSSL)
resp, err := httpClient.Get(vmdkFilePath)
log.Print(" [DEBUG] Absolute vmdk path: " + vmdkFilePath)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
err = upload(context.Background(), ovfFileItem, resp.Body, deviceObj.Url, ovfFileItem.Size, currBytesRead)
err = upload(context.Background(), client, ovfFileItem, resp.Body, deviceObj.Url, ovfFileItem.Size, currBytesRead)
return err
}

func uploadOvaDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
func uploadOvaDisksFromLocal(client *govmomi.Client, filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
diskName := ovfFileItem.Path
ovaFile, err := os.Open(filePath)
if err != nil {
Expand All @@ -237,23 +238,23 @@ func uploadOvaDisksFromLocal(filePath string, ovfFileItem types.OvfFileItem, dev
_ = ovaFile.Close()
}(ovaFile)

err = findAndUploadDiskFromOva(ovaFile, diskName, ovfFileItem, deviceObj, currBytesRead)
err = findAndUploadDiskFromOva(client, ovaFile, diskName, ovfFileItem, deviceObj, currBytesRead)
return err
}

func uploadOvaDisksFromURL(filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64,
func uploadOvaDisksFromURL(client *govmomi.Client, filePath string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64,
allowUnverifiedSSL bool) error {
diskName := ovfFileItem.Path
client := getClient(allowUnverifiedSSL)
resp, err := client.Get(filePath)
httpClient := getClient(allowUnverifiedSSL)
resp, err := httpClient.Get(filePath)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode == http.StatusOK {
err = findAndUploadDiskFromOva(resp.Body, diskName, ovfFileItem, deviceObj, currBytesRead)
err = findAndUploadDiskFromOva(client, resp.Body, diskName, ovfFileItem, deviceObj, currBytesRead)
if err != nil {
return err
}
Expand Down Expand Up @@ -345,7 +346,7 @@ func getOvfDescriptorFromOva(ovaFile io.Reader) (string, error) {
return "", fmt.Errorf("ovf file not found inside the ova")
}

func findAndUploadDiskFromOva(ovaFile io.Reader, diskName string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
func findAndUploadDiskFromOva(client *govmomi.Client, ovaFile io.Reader, diskName string, ovfFileItem types.OvfFileItem, deviceObj types.HttpNfcLeaseDeviceUrl, currBytesRead *int64) error {
ovaReader := tar.NewReader(ovaFile)
for {
fileHdr, err := ovaReader.Next()
Expand All @@ -356,7 +357,7 @@ func findAndUploadDiskFromOva(ovaFile io.Reader, diskName string, ovfFileItem ty
return err
}
if fileHdr.Name == diskName {
err = upload(context.Background(), ovfFileItem, ovaReader, deviceObj.Url, ovfFileItem.Size, currBytesRead)
err = upload(context.Background(), client, ovfFileItem, ovaReader, deviceObj.Url, ovfFileItem.Size, currBytesRead)
if err != nil {
return fmt.Errorf("error while uploading the file %s %s", diskName, err)
}
Expand Down Expand Up @@ -557,7 +558,7 @@ func (o *OvfHelper) GetImportSpec(client *govmomi.Client) (*types.OvfCreateImpor
return is, nil
}

func (o *OvfHelper) DeployOvf(spec *types.OvfCreateImportSpecResult) error {
return DeployOvfAndGetResult(spec, o.ResourcePool, o.Folder, o.HostSystem,
func (o *OvfHelper) DeployOvf(client *govmomi.Client, spec *types.OvfCreateImportSpecResult) error {
return DeployOvfAndGetResult(client, spec, o.ResourcePool, o.Folder, o.HostSystem,
o.FilePath, o.DeployOva, o.IsLocal, o.AllowUnverifiedSSL)
}
49 changes: 49 additions & 0 deletions vsphere/internal/helper/vsanclient/vsan_client_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"context"

"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/provider"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
vimtypes "github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vsan"
"github.com/vmware/govmomi/vsan/methods"
vsantypes "github.com/vmware/govmomi/vsan/types"
)

Expand All @@ -30,3 +33,49 @@ func GetVsanConfig(vsanClient *vsan.Client, cluster vimtypes.ManagedObjectRefere

return vsanConfig, err
}

func ConvertToStretchedCluster(vsanClient *vsan.Client, client *govmomi.Client, req vsantypes.VSANVcConvertToStretchedCluster) error {
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()

res, err := methods.VSANVcConvertToStretchedCluster(ctx, vsanClient, &req)

if err != nil {
return err
}

task := object.NewTask(client.Client, res.Returnval)
return task.Wait(ctx)
}

// removing the witness host automatically disables stretched cluster.
func RemoveWitnessHost(vsanClient *vsan.Client, client *govmomi.Client, req vsantypes.VSANVcRemoveWitnessHost) error {
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()

res, err := methods.VSANVcRemoveWitnessHost(ctx, vsanClient, &req)

if err != nil {
return err
}

task := object.NewTask(client.Client, res.Returnval)
return task.Wait(ctx)
}

func GetWitnessHosts(vsanClient *vsan.Client, cluster vimtypes.ManagedObjectReference) (*vsantypes.VSANVcGetWitnessHostsResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()

req := vsantypes.VSANVcGetWitnessHosts{
This: vsan.VsanVcStretchedClusterSystem,
Cluster: cluster.Reference(),
}

res, err := methods.VSANVcGetWitnessHosts(ctx, vsanClient, &req)
if err != nil {
return nil, err
}

return res, err
}
Loading

0 comments on commit c7e2fc4

Please sign in to comment.