Skip to content

Commit 957e988

Browse files
committed
nsxt_policy_ods_pre_defined_runbook data source
Implement nsxt_policy_ods_pre_defined_runbook for ODS runbook framework. Signed-off-by: Kobi Samoray <[email protected]>
1 parent 332e0ac commit 957e988

4 files changed

+155
-0
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
2+
SPDX-License-Identifier: MPL-2.0 */
3+
4+
package nsxt
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha"
12+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
13+
)
14+
15+
func dataSourceNsxtPolicyODSPreDefinedRunbook() *schema.Resource {
16+
return &schema.Resource{
17+
Read: dataSourceNsxtPolicyODSPreDefinedRunbookRead,
18+
19+
Schema: map[string]*schema.Schema{
20+
"id": getDataSourceIDSchema(),
21+
"display_name": getDataSourceDisplayNameSchema(),
22+
"description": getDataSourceDescriptionSchema(),
23+
"path": getPathSchema(),
24+
},
25+
}
26+
}
27+
28+
func dataSourceNsxtPolicyODSPreDefinedRunbookRead(d *schema.ResourceData, m interface{}) error {
29+
connector := getPolicyConnector(m)
30+
client := sha.NewPreDefinedRunbooksClient(connector)
31+
32+
objID := d.Get("id").(string)
33+
objName := d.Get("display_name").(string)
34+
var obj model.OdsPredefinedRunbook
35+
if objID != "" {
36+
// Get by id
37+
objGet, err := client.Get(objID)
38+
if err != nil {
39+
return handleDataSourceReadError(d, "OdsPredefinedRunbook", objID, err)
40+
}
41+
obj = objGet
42+
} else if objName == "" {
43+
return fmt.Errorf("error obtaining OdsPredefinedRunbook ID or name during read")
44+
} else {
45+
// Get by full name/prefix
46+
objList, err := client.List(nil, nil, nil, nil, nil, nil)
47+
if err != nil {
48+
return handleListError("OdsPredefinedRunbook", err)
49+
}
50+
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
51+
var perfectMatch []model.OdsPredefinedRunbook
52+
var prefixMatch []model.OdsPredefinedRunbook
53+
for _, objInList := range objList.Results {
54+
if strings.HasPrefix(*objInList.DisplayName, objName) {
55+
prefixMatch = append(prefixMatch, objInList)
56+
}
57+
if *objInList.DisplayName == objName {
58+
perfectMatch = append(perfectMatch, objInList)
59+
}
60+
}
61+
if len(perfectMatch) > 0 {
62+
if len(perfectMatch) > 1 {
63+
return fmt.Errorf("found multiple OdsPredefinedRunbook with name '%s'", objName)
64+
}
65+
obj = perfectMatch[0]
66+
} else if len(prefixMatch) > 0 {
67+
if len(prefixMatch) > 1 {
68+
return fmt.Errorf("found multiple OdsPredefinedRunbooks with name starting with '%s'", objName)
69+
}
70+
obj = prefixMatch[0]
71+
} else {
72+
return fmt.Errorf("OdsPredefinedRunbook with name '%s' was not found", objName)
73+
}
74+
}
75+
76+
d.SetId(*obj.Id)
77+
d.Set("display_name", obj.DisplayName)
78+
d.Set("description", obj.Description)
79+
d.Set("path", obj.Path)
80+
81+
return nil
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
2+
SPDX-License-Identifier: MPL-2.0 */
3+
4+
package nsxt
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceNsxtPolicyODSPredefinedRunbook_basic(t *testing.T) {
14+
name := "ControllerConn"
15+
testResourceName := "data.nsxt_policy_ods_pre_defined_runbook.test"
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
testAccOnlyLocalManager(t)
20+
testAccPreCheck(t)
21+
testAccNSXVersion(t, "4.1.0")
22+
},
23+
Providers: testAccProviders,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
29+
resource.TestCheckResourceAttrSet(testResourceName, "path"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name string) string {
37+
return fmt.Sprintf(`
38+
data "nsxt_policy_ods_pre_defined_runbook" "test" {
39+
display_name = "%s"
40+
}`, name)
41+
}

Diff for: nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ func Provider() *schema.Provider {
324324
"nsxt_policy_vtep_ha_host_switch_profile": dataSourceNsxtVtepHAHostSwitchProfile(),
325325
"nsxt_policy_distributed_flood_protection_profile": dataSourceNsxtPolicyDistributedFloodProtectionProfile(),
326326
"nsxt_policy_gateway_flood_protection_profile": dataSourceNsxtPolicyGatewayFloodProtectionProfile(),
327+
"nsxt_policy_ods_pre_defined_runbook": dataSourceNsxtPolicyODSPreDefinedRunbook(),
327328
},
328329

329330
ResourcesMap: map[string]*schema.Resource{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
subcategory: "ODS Runbook"
3+
layout: "nsxt"
4+
page_title: "NSXT: policy_ods_pre_defined_runbook"
5+
description: Policy ODS pre-defined runbook data source.
6+
---
7+
8+
# nsxt_policy_ods_pre_defined_runbook
9+
10+
This data source provides information about policy ODS pre-defined runbook configured on NSX.
11+
This data source is applicable to NSX Policy Manager.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "nsxt_policy_ods_pre_defined_runbook" "test" {
17+
display_name = "OverlayTunnel"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
* `id` - (Optional) The ID of ODS pre-defined runbook to retrieve. If ID is specified, no additional argument should be configured.
24+
* `display_name` - (Optional) The Display Name prefix of the ODS pre-defined runbook to retrieve.
25+
26+
## Attributes Reference
27+
28+
In addition to arguments listed above, the following attributes are exported:
29+
30+
* `description` - The description of the resource.
31+
* `path` - The NSX path of the policy resource.

0 commit comments

Comments
 (0)