vmware / go-vcloud-director

Golang SDK for VMware Cloud Director
Other
76 stars 79 forks source link

Patch XML namespace problem for vm. UpdateNetworkConnectionSection #677

Closed Didainius closed 2 months ago

Didainius commented 2 months ago

Closes #429

Problem

In some cases, vm.UpdateNetworkConnectionSection fails with error similar toHTTP 400 Bad Request - cvc-elt.1.a: Cannot find the declaration of element 'NetworkConnectionSection'

The cause

The vm.UpdateNetworkConnectionSection at first retrieves existing network configuration (vm.GetNetworkConnectionSection) and the modifies it.

In some environments, there is a problem that in some cases VCD randomly (not always) puts the payload for GET query into ns2 namespace.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:NetworkConnectionSection xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ns2="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:common=......

For reference, the regular normal payload that this call returns (not wrapped into ns2 namespace)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<NetworkConnectionSection xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:vssd="[http://schemas.dmtf.org/wbem/wscim/1/cim-schema/](http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData).....

Because Go XML library has issues with namespace handling (https://github.com/golang/go/issues/9519), the Xmlns attribute value http://www.vmware.com/vcloud/v1.5 gets lost in translation and this causes the described error when update occurs.

The fix

The fix is to set hardcoded setting for Xmlns to http://www.vmware.com/vcloud/v1.5 for every update call. This will prevent malformed XML document query.

Testing

I have triggered tests that include call to the methodvm.UpdateNetworkConnectionSection and none of them failed: