-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
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 := "DuplicateVtepDetector" | ||
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 and VMC. | ||
|
||
## 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. |