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

[Fleet] Allow to specify kibana.version when fetching categories #695

Merged
merged 2 commits into from
May 11, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Support filtering /categories using `kibana.version` query param [#695](https://github.com/elastic/package-registry/pull/695)

### Deprecated

### Known Issues
Expand Down
17 changes: 17 additions & 0 deletions categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strconv"
"time"

"github.com/Masterminds/semver/v3"

"github.com/elastic/package-registry/util"
)

Expand All @@ -33,6 +35,7 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func
query := r.URL.Query()
var experimental bool
var includePolicyTemplates bool
var kibanaVersion *semver.Version

// Read query filter params which can affect the output
if len(query) > 0 {
Expand All @@ -44,6 +47,14 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func
}
}

if v := query.Get("kibana.version"); v != "" {
kibanaVersion, err = semver.NewVersion(v)
if err != nil {
badRequest(w, fmt.Sprintf("invalid Kibana version '%s': %s", v, err))
return
}
}

if v := query.Get("include_policy_templates"); v != "" {
includePolicyTemplates, err = strconv.ParseBool(v)
if err != nil {
Expand All @@ -56,6 +67,12 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func
packageList := map[string]util.Package{}
// Get unique list of newest packages
for _, p := range packages {
// Check if the package is compatible with Kibana version
if kibanaVersion != nil {
if valid := p.HasKibanaVersion(kibanaVersion); !valid {
continue
}
}

// Skip internal packages
if p.Internal {
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestEndpoints(t *testing.T) {
{"/categories", "/categories", "categories.json", categoriesHandler(packagesBasePaths, testCacheTime)},
{"/categories?experimental=true", "/categories", "categories-experimental.json", categoriesHandler(packagesBasePaths, testCacheTime)},
{"/categories?experimental=foo", "/categories", "categories-experimental-error.json", categoriesHandler(packagesBasePaths, testCacheTime)},
{"/categories?experimental=true&kibana.version=6.5.2", "/categories", "categories-kibana652.json", categoriesHandler(packagesBasePaths, testCacheTime)},
{"/categories?include_policy_templates=true", "/categories", "categories-include-policy-templates.json", categoriesHandler(packagesBasePaths, testCacheTime)},
{"/categories?include_policy_templates=foo", "/categories", "categories-include-policy-templates-error.json", categoriesHandler(packagesBasePaths, testCacheTime)},
{"/search?kibana.version=6.5.2", "/search", "search-kibana652.json", searchHandler(packagesBasePaths, testCacheTime)},
Expand Down
32 changes: 32 additions & 0 deletions testdata/generated/categories-kibana652.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"id": "aws",
"title": "AWS",
"count": 1
},
{
"id": "containers",
"title": "Containers",
"count": 1
},
{
"id": "custom",
"title": "Custom",
"count": 6
},
{
"id": "message_queue",
"title": "Message Queue",
"count": 1
},
{
"id": "monitoring",
"title": "Monitoring",
"count": 1
},
{
"id": "web",
"title": "Web",
"count": 1
}
]