Skip to content

Commit

Permalink
nsxt_policy_ods_pre_defined_runbook data source
Browse files Browse the repository at this point in the history
Implement nsxt_policy_ods_pre_defined_runbook for ODS runbook framework.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Nov 29, 2023
1 parent 2e5e2bc commit 03aad86
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
82 changes: 82 additions & 0 deletions nsxt/data_source_nsxt_policy_ods_pre_defined_runbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* 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/infra/sha"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

func dataSourceNsxtPolicyODSPreDefinedRunbook() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtPolicyODSPreDefinedRunbookRead,

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

func dataSourceNsxtPolicyODSPreDefinedRunbookRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := sha.NewPreDefinedRunbooksClient(connector)

objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj model.OdsPredefinedRunbook
if objID != "" {
// Get by id
objGet, err := client.Get(objID)
if err != nil {
return handleDataSourceReadError(d, "OdsPredefinedRunbook", objID, err)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("error obtaining OdsPredefinedRunbook ID or name during read")
} else {
// Get by full name/prefix
objList, err := client.List(nil, nil, nil, nil, nil, nil)
if err != nil {
return handleListError("OdsPredefinedRunbook", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.OdsPredefinedRunbook
var prefixMatch []model.OdsPredefinedRunbook
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 OdsPredefinedRunbook with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("found multiple OdsPredefinedRunbooks with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("OdsPredefinedRunbook with name '%s' was not found", objName)
}
}

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

return nil
}
41 changes: 41 additions & 0 deletions nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceNsxtPolicyODSPredefinedRunbook_basic(t *testing.T) {
name := "ControllerConn"
testResourceName := "data.nsxt_policy_ods_pre_defined_runbook.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccOnlyLocalManager(t)
testAccPreCheck(t)
testAccNSXVersion(t, "4.1.0")
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
),
},
},
})
}

func testAccNsxtPolicyODSPredefinedRunbookReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_policy_ods_pre_defined_runbook" "test" {
display_name = "%s"
}`, name)
}
1 change: 1 addition & 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_policy_host_transport_node": dataSourceNsxtPolicyHostTransportNode(),
"nsxt_manager_cluster_node": dataSourceNsxtManagerClusterNode(),
"nsxt_policy_host_transport_node_profile": dataSourceNsxtPolicyHostTransportNodeProfile(),
"nsxt_policy_ods_pre_defined_runbook": dataSourceNsxtPolicyODSPreDefinedRunbook(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/policy_ods_pre_defined_runbook.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
subcategory: "ODS Runbook"
layout: "nsxt"
page_title: "NSXT: policy_ods_pre_defined_runbook"
description: Policy ODS pre-defined runbook data source.
---

# nsxt_policy_ods_pre_defined_runbook

This data source provides information about policy ODS pre-defined runbook configured on NSX.
This data source is applicable to NSX Policy Manager.

## Example Usage

```hcl
data "nsxt_policy_ods_pre_defined_runbook" "test" {
display_name = "OverlayTunnel"
}
```

## Argument Reference

* `id` - (Optional) The ID of ODS pre-defined runbook to retrieve. If ID is specified, no additional argument should be configured.
* `display_name` - (Optional) The Display Name prefix of the ODS pre-defined runbook to retrieve.

## Attributes Reference

In addition to arguments listed above, the following attributes are exported:

* `description` - The description of the resource.
* `path` - The NSX path of the policy resource.

0 comments on commit 03aad86

Please sign in to comment.