From 901273c8a609fcb98cea71299b3a0ba00be1cc62 Mon Sep 17 00:00:00 2001 From: Dainius Date: Fri, 10 Apr 2020 08:58:59 +0300 Subject: [PATCH] Add fix for vapp.RemoveNetwork() and vapp.RemoveNetworkAsync() (#299) * Use HTTP DELETE call instead of vApp network update to remove vApp networks --- CHANGELOG.md | 2 ++ govcd/vapp.go | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21eee7af5..5fa87d2cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ NOTES: BUGS FIXED: * Fix issue in Queries with vCD 10 version, which do not return network pool or provider VDC[#293](https://github.com/vmware/go-vcloud-director/pull/293) * Session timeout for media, catalog item upload [#294](https://github.com/vmware/go-vcloud-director/pull/294) +* Fix `vapp.RemoveNetwork`, `vapp.RemoveNetworkAsync` to use `DELETE` API call instead of update + which can apply incorrect remaining vApp network configurations [#299](https://github.com/vmware/go-vcloud-director/pull/299) ## 2.6.0 (March 13, 2010) diff --git a/govcd/vapp.go b/govcd/vapp.go index 2bc99972b..066c6c4fa 100644 --- a/govcd/vapp.go +++ b/govcd/vapp.go @@ -1194,24 +1194,25 @@ func (vapp *VApp) RemoveNetworkAsync(identifier string) (Task, error) { } networkConfigurations := vapp.VApp.NetworkConfigSection.NetworkConfig - isNetworkFound := false - for index, networkConfig := range networkConfigurations { + for _, networkConfig := range networkConfigurations { networkId, err := GetUuidFromHref(networkConfig.Link.HREF, false) if err != nil { return Task{}, fmt.Errorf("unable to get network ID from HREF: %s", err) } if networkId == identifier || networkConfig.NetworkName == identifier { - isNetworkFound = true - networkConfigurations = append(networkConfigurations[:index], networkConfigurations[index+1:]...) - break + deleteUrl := vapp.client.VCDHREF.String() + "/network/" + networkId + errMessage := fmt.Sprintf("detaching vApp network %s (id '%s'): %%s", networkConfig.NetworkName, networkId) + task, err := vapp.client.ExecuteTaskRequest(deleteUrl, http.MethodDelete, types.AnyXMLMime, errMessage, nil) + if err != nil { + return Task{}, err + } + + return task, nil } } - if !isNetworkFound { - return Task{}, fmt.Errorf("network to remove %s, wasn't found", identifier) - } + return Task{}, fmt.Errorf("network to remove %s, wasn't found", identifier) - return updateNetworkConfigurations(vapp, networkConfigurations) } // Removes vApp isolated network