Skip to content

Commit

Permalink
Region, Supervisor and Supervisor Zones (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Didainius authored Nov 7, 2024
1 parent e6eba21 commit 9df0deb
Show file tree
Hide file tree
Showing 25 changed files with 996 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .changes/v4.0.0/1343-features.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* **New Resource:** `vcd_vcenter` to manage vCenter servers [GH-1343]
* **New Resource:** `vcd_vcenter` to manage vCenter servers [GH-1343, GH-1348]
* **New Resource:** `vcd_nsxt_manager` to manage NSX-T Managers [GH-1343]
File renamed without changes.
4 changes: 4 additions & 0 deletions .changes/v4.0.0/1348-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* **New Resource:** `vcd_tm_region` to manage Regions [GH-1348]
* **New Data Source:** `vcd_tm_region` to read Regions [GH-1348]
* **New Data Source:** `vcd_tm_supervisor` to read Supervisors [GH-1348]
* **New Data Source:** `vcd_tm_supervisor_zone` to read Supervisor Zones [GH-1348]
3 changes: 3 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ testextnetwork: fmtcheck
testtm: fmtcheck
@sh -c "'$(CURDIR)/scripts/runtest.sh' tm"

testtm-binary-prepare: install
cd vcd && go test -tags tm -vcd-add-provider -vcd-short -v .

# vets all .go files
vet:
@echo "go vet ."
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/kr/pretty v0.3.1
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.4
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.5
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,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/go-vcloud-director/v3 v3.0.0-alpha.4 h1:1BGcNaKnXZlNBgK7hUTQPU/F0k5DncP5757NtvadtAw=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.4/go.mod h1:68KHsVns52dsq/w5JQYzauaU/+NAi1FmCxhBrFc/VoQ=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.5 h1:loocPkclRMoVpiCSBWjs7BSbxj453NzqV+qoZwZvedQ=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.5/go.mod h1:68KHsVns52dsq/w5JQYzauaU/+NAi1FmCxhBrFc/VoQ=
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
11 changes: 7 additions & 4 deletions vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ type TestConfig struct {
NsxtManagerPassword string `json:"nsxtManagerPassword"`
NsxtManagerUrl string `json:"nsxtManagerUrl"`

CreateVcenter bool `json:"createVcenter"`
VcenterUsername string `json:"vcenterUsername"`
VcenterPassword string `json:"vcenterPassword"`
VcenterUrl string `json:"vcenterUrl"`
CreateVcenter bool `json:"createVcenter"`
VcenterUsername string `json:"vcenterUsername"`
VcenterPassword string `json:"vcenterPassword"`
VcenterUrl string `json:"vcenterUrl"`
VcenterStorageProfile string `json:"vcenterStorageProfile"`
VcenterSupervisor string `json:"vcenterSupervisor"`
VcenterSupervisorZone string `json:"vcenterSupervisorZone"`
} `json:"tm,omitempty"`
VCD struct {
Org string `json:"org"`
Expand Down
91 changes: 91 additions & 0 deletions vcd/datasource_vcd_tm_region.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package vcd

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v3/govcd"
"github.com/vmware/go-vcloud-director/v3/types/v56"
)

func datasourceVcdTmRegion() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdTmRegionRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: fmt.Sprintf("%s name", labelTmRegion),
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("%s description", labelTmRegion),
},
"is_enabled": {
Type: schema.TypeBool,
Computed: true,
Description: fmt.Sprintf("Defines whether the %s is enabled or not", labelTmRegion),
},
"nsx_manager_id": {
Type: schema.TypeString,
Computed: true,
Description: "NSX Manager ID",
},
"cpu_capacity_mhz": {
Type: schema.TypeInt,
Computed: true,
Description: "CPU Capacity in MHz",
},
"cpu_reservation_capacity_mhz": {
Type: schema.TypeInt,
Computed: true,
Description: "CPU reservation in MHz",
},
"memory_capacity_mib": {
Type: schema.TypeInt,
Computed: true,
Description: "Memory capacity in MiB",
},
"memory_reservation_capacity_mib": {
Type: schema.TypeInt,
Computed: true,
Description: "Memory reservation in MiB",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Status of the %s", labelTmRegion),
},
"supervisor_ids": {
Type: schema.TypeSet,
Computed: true,
Description: fmt.Sprintf("A set of supervisor IDs used in this %s", labelTmRegion),
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"storage_policy_names": {
Type: schema.TypeSet,
Computed: true,
Description: "A set of storage policies",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func datasourceVcdTmRegionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
c := crudConfig[*govcd.Region, types.Region]{
entityLabel: labelTmRegion,
getEntityFunc: vcdClient.GetRegionByName,
stateStoreFunc: setRegionData,
}
return readDatasource(ctx, d, meta, c)
}
68 changes: 68 additions & 0 deletions vcd/datasource_vcd_tm_supervisor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package vcd

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v3/types/v56"
)

