Skip to content

Commit

Permalink
api: tags error handling
Browse files Browse the repository at this point in the history
Checks if `err` is not `nil` after calling `c.Do`. If an error occurs, it returns `nil` and the error. If no error occurs, it returns the `nil` or the result and `nil` depending on the function.

This ensures that the functions handle errors gracefully and avoids unspecified behavior during the return process.

Ref: #3112

Signed-off-by: Ryan Johnson <ryan.johnson@broadcom.com>
  • Loading branch information
tenthirtyam committed Oct 7, 2024
1 parent 5aff45e commit b7305f5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
26 changes: 21 additions & 5 deletions vapi/tags/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func (c *Manager) CreateCategory(ctx context.Context, category *Category) (strin
}
url := c.Resource(internal.CategoryPath)
var res string
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil {
return "", err
}
return res, nil
}

// UpdateCategory updates one or more of the AssociableTypes, Cardinality,
Expand All @@ -108,13 +111,20 @@ func (c *Manager) UpdateCategory(ctx context.Context, category *Category) error
},
}
url := c.Resource(internal.CategoryPath).WithID(category.ID)
return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
req := url.Request(http.MethodPatch, spec)
if err := c.Do(ctx, req, nil); err != nil {
return err
}
return nil
}

// DeleteCategory deletes a category.
func (c *Manager) DeleteCategory(ctx context.Context, category *Category) error {
url := c.Resource(internal.CategoryPath).WithID(category.ID)
return c.Do(ctx, url.Request(http.MethodDelete), nil)
if err := c.Do(ctx, url.Request(http.MethodDelete), nil); err != nil {
return err
}
return nil
}

// GetCategory fetches the category information for the given identifier.
Expand All @@ -134,14 +144,20 @@ func (c *Manager) GetCategory(ctx context.Context, id string) (*Category, error)
}
url := c.Resource(internal.CategoryPath).WithID(id)
var res Category
return &res, c.Do(ctx, url.Request(http.MethodGet), &res)
if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil {
return nil, err
}
return &res, nil
}

// ListCategories returns all category IDs in the system.
func (c *Manager) ListCategories(ctx context.Context) ([]string, error) {
url := c.Resource(internal.CategoryPath)
var res []string
return res, c.Do(ctx, url.Request(http.MethodGet), &res)
if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil {
return nil, err
}
return res, nil
}

// GetCategories fetches a list of category information in the system.
Expand Down
34 changes: 26 additions & 8 deletions vapi/tags/tag_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (c *Manager) AttachTag(ctx context.Context, tagID string, ref mo.Reference)
}
spec := internal.NewAssociation(ref)
url := c.Resource(internal.AssociationPath).WithID(id).WithAction("attach")
return c.Do(ctx, url.Request(http.MethodPost, spec), nil)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), nil); err != nil {
return err
}
return nil
}

// DetachTag detaches a tag ID from a managed object.
Expand All @@ -57,7 +60,10 @@ func (c *Manager) DetachTag(ctx context.Context, tagID string, ref mo.Reference)
}
spec := internal.NewAssociation(ref)
url := c.Resource(internal.AssociationPath).WithID(id).WithAction("detach")
return c.Do(ctx, url.Request(http.MethodPost, spec), nil)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), nil); err != nil {
return err
}
return nil
}

// batchResponse is the response type used by attach/detach operations which
Expand Down Expand Up @@ -93,7 +99,10 @@ func (c *Manager) AttachTagToMultipleObjects(ctx context.Context, tagID string,
}{ids}

url := c.Resource(internal.AssociationPath).WithID(id).WithAction("attach-tag-to-multiple-objects")
return c.Do(ctx, url.Request(http.MethodPost, spec), nil)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), nil); err != nil {
return err
}
return nil
}

