From 05103db6f522e593a54a4ae3966a71c567519e5e Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Fri, 20 Oct 2023 22:52:41 +0000 Subject: [PATCH] Support realized_id in ip pool data source This id should be used in transport node resource Signed-off-by: Anna Khmelnitsky --- nsxt/data_source_nsxt_policy_ip_pool.go | 71 +++++--------------- nsxt/data_source_nsxt_policy_ip_pool_test.go | 1 + nsxt/resource_nsxt_policy_ip_pool.go | 6 ++ nsxt/resource_nsxt_policy_ip_pool_test.go | 3 + website/docs/d/policy_ip_pool.html.markdown | 1 + website/docs/r/policy_ip_pool.html.markdown | 1 + 6 files changed, 29 insertions(+), 54 deletions(-) diff --git a/nsxt/data_source_nsxt_policy_ip_pool.go b/nsxt/data_source_nsxt_policy_ip_pool.go index 8016dbf25..3a378608e 100644 --- a/nsxt/data_source_nsxt_policy_ip_pool.go +++ b/nsxt/data_source_nsxt_policy_ip_pool.go @@ -4,13 +4,10 @@ package nsxt import ( - "fmt" - "strings" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" - "github.com/vmware/terraform-provider-nsxt/api/infra" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" ) func dataSourceNsxtPolicyIPPool() *schema.Resource { @@ -23,61 +20,27 @@ func dataSourceNsxtPolicyIPPool() *schema.Resource { "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), "context": getContextSchema(), + "realized_id": { + Type: schema.TypeString, + Description: "The ID of the realized resource", + Computed: true, + }, }, } } func dataSourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error { - connector := getPolicyConnector(m) - client := infra.NewIpPoolsClient(getSessionContext(d, m), connector) - - objID := d.Get("id").(string) - objName := d.Get("display_name").(string) - var obj model.IpAddressPool - if objID != "" { - // Get by id - objGet, err := client.Get(objID) - if err != nil { - return handleDataSourceReadError(d, "IpAddressPool", objID, err) - } - obj = objGet - } else if objName == "" { - return fmt.Errorf("Error obtaining IpAddressPool 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("IpAddressPool", err) - } - // go over the list to find the correct one (prefer a perfect match. If not - prefix match) - var perfectMatch []model.IpAddressPool - var prefixMatch []model.IpAddressPool - 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 IpAddressPools with name '%s'", objName) - } - obj = perfectMatch[0] - } else if len(prefixMatch) > 0 { - if len(prefixMatch) > 1 { - return fmt.Errorf("Found multiple IpAddressPools with name starting with '%s'", objName) - } - obj = prefixMatch[0] - } else { - return fmt.Errorf("IpAddressPool with name '%s' was not found", objName) - } + obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "IpAddressPool", nil) + if err != nil { + return err } - d.SetId(*obj.Id) - d.Set("display_name", obj.DisplayName) - d.Set("description", obj.Description) - d.Set("path", obj.Path) + converter := bindings.NewTypeConverter() + dataValue, errors := converter.ConvertToGolang(obj, model.IpAddressPoolBindingType()) + if len(errors) > 0 { + return errors[0] + } + pool := dataValue.(model.IpAddressPool) + d.Set("realized_id", pool.RealizationId) return nil } diff --git a/nsxt/data_source_nsxt_policy_ip_pool_test.go b/nsxt/data_source_nsxt_policy_ip_pool_test.go index 2587ce8f2..669606083 100644 --- a/nsxt/data_source_nsxt_policy_ip_pool_test.go +++ b/nsxt/data_source_nsxt_policy_ip_pool_test.go @@ -49,6 +49,7 @@ func testAccDataSourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preC Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(testResourceName, "display_name", name), resource.TestCheckResourceAttr(testResourceName, "description", name), + resource.TestCheckResourceAttrSet(testResourceName, "realized_id"), resource.TestCheckResourceAttrSet(testResourceName, "path"), ), }, diff --git a/nsxt/resource_nsxt_policy_ip_pool.go b/nsxt/resource_nsxt_policy_ip_pool.go index f63ebf58f..74c331f3b 100644 --- a/nsxt/resource_nsxt_policy_ip_pool.go +++ b/nsxt/resource_nsxt_policy_ip_pool.go @@ -33,6 +33,11 @@ func resourceNsxtPolicyIPPool() *schema.Resource { "revision": getRevisionSchema(), "tag": getTagsSchema(), "context": getContextSchema(), + "realized_id": { + Type: schema.TypeString, + Description: "The ID of the realized resource", + Computed: true, + }, }, } } @@ -77,6 +82,7 @@ func resourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error { d.Set("nsx_id", pool.Id) d.Set("path", pool.Path) d.Set("revision", pool.Revision) + d.Set("realized_id", pool.RealizationId) return nil } diff --git a/nsxt/resource_nsxt_policy_ip_pool_test.go b/nsxt/resource_nsxt_policy_ip_pool_test.go index d4efd10bf..fa027834b 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_test.go +++ b/nsxt/resource_nsxt_policy_ip_pool_test.go @@ -30,6 +30,7 @@ func TestAccResourceNsxtPolicyIPPool_minimal(t *testing.T) { testAccNSXPolicyIPPoolCheckExists(testResourceName), resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), resource.TestCheckResourceAttr(testResourceName, "display_name", name), + resource.TestCheckResourceAttrSet(testResourceName, "realized_id"), ), }, }, @@ -69,6 +70,7 @@ func testAccResourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preChe resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), resource.TestCheckResourceAttr(testResourceName, "display_name", name), resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"), + resource.TestCheckResourceAttrSet(testResourceName, "realized_id"), ), }, { @@ -78,6 +80,7 @@ func testAccResourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preChe resource.TestCheckResourceAttr(testResourceName, "tag.#", "2"), resource.TestCheckResourceAttr(testResourceName, "display_name", updatedName), resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"), + resource.TestCheckResourceAttrSet(testResourceName, "realized_id"), ), }, }, diff --git a/website/docs/d/policy_ip_pool.html.markdown b/website/docs/d/policy_ip_pool.html.markdown index 0caf66ed9..bbf086ab5 100644 --- a/website/docs/d/policy_ip_pool.html.markdown +++ b/website/docs/d/policy_ip_pool.html.markdown @@ -47,3 +47,4 @@ 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. +* `realized_id` - The id of realized pool object. This id should be used in `nsxt_transport_node` resource. diff --git a/website/docs/r/policy_ip_pool.html.markdown b/website/docs/r/policy_ip_pool.html.markdown index 6461766e6..2961ab510 100644 --- a/website/docs/r/policy_ip_pool.html.markdown +++ b/website/docs/r/policy_ip_pool.html.markdown @@ -72,6 +72,7 @@ In addition to arguments listed above, the following attributes are exported: * `id` - ID of the IP Pool. * `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. * `path` - The NSX path of the resource. +* `realized_id` - The id of realized pool object. This id should be used in `nsxt_transport_node` resource. ## Importing