func datasourceVcdTmSupervisor() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdTmSupervisorRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the Supervisor",
},
"vcenter_id": {
Type: schema.TypeString,
Required: true,
Description: "Parent vCenter ID",
},
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: "Parent Region ID",
},
},
}
}

func datasourceVcdTmSupervisorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)

s, err := vcdClient.GetSupervisorByNameAndVcenterId(d.Get("name").(string), d.Get("vcenter_id").(string))
if err != nil {
return diag.Errorf("error getting Supervisor: %s", err)
}

err = setSupervisorData(d, s.Supervisor)
if err != nil {
return diag.Errorf("error storing Supervisor data: %s", err)
}

d.SetId(s.Supervisor.SupervisorID)

return nil
}

func setSupervisorData(d *schema.ResourceData, s *types.Supervisor) error {
vCenterId := ""
if s.VirtualCenter != nil {
vCenterId = s.VirtualCenter.ID
}
dSet(d, "vcenter_id", vCenterId)

regionId := ""
if s.Region != nil {
regionId = s.Region.ID
}

dSet(d, "region_id", regionId)

return nil
}
113 changes: 113 additions & 0 deletions vcd/datasource_vcd_tm_supervisor_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package vcd

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v3/govcd"
)

const labelSupervisorZone = "Supervisor Zone"

func datasourceVcdTmSupervisorZone() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdTmSupervisorZoneRead,

Schema: map[string]*schema.Schema{
"supervisor_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the Supervisor",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: fmt.Sprintf("Name of the %s", labelSupervisorZone),
},
"vcenter_id": {
Type: schema.TypeString,
Computed: true,
Description: "Parent vCenter ID",
},
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: "Parent Region ID",
},
"cpu_capacity_mhz": {
Type: schema.TypeInt,
Computed: true,
Description: "CPU Capacity in MHz",
},
"cpu_used_mhz": {
Type: schema.TypeInt,
Computed: true,
Description: "CPU used in MHz",
},
"memory_capacity_mib": {
Type: schema.TypeInt,
Computed: true,
Description: "Memory capacity in MiB",
},
"memory_used_mib": {
Type: schema.TypeInt,
Computed: true,
Description: "Memory used in MiB",
},
},
}
}

func datasourceVcdTmSupervisorZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)

s, err := vcdClient.GetSupervisorById(d.Get("supervisor_id").(string))
if err != nil {
return diag.Errorf("error getting Supervisor: %s", err)
}

sz, err := s.GetSupervisorZoneByName(d.Get("name").(string))
if err != nil {
return diag.Errorf("error getting Supervisor Zone '%s': %s", d.Get("name").(string), err)
}

err = setSupervisorZoneData(d, sz)
if err != nil {
return diag.Errorf("error storing Supervisor data: %s", err)
}

d.SetId(sz.SupervisorZone.ID)

return nil
}

