Remove empty tags 2 (#16113)

* Add support for RemoveEmptyTags in API client

* Add changelog

---------

Co-authored-by: Rémi Lapeyre <remi.lapeyre@lenstra.fr>
This commit is contained in:
Dhia Ayachi 2023-02-06 14:12:43 -05:00 committed by GitHub
parent c680a35b36
commit e42ab7e429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

3
.changelog/14244.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
client: add support for RemoveEmptyTags in Prepared Queries templates.
```

View File

@ -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.

View File

@ -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)
}
}