generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Made minor changes to documentation. - Standard `errors` and `fmt` were used for custom errors. - field `Node` was renamed to `Host`. - field `VmTags` was renamed to `Tags` - package `virtual_machine` was renamed to `virtualmachine` - `driver` and `testing` were moved to `common` location
- Loading branch information
1 parent
08527f1
commit b571315
Showing
21 changed files
with
442 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package driver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common" | ||
"github.com/vmware/govmomi" | ||
"github.com/vmware/govmomi/find" | ||
"github.com/vmware/govmomi/object" | ||
"github.com/vmware/govmomi/vapi/rest" | ||
) | ||
|
||
type VCenterDriver struct { | ||
Ctx context.Context | ||
Client *govmomi.Client | ||
RestClient *rest.Client | ||
Finder *find.Finder | ||
Datacenter *object.Datacenter | ||
} | ||
|
||
func NewDriver(config common.ConnectConfig) (*VCenterDriver, error) { | ||
ctx := context.Background() | ||
|
||
vcenterUrl, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse URL: %w", err) | ||
} | ||
vcenterUrl.User = url.UserPassword(config.Username, config.Password) | ||
|
||
client, err := govmomi.NewClient(ctx, vcenterUrl, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create govmomi Client: %w", err) | ||
} | ||
|
||
restClient := rest.NewClient(client.Client) | ||
err = restClient.Login(ctx, vcenterUrl.User) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to login to REST API endpoint: %w", err) | ||
} | ||
|
||
finder := find.NewFinder(client.Client, true) | ||
datacenter, err := finder.DatacenterOrDefault(ctx, config.Datacenter) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to find datacenter: %w", err) | ||
} | ||
finder.SetDatacenter(datacenter) | ||
|
||
return &VCenterDriver{ | ||
Ctx: ctx, | ||
Client: client, | ||
RestClient: restClient, | ||
Finder: finder, | ||
Datacenter: datacenter, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.