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

Add support for RemoveEmptyTags in API client #14244

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .changelog/14244.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
client: add support for RemoveEmptyTags in Prepared Queries templates.
```
6 changes: 6 additions & 0 deletions api/prepared_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ type QueryTemplate struct {
// Regexp allows specifying a regex pattern to match against the name
// of the query being executed.
Regexp string

// RemoveEmptyTags if set to true, will cause the Tags list inside
// the Service structure to be stripped of any empty strings. This is useful
// when interpolating into tags in a way where the tag is optional, and
// where searching for an empty tag would yield no results from the query.
RemoveEmptyTags bool
}

// PreparedQueryDefinition defines a complete prepared query.
Expand Down
52 changes: 52 additions & 0 deletions api/prepared_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,55 @@ func TestAPI_PreparedQuery(t *testing.T) {
t.Fatalf("bad: %v", defs)
}
}

func TestAPI_PreparedQueryRemoveEmptyTags(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
defer s.Stop()

def := &PreparedQueryDefinition{
Name: "test",
Service: ServiceQuery{
Service: "redis",
},
Template: QueryTemplate{
RemoveEmptyTags: false,
},
}

query := c.PreparedQuery()
var err error
def.ID, _, err = query.Create(def, nil)
if err != nil {
t.Fatalf("err: %s", err)
}

queries, _, err := query.Get(def.ID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(queries) != 1 {
t.Fatalf("wrong length: %#v", queries)
}
if queries[0].Template.RemoveEmptyTags {
t.Fatalf("wrong value: %v", queries[0].Template.RemoveEmptyTags)
}

def.Template.RemoveEmptyTags = true
_, err = query.Update(def, nil)
if err != nil {
t.Fatalf("err: %s", err)
}

queries, _, err = query.Get(def.ID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(queries) != 1 {
t.Fatalf("wrong length: %#v", queries)
}
if !queries[0].Template.RemoveEmptyTags {
t.Fatalf("wrong value: %v", queries[0].Template.RemoveEmptyTags)
}

}