Skip to content

Commit 4ac1946

Browse files
authored
Fix catalog delete (#392)
* Fix Issue #390: catalog.Delete() ignores task Signed-off-by: Giuseppe Maxia <gmaxia@vmware.com>
1 parent b6c91e5 commit 4ac1946

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

.changes/v2.13.0/392-bug-fixes.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* Fix Issue #390: `catalog.Delete()` ignores returned task and responds immediately which could have caused failures [GH-392]
2+

govcd/admincatalog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewAdminCatalog(client *Client) *AdminCatalog {
2929
}
3030
}
3131

32-
// Deletes the Catalog, returning an error if the vCD call fails.
32+
// Delete deletes the Catalog, returning an error if the vCD call fails.
3333
// Link to API call: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/DELETE-Catalog.html
3434
func (adminCatalog *AdminCatalog) Delete(force, recursive bool) error {
3535
catalog := NewCatalog(adminCatalog.client)

govcd/catalog.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewCatalog(client *Client) *Catalog {
4242
}
4343
}
4444

45-
// Deletes the Catalog, returning an error if the vCD call fails.
45+
// Delete deletes the Catalog, returning an error if the vCD call fails.
4646
// Link to API call: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/DELETE-Catalog.html
4747
func (catalog *Catalog) Delete(force, recursive bool) error {
4848

@@ -52,7 +52,7 @@ func (catalog *Catalog) Delete(force, recursive bool) error {
5252
return err
5353
}
5454
if catalogID == "" {
55-
return fmt.Errorf("empty ID returned for catalog ID %s", catalog.Catalog.ID)
55+
return fmt.Errorf("empty ID returned for catalog %s", catalog.Catalog.Name)
5656
}
5757
adminCatalogHREF.Path += "/admin/catalog/" + catalogID
5858

@@ -61,13 +61,18 @@ func (catalog *Catalog) Delete(force, recursive bool) error {
6161
"recursive": strconv.FormatBool(recursive),
6262
}, http.MethodDelete, adminCatalogHREF, nil)
6363

64-
_, err = checkResp(catalog.client.Http.Do(req))
65-
64+
resp, err := checkResp(catalog.client.Http.Do(req))
6665
if err != nil {
67-
return fmt.Errorf("error deleting Catalog %s: %s", catalog.Catalog.ID, err)
66+
return fmt.Errorf("error deleting Catalog %s: %s", catalog.Catalog.Name, err)
6867
}
69-
70-
return nil
68+
task := NewTask(catalog.client)
69+
if err = decodeBody(types.BodyTypeXML, resp, task.Task); err != nil {
70+
return fmt.Errorf("error decoding task response: %s", err)
71+
}
72+
if task.Task.Status == "error" {
73+
return fmt.Errorf(combinedTaskErrorMessage(task.Task, fmt.Errorf("catalog %s not properly destroyed", catalog.Catalog.Name)))
74+
}
75+
return task.WaitTaskCompletion()
7176
}
7277

7378
// Envelope is a ovf description root element. File contains information for vmdk files.

govcd/catalog_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ func (vcd *TestVCD) Test_DeleteCatalog(check *C) {
131131
// If something fails after this point, the entity will be removed
132132
AddToCleanupList(TestDeleteCatalog, "catalog", vcd.config.VCD.Org, "Test_DeleteCatalog")
133133
check.Assert(adminCatalog.AdminCatalog.Name, Equals, TestDeleteCatalog)
134+
135+
checkUploadOvf(vcd, check, vcd.config.OVA.OvaPath, TestDeleteCatalog, TestUploadOvf)
136+
err = adminCatalog.Delete(false, false)
137+
check.Assert(err, NotNil)
138+
// Catalog is not empty. An attempt to delete without recursion will fail
139+
check.Assert(strings.Contains(err.Error(), "You must remove"), Equals, true)
140+
134141
err = adminCatalog.Delete(true, true)
135142
check.Assert(err, IsNil)
136143
doesCatalogExist(check, org)
@@ -317,14 +324,14 @@ func countFolders() int {
317324
}
318325

319326
func checkUploadOvf(vcd *TestVCD, check *C, ovaFileName, catalogName, itemName string) {
320-
catalog, org := findCatalog(vcd, check, vcd.config.VCD.Catalog.Name)
327+
catalog, org := findCatalog(vcd, check, catalogName)
321328

322329
uploadTask, err := catalog.UploadOvf(ovaFileName, itemName, "upload from test", 1024)
323330
check.Assert(err, IsNil)
324331
err = uploadTask.WaitTaskCompletion()
325332
check.Assert(err, IsNil)
326333

327-
AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+vcd.config.VCD.Catalog.Name, "Test_UploadOvf")
334+
AddToCleanupList(itemName, "catalogItem", vcd.org.Org.Name+"|"+catalogName, "checkUploadOvf")
328335

329336
catalog, err = org.GetCatalogByName(catalogName, false)
330337
check.Assert(err, IsNil)
@@ -772,9 +779,6 @@ func cleanupCatalogOrgVdc(check *C, sharedCatalog Catalog, vdc *Vdc, vcd *TestVC
772779
err := sharedCatalog.Delete(true, true)
773780
check.Assert(err, IsNil)
774781

775-
// There are cases where it just takes a a few seconds after catalog deletion when one can delete VDC
776-
time.Sleep(2 * time.Second)
777-
778782
err = vdc.DeleteWait(true, true)
779783
check.Assert(err, IsNil)
780784

govcd/catalogitem.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (catalogItem *CatalogItem) GetVAppTemplate() (VAppTemplate, error) {
3737

3838
}
3939

40-
// Deletes the Catalog Item, returning an error if the vCD call fails.
40+
// Delete deletes the Catalog Item, returning an error if the vCD call fails.
4141
// Link to API call: https://code.vmware.com/apis/220/vcloud#/doc/doc/operations/DELETE-CatalogItem.html
4242
func (catalogItem *CatalogItem) Delete() error {
4343
util.Logger.Printf("[TRACE] Deleting catalog item: %#v", catalogItem.CatalogItem)

0 commit comments

Comments
 (0)