@@ -10,6 +10,7 @@ package nsxt
1010import (
1111 "fmt"
1212 "log"
13+ "net/url"
1314 "reflect"
1415 "strconv"
1516 "strings"
@@ -151,7 +152,7 @@ func DatasourceRead(d *schema.ResourceData, meta interface{}, objType string, s
151152 nsxID := d .Get ("nsx_id" ).(string )
152153 isInfraObject := false
153154 isProjectInfra := false
154- var url string
155+ var uri string
155156 if strings .HasPrefix (objType , "ProjectInfra" ) {
156157 objType = strings .TrimPrefix (objType , "ProjectInfra" )
157158 isProjectInfra = true
@@ -168,27 +169,29 @@ func DatasourceRead(d *schema.ResourceData, meta interface{}, objType string, s
168169 }
169170
170171 // Get the object from NSX using hidden full text search API
171- url = nsxtClient .Config .BasePath + "/search?query=resource_type:" + objType
172+ uri = nsxtClient .Config .BasePath + "/search?query=resource_type:" + objType
172173 if nsxID != "" {
173- url += "%20AND%20id:" + nsxID
174+ encodedNsxId := url .QueryEscape (nsxID )
175+ uri += "%20AND%20id:" + encodedNsxId
174176 }
175177 if displayName != "" {
176- url += "%20AND%20display_name:" + displayName
178+ encodedDisplayName := url .QueryEscape (displayName )
179+ uri += "%20AND%20display_name:" + encodedDisplayName
177180 }
178181 if d .Get ("parent_path" ) != nil {
179182 parentPath := d .Get ("parent_path" ).(string )
180183 if parentPath != "" {
181- url += "%20AND%20parent_path:\" " + parentPath + "\" "
184+ uri += "%20AND%20parent_path:\" " + parentPath + "\" "
182185 }
183186 }
184187 if isProjectInfra {
185188 // If it is project/infra object, search in context of project
186- url += "&context=projects:" + "/orgs/" + nsxtClient .Config .OrgID + "/projects/" + nsxtClient .Config .ProjectID
189+ uri += "&context=projects:" + "/orgs/" + nsxtClient .Config .OrgID + "/projects/" + nsxtClient .Config .ProjectID
187190 } else if ! isInfraObject && ! isProjectInfra {
188191 // If not infra and project/infra object, search in context of VPC
189- url += "&context=vpcs:" + "/orgs/" + nsxtClient .Config .OrgID + "/projects/" + nsxtClient .Config .ProjectID + "/vpcs/" + nsxtClient .Config .VpcID
192+ uri += "&context=vpcs:" + "/orgs/" + nsxtClient .Config .OrgID + "/projects/" + nsxtClient .Config .ProjectID + "/vpcs/" + nsxtClient .Config .VpcID
190193 }
191- err := nsxtClient .NsxtSession .Get (url , & obj )
194+ err := nsxtClient .NsxtSession .Get (uri , & obj )
192195 if err != nil {
193196 return fmt .Errorf ("[ERROR] Search failed for object %s %v" , objType , err )
194197 } else {
@@ -223,10 +226,10 @@ func DatasourceRead(d *schema.ResourceData, meta interface{}, objType string, s
223226 }
224227 } else if objMap ["result_count" ].(float64 ) == 0 {
225228 // get the entity using listing api, for some VPC resources(eg VpcIpAddressAllocation), search API isn't indexed
226- url = ComputePolicyPath (d , objType , false , nsxtClient , true )
229+ uri = ComputePolicyPath (d , objType , false , nsxtClient , true )
227230 // keep maximum allowed page size for pagination
228- url += "?page_size=1000"
229- err := nsxtClient .NsxtSession .Get (url , & obj )
231+ uri += "?page_size=1000"
232+ err := nsxtClient .NsxtSession .Get (uri , & obj )
230233 if err != nil {
231234 return fmt .Errorf ("[ERROR] GET failed for object %s %v" , objType , err )
232235 } else {
@@ -284,8 +287,8 @@ func DatasourceRead(d *schema.ResourceData, meta interface{}, objType string, s
284287 break
285288 } else {
286289 // get next page using cursor
287- url += "&cursor=" + strconv .Itoa (cursor )
288- err = nsxtClient .NsxtSession .Get (url , & obj )
290+ uri += "&cursor=" + strconv .Itoa (cursor )
291+ err = nsxtClient .NsxtSession .Get (uri , & obj )
289292 if err != nil {
290293 return fmt .Errorf ("[ERROR] GET failed for object %s %v" , objType , err )
291294 } else {
@@ -316,21 +319,22 @@ func DatasourceReadForVM(d *schema.ResourceData, meta interface{}, objType strin
316319 displayName := d .Get ("display_name" ).(string )
317320 externalID := d .Get ("external_id" ).(string )
318321 powerState := d .Get ("power_state" ).(string )
319- var url string
322+ var uri string
320323
321324 // Get the object from NSX using hidden full text search API for specific VPC
322- url = nsxtClient .Config .BasePath + "/search?query=resource_type:" + objType
325+ uri = nsxtClient .Config .BasePath + "/search?query=resource_type:" + objType
323326 if externalID != "" {
324- url += "%20AND%20external_id:" + externalID
327+ uri += "%20AND%20external_id:" + externalID
325328 }
326329 if displayName != "" {
327- url += "%20AND%20display_name:" + displayName
330+ encodedDisplayName := url .QueryEscape (displayName )
331+ uri += "%20AND%20display_name:" + encodedDisplayName
328332 }
329333 if powerState != "" {
330- url += "%20AND%20power_state:" + powerState
334+ uri += "%20AND%20power_state:" + powerState
331335 }
332- url += "&context=vpcs:" + "/orgs/" + nsxtClient .Config .OrgID + "/projects/" + nsxtClient .Config .ProjectID + "/vpcs/" + nsxtClient .Config .VpcID
333- err := nsxtClient .NsxtSession .Get (url , & obj )
336+ uri += "&context=vpcs:" + "/orgs/" + nsxtClient .Config .OrgID + "/projects/" + nsxtClient .Config .ProjectID + "/vpcs/" + nsxtClient .Config .VpcID
337+ err := nsxtClient .NsxtSession .Get (uri , & obj )
334338 if err != nil {
335339 log .Printf ("[ERROR] Search failed for object %s %v\n " , objType , err )
336340 } else {
0 commit comments