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 filter to search by subscription #825

Closed
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func TestEndpoints(t *testing.T) {
{"/search?type=input&prerelease=true", "/search", "search-input-packages.json", searchHandler(indexer, testCacheTime)},
{"/search?type=input&package=integration_input&prerelease=true", "/search", "search-input-integration-package.json", searchHandler(indexer, testCacheTime)},
{"/search?type=integration&package=integration_input&prerelease=true", "/search", "search-integration-integration-package.json", searchHandler(indexer, testCacheTime)},
{"/search?elastic.subscription=basic&prerelease=true", "/search", "search-basic-subscription.json", searchHandler(indexer, testCacheTime)},
{"/search?elastic.subscription=gold&prerelease=true", "/search", "search-gold-subscription.json", searchHandler(indexer, testCacheTime)},
{"/search?package=example&elastic.subscription=basic&prerelease=true", "/search", "search-package-basic-subscription.json", searchHandler(indexer, testCacheTime)},
{"/search?package=example&elastic.subscription=gold&prerelease=true", "/search", "search-package-gold-subscription.json", searchHandler(indexer, testCacheTime)},
{"/favicon.ico", "", "favicon.ico", faviconHandleFunc},

// Removed flags, kept to ensure that they don't break requests from old versions.
Expand Down
43 changes: 42 additions & 1 deletion packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ type PolicyTemplate struct {
}

type Conditions struct {
Kibana *KibanaConditions `config:"kibana,omitempty" json:"kibana,omitempty" yaml:"kibana,omitempty"`
Kibana *KibanaConditions `config:"kibana,omitempty" json:"kibana,omitempty" yaml:"kibana,omitempty"`
Elastic *ElasticConditions `config:"elastic,omitempty" json:"elastic,omitempty" yaml"elastic,omitempty"`
}

// KibanaConditions defines conditions for Kibana (e.g. required version).
Expand All @@ -130,6 +131,38 @@ type KibanaConditions struct {
constraint *semver.Constraints
}

// ElasticConditions defines conditions related to Elastic subscriptions or partnerships.
type ElasticConditions struct {
Subscription string `config:"subscription" json:"subscription" yaml:"subscription"`
}

var elasticSubscriptions = map[string]int{
"basic": 1,
"gold": 2,
"platinum": 3,
"enterprise": 4,
}

// CheckSubscription returns true if the given subscription includes the subscription in the condition.
// It always returns true if there is no specified subscription.
// It returns false if any of the subscriptions are unknown.
func (ec *ElasticConditions) CheckSubscription(targetSubscription string) bool {
if ec.Subscription == "" {
return true
}

level, ok := elasticSubscriptions[ec.Subscription]
if !ok {
return false
}
targetLevel, ok := elasticSubscriptions[targetSubscription]
if !ok {
return false
}

return targetLevel >= level
}

type Version struct {
Min string `config:"min,omitempty" json:"min,omitempty"`
Max string `config:"max,omitempty" json:"max,omitempty"`
Expand Down Expand Up @@ -386,6 +419,14 @@ func isPrerelease(version *semver.Version) bool {
return version.Prerelease() != ""
}

func (p *Package) AllowedInSubscription(subscription string) bool {
if p.Conditions == nil || p.Conditions.Elastic == nil {
return true
}

return p.Conditions.Elastic.CheckSubscription(subscription)
}

// LoadAssets (re)loads all the assets of the package
// Based on the time when this is called, it might be that not all assets for a package exist yet, so it is reset every time.
func (p *Package) LoadAssets() (err error) {
Expand Down
53 changes: 53 additions & 0 deletions packages/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,59 @@ func TestHasKibanaVersion(t *testing.T) {
}
}

func TestElasticSubscriptionIncludes(t *testing.T) {
cases := []struct {
title string
querySubscription string
condition ElasticConditions
expected bool
}{
{
title: "no condition",
querySubscription: "basic",
condition: ElasticConditions{},
expected: true,
},
{
title: "same subscription",
querySubscription: "basic",
condition: ElasticConditions{Subscription: "basic"},
expected: true,
},
{
title: "subscription of higher level",
querySubscription: "gold",
condition: ElasticConditions{Subscription: "basic"},
expected: true,
},
{
title: "subscription of lower level",
querySubscription: "gold",
condition: ElasticConditions{Subscription: "enterprise"},
expected: false,
},
{
title: "invalid subscription in package",
querySubscription: "gold",
condition: ElasticConditions{Subscription: "invalid"},
expected: false,
},
{
title: "invalid target subscription",
querySubscription: "invalid",
condition: ElasticConditions{Subscription: "enterprise"},
expected: false,
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
supported := c.condition.CheckSubscription(c.querySubscription)
assert.Equal(t, c.expected, supported)
})
}
}

func TestNewPackageFromPath(t *testing.T) {
packagePath := "../testdata/package/reference/1.0.0"
absPath, err := filepath.Abs(packagePath)
Expand Down
5 changes: 5 additions & 0 deletions packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type Filter struct {
PackageName string
PackageVersion string
PackageType string
Subscription string

// Deprecated, release tags to be removed.
Experimental bool
Expand Down Expand Up @@ -290,6 +291,10 @@ func (f *Filter) Apply(ctx context.Context, packages Packages) Packages {
}
}

if f.Subscription != "" && !p.AllowedInSubscription(f.Subscription) {
continue
}

if f.PackageName != "" && f.PackageName != p.Name {
continue
}
Expand Down
6 changes: 6 additions & 0 deletions packages/testdata/marshaler/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,9 @@
"conditions": {
"kibana": {
"version": "^7.16.0 || ^8.0.0"
},
"elastic": {
"subscription": "gold"
}
},
"owner": {
Expand Down Expand Up @@ -1544,6 +1547,9 @@
"conditions": {
"kibana": {
"version": "\u003e6.7.0"
},
"elastic": {
"subscription": "gold"
}
},
"categories": [
Expand Down
4 changes: 4 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func newSearchFilterFromQuery(query url.Values) (*packages.Filter, error) {
}
}

if v := query.Get("elastic.subscription"); v != "" {
filter.Subscription = v
}

if v := query.Get("category"); v != "" {
filter.Category = v
}
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/example/1.2.0-rc1/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"conditions": {
"kibana": {
"version": "^7.16.0 || ^8.0.0"
},
"elastic": {
"subscription": "gold"
}
},
"owner": {
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/longdocs/1.0.4/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"conditions": {
"kibana": {
"version": ">6.7.0"
},
"elastic": {
"subscription": "gold"
}
},
"categories": [
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/search-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@
"conditions": {
"kibana": {
"version": ">6.7.0"
},
"elastic": {
"subscription": "gold"
}
},
"categories": [
Expand Down
Loading