Skip to content

Commit

Permalink
Merge branch 'main' into feature/pvlan-mapping-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
GCHQDeveloper609 authored Oct 22, 2024
2 parents 4d6794c + a120bd8 commit d36096e
Show file tree
Hide file tree
Showing 25 changed files with 657 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.22.6
1.22.8
184 changes: 115 additions & 69 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Learn more:
[developer.hashicorp.com][terraform-install] and
[the project][terraform-github] on GitHub.

- [Go 1.22.6][golang-install]
- [Go 1.22.8][golang-install]

Required if building the provider.

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/hashicorp/terraform-provider-vsphere

go 1.22.6
go 1.22.8

require (
github.com/davecgh/go-spew v1.1.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/mitchellh/copystructure v1.2.0
github.com/vmware/govmomi v0.42.0
github.com/vmware/govmomi v0.44.1
)

require (
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/vmware/govmomi v0.42.0 h1:MbvAlVfjNBE1mHMaQ7yOSop1KLB0/93x6VAGuCtjqtI=
github.com/vmware/govmomi v0.42.0/go.mod h1:1H5LWwsBif8HKZqbFp0FdoKTHyJE4FzL6ACequMKYQg=
github.com/vmware/govmomi v0.44.1 h1:Hbt0nvVY8fnp3DGRJHngLflTos/uRrW54lhmJ/zKZFc=
github.com/vmware/govmomi v0.44.1/go.mod h1:uoLVU9zlXC4p4GmLVG+ZJmBC0Gn3Q7mytOJvi39OhxA=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down Expand Up @@ -194,8 +194,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
33 changes: 32 additions & 1 deletion vsphere/data_source_vsphere_datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
package vsphere

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/mo"
)

func dataSourceVSphereDatacenter() *schema.Resource {
Expand All @@ -19,11 +23,18 @@ func dataSourceVSphereDatacenter() *schema.Resource {
Description: "The name of the datacenter. This can be a name or path. Can be omitted if there is only one datacenter in your inventory.",
Optional: true,
},
"virtual_machines": {
Type: schema.TypeSet,
Description: "List of all virtual machines included in the vSphere datacenter object.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.Background()
client := meta.(*Client).vimClient
datacenter := d.Get("name").(string)
dc, err := getDatacenter(client, datacenter)
Expand All @@ -32,6 +43,26 @@ func dataSourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) e
}
id := dc.Reference().Value
d.SetId(id)

finder := find.NewFinder(client.Client)
finder.SetDatacenter(dc)
viewManager := view.NewManager(client.Client)
view, err := viewManager.CreateContainerView(ctx, dc.Reference(), []string{"VirtualMachine"}, true)
if err != nil {
return fmt.Errorf("error fetching datacenter: %s", err)
}
defer view.Destroy(ctx)
var vms []mo.VirtualMachine
err = view.Retrieve(ctx, []string{"VirtualMachine"}, []string{"name"}, &vms)
if err != nil {
return fmt.Errorf("error fetching virtual machines: %s", err)
}
vmNames := []string{}
for v := range vms {
vmNames = append(vmNames, vms[v].Name)
}
err = d.Set("virtual_machines", vmNames)
if err != nil {
return fmt.Errorf("error setting virtual_machines: %s", err)
}
return nil
}
36 changes: 36 additions & 0 deletions vsphere/data_source_vsphere_datacenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ func testAccDataSourceVSphereDatacenterPreCheck(t *testing.T) {
}
}

func TestAccDataSourceVSphereDatacenter_getVirtualMachines(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
RunSweepers()
testAccPreCheck(t)
testAccDataSourceVSphereDatacenterPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDatacenterConfigGetVirtualMachines(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.vsphere_datacenter.dc",
"id",
testAccDataSourceVSphereDatacenterExpectedRegexp,
),
testCheckOutputBool("found_virtual_machines", "true"),
),
},
},
})
}

