4
4
package nsxt
5
5
6
6
import (
7
- "fmt"
8
- "strings"
9
-
10
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
- "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
12
8
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"
14
11
)
15
12
16
13
func dataSourceNsxtPolicyIPPool () * schema.Resource {
@@ -23,61 +20,27 @@ func dataSourceNsxtPolicyIPPool() *schema.Resource {
23
20
"description" : getDataSourceDescriptionSchema (),
24
21
"path" : getPathSchema (),
25
22
"context" : getContextSchema (),
23
+ "realized_id" : {
24
+ Type : schema .TypeString ,
25
+ Description : "The ID of the realized resource" ,
26
+ Computed : true ,
27
+ },
26
28
},
27
29
}
28
30
}
29
31
30
32
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
76
36
}
77
37
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 )
82
45
return nil
83
46
}
0 commit comments