Skip to content

Commit

Permalink
Merge branch 'master' into ha-vip
Browse files Browse the repository at this point in the history
  • Loading branch information
2ez4szliu authored Sep 16, 2023
2 parents 62dcb01 + edf823b commit f563fc9
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 22 deletions.
84 changes: 84 additions & 0 deletions nsxt/data_source_nsxt_failure_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"strings"

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

func dataSourceNsxtFailureDomain() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtFailureDomainRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
},
}
}

func dataSourceNsxtFailureDomainRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := nsx.NewFailureDomainsClient(connector)

objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj model.FailureDomain
if objID != "" {
// Get by id
objGet, err := client.Get(objID)
if isNotFoundError(err) {
return fmt.Errorf("FailureDomain with ID %s was not found", objID)
}

if err != nil {
return fmt.Errorf("error while reading FailureDomain %s: %v", objID, err)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("error obtaining FailureDomain ID or name during read")
} else {
// Get by full name/prefix
objList, err := client.List()
if err != nil {
return fmt.Errorf("error while reading FailureDomains: %v", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.FailureDomain
var prefixMatch []model.FailureDomain
for _, objInList := range objList.Results {
if strings.HasPrefix(*objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
if *objInList.DisplayName == objName {
perfectMatch = append(perfectMatch, objInList)
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("found multiple FailureDomains with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("found multiple FailureDomains with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("FailureDomain with name '%s' was not found", objName)
}
}

d.SetId(*obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)

return nil
}
11 changes: 7 additions & 4 deletions nsxt/policy_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ func validatePolicyRuleSequence(d *schema.ResourceData) error {
func getPolicyRulesFromSchema(d *schema.ResourceData) []model.Rule {
rules := d.Get("rule").([]interface{})
var ruleList []model.Rule
lastSequence := int64(0)
for _, rule := range rules {
data := rule.(map[string]interface{})
displayName := data["display_name"].(string)
Expand Down Expand Up @@ -469,6 +470,11 @@ func getPolicyRulesFromSchema(d *schema.ResourceData) []model.Rule {
}

resourceType := "Rule"
if sequenceNumber == 0 {
sequenceNumber = lastSequence + 1
}
lastSequence = sequenceNumber

elem := model.Rule{
ResourceType: &resourceType,
Id: &id,
Expand All @@ -489,10 +495,7 @@ func getPolicyRulesFromSchema(d *schema.ResourceData) []model.Rule {
Services: getPathListFromMap(data, "services"),
Scope: getPathListFromMap(data, "scope"),
Profiles: getPathListFromMap(data, "profiles"),
}

if sequenceNumber > 0 {
elem.SequenceNumber = &sequenceNumber
SequenceNumber: &sequenceNumber,
}

ruleList = append(ruleList, elem)
Expand Down
2 changes: 2 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func Provider() *schema.Provider {
"nsxt_uplink_host_switch_profile": dataSourceNsxtUplinkHostSwitchProfile(),
"nsxt_compute_manager": dataSourceNsxtComputeManager(),
"nsxt_transport_node_realization": dataSourceNsxtTransportNodeRealization(),
"nsxt_failure_domain": dataSourceNsxtFailureDomain(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -426,6 +427,7 @@ func Provider() *schema.Provider {
"nsxt_uplink_host_switch_profile": resourceNsxtUplinkHostSwitchProfile(),
"nsxt_transport_node": resourceNsxtTransportNode(),
"nsxt_cluster_virtual_ip": resourceNsxtClusterVirualIP(),
"nsxt_failure_domain": resourceNsxtFailureDomain(),
},

ConfigureFunc: providerConfigure,
Expand Down
156 changes: 156 additions & 0 deletions nsxt/resource_nsxt_failure_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"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/services/nsxt-mp/nsx"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
)

var edgeServices = []string{
"active",
"standby",
"no_preference",
}

func resourceNsxtFailureDomain() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtFailureDomainCreate,
Read: resourceNsxtFailureDomainRead,
Update: resourceNsxtFailureDomainUpdate,
Delete: resourceNsxtFailureDomainDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"preferred_edge_services": {
Type: schema.TypeString,
Description: "Set preference for failure domain",
Optional: true,
Default: "no_preference",
ValidateFunc: validation.StringInSlice(edgeServices, false),
},
},
}
}

func resourceNsxtFailureDomainRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining FailureDomain ID")
}

client := nsx.NewFailureDomainsClient(connector)
obj, err := client.Get(id)
if err != nil {
return fmt.Errorf("error during FailureDomain read: %v", err)
}

d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
setMPTagsInSchema(d, obj.Tags)
d.Set("revision", obj.Revision)

preferPtr := obj.PreferredActiveEdgeServices
preferStr := "no_preference"
if preferPtr != nil {
if *preferPtr == true {
preferStr = "active"
} else {
preferStr = "standby"
}
}
d.Set("preferred_edge_services", preferStr)
return nil
}

func failureDomainSchemaToModel(d *schema.ResourceData) model.FailureDomain {
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getMPTagsFromSchema(d)

obj := model.FailureDomain{
DisplayName: &displayName,
Description: &description,
Tags: tags,
}

preferredEdgeServices := d.Get("preferred_edge_services").(string)
if preferredEdgeServices != "no_preference" {
activePrefer := true
standbyPrefer := false
if preferredEdgeServices == "active" {
obj.PreferredActiveEdgeServices = &activePrefer
} else {
obj.PreferredActiveEdgeServices = &standbyPrefer
}
}
return obj
}

func resourceNsxtFailureDomainCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := nsx.NewFailureDomainsClient(connector)

failureDomain := failureDomainSchemaToModel(d)
obj, err := client.Create(failureDomain)
if err != nil {
id := ""
if obj.Id != nil {
id = *obj.Id
}
return handleCreateError("Failure Domain", id, err)
}
log.Printf("[INFO] FailureDomain with ID %s created", *obj.Id)
d.SetId(*obj.Id)

return resourceNsxtFailureDomainRead(d, m)
}

func resourceNsxtFailureDomainUpdate(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining FailureDomain ID")
}

connector := getPolicyConnector(m)
client := nsx.NewFailureDomainsClient(connector)

failureDomain := failureDomainSchemaToModel(d)
revision := int64(d.Get("revision").(int))
failureDomain.Revision = &revision

_, err := client.Update(id, failureDomain)
if err != nil {
return handleCreateError("FailureDomain", id, err)
}

return resourceNsxtFailureDomainRead(d, m)
}

func resourceNsxtFailureDomainDelete(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining FailureDomain ID")
}
connector := getPolicyConnector(m)
client := nsx.NewFailureDomainsClient(connector)
err := client.Delete(id)
if err != nil {
return handleDeleteError("FailureDomain", id, err)
}
return nil
}
Loading

0 comments on commit f563fc9

Please sign in to comment.