func setSupervisorZoneData(d *schema.ResourceData, s *govcd.SupervisorZone) error {
if s == nil {
return fmt.Errorf("error")
}
vCenterId := ""
if s.SupervisorZone.VirtualCenter != nil {
vCenterId = s.SupervisorZone.VirtualCenter.ID
}
dSet(d, "vcenter_id", vCenterId)
dSet(d, "name", s.SupervisorZone.Name)
supervisorId := ""
if s.SupervisorZone.Supervisor != nil {
supervisorId = s.SupervisorZone.Supervisor.ID
}
dSet(d, "supervisor_id", supervisorId)

regionId := ""
if s.SupervisorZone.Region != nil {
regionId = s.SupervisorZone.Region.ID
}
dSet(d, "region_id", regionId)
dSet(d, "cpu_capacity_mhz", s.SupervisorZone.TotalCPUCapacityMHz)
dSet(d, "cpu_used_mhz", s.SupervisorZone.CpuUsedMHz)
dSet(d, "memory_capacity_mib", s.SupervisorZone.TotalMemoryCapacityMiB)
dSet(d, "memory_used_mib", s.SupervisorZone.MemoryUsedMiB)

return nil
}
4 changes: 4 additions & 0 deletions vcd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ var globalDataSourceMap = map[string]*schema.Resource{
"vcd_tm_org": datasourceVcdTmOrg(), // 4.0
"vcd_tm_region_storage_policy": datasourceVcdTmRegionStoragePolicy(), // 4.0
"vcd_tm_content_library": datasourceVcdTmContentLibrary(), // 4.0
"vcd_tm_supervisor": datasourceVcdTmSupervisor(), // 4.0
"vcd_tm_supervisor_zone": datasourceVcdTmSupervisorZone(), // 4.0
"vcd_tm_region": datasourceVcdTmRegion(), // 4.0
}

var globalResourceMap = map[string]*schema.Resource{
Expand Down Expand Up @@ -308,6 +311,7 @@ var globalResourceMap = map[string]*schema.Resource{
"vcd_nsxt_manager": resourceVcdNsxtManager(), // 4.0
"vcd_vcenter": resourceVcdVcenter(), // 4.0
"vcd_tm_org": resourceVcdTmOrg(), // 4.0
"vcd_tm_region": resourceVcdTmRegion(), // 4.0
}

// Provider returns a terraform.ResourceProvider.
Expand Down
12 changes: 8 additions & 4 deletions vcd/resource_generic_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type crudConfig[O updateDeleter[O, I], I any] struct {
entityLabel string

// getTypeFunc is responsible for converting schema fields to inner type
getTypeFunc func(d *schema.ResourceData) (*I, error)
getTypeFunc func(*VCDClient, *schema.ResourceData) (*I, error)
// stateStoreFunc is responsible for storing state
stateStoreFunc func(d *schema.ResourceData, outerType O) error

Expand Down Expand Up @@ -63,12 +63,13 @@ type schemaHook func(*VCDClient, *schema.ResourceData) error
type outerEntityHookInnerEntityType[O, I any] func(*schema.ResourceData, O, I) error

func createResource[O updateDeleter[O, I], I any](ctx context.Context, d *schema.ResourceData, meta interface{}, c crudConfig[O, I]) diag.Diagnostics {
t, err := c.getTypeFunc(d)
vcdClient := meta.(*VCDClient)

t, err := c.getTypeFunc(vcdClient, d)
if err != nil {
return diag.Errorf("error getting %s type on create: %s", c.entityLabel, err)
}

vcdClient := meta.(*VCDClient)
err = execSchemaHook(vcdClient, d, c.preCreateHooks)
if err != nil {
return diag.Errorf("error executing pre-create %s hooks: %s", c.entityLabel, err)
Expand All @@ -88,7 +89,8 @@ func createResource[O updateDeleter[O, I], I any](ctx context.Context, d *schema
}

func updateResource[O updateDeleter[O, I], I any](ctx context.Context, d *schema.ResourceData, meta interface{}, c crudConfig[O, I]) diag.Diagnostics {
t, err := c.getTypeFunc(d)
vcdClient := meta.(*VCDClient)
t, err := c.getTypeFunc(vcdClient, d)
if err != nil {
return diag.Errorf("error getting %s type on update: %s", c.entityLabel, err)
}
Expand Down Expand Up @@ -116,6 +118,8 @@ func readResource[O updateDeleter[O, I], I any](_ context.Context, d *schema.Res
if err != nil {
if govcd.ContainsNotFound(err) {
util.Logger.Printf("[DEBUG] entity '%s' with ID '%s' not found. Removing from state", c.entityLabel, d.Id())
d.SetId("")
return nil
}
return diag.Errorf("error getting %s: %s", c.entityLabel, err)
}
Expand Down
Loading

0 comments on commit 9df0deb

Please sign in to comment.