From dad16afe966e670d65295c681a519e85a782af10 Mon Sep 17 00:00:00 2001 From: Nicolas Ruflin Date: Thu, 24 Oct 2019 14:08:51 +0200 Subject: [PATCH] Adjust API output for Kibana compatibility version (#134) Previously the json output for the compatible Kibana version looked as following: ``` "kibana": { "version.min": "7.0.0" } ``` As this causes troubles on the JS side, it is changed to: ``` "kibana": { "version": { "min": "7.0.0" } } ``` This should make it easier to parse. To make it for package creators still possible to use the short notation in the manifest file, go-ucfg is used to read the manifest file so we get this functioanlity for free. An additional change which happened in this PR is that if a max or min version are empty, it is not part of the API output anymore. Closes https://github.com/elastic/integrations-registry/issues/44 --- CHANGELOG.md | 3 ++ docs/api/package.json | 5 ++- testdata/package/example-0.0.2/index.json | 5 ++- testdata/package/example-1.0.0/index.json | 5 ++- testdata/package/foo-1.0.0/index.json | 5 ++- testdata/package/internal-1.2.0/index.json | 3 +- util/package.go | 48 +++++++++++----------- util/package_test.go | 18 +++++--- 8 files changed, 53 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2995e012e..68ad21b48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Breaking changes +* Package Kibana compatiblity version is changed to `"kibana": { "max": "1.2.3"}` [#134](https://github.com/elastic/integrations-registry/pull/134) + + ### Bugfixes * Change empty /search API output from `null` to `[]`. [#111](https://github.com/elastic/integrations-registry/pull/111) diff --git a/docs/api/package.json b/docs/api/package.json index 14f3b00a1..7084554cf 100644 --- a/docs/api/package.json +++ b/docs/api/package.json @@ -11,8 +11,9 @@ ], "requirement": { "kibana": { - "version.min": "7.0.0", - "version.max": "" + "version": { + "min": "7.0.0" + } } }, "screenshots": [ diff --git a/testdata/package/example-0.0.2/index.json b/testdata/package/example-0.0.2/index.json index 1c1461798..5cad8ca6b 100644 --- a/testdata/package/example-0.0.2/index.json +++ b/testdata/package/example-0.0.2/index.json @@ -9,8 +9,9 @@ ], "requirement": { "kibana": { - "version.min": "6.0.0", - "version.max": "" + "version": { + "min": "6.0.0" + } } }, "assets": [ diff --git a/testdata/package/example-1.0.0/index.json b/testdata/package/example-1.0.0/index.json index 14f3b00a1..7084554cf 100644 --- a/testdata/package/example-1.0.0/index.json +++ b/testdata/package/example-1.0.0/index.json @@ -11,8 +11,9 @@ ], "requirement": { "kibana": { - "version.min": "7.0.0", - "version.max": "" + "version": { + "min": "7.0.0" + } } }, "screenshots": [ diff --git a/testdata/package/foo-1.0.0/index.json b/testdata/package/foo-1.0.0/index.json index 1fadf48dd..94959a00f 100644 --- a/testdata/package/foo-1.0.0/index.json +++ b/testdata/package/foo-1.0.0/index.json @@ -9,8 +9,9 @@ ], "requirement": { "kibana": { - "version.min": "7.0.0", - "version.max": "" + "version": { + "min": "7.0.0" + } } }, "assets": [ diff --git a/testdata/package/internal-1.2.0/index.json b/testdata/package/internal-1.2.0/index.json index 2cbdc90d3..2236c3575 100644 --- a/testdata/package/internal-1.2.0/index.json +++ b/testdata/package/internal-1.2.0/index.json @@ -7,8 +7,7 @@ "categories": [], "requirement": { "kibana": { - "version.min": "", - "version.max": "" + "version": {} } }, "assets": [ diff --git a/util/package.go b/util/package.go index a401b4159..c9a71142a 100644 --- a/util/package.go +++ b/util/package.go @@ -6,15 +6,16 @@ package util import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" "github.com/pkg/errors" - yaml "gopkg.in/yaml.v2" "github.com/blang/semver" + + "github.com/elastic/go-ucfg" + "github.com/elastic/go-ucfg/yaml" ) const defaultType = "integration" @@ -45,12 +46,16 @@ type Requirement struct { } type Kibana struct { - Min string `yaml:"version.min" json:"version.min"` - Max string `yaml:"version.max" json:"version.max"` + Version Version `yaml:"version,omitempty" json:"version,omitempty"` minSemVer semver.Version maxSemVer semver.Version } +type Version struct { + Min string `yaml:"min,omitempty" json:"min,omitempty"` + Max string `yaml:"max,omitempty" json:"max,omitempty"` +} + type Image struct { Src string `yaml:"src" json:"src,omitempty"` Title string `yaml:"title" json:"title,omitempty"` @@ -66,13 +71,10 @@ func (i Image) getPath(p *Package) string { // The package name passed contains the version of the package. func NewPackage(basePath, packageName string) (*Package, error) { - manifest, err := ioutil.ReadFile(basePath + "/" + packageName + "/manifest.yml") - if err != nil { - return nil, err - } + manifest, err := yaml.NewConfigWithFile(basePath+"/"+packageName+"/manifest.yml", ucfg.PathSep(".")) var p = &Package{} - err = yaml.Unmarshal(manifest, p) + err = manifest.Unpack(p) if err != nil { return nil, err } @@ -93,17 +95,17 @@ func NewPackage(basePath, packageName string) (*Package, error) { } } - if p.Requirement.Kibana.Max != "" { - p.Requirement.Kibana.maxSemVer, err = semver.Parse(p.Requirement.Kibana.Max) + if p.Requirement.Kibana.Version.Max != "" { + p.Requirement.Kibana.maxSemVer, err = semver.Parse(p.Requirement.Kibana.Version.Max) if err != nil { - return nil, errors.Wrapf(err, "invalid Kibana max version: %s", p.Requirement.Kibana.Max) + return nil, errors.Wrapf(err, "invalid Kibana max version: %s", p.Requirement.Kibana.Version.Max) } } - if p.Requirement.Kibana.Min != "" { - p.Requirement.Kibana.minSemVer, err = semver.Parse(p.Requirement.Kibana.Min) + if p.Requirement.Kibana.Version.Min != "" { + p.Requirement.Kibana.minSemVer, err = semver.Parse(p.Requirement.Kibana.Version.Min) if err != nil { - return nil, errors.Wrapf(err, "invalid Kibana min version: %s", p.Requirement.Kibana.Min) + return nil, errors.Wrapf(err, "invalid Kibana min version: %s", p.Requirement.Kibana.Version.Min) } } @@ -142,13 +144,13 @@ func (p *Package) HasCategory(category string) bool { func (p *Package) HasKibanaVersion(version *semver.Version) bool { if version != nil { - if p.Requirement.Kibana.Max != "" { + if p.Requirement.Kibana.Version.Max != "" { if version.GT(p.Requirement.Kibana.maxSemVer) { return false } } - if p.Requirement.Kibana.Min != "" { + if p.Requirement.Kibana.Version.Min != "" { if version.LT(p.Requirement.Kibana.minSemVer) { return false } @@ -228,17 +230,17 @@ func (p *Package) Validate() error { return fmt.Errorf("no description set") } - if p.Requirement.Kibana.Max != "" { - _, err := semver.Parse(p.Requirement.Kibana.Max) + if p.Requirement.Kibana.Version.Max != "" { + _, err := semver.Parse(p.Requirement.Kibana.Version.Max) if err != nil { - return fmt.Errorf("invalid max kibana version: %s, %s", p.Requirement.Kibana.Max, err) + return fmt.Errorf("invalid max kibana version: %s, %s", p.Requirement.Kibana.Version.Max, err) } } - if p.Requirement.Kibana.Min != "" { - _, err := semver.Parse(p.Requirement.Kibana.Min) + if p.Requirement.Kibana.Version.Min != "" { + _, err := semver.Parse(p.Requirement.Kibana.Version.Min) if err != nil { - return fmt.Errorf("invalid min Kibana version: %s, %s", p.Requirement.Kibana.Min, err) + return fmt.Errorf("invalid min Kibana version: %s, %s", p.Requirement.Kibana.Version.Min, err) } } diff --git a/util/package_test.go b/util/package_test.go index 5b52e34c1..03aca70c1 100644 --- a/util/package_test.go +++ b/util/package_test.go @@ -35,8 +35,10 @@ var packageTests = []struct { Title: &title, Requirement: Requirement{ Kibana{ - Min: "1.2.3", - Max: "bar", + Version: Version{ + Min: "1.2.3", + Max: "bar", + }, }, }, }, @@ -48,8 +50,10 @@ var packageTests = []struct { Title: &title, Requirement: Requirement{ Kibana{ - Min: "foo", - Max: "4.5.6", + Version: Version{ + Min: "foo", + Max: "4.5.6", + }, }, }, }, @@ -62,8 +66,10 @@ var packageTests = []struct { Description: "my description", Requirement: Requirement{ Kibana{ - Min: "1.2.3", - Max: "4.5.6", + Version: Version{ + Min: "1.2.3", + Max: "4.5.6", + }, }, }, Categories: []string{"metrics", "logs", "foo"},