Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsxt_policy_ods_pre_defined_runbook data source #1014

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the search API work here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I tried - it doesn't seem to work

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 @@ -324,6 +324,7 @@ func Provider() *schema.Provider {
"nsxt_policy_vtep_ha_host_switch_profile": dataSourceNsxtVtepHAHostSwitchProfile(),
"nsxt_policy_distributed_flood_protection_profile": dataSourceNsxtPolicyDistributedFloodProtectionProfile(),
"nsxt_policy_gateway_flood_protection_profile": dataSourceNsxtPolicyGatewayFloodProtectionProfile(),
"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.
Loading