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

Implement TagsWithMetadata to avoid non-modifiable result sets #462

Merged
merged 2 commits into from
Sep 10, 2020
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
15 changes: 15 additions & 0 deletions pkg/entities/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ const (
reporting
type
`
graphqlEntityStructTagsFields = `
tagsWithMetadata {
key
values {
mutable
value
}
}
tags {
key
values
}
`

graphqlApmApplicationEntityFields = `
... on ApmApplicationEntity {
Expand Down Expand Up @@ -275,13 +288,15 @@ const (

getEntitiesQuery = `query($guids: [String!]!) { actor { entities(guids: $guids) {` +
graphqlEntityStructFields +
graphqlEntityStructTagsFields +
graphqlApmApplicationEntityFields +
graphqlBrowserApplicationEntityFields +
graphqlMobileApplicationEntityFields +
` } } }`

getEntityQuery = `query($guid: String!) { actor { entity(guid: $guid) {` +
graphqlEntityStructFields +
graphqlEntityStructTagsFields +
graphqlApmApplicationEntityFields +
graphqlBrowserApplicationEntityFields +
graphqlMobileApplicationEntityFields +
Expand Down
64 changes: 51 additions & 13 deletions pkg/entities/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,36 @@ func (e *Entities) ListTags(guid string) ([]*Tag, error) {
return nil, err
}

return resp.Actor.Entity.Tags, nil
return filterMutable(resp)
}

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

for _, responseTag := range resp.Actor.Entity.TagsWithMetadata {
if responseTag != nil {
tag := Tag{
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
}

// AddTags writes tags to the entity specified by the provided entity GUID.
Expand Down Expand Up @@ -121,23 +150,32 @@ func parseTagMutationErrors(errors []tagMutationError) string {
return strings.Join(messages, ", ")
}

// EntityTagValueWithMetadata - The value and metadata of a single entity tag.
type EntityTagValueWithMetadata struct {
// Whether or not the tag can be mutated by the user.
Mutable bool `json:"mutable"`
// The tag value.
Value string `json:"value"`
}

// EntityTagWithMetadata - The tags with metadata of the entity.
type EntityTagWithMetadata struct {
// The tag's key.
Key string `json:"key"`
// A list of tag values with metadata information.
Values []EntityTagValueWithMetadata `json:"values"`
}

var listTagsQuery = `
query($guid:EntityGuid!) {
actor {
entity(guid: $guid) {
tags {
values
key
}
}
}
}
`
query($guid:EntityGuid!) { actor { entity(guid: $guid) {` +
graphqlEntityStructTagsFields +
` } } }`

type listTagsResponse struct {
Actor struct {
Entity struct {
Tags []*Tag
Tags []*Tag
TagsWithMetadata []*EntityTagWithMetadata
}
}
}
Expand Down