Skip to content

Commit

Permalink
Failure Domain in Edge Cluster (#958)
Browse files Browse the repository at this point in the history
* Failure Domain in Edge Cluster

Signed-off-by: graysonwu <[email protected]>

---------

Signed-off-by: graysonwu <[email protected]>
  • Loading branch information
GraysonWu authored Oct 16, 2023
1 parent 062a8cc commit 80feaa8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
64 changes: 63 additions & 1 deletion nsxt/resource_nsxt_edge_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
)

var failureDomainAllocationOptions = []string{
"enable",
"disable",
}

func resourceNsxtEdgeCluster() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtEdgeClusterCreate,
Expand Down Expand Up @@ -95,6 +103,12 @@ func resourceNsxtEdgeCluster() *schema.Resource {
},
},
},
"failure_domain_allocation": {
Type: schema.TypeString,
Description: "Flag to enable/disable failure domain based allocation",
Optional: true,
ValidateFunc: validation.StringInSlice(failureDomainAllocationOptions, false),
},
},
}
}
Expand All @@ -107,14 +121,16 @@ func resourceNsxtEdgeClusterCreate(d *schema.ResourceData, m interface{}) error
displayName := d.Get("display_name").(string)
tags := getMPTagsFromSchema(d)
clusterProfileBindings := getClusterProfileBindingsFromSchema(d)

members := getEdgeClusterMembersFromSchema(d)
allocationRules := getAllocationRulesFromSchema(d)

obj := model.EdgeCluster{
Description: &description,
DisplayName: &displayName,
Tags: tags,
ClusterProfileBindings: clusterProfileBindings,
Members: members,
AllocationRules: allocationRules,
}

log.Printf("[INFO] Creating Edge Cluster with name %s", displayName)
Expand Down Expand Up @@ -160,6 +176,27 @@ func getEdgeClusterMembersFromSchema(d *schema.ResourceData) []model.EdgeCluster
return members
}

func getAllocationRulesFromSchema(d *schema.ResourceData) []model.AllocationRule {
failureDomainAllocation := d.Get("failure_domain_allocation").(string)
if failureDomainAllocation != "" {
enableFailureDomain := true
if failureDomainAllocation == "disable" {
enableFailureDomain = false
}
failureDomain := model.AllocationBasedOnFailureDomain{
ActionType: model.AllocationRuleAction_ACTION_TYPE_ALLOCATIONBASEDONFAILUREDOMAIN,
Enabled: &enableFailureDomain,
}
converter := bindings.NewTypeConverter()
failureDomainValue, errs := converter.ConvertToVapi(failureDomain, model.AllocationBasedOnFailureDomainBindingType())
if errs != nil {
return nil
}
return []model.AllocationRule{{Action: failureDomainValue.(*data.StructValue)}}
}
return nil
}

func resourceNsxtEdgeClusterRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
id := d.Id()
Expand All @@ -183,6 +220,7 @@ func resourceNsxtEdgeClusterRead(d *schema.ResourceData, m interface{}) error {
d.Set("member_node_type", obj.MemberNodeType)
setMemberListInSchema(d, obj.Members)
setNodeRtepIPsInSchema(d, obj.NodeRtepIps)
setFailureDomainAllocationInSchema(d, obj.AllocationRules)
return nil
}

Expand All @@ -209,6 +247,27 @@ func setNodeRtepIPsInSchema(d *schema.ResourceData, nodeRtepIPs []model.NodeRtep
return d.Set("node_rtep_ips", expressionList)
}

func setFailureDomainAllocationInSchema(d *schema.ResourceData, allocationRules []model.AllocationRule) error {
converter := bindings.NewTypeConverter()
for _, allocationRule := range allocationRules {
structValue := allocationRule.Action
actionInterface, errs := converter.ConvertToGolang(structValue, model.AllocationBasedOnFailureDomainBindingType())
if errs != nil {
continue
}
action := actionInterface.(model.AllocationBasedOnFailureDomain)
if action.Enabled != nil {
if *action.Enabled {
d.Set("failure_domain_allocation", "enable")
} else {
d.Set("failure_domain_allocation", "disable")
}
break
}
}
return nil
}

func setMemberListInSchema(d *schema.ResourceData, members []model.EdgeClusterMember) error {
var expresionList []map[string]interface{}
for _, member := range members {
Expand Down Expand Up @@ -237,13 +296,16 @@ func resourceNsxtEdgeClusterUpdate(d *schema.ResourceData, m interface{}) error
tags := getMPTagsFromSchema(d)
members := getEdgeClusterMembersFromSchema(d)
clusterProfileBindings := getClusterProfileBindingsFromSchema(d)
allocationRules := getAllocationRulesFromSchema(d)

obj := model.EdgeCluster{
Revision: &revision,
Description: &description,
DisplayName: &displayName,
Tags: tags,
ClusterProfileBindings: clusterProfileBindings,
Members: members,
AllocationRules: allocationRules,
}

_, err := client.Update(id, obj)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/edge_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The following arguments are supported:
* `description` - (Optional) Description of the resource.
* `tag` - (Optional) A list of scope + tag pairs to associate with this resource.
* `edge_ha_profile_id` - (Optional) Edge high availability cluster profile ID.
* `failure_domain_allocation` - (Optional) Flag to enable failure domain based allocation. Enable placement algorithm to consider failure domain of edge transport nodes and place active and standby contexts in different failure domains. Supported values are `enable` and `disable`.
* `member` - (Optional) Edge cluster members
* `description` - (Optional) Description of this resource.
* `display_name` - (Optional) The display name of this resource. Defaults to ID if not set.
Expand Down

0 comments on commit 80feaa8

Please sign in to comment.