Skip to content

Commit

Permalink
Merge pull request #1008 from vmware/pool-realized-id
Browse files Browse the repository at this point in the history
Support realized_id in ip pool data source
  • Loading branch information
annakhm authored Nov 7, 2023
2 parents 6fcb37c + 05103db commit d7b914e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 54 deletions.
71 changes: 17 additions & 54 deletions nsxt/data_source_nsxt_policy_ip_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
1 change: 1 addition & 0 deletions nsxt/data_source_nsxt_policy_ip_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
Expand Down
6 changes: 6 additions & 0 deletions nsxt/resource_nsxt_policy_ip_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions nsxt/resource_nsxt_policy_ip_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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"),
),
},
{
Expand All @@ -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"),
),
},
},
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/policy_ip_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions website/docs/r/policy_ip_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d7b914e

Please sign in to comment.