From c2670e13e4ee02c4589fcf8b3f3ad4d96cf26aac Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 10 May 2021 10:09:45 -0400 Subject: [PATCH 1/2] [Fleet] Allow to specify kibana.version when fetching categories --- categories.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/categories.go b/categories.go index 39419ca89..8c0260061 100644 --- a/categories.go +++ b/categories.go @@ -12,6 +12,8 @@ import ( "strconv" "time" + "github.com/Masterminds/semver/v3" + "github.com/elastic/package-registry/util" ) @@ -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 { @@ -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 { @@ -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 { From 1db74edcca0ee9ced1ae1dc7312d210c2096bd64 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 11 May 2021 08:31:18 -0400 Subject: [PATCH 2/2] Add tests and update changelog --- CHANGELOG.md | 2 ++ main_test.go | 1 + testdata/generated/categories-kibana652.json | 32 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 testdata/generated/categories-kibana652.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 526d2ac69..a263a7e53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/main_test.go b/main_test.go index 7e3c18027..e8ba49c5a 100644 --- a/main_test.go +++ b/main_test.go @@ -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)}, diff --git a/testdata/generated/categories-kibana652.json b/testdata/generated/categories-kibana652.json new file mode 100644 index 000000000..dd169a964 --- /dev/null +++ b/testdata/generated/categories-kibana652.json @@ -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 + } +] \ No newline at end of file