-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
546 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.