Skip to content

Commit d7b914e

Browse files
authored
Merge pull request #1008 from vmware/pool-realized-id
Support realized_id in ip pool data source
2 parents 6fcb37c + 05103db commit d7b914e

6 files changed

+29
-54
lines changed

nsxt/data_source_nsxt_policy_ip_pool.go

+17-54
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
package nsxt
55

66
import (
7-
"fmt"
8-
"strings"
9-
107
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11-
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
128

13-
"github.com/vmware/terraform-provider-nsxt/api/infra"
9+
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
10+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
1411
)
1512

1613
func dataSourceNsxtPolicyIPPool() *schema.Resource {
@@ -23,61 +20,27 @@ func dataSourceNsxtPolicyIPPool() *schema.Resource {
2320
"description": getDataSourceDescriptionSchema(),
2421
"path": getPathSchema(),
2522
"context": getContextSchema(),
23+
"realized_id": {
24+
Type: schema.TypeString,
25+
Description: "The ID of the realized resource",
26+
Computed: true,
27+
},
2628
},
2729
}
2830
}
2931

3032
func dataSourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error {
31-
connector := getPolicyConnector(m)
32-
client := infra.NewIpPoolsClient(getSessionContext(d, m), connector)
33-
34-
objID := d.Get("id").(string)
35-
objName := d.Get("display_name").(string)
36-
var obj model.IpAddressPool
37-
if objID != "" {
38-
// Get by id
39-
objGet, err := client.Get(objID)
40-
if err != nil {
41-
return handleDataSourceReadError(d, "IpAddressPool", objID, err)
42-
}
43-
obj = objGet
44-
} else if objName == "" {
45-
return fmt.Errorf("Error obtaining IpAddressPool ID or name during read")
46-
} else {
47-
// Get by full name/prefix
48-
objList, err := client.List(nil, nil, nil, nil, nil, nil)
49-
if err != nil {
50-
return handleListError("IpAddressPool", err)
51-
}
52-
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
53-
var perfectMatch []model.IpAddressPool
54-
var prefixMatch []model.IpAddressPool
55-
for _, objInList := range objList.Results {
56-
if strings.HasPrefix(*objInList.DisplayName, objName) {
57-
prefixMatch = append(prefixMatch, objInList)
58-
}
59-
if *objInList.DisplayName == objName {
60-
perfectMatch = append(perfectMatch, objInList)
61-
}
62-
}
63-
if len(perfectMatch) > 0 {
64-
if len(perfectMatch) > 1 {
65-
return fmt.Errorf("Found multiple IpAddressPools with name '%s'", objName)
66-
}
67-
obj = perfectMatch[0]
68-
} else if len(prefixMatch) > 0 {
69-
if len(prefixMatch) > 1 {
70-
return fmt.Errorf("Found multiple IpAddressPools with name starting with '%s'", objName)
71-
}
72-
obj = prefixMatch[0]
73-
} else {
74-
return fmt.Errorf("IpAddressPool with name '%s' was not found", objName)
75-
}
33+
obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "IpAddressPool", nil)
34+
if err != nil {
35+
return err
7636
}
7737

78-
d.SetId(*obj.Id)
79-
d.Set("display_name", obj.DisplayName)
80-
d.Set("description", obj.Description)
81-
d.Set("path", obj.Path)
38+
converter := bindings.NewTypeConverter()
39+
dataValue, errors := converter.ConvertToGolang(obj, model.IpAddressPoolBindingType())
40+
if len(errors) > 0 {
41+
return errors[0]
42+
}
43+
pool := dataValue.(model.IpAddressPool)
44+
d.Set("realized_id", pool.RealizationId)
8245
return nil
8346
}

nsxt/data_source_nsxt_policy_ip_pool_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func testAccDataSourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preC
4949
Check: resource.ComposeTestCheckFunc(
5050
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
5151
resource.TestCheckResourceAttr(testResourceName, "description", name),
52+
resource.TestCheckResourceAttrSet(testResourceName, "realized_id"),
5253
resource.TestCheckResourceAttrSet(testResourceName, "path"),
5354
),
5455
},

nsxt/resource_nsxt_policy_ip_pool.go

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func resourceNsxtPolicyIPPool() *schema.Resource {
3333
"revision": getRevisionSchema(),
3434
"tag": getTagsSchema(),
3535
"context": getContextSchema(),
36+
"realized_id": {
37+
Type: schema.TypeString,
38+
Description: "The ID of the realized resource",
39+
Computed: true,
40+
},
3641
},
3742
}
3843
}
@@ -77,6 +82,7 @@ func resourceNsxtPolicyIPPoolRead(d *schema.ResourceData, m interface{}) error {
7782
d.Set("nsx_id", pool.Id)
7883
d.Set("path", pool.Path)
7984
d.Set("revision", pool.Revision)
85+
d.Set("realized_id", pool.RealizationId)
8086

8187
return nil
8288
}

nsxt/resource_nsxt_policy_ip_pool_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestAccResourceNsxtPolicyIPPool_minimal(t *testing.T) {
3030
testAccNSXPolicyIPPoolCheckExists(testResourceName),
3131
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
3232
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
33+
resource.TestCheckResourceAttrSet(testResourceName, "realized_id"),
3334
),
3435
},
3536
},
@@ -69,6 +70,7 @@ func testAccResourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preChe
6970
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
7071
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
7172
resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"),
73+
resource.TestCheckResourceAttrSet(testResourceName, "realized_id"),
7274
),
7375
},
7476
{
@@ -78,6 +80,7 @@ func testAccResourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preChe
7880
resource.TestCheckResourceAttr(testResourceName, "tag.#", "2"),
7981
resource.TestCheckResourceAttr(testResourceName, "display_name", updatedName),
8082
resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"),
83+
resource.TestCheckResourceAttrSet(testResourceName, "realized_id"),
8184
),
8285
},
8386
},

website/docs/d/policy_ip_pool.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ In addition to arguments listed above, the following attributes are exported:
4747

4848
* `description` - The description of the resource.
4949
* `path` - The NSX path of the policy resource.
50+
* `realized_id` - The id of realized pool object. This id should be used in `nsxt_transport_node` resource.

website/docs/r/policy_ip_pool.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ In addition to arguments listed above, the following attributes are exported:
7272
* `id` - ID of the IP Pool.
7373
* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging.
7474
* `path` - The NSX path of the resource.
75+
* `realized_id` - The id of realized pool object. This id should be used in `nsxt_transport_node` resource.
7576

7677
## Importing
7778

0 commit comments

Comments
 (0)