Skip to content

Commit

Permalink
fix: separate vsan fault domains and stretched cluster (#2064)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxinyu08 authored Nov 17, 2023
1 parent 9c25530 commit 38e34e5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
27 changes: 27 additions & 0 deletions vsphere/internal/helper/testhelper/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,30 @@ func ConfigDSClusterData() string {
`, os.Getenv("TF_VAR_VSPHERE_DS_CLUSTER1"))
}

func ConfigDataVsanHost1() string {
return fmt.Sprintf(`
data "vsphere_host" "roothost1" {
name = "%s"
datacenter_id = data.vsphere_datacenter.rootdc1.id
}
`, os.Getenv("TF_VSPHERE_VSAN_HOST_1"))
}

func ConfigDataVsanHost2() string {
return fmt.Sprintf(`
data "vsphere_host" "roothost2" {
name = "%s"
datacenter_id = data.vsphere_datacenter.rootdc1.id
}
`, os.Getenv("TF_VSPHERE_VSAN_HOST_2"))
}

func ConfigDataVsanWitnessHost() string {
return fmt.Sprintf(`
data "vsphere_host" "roothost3" {
name = "%s"
datacenter_id = data.vsphere_datacenter.rootdc1.id
}
`, os.Getenv("TF_VSPHERE_VSAN_WITNESS_HOST"))
}
30 changes: 18 additions & 12 deletions vsphere/resource_vsphere_compute_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,10 @@ func resourceVSphereComputeCluster() *schema.Resource {
},
},
"vsan_fault_domains": {
Type: schema.TypeSet,
Optional: true,
Description: "The configuration for vSAN fault domains.",
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"vsan_stretched_cluster"},
Description: "The configuration for vSAN fault domains.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fault_domain": {
Expand All @@ -615,10 +616,11 @@ func resourceVSphereComputeCluster() *schema.Resource {
},
},
"vsan_stretched_cluster": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "The configuration for stretched cluster.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"vsan_fault_domains"},
Description: "The configuration for stretched cluster.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"preferred_fault_domain_host_ids": {
Expand Down Expand Up @@ -1500,9 +1502,11 @@ func expandClusterConfigSpecEx(d *schema.ResourceData, version viapi.VSphereVers
DrsConfig: expandClusterDrsConfigInfo(d, version),
}

obj.VsanHostConfigSpec, err = expandVsanHostConfig(d, props.ConfigurationEx.(*types.ClusterConfigInfoEx).VsanHostConfig)
if err != nil {
return nil, err
if _, stretchedClusterConfExist := d.GetOk("vsan_stretched_cluster"); !stretchedClusterConfExist {
obj.VsanHostConfigSpec, err = expandVsanHostConfig(d, props.ConfigurationEx.(*types.ClusterConfigInfoEx).VsanHostConfig)
if err != nil {
return nil, err
}
}

if version.Newer(viapi.VSphereVersion{Product: version.Product, Major: 6, Minor: 5}) {
Expand Down Expand Up @@ -2029,8 +2033,10 @@ func flattenClusterConfigSpecEx(d *schema.ResourceData, obj *types.ClusterConfig
return err
}

if err := flattenClusterVsanHostConfigInfo(d, obj.VsanHostConfig); err != nil {
return err
if _, stretchedClusterConfExist := d.GetOk("vsan_stretched_cluster"); !stretchedClusterConfExist {
if err := flattenClusterVsanHostConfigInfo(d, obj.VsanHostConfig); err != nil {
return err
}
}

if version.Newer(viapi.VSphereVersion{Product: version.Product, Major: 6, Minor: 5}) {
Expand Down
25 changes: 19 additions & 6 deletions vsphere/resource_vsphere_compute_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func TestAccResourceVSphereComputeCluster_vsanStretchedCluster(t *testing.T) {
RunSweepers()
testAccPreCheck(t)
testAccResourceVSphereComputeClusterPreCheck(t)
testAccResourceVSphereComputeClusterVSANStretchedClusterPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereComputeClusterCheckExists(false),
Expand Down Expand Up @@ -690,6 +691,18 @@ func testAccResourceVSphereComputeClusterVSANEsaPreCheck(t *testing.T) {
}
}

func testAccResourceVSphereComputeClusterVSANStretchedClusterPreCheck(t *testing.T) {
if os.Getenv("TF_VSPHERE_VSAN_HOST_1") == "" {
t.Skip("set TF_VSPHERE_VSAN_HOST_1 to run vsphere_compute_cluster stretched cluster acceptance tests")
}
if os.Getenv("TF_VSPHERE_VSAN_HOST_2") == "" {
t.Skip("set TF_VSPHERE_VSAN_HOST_2 to run vsphere_compute_cluster stretched cluster acceptance tests")
}
if os.Getenv("TF_VSPHERE_VSAN_WITNESS_HOST") == "" {
t.Skip("set TF_VSPHERE_VSAN_WITNESS_HOST to run vsphere_compute_cluster stretched cluster acceptance tests")
}
}

func testAccResourceVSphereComputeClusterCheckExists(expected bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, err := testGetComputeCluster(s, "compute_cluster", resourceVSphereComputeClusterName)
Expand Down Expand Up @@ -1183,9 +1196,9 @@ resource "vsphere_compute_cluster" "compute_cluster" {
`,
testhelper.CombineConfigs(
testhelper.ConfigDataRootDC1(),
testhelper.ConfigDataRootHost1(),
testhelper.ConfigDataRootHost2(),
testhelper.ConfigDataRootHost3(),
testhelper.ConfigDataVsanHost1(),
testhelper.ConfigDataVsanHost2(),
testhelper.ConfigDataVsanWitnessHost(),
),
)
}
Expand All @@ -1206,9 +1219,9 @@ resource "vsphere_compute_cluster" "compute_cluster" {
`,
testhelper.CombineConfigs(
testhelper.ConfigDataRootDC1(),
testhelper.ConfigDataRootHost1(),
testhelper.ConfigDataRootHost2(),
testhelper.ConfigDataRootHost3(),
testhelper.ConfigDataVsanHost1(),
testhelper.ConfigDataVsanHost2(),
testhelper.ConfigDataVsanWitnessHost(),
),
)
}
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/compute_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,11 @@ resource "vsphere_compute_cluster" "compute_cluster" {
host_ids = [data.vsphere_host.faultdomain2_hosts.*.id]
}
}
vsan_stretched_cluster {
preferred_fault_domain_host_ids = [data.vsphere_host.preferred_fault_domain_host.*.id]
secondary_fault_domain_host_ids = [data.vsphere_host.secondary_fault_domain_host.*.id]
witness_node = data.vsphere_host.witness_host.id
}
}
```

Expand Down

0 comments on commit 38e34e5

Please sign in to comment.