Skip to content

Commit 43f01ae

Browse files
committed
review: code fixed
- 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
1 parent c89bb13 commit 43f01ae

File tree

20 files changed

+431
-432
lines changed

20 files changed

+431
-432
lines changed

.web-docs/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ packer plugins install github.com/hashicorp/vsphere
5151

5252
#### Data Sources
5353

54-
- [vsphere-virtual_machine](/packer/integrations/hashicorp/vsphere/latest/components/data-source/vsphere-virtual_machine) -
55-
This datasource returns name of existing virtual machine that matches all defined filters to use
56-
it as a builder source for `vsphere-clone`.
54+
- [vsphere-virtualmachine](/packer/integrations/hashicorp/vsphere/latest/components/data-source/vsphere-virtualmachine) -
55+
This data source returns the name of a virtual machine that matches all defined filters.
5756

5857
#### Post-Processors
5958

Original file line numberDiff line numberDiff line change
@@ -1,74 +1,71 @@
1-
Type: `vsphere-virtual_machine`
2-
Artifact BuilderId: `vsphere.virtual_machine`
1+
Type: `vsphere-virtualmachine`
2+
Artifact BuilderId: `vsphere.virtualmachine`
33

4-
This datasource is able to get information about existing virtual machines from vSphere
4+
This data source retrieves information about existing virtual machines from vSphere
55
and return name of one virtual machine that matches all specified filters. This virtual
6-
machine can later be used in the vSphere Clone builder to select template.
6+
machine can be used in the vSphere Clone builder to select a template.
77

88
## Configuration Reference
99

1010
### Filters Configuration
1111

1212
**Optional:**
1313

14-
<!-- Code generated from the comments of the Config struct in datasource/virtual_machine/data.go; DO NOT EDIT MANUALLY -->
14+
<!-- Code generated from the comments of the Config struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->
1515

1616
- `name` (string) - Basic filter with glob support (e.g. `nginx_basic*`). Defaults to `*`.
1717
Using strict globs will not reduce execution time because vSphere API returns the full inventory.
1818
But can be used for better readability over regular expressions.
1919