func testAccDataSourceVSphereDatacenterConfig() string {
return fmt.Sprintf(`
data "vsphere_datacenter" "dc" {
Expand All @@ -77,3 +101,15 @@ data "vsphere_datacenter" "dc" {
const testAccDataSourceVSphereDatacenterConfigDefault = `
data "vsphere_datacenter" "dc" {}
`

func testAccDataSourceVSphereDatacenterConfigGetVirtualMachines() string {
return fmt.Sprintf(`
data "vsphere_datacenter" "dc" {
name = "%s"
}
output "found_virtual_machines" {
value = "${length(data.vsphere_datacenter.dc.virtual_machines) >= 1 ? "true" : "false" }"
}
`, os.Getenv("TF_VAR_VSPHERE_DATACENTER"),
)
}
26 changes: 26 additions & 0 deletions vsphere/data_source_vsphere_datastore_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package vsphere

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datastore"
)

func dataSourceVSphereDatastoreCluster() *schema.Resource {
Expand All @@ -24,15 +26,39 @@ func dataSourceVSphereDatastoreCluster() *schema.Resource {
Optional: true,
Description: "The managed object ID of the datacenter the cluster is located in. Not required if using an absolute path.",
},
"datastores": {
Type: schema.TypeSet,
Computed: true,
Description: "The names of datastores included in the datastore cluster.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceVSphereDatastoreClusterRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.Background()
client := meta.(*Client).vimClient
pod, err := resourceVSphereDatastoreClusterGetPodFromPath(meta, d.Get("name").(string), d.Get("datacenter_id").(string))
if err != nil {
return fmt.Errorf("error loading datastore cluster: %s", err)
}
d.SetId(pod.Reference().Value)
dsNames := []string{}
childDatastores, err := pod.Children(ctx)
if err != nil {
return fmt.Errorf("error retrieving datastores in datastore cluster: %s", err)
}
for d := range childDatastores {
ds, err := datastore.FromID(client, childDatastores[d].Reference().Value)
if err != nil {
return fmt.Errorf("error retrieving datastore: %s", err)
}
dsNames = append(dsNames, ds.Name())
}
err = d.Set("datastores", dsNames)
if err != nil {
return fmt.Errorf("cannot set datastores: %s", err)
}
return nil
}
45 changes: 43 additions & 2 deletions vsphere/data_source_vsphere_datastore_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vsphere

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper"
Expand Down Expand Up @@ -55,6 +56,27 @@ func TestAccDataSourceVSphereDatastoreCluster_absolutePathNoDatacenter(t *testin
},
})
}
func TestAccDataSourceVSphereDatastoreCluster_getDatastores(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
RunSweepers()
testAccPreCheck(t)
testAccResourceVSphereDatastoreClusterPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDatastoreClusterGetDatastores(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"data.vsphere_datastore_cluster.datastore_cluster_data", "id",
"vsphere_datastore_cluster.datastore_cluster", "id",
),
),
},
},
})
}

func testAccDataSourceVSphereDatastoreClusterConfigBasic() string {
return fmt.Sprintf(`
Expand All @@ -66,8 +88,8 @@ resource "vsphere_datastore_cluster" "datastore_cluster" {
}
data "vsphere_datastore_cluster" "datastore_cluster_data" {
name = "${vsphere_datastore_cluster.datastore_cluster.name}"
datacenter_id = "${vsphere_datastore_cluster.datastore_cluster.datacenter_id}"
name = vsphere_datastore_cluster.datastore_cluster.name
datacenter_id = vsphere_datastore_cluster.datastore_cluster.datacenter_id
}
`,
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
Expand All @@ -90,3 +112,22 @@ data "vsphere_datastore_cluster" "datastore_cluster_data" {
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
)
}

func testAccDataSourceVSphereDatastoreClusterGetDatastores() string {
return fmt.Sprintf(`
resource "vsphere_datastore_cluster" "datastore_cluster" {
name = "%s"
datacenter_id = "%s"
}
data "vsphere_datastore_cluster" "datastore_cluster_data" {
name = "${vsphere_datastore_cluster.datastore_cluster.name}"
datacenter_id = "${vsphere_datastore_cluster.datastore_cluster.datacenter_id}"
}
output "found_datastores" {
value = "${length(data.vsphere_datastore_cluster.datastore_cluster_data.datastores) >= 1 ? "true" : "false" }"
}
`, os.Getenv("TF_VAR_VSPHERE_DATASTORE_CLUSTER_NAME"), os.Getenv("TF_VAR_VSPHERE_DATACENTER_ID"),
)
}
21 changes: 20 additions & 1 deletion vsphere/data_source_vsphere_datastore_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package vsphere

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datastore"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/storagepod"
"github.com/vmware/govmomi/object"
)

Expand Down Expand Up @@ -36,6 +38,7 @@ func dataSourceVSphereDatastoreStats() *schema.Resource {
}

func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.Background()
client := meta.(*Client).vimClient
var dc *object.Datacenter
if dcID, ok := d.GetOk("datacenter_id"); ok {
Expand All @@ -49,6 +52,23 @@ func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{
if err != nil {
return fmt.Errorf("error listing datastores: %s", err)
}
storagePods, err := storagepod.List(client)
if err != nil {
return fmt.Errorf("error retrieving storage pods: %s", err)
}
for s := range storagePods {
childDatastores, err := storagePods[s].Children(ctx)
if err != nil {
return fmt.Errorf("error retrieving datastores in datastore cluster: %s", err)
}
for c := range childDatastores {
ds, err := datastore.FromID(client, childDatastores[c].Reference().Value)
if err != nil {
return fmt.Errorf("error retrieving datastore: %s", err)
}
dss = append(dss, ds)
}
}
for i := range dss {
ds, err := datastore.FromPath(client, dss[i].Name(), dc)
if err != nil {
Expand All @@ -66,6 +86,5 @@ func dataSourceVSphereDatastoreStatsRead(d *schema.ResourceData, meta interface{
d.Set("free_space", fr)
}
d.SetId(fmt.Sprintf("%s_stats", dc.Reference().Value))

return nil
}
14 changes: 7 additions & 7 deletions vsphere/data_source_vsphere_datastore_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func TestAccDataSourceVSphereDatastoreStats_basic(t *testing.T) {
Config: testAccDataSourceVSphereDatastoreStatsConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.vsphere_datastore_stats.datastore_stats", "datacenter_id", os.Getenv("VSPHERE_DATACENTER"),
"data.vsphere_datastore_stats.datastore_stats", "datacenter_id", os.Getenv("TF_VAR_VSPHERE_DATACENTER"),
),
resource.TestCheckResourceAttr(
"data.vsphere_datastore_stats.datastore_stats", "id", fmt.Sprintf("%s_stats", os.Getenv("VSPHERE_DATACENTER")),
"data.vsphere_datastore_stats.datastore_stats", "id", fmt.Sprintf("%s_stats", os.Getenv("TF_VAR_VSPHERE_DATACENTER")),
),
testCheckOutputBool("found_free_space", "true"),
testCheckOutputBool("found_capacity", "true"),
Expand All @@ -40,13 +40,13 @@ func TestAccDataSourceVSphereDatastoreStats_basic(t *testing.T) {
}

func testAccDataSourceVSphereDatastoreStatsPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_DATACENTER") == "" {
if os.Getenv("TF_VAR_VSPHERE_DATACENTER") == "" {
t.Skip("set TF_VAR_VSPHERE_DATACENTER to run vsphere_datastore_stats acceptance tests")
}
if os.Getenv("VSPHERE_USER") == "" {
t.Skip("set TF_VAR_VSPHERE_DATACENTER to run vsphere_datastore_stats acceptance tests")
if os.Getenv("TF_VAR_VSPHERE_USER") == "" {
t.Skip("set TF_VAR_VSPHERE_USER to run vsphere_datastore_stats acceptance tests")
}
if os.Getenv("VSPHERE_PASSWORD") == "" {
if os.Getenv("TF_VAR_VSPHERE_PASSWORD") == "" {
t.Skip("set TF_VAR_VSPHERE_PASSWORD to run vsphere_datastore_stats acceptance tests")
}
}
Expand Down Expand Up @@ -82,5 +82,5 @@ output "capacity_values_exist" {
free >= 1
])
}
`, os.Getenv("VSPHERE_DATACENTER"))
`, os.Getenv("TF_VAR_VSPHERE_DATACENTER"))
}
1 change: 1 addition & 0 deletions vsphere/data_source_vsphere_host_thumbprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func dataSourceVSphereHostThumbprint() *schema.Resource {
"insecure": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Boolean that can be set to true to disable SSL certificate verification.",
},
},
Expand Down
Loading

0 comments on commit d36096e

Please sign in to comment.