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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions pkg/entities/tags_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ func TestIntegrationListTags(t *testing.T) {
require.Greater(t, len(actual), 0)
}

func TestIntegrationGetTagsForEntity(t *testing.T) {
t.Parallel()

var (
// GUID of Dummy App
testGUID = common.EntityGUID("MjUyMDUyOHxBUE18QVBQTElDQVRJT058MjE1MDM3Nzk1")
)

client := newIntegrationTestClient(t)

actual, err := client.GetTagsForEntity(testGUID)
require.NoError(t, err)
require.Greater(t, len(actual), 0)

actual, err = client.GetTagsForEntityMutable(testGUID)
require.NoError(t, err)
require.Greater(t, len(actual), 0)
}

func TestIntegrationTaggingAddTagsToEntity(t *testing.T) {
t.Parallel()

Expand Down