diff --git a/pkg/entities/tags.go b/pkg/entities/tags.go index af9ddd7e0..c714a707f 100644 --- a/pkg/entities/tags.go +++ b/pkg/entities/tags.go @@ -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) { @@ -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. diff --git a/pkg/entities/tags_integration_test.go b/pkg/entities/tags_integration_test.go index 15b22b304..532cba592 100644 --- a/pkg/entities/tags_integration_test.go +++ b/pkg/entities/tags_integration_test.go @@ -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()