Skip to content

Commit

Permalink
Merge pull request #685 from kaiyan-sheng/input_groups
Browse files Browse the repository at this point in the history
Add input groups support
  • Loading branch information
kaiyan-sheng authored Apr 14, 2021
2 parents 721d933 + ab574d3 commit 453ff36
Show file tree
Hide file tree
Showing 50 changed files with 2,333 additions and 54 deletions.
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

* Add input groups support. [#685] (https://github.com/elastic/package-registry/pull/685)

### Deprecated

### Known Issues
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ The `/search` API endpoint has few additional query parameters. More might be ad
a package requires 7.4, the package will not be returned or an older compatible package will be shown.
By default this endpoint always returns only the newest compatible package.
* category: Filters the package by the given category. Available categories can be seend when going to `/categories` endpoint.
* package: Filters by a specific package name, for example `mysql`. Returns the most recent version.
* package: Filters by a specific package name, for example `mysql`. Returns the most recent version.
* internal: This can be set to true, to also list internal packages. This is set to `false` by default.
* all: This can be set to true to list all package versions. This is set to `false` by default.
* experimental: This can be set to true to list packages considered to be experimental. This is set to `false` by default.

The different query parameters above can be combined, so `?package=mysql&kibana=7.3.0` will return all mysql package versions
which are compatible with `7.3.0`.

The `/categories` API endpoint has two additional query parameters.

* experimental: This can be set to true to list categories from experimental packages. This is set to `false` by default.
* include_policy_templates: This can be set to true to include categories from policy templates. This is set to `false` by default.

## Package structure

The structure of each package is standardised. It looks as following:
Expand Down Expand Up @@ -196,4 +201,4 @@ CI automatically creates a new Docker image which will be available under `docke
After the new registry Docker image is available, update the following projects that consume it:
- Integrations: Update the version of the Package Registry Docker image as shown in this [sample PR](https://github.com/elastic/integrations/pull/581).
- Package Storage: Follow the [documentation](https://github.com/elastic/package-storage#update-package-registry-for-a-distribution) on how to update the distributions.
- Kibana: Do this only after all Package Storage distributions have been updated and released as new Docker images. Update the version of the Package Registry Distribution Docker image as shown in this [sample PR](https://github.com/elastic/kibana/pull/89776).
- Kibana: Do this only after all Package Storage distributions have been updated and released as new Docker images. Update the version of the Package Registry Distribution Docker image as shown in this [sample PR](https://github.com/elastic/kibana/pull/89776).
36 changes: 36 additions & 0 deletions categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func

query := r.URL.Query()
var experimental bool
var includePolicyTemplates bool

// Read query filter params which can affect the output
if len(query) > 0 {
if v := query.Get("experimental"); v != "" {
Expand All @@ -41,6 +43,14 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func
return
}
}

if v := query.Get("include_policy_templates"); v != "" {
includePolicyTemplates, err = strconv.ParseBool(v)
if err != nil {
badRequest(w, fmt.Sprintf("invalid 'include_policy_templates' query param: '%s'", v))
return
}
}
}

packageList := map[string]util.Package{}
Expand Down Expand Up @@ -81,6 +91,32 @@ func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func

categories[c].Count = categories[c].Count + 1
}

if includePolicyTemplates {
for _, t := range p.PolicyTemplates {
// Skip when policy template level `categories` is empty and there is only one policy template
if t.Categories == nil && len(p.PolicyTemplates) == 1 {
break
}

for _, c := range p.Categories {
categories[c].Count = categories[c].Count + 1
}

// Add policy template level categories.
for _, c := range t.Categories {
if _, ok := categories[c]; !ok {
categories[c] = &Category{
Id: c,
Title: c,
Count: 0,
}
}

categories[c].Count = categories[c].Count + 1
}
}
}
}

data, err := getCategoriesOutput(categories)
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ 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?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)},
{"/search?kibana.version=7.2.1", "/search", "search-kibana721.json", searchHandler(packagesBasePaths, testCacheTime)},
{"/search?category=web", "/search", "search-category-web.json", searchHandler(packagesBasePaths, testCacheTime)},
Expand Down
7 changes: 6 additions & 1 deletion testdata/generated/categories-experimental.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
{
"id": "aws",
"title": "AWS",
"count": 1
"count": 2
},
{
"id": "azure",
"title": "Azure",
"count": 1
},
{
"id": "cloud",
"title": "Cloud",
"count": 1
},
{
"id": "containers",
"title": "Containers",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid 'include_policy_templates' query param: 'foo'
52 changes: 52 additions & 0 deletions testdata/generated/categories-include-policy-templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"id": "aws",
"title": "AWS",
"count": 2
},
{
"id": "azure",
"title": "Azure",
"count": 1
},
{
"id": "cloud",
"title": "Cloud",
"count": 2
},
{
"id": "compute",
"title": "compute",
"count": 1
},
{
"id": "containers",
"title": "Containers",
"count": 1
},
{
"id": "crm",
"title": "CRM",
"count": 1
},
{
"id": "custom",
"title": "Custom",
"count": 13
},
{
"id": "message_queue",
"title": "Message Queue",
"count": 1
},
{
"id": "monitoring",
"title": "Monitoring",
"count": 1
},
{
"id": "web",
"title": "Web",
"count": 3
}
]
10 changes: 10 additions & 0 deletions testdata/generated/categories.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
[
{
"id": "aws",
"title": "AWS",
"count": 1
},
{
"id": "azure",
"title": "Azure",
"count": 1
},
{
"id": "cloud",
"title": "Cloud",
"count": 1
},
{
"id": "containers",
"title": "Containers",
Expand Down
Loading

0 comments on commit 453ff36

Please sign in to comment.