From b2237c7d38aa8910f41d370f8a08ef56d141d116 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Wed, 25 Oct 2023 17:36:50 +0300 Subject: [PATCH] nsxt_policy_ods_pre_defined_runbook data source Implement nsxt_policy_ods_pre_defined_runbook for ODS runbook framework. Signed-off-by: Kobi Samoray --- ...rce_nsxt_policy_ods_pre_defined_runbook.go | 82 +++++++++++++++++++ ...sxt_policy_ods_pre_defined_runbook_test.go | 41 ++++++++++ nsxt/provider.go | 1 + ...licy_ods_pre_defined_runbook.html.markdown | 31 +++++++ 4 files changed, 155 insertions(+) create mode 100644 nsxt/data_source_nsxt_policy_ods_pre_defined_runbook.go create mode 100644 nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go create mode 100644 website/docs/d/policy_ods_pre_defined_runbook.html.markdown diff --git a/nsxt/data_source_nsxt_policy_ods_pre_defined_runbook.go b/nsxt/data_source_nsxt_policy_ods_pre_defined_runbook.go new file mode 100644 index 000000000..eeeb82a28 --- /dev/null +++ b/nsxt/data_source_nsxt_policy_ods_pre_defined_runbook.go @@ -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 +} diff --git a/nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go b/nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go new file mode 100644 index 000000000..880e65ae3 --- /dev/null +++ b/nsxt/data_source_nsxt_policy_ods_pre_defined_runbook_test.go @@ -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) +} diff --git a/nsxt/provider.go b/nsxt/provider.go index 43dec2f7e..baafe8c67 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -292,6 +292,7 @@ func Provider() *schema.Provider { "nsxt_compute_collection": dataSourceNsxtComputeCollection(), "nsxt_compute_manager_realization": dataSourceNsxtComputeManagerRealization(), "nsxt_policy_host_transport_node": dataSourceNsxtPolicyHostTransportNode(), + "nsxt_policy_ods_pre_defined_runbook": dataSourceNsxtPolicyODSPreDefinedRunbook(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/docs/d/policy_ods_pre_defined_runbook.html.markdown b/website/docs/d/policy_ods_pre_defined_runbook.html.markdown new file mode 100644 index 000000000..b1c71da3f --- /dev/null +++ b/website/docs/d/policy_ods_pre_defined_runbook.html.markdown @@ -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.