Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tags): added method to get only mutable tags #829

Merged
merged 7 commits into from
Oct 21, 2021
50 changes: 50 additions & 0 deletions pkg/entities/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (e *Entities) GetTagsForEntity(guid common.EntityGUID) ([]*EntityTag, error
return e.GetTagsForEntityWithContext(context.Background(), guid)
}

// GetTagsForEntityMutable returns a collection of all tags (mutable only) for a given
// entity by entity GUID.
func (e *Entities) GetTagsForEntityMutable(guid common.EntityGUID) ([]*EntityTag, error) {
return e.GetTagsForEntityWithContextMutable(context.Background(), guid)
}

// GetTagsForEntityWithContext returns a collection of all tags (mutable and not) for a given
// entity by entity GUID.
func (e *Entities) GetTagsForEntityWithContext(ctx context.Context, guid common.EntityGUID) ([]*EntityTag, error) {
Expand All @@ -45,6 +51,50 @@ func (e *Entities) GetTagsForEntityWithContext(ctx context.Context, guid common.
return resp.Actor.Entity.Tags, nil
}

// GetTagsForEntityWithContextMutable returns a collection of all tags (mutable only) for a given
// entity by entity GUID.
func (e *Entities) GetTagsForEntityWithContextMutable(ctx context.Context, guid common.EntityGUID) ([]*EntityTag, error) {
resp := getTagsResponse{}
vars := map[string]interface{}{
"guid": guid,
}

if err := e.client.NerdGraphQueryWithContext(ctx, listTagsQuery, vars, &resp); err != nil {
return nil, err
}

return filterEntityTagMutable(resp)
}

// filterMutable removes tag values that are read-only from the received response.
func filterEntityTagMutable(resp getTagsResponse) ([]*EntityTag, error) {
var tags []*EntityTag

for _, responseTag := range resp.Actor.Entity.TagsWithMetadata {
if responseTag != nil {
tag := EntityTag{
Key: responseTag.Key,
}

mutable := 0
for _, responseTagValue := range responseTag.Values {
if responseTagValue.Mutable {
mutable++
tag.Values = append(tag.Values, responseTagValue.Value)
}
}

// All values were mutable
if len(responseTag.Values) == mutable {
tags = append(tags, &tag)
}

}
}

return tags, nil
}

// ListTags returns a collection of mutable tags for a given entity by entity GUID.
//
// Deprecated: Use GetTagsForEntity instead.
Expand Down