// AttachMultipleTagsToObject attaches multiple tag IDs to a managed object.
Expand All @@ -103,7 +112,7 @@ func (c *Manager) AttachTagToMultipleObjects(ctx context.Context, tagID string,
// not successfully attached to the managed object reference it might leave the
// managed object reference in a partially tagged state and needs to be resolved
// by the caller. In this case BatchErrors is returned and can be used to
// analyse failure reasons on each failed tag.
// analyze failure reasons on each failed tag.
//
// Specified tagIDs must use URN-notation instead of display names or a generic
// error will be returned and no tagging operation will be performed. If the
Expand Down Expand Up @@ -156,7 +165,7 @@ func (c *Manager) AttachMultipleTagsToObject(ctx context.Context, tagIDs []strin
// not successfully detached from the managed object reference it might leave
// the managed object reference in a partially tagged state and needs to be
// resolved by the caller. In this case BatchErrors is returned and can be used
// to analyse failure reasons on each failed tag.
// to analyze failure reasons on each failed tag.
//
// Specified tagIDs must use URN-notation instead of display names or a generic
// error will be returned and no tagging operation will be performed. If the
Expand Down Expand Up @@ -207,7 +216,10 @@ func (c *Manager) ListAttachedTags(ctx context.Context, ref mo.Reference) ([]str
spec := internal.NewAssociation(ref)
url := c.Resource(internal.AssociationPath).WithAction("list-attached-tags")
var res []string
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil {
return nil, err
}
return res, nil
}

// GetAttachedTags fetches the array of tags attached to the given object.
Expand Down Expand Up @@ -291,7 +303,10 @@ func (c *Manager) ListAttachedObjectsOnTags(ctx context.Context, tagID []string)

url := c.Resource(internal.AssociationPath).WithAction("list-attached-objects-on-tags")
var res []AttachedObjects
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil {
return nil, err
}
return res, nil
}

// GetAttachedObjectsOnTags combines ListAttachedObjectsOnTags and populates each Tag field.
Expand Down Expand Up @@ -362,7 +377,10 @@ func (c *Manager) ListAttachedTagsOnObjects(ctx context.Context, objectID []mo.R

url := c.Resource(internal.AssociationPath).WithAction("list-attached-tags-on-objects")
var res []AttachedTags
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil {
return nil, err
}
return res, nil
}

// GetAttachedTagsOnObjects calls ListAttachedTagsOnObjects and populates each Tags field.
Expand Down
31 changes: 24 additions & 7 deletions vapi/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ func (c *Manager) CreateTag(ctx context.Context, tag *Tag) (string, error) {
}
url := c.Resource(internal.TagPath)
var res string
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil {
return "", err
}
return res, nil
}

// UpdateTag can update one or both of the tag Description and Name fields.
Expand All @@ -106,13 +109,19 @@ func (c *Manager) UpdateTag(ctx context.Context, tag *Tag) error {
},
}
url := c.Resource(internal.TagPath).WithID(tag.ID)
return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
if err := c.Do(ctx, url.Request(http.MethodPatch, spec), nil); err != nil {
return err
}
return nil
}

// DeleteTag deletes an existing tag.
func (c *Manager) DeleteTag(ctx context.Context, tag *Tag) error {
url := c.Resource(internal.TagPath).WithID(tag.ID)
return c.Do(ctx, url.Request(http.MethodDelete), nil)
if err := c.Do(ctx, url.Request(http.MethodDelete), nil); err != nil {
return err
}
return nil
}

// GetTag fetches the tag information for the given identifier.
Expand All @@ -133,8 +142,10 @@ func (c *Manager) GetTag(ctx context.Context, id string) (*Tag, error) {

url := c.Resource(internal.TagPath).WithID(id)
var res Tag
return &res, c.Do(ctx, url.Request(http.MethodGet), &res)

if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil {
return nil, err
}
return &res, nil
}

// GetTagForCategory fetches the tag information for the given identifier in the given category.
Expand Down Expand Up @@ -165,7 +176,10 @@ func (c *Manager) GetTagForCategory(ctx context.Context, id, category string) (*
func (c *Manager) ListTags(ctx context.Context) ([]string, error) {
url := c.Resource(internal.TagPath)
var res []string
return res, c.Do(ctx, url.Request(http.MethodGet), &res)
if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil {
return nil, err
}
return res, nil
}

// GetTags fetches an array of tag information in the system.
Expand Down Expand Up @@ -203,7 +217,10 @@ func (c *Manager) ListTagsForCategory(ctx context.Context, id string) ([]string,
}{id}
url := c.Resource(internal.TagPath).WithID(id).WithAction("list-tags-for-category")
var res []string
return res, c.Do(ctx, url.Request(http.MethodPost, body), &res)
if err := c.Do(ctx, url.Request(http.MethodPost, body), &res); err != nil {
return nil, err
}
return res, nil
}

// The id parameter can be a Category ID or Category Name.
Expand Down

0 comments on commit b7305f5

Please sign in to comment.