2020
- `name_regex` (string) - Extended name filter with regular expressions support (e.g. `nginx[-_]basic[0-9]*`). Default is empty.
2121
The match of the regular expression is checked by substring. Use `^` and `$` to define a full string.
22-
E.g. the `^[^_]+$` filter will search names without any underscores.
22+
For example, the `^[^_]+$` filter will search names without any underscores.
2323
The expression must use [Go Regex Syntax](https://pkg.go.dev/regexp/syntax).
2424

2525
- `template` (bool) - Filter to return only objects that are virtual machine templates.
26-
Defaults to `false` and returns all VMs.
26+
Defaults to `false` and returns all virtual machines.
2727

28-
- `node` (string) - Filter to search virtual machines only on the specified node.
28+
- `host` (string) - Filter to search virtual machines only on the specified ESX host.
2929

30-
- `vm_tags` ([]Tag) - Filter to return only that virtual machines that have attached all specifies tags.
31-
Specify one or more `vm_tags` blocks to define list of tags that will make up the filter.
32-
Should work since vCenter 6.7. To avoid incompatibility, REST client is being
33-
initialized only when at least one tag has been defined in the config.
30+
- `tags` ([]Tag) - Filter to return only that virtual machines that have attached all specifies tags.
31+
Specify one or more `tags` blocks to define list of tags for the filter.
3432

3533
- `latest` (bool) - This filter determines how to handle multiple machines that were matched with all
3634
previous filters. Machine creation time is being used to find latest.
3735
By default, multiple matching machines results in an error.
3836

39-
<!-- End of code generated from the comments of the Config struct in datasource/virtual_machine/data.go; -->
37+
<!-- End of code generated from the comments of the Config struct in datasource/virtualmachine/data.go; -->
4038

4139

4240
### Tags Filter Configuration
4341

44-
<!-- Code generated from the comments of the Tag struct in datasource/virtual_machine/data.go; DO NOT EDIT MANUALLY -->
42+
<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->
4543

46-
Example of multiple vm_tags blocks in HCL format:
47-
```
44+
HCL Example:
4845

46+
```hcl
4947
vm_tags {
5048
category = "team"
5149
name = "operations"
5250
}
5351
vm_tags {
54-
category = "SLA"
52+
category = "sla"
5553
name = "gold"
5654
}
57-
5855
```
5956

60-
<!-- End of code generated from the comments of the Tag struct in datasource/virtual_machine/data.go; -->
57+
<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; -->
6158

6259

6360
**Required:**
6461

65-
<!-- Code generated from the comments of the Tag struct in datasource/virtual_machine/data.go; DO NOT EDIT MANUALLY -->
62+
<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->
6663

6764
- `name` (string) - Tag with this name must be attached to virtual machine which should pass the Tags Filter.
6865

6966
- `category` (string) - Name of the category that contains this tag. Both tag and category must be specified.
7067

71-
<!-- End of code generated from the comments of the Tag struct in datasource/virtual_machine/data.go; -->
68+
<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; -->
7269

7370

7471
### Connection Configuration
@@ -100,48 +97,47 @@ Example of multiple vm_tags blocks in HCL format:
10097

10198
## Output
10299

103-
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/virtual_machine/data.go; DO NOT EDIT MANUALLY -->
100+
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->
104101

105102
- `vm_name` (string) - Name of the found virtual machine.
106103

107-
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/virtual_machine/data.go; -->
104+
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; -->
108105

109106

110107
## Example Usage
111108

112-
This is a very basic example that connects to vSphere cluster and tries to search
113-
the latest virtual machine that matches all filters. The machine name is then printed
114-
to console as output variable.
109+
This example demonstrates how to connect to vSphere cluster and search for the latest virtual machine
110+
that matches the filters. The name of the machine is then output to the console as an output variable.
115111
```hcl
116-
data "vsphere-virtual_machine" "default" {
117-
vcenter_server = "vcenter.example.org"
112+
data "vsphere-virtualmachine" "default" {
113+
vcenter_server = "vcenter.example.com"
118114
insecure_connection = true
119-
username = "administrator@example.org"
120-
password = "St4ongPa$$w0rd"
121-
datacenter = "AZ1"
115+
username = "administrator@vsphere.local"
116+
password = "VMware1!"
117+
datacenter = "dc-01"
122118
latest = true
123119
vm_tags {
124120
category = "team"
125121
name = "operations"
126122
}
127123
vm_tags {
128-
category = "SLA"
124+
category = "sla"
129125
name = "gold"
130126
}
131127
132128
}
133129
134130
locals {
135-
vm_name = data.vsphere-virtual_machine.default.vm_name
131+
vm_name = data.vsphere-virtualmachine.default.vm_name
136132
}
137133
138-
source "null" "basic-example" {
134+
source "null" "example" {
139135
communicator = "none"
140136
}
141137
142138
build {
143139
sources = [
144-
"source.null.basic-example"
140+
"source.null.example"
145141
]
146142
147143
provisioner "shell-local" {
@@ -150,6 +146,4 @@ build {
150146
]
151147
}
152148
}
153-
154-
155149
```

.web-docs/metadata.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ integration {
3636
component {
3737
type = "data-source"
3838
name = "vSphere Virtual Machine"
39-
slug = "vsphere-virtual_machine"
39+
slug = "vsphere-virtualmachine"
4040
}
4141
}

datasource/common/driver/driver.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package driver
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"net/url"
10+
11+
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common"
12+
"github.com/vmware/govmomi"
13+
"github.com/vmware/govmomi/find"
14+
"github.com/vmware/govmomi/object"
15+
"github.com/vmware/govmomi/vapi/rest"
16+
)
17+
18+
type VCenterDriver struct {
19+
Ctx context.Context
20+
Client *govmomi.Client
21+
RestClient *rest.Client
22+
Finder *find.Finder
23+
Datacenter *object.Datacenter
24+
}
25+
26+
func NewDriver(config common.ConnectConfig) (*VCenterDriver, error) {
27+
ctx := context.Background()
28+
29+
vcenterUrl, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
30+
if err != nil {
31+
return nil, fmt.Errorf("failed to parse URL: %w", err)
32+
}
33+
vcenterUrl.User = url.UserPassword(config.Username, config.Password)
34+
35+
client, err := govmomi.NewClient(ctx, vcenterUrl, true)
36+
if err != nil {
37+
return nil, fmt.Errorf("failed to create govmomi Client: %w", err)
38+
}
39+
40+
restClient := rest.NewClient(client.Client)
41+
err = restClient.Login(ctx, vcenterUrl.User)
42+
if err != nil {
43+
return nil, fmt.Errorf("failed to login to REST API endpoint: %w", err)
44+
}
45+
46+
finder := find.NewFinder(client.Client, true)
47+
datacenter, err := finder.DatacenterOrDefault(ctx, config.Datacenter)
48+
if err != nil {
49+
return nil, fmt.Errorf("failed to find datacenter: %w", err)
50+
}
51+
finder.SetDatacenter(datacenter)
52+
53+
return &VCenterDriver{
54+
Ctx: ctx,
55+
Client: client,
56+
RestClient: restClient,
57+
Finder: finder,
58+
Datacenter: datacenter,
59+
}, nil
60+
}
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package testing
25

36
import (
47
"context"
8+
"fmt"
59

6-
"github.com/pkg/errors"
710
"github.com/vmware/govmomi/object"
811
"github.com/vmware/govmomi/vapi/tags"
912
)
1013

14+
// MarkSimulatedVmAsTemplate powers off the virtual machine before converting it to a template (because the simulator
15+
// creates all virtual machines in an online state).
1116
func MarkSimulatedVmAsTemplate(ctx context.Context, vm *object.VirtualMachine) error {
1217
task, err := vm.PowerOff(ctx)
1318
if err != nil {
14-
return errors.Wrap(err, "failed to issue powering off command to the machine")
19+
return fmt.Errorf("failed to issue powering off command to the machine: %w", err)
1520
}
1621
err = task.Wait(ctx)
1722
if err != nil {
18-
return errors.Wrap(err, "failed to power off the machine")
23+
return fmt.Errorf("failed to power off the machine: %w", err)
1924
}
2025
err = vm.MarkAsTemplate(ctx)
2126
if err != nil {
22-
return errors.Wrap(err, "failed to mark VM as a template")
27+
return fmt.Errorf("failed to mark virtual machine as a template: %w", err)
2328
}
2429
return nil
2530
}
2631

27-
// Try to find category passed by name, create category if not found and return category ID.
32+
// FindOrCreateCategory tries to find category passed by name, creates category if not found and returns category ID.
2833
// Category will be created with "MULTIPLE" constraint.
2934
func FindOrCreateCategory(ctx context.Context, man *tags.Manager, catName string) (string, error) {
3035
categoryList, err := man.GetCategories(ctx)
3136
if err != nil {
32-
return "", errors.Wrap(err, "cannot get categories from cluster")
37+
return "", fmt.Errorf("cannot return categories from cluster: %w", err)
3338
}
3439
for _, category := range categoryList {
3540
if category.Name == catName {
@@ -38,16 +43,16 @@ func FindOrCreateCategory(ctx context.Context, man *tags.Manager, catName string
3843
}
3944
newCategoryID, err := man.CreateCategory(ctx, &tags.Category{Name: catName, Cardinality: "MULTIPLE"})
4045
if err != nil {
41-
return "", errors.Wrap(err, "cannot create category")
46+
return "", fmt.Errorf("cannot create category: %w", err)
4247
}
4348
return newCategoryID, nil
4449
}
4550

46-
// Try to find the tagName in category with catID, create if not found and return tag ID.
51+
// FindOrCreateTag tries to find the tagName in category with catID, creates if not found and returns tag ID.
4752
func FindOrCreateTag(ctx context.Context, man *tags.Manager, catID string, tagName string) (string, error) {
4853
tagsInCategory, err := man.GetTagsForCategory(ctx, catID)
4954
if err != nil {
50-
return "", errors.Wrap(err, "cannot get tags for category")
55+
return "", fmt.Errorf("cannot return tags for category: %w", err)
5156
}
5257
for _, tag := range tagsInCategory {
5358
if tag.Name == tagName {
@@ -56,7 +61,7 @@ func FindOrCreateTag(ctx context.Context, man *tags.Manager, catID string, tagNa
5661
}
5762
newTagID, err := man.CreateTag(ctx, &tags.Tag{Name: tagName, CategoryID: catID})
5863
if err != nil {
59-
return "", errors.Wrap(err, "cannot create tag")
64+
return "", fmt.Errorf("cannot create tag: %w", err)
6065
}
6166
return newTagID, nil
6267
}

0 commit comments

Comments
 (0)