Skip to content

Commit 9c99302

Browse files
committed
Add Edge high availability profile data source
For v9.0 and above. Signed-off-by: Kobi Samoray <[email protected]>
1 parent 1c22350 commit 9c99302

4 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "Fabric"
3+
page_title: "NSXT: nsxt_policy_edge_high_availability_profile"
4+
description: A policy Edge high availability profile data source.
5+
---
6+
7+
# nsxt_policy_edge_high_availability_profile
8+
9+
This data source provides information about policy Edge high availability profile configured on NSX.
10+
This data source is applicable to NSX Policy Manager.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "nsxt_policy_edge_high_availability_profile" "edge_ha_profile" {
16+
display_name = "edge_ha_profile1"
17+
site_path = data.nsxt_policy_site.paris.path
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
* `id` - (Optional) The ID of policy Edge high availability profile to retrieve.
24+
* `display_name` - (Optional) The Display Name prefix of the Edge high availability profile to retrieve.
25+
* `site_path` - (Optional) The path of the site which the Edge high availability profile belongs to.
26+
27+
## Attributes Reference
28+
29+
In addition to arguments listed above, the following attributes are exported:
30+
31+
* `description` - The description of the resource.
32+
* `path` - The NSX path of the policy resource.
33+
* `unique_id` - A unique identifier assigned by the system.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package nsxt
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
13+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
14+
)
15+
16+
func dataSourceNsxtPolicyEdgeHighAvailabilityProfile() *schema.Resource {
17+
return &schema.Resource{
18+
Read: dataSourceNsxtPolicyEdgeHighAvailabilityProfileRead,
19+
20+
Schema: map[string]*schema.Schema{
21+
"id": getDataSourceIDSchema(),
22+
"display_name": getDataSourceDisplayNameSchema(),
23+
"description": getDataSourceDescriptionSchema(),
24+
"path": getPathSchema(),
25+
"site_path": {
26+
Type: schema.TypeString,
27+
Description: "Path of the site this Transport Node Collection belongs to",
28+
Optional: true,
29+
ValidateFunc: validatePolicyPath(),
30+
},
31+
"unique_id": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "A unique identifier assigned by the system",
35+
},
36+
},
37+
}
38+
}
39+
40+
func dataSourceNsxtPolicyEdgeHighAvailabilityProfileRead(d *schema.ResourceData, m interface{}) error {
41+
connector := getPolicyConnector(m)
42+
client := enforcement_points.NewEdgeClusterHighAvailabilityProfilesClient(connector)
43+
44+
objID := d.Get("id").(string)
45+
objName := d.Get("display_name").(string)
46+
objSitePath := d.Get("site_path").(string)
47+
objSiteID := defaultSite
48+
// For local manager, if site path is not provided, use default site
49+
if objSitePath != "" {
50+
objSiteID = getPolicyIDFromPath(objSitePath)
51+
}
52+
var obj model.PolicyEdgeHighAvailabilityProfile
53+
if objID != "" {
54+
// Get by id
55+
objGet, err := client.Get(objSiteID, getPolicyEnforcementPoint(m), objID)
56+
if err != nil {
57+
return handleDataSourceReadError(d, "EdgeHighAvailabilityProfile", objID, err)
58+
}
59+
obj = objGet
60+
} else if objName == "" {
61+
return fmt.Errorf("Error obtaining EdgeHighAvailabilityProfile ID or name during read")
62+
} else {
63+
// Get by full name/prefix
64+
objList, err := client.List(objSiteID, getPolicyEnforcementPoint(m), nil, nil, nil, nil, nil, nil, nil)
65+
if err != nil {
66+
return handleListError("EdgeHighAvailabilityProfile", err)
67+
}
68+
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
69+
var perfectMatch []model.PolicyEdgeHighAvailabilityProfile
70+
var prefixMatch []model.PolicyEdgeHighAvailabilityProfile
71+
for _, objInList := range objList.Results {
72+
if strings.HasPrefix(*objInList.DisplayName, objName) {
73+
prefixMatch = append(prefixMatch, objInList)
74+
}
75+
if *objInList.DisplayName == objName {
76+
perfectMatch = append(perfectMatch, objInList)
77+
}
78+
}
79+
if len(perfectMatch) > 0 {
80+
if len(perfectMatch) > 1 {
81+
return fmt.Errorf("Found multiple EdgeHighAvailabilityProfile with name '%s'", objName)
82+
}
83+
obj = perfectMatch[0]
84+
} else if len(prefixMatch) > 0 {
85+
if len(prefixMatch) > 1 {
86+
return fmt.Errorf("Found multiple EdgeHighAvailabilityProfiles with name starting with '%s'", objName)
87+
}
88+
obj = prefixMatch[0]
89+
} else {
90+
return fmt.Errorf("EdgeHighAvailabilityProfile with name '%s' was not found", objName)
91+
}
92+
}
93+
94+
d.SetId(*obj.Id)
95+
d.Set("display_name", obj.DisplayName)
96+
d.Set("description", obj.Description)
97+
d.Set("path", obj.Path)
98+
d.Set("unique_id", obj.UniqueId)
99+
100+
return nil
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package nsxt
6+
7+
import (
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceNsxtPolicyEdgeHighAvailabilityProfile_basic(t *testing.T) {
14+
testResourceName := "data.nsxt_policy_edge_high_availability_profile.test"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() {
18+
testAccOnlyLocalManager(t)
19+
testAccPreCheck(t)
20+
testAccNSXVersion(t, "9.0.0")
21+
},
22+
Providers: testAccProviders,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccNSXEdgeHighAvailabilityProfileReadTemplate(),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyEdgeHighAvailabilityProfileUpdateAttributes["display_name"]),
28+
resource.TestCheckResourceAttrSet(testResourceName, "id"),
29+
resource.TestCheckResourceAttrSet(testResourceName, "unique_id"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccNSXEdgeHighAvailabilityProfileReadTemplate() string {
37+
return testAccNsxtPolicyEdgeHighAvailabilityProfileMinimalistic() + `
38+
data "nsxt_policy_edge_high_availability_profile" "test" {
39+
display_name = nsxt_policy_edge_high_availability_profile.test.display_name
40+
41+
depends_on = [nsxt_policy_edge_high_availability_profile.test]
42+
}`
43+
}

nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func Provider() *schema.Provider {
345345
"nsxt_policy_services": dataSourceNsxtPolicyServices(),
346346
"nsxt_policy_groups": dataSourceNsxtPolicyGroups(),
347347
"nsxt_policy_edge_transport_node": dataSourceNsxtPolicyEdgeTransportNode(),
348+
"nsxt_policy_edge_high_availability_profile": dataSourceNsxtPolicyEdgeHighAvailabilityProfile(),
348349
},
349350

350351
ResourcesMap: map[string]*schema.Resource{

0 commit comments

Comments
 (0)