-
Notifications
You must be signed in to change notification settings - Fork 85
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
ksamoray
wants to merge
1
commit into
vmware:master
Choose a base branch
from
ksamoray:ds-pre-defined-runbooks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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
41
nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go
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,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) | ||
} |
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
31 changes: 31 additions & 0 deletions
31
website/docs/d/policy_ods_pre_defined_runbook.html.markdown
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,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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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