diff --git a/CHANGELOG.md b/CHANGELOG.md index abffa9c0e..bc1cf6500 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Generate package index.json files. [#479](https://github.com/elastic/package-registry/pull/479) * Add validation for dataset type. [#501](https://github.com/elastic/package-registry/pull/501) * Add -dry-run flag. [#503](https://github.com/elastic/package-registry/pull/503) +* Encode fields in Kibana objects if not encoded. [#506](https://github.com/elastic/package-registry/pull/506) ### Deprecated diff --git a/dev/generator/main.go b/dev/generator/main.go index 123a0cd06..fec4263ee 100644 --- a/dev/generator/main.go +++ b/dev/generator/main.go @@ -185,14 +185,16 @@ func buildPackage(packagesBasePath string, p util.Package) error { if err != nil { return err } - output, err := encodedSavedObject(data) + output, changed, err := encodedSavedObject(data) if err != nil { return err } - err = ioutil.WriteFile(file, []byte(output), 0644) - if err != nil { - return err + if changed { + err = ioutil.WriteFile(file, output, 0644) + if err != nil { + return err + } } } return nil @@ -309,38 +311,39 @@ var ( // which are stored in encoded JSON in Kibana. // The reason is that for versioning it is much nicer to have the full // json so only on packaging this is changed. -func encodedSavedObject(data []byte) (string, error) { +func encodedSavedObject(data []byte) ([]byte, bool, error) { savedObject := MapStr{} err := json.Unmarshal(data, &savedObject) if err != nil { - return "", errors.Wrapf(err, "unmarshalling saved object failed") + return nil, false, errors.Wrapf(err, "unmarshalling saved object failed") } + var changed bool for _, v := range fieldsToEncode { out, err := savedObject.GetValue(v) - // This means the key did not exists, no conversion needed + // This means the key did not exists, no conversion needed. if err != nil { continue } // It may happen that some objects existing in example directory might be already encoded. - // In this case skip the encoding. + // In this case skip encoding the field and move to the next one. _, isString := out.(string) if isString { - return "", fmt.Errorf("expect non-string field type (fieldName: %s)", v) + continue } - // Marshal the value to encode it properly + // Marshal the value to encode it properly. r, err := json.Marshal(&out) if err != nil { - return "", err + return nil, false, err } _, err = savedObject.Put(v, string(r)) if err != nil { - return "", errors.Wrapf(err, "can't put value to the saved object") + return nil, false, errors.Wrapf(err, "can't put value to the saved object") } - + changed = true } - return savedObject.StringToPrint(), nil + return []byte(savedObject.StringToPrint()), changed, nil } diff --git a/dev/generator/main_test.go b/dev/generator/main_test.go index 6b2737dc7..a39bff9f1 100644 --- a/dev/generator/main_test.go +++ b/dev/generator/main_test.go @@ -17,6 +17,7 @@ func TestEncodeSavedObject(t *testing.T) { data, err := ioutil.ReadFile(file) assert.NoError(t, err) - _, err = encodedSavedObject(data) + _, changed, err := encodedSavedObject(data) assert.NoError(t, err) + assert.True(t, changed) } diff --git a/docs/api/example-0.0.2.tar.gz-preview.txt b/docs/api/example-0.0.2.tar.gz-preview.txt index aeb3d658c..6265f1c21 100644 --- a/docs/api/example-0.0.2.tar.gz-preview.txt +++ b/docs/api/example-0.0.2.tar.gz-preview.txt @@ -15,7 +15,7 @@ 0 example-0.0.2/kibana/index-pattern/ 91348 example-0.0.2/kibana/index-pattern/filebeat.json 0 example-0.0.2/kibana/infrastructure-ui-source/ -797 example-0.0.2/kibana/infrastructure-ui-source/default.json +1013 example-0.0.2/kibana/infrastructure-ui-source/default.json 0 example-0.0.2/kibana/visualization/ 1863 example-0.0.2/kibana/visualization/0a994af0-5c9d-11e9-8477-077ec9664dbd.json 1982 example-0.0.2/kibana/visualization/36f872a0-5c03-11e9-85b4-19d0072eb4f2.json diff --git a/testdata/public/package/example/0.0.2/kibana/infrastructure-ui-source/default.json b/testdata/public/package/example/0.0.2/kibana/infrastructure-ui-source/default.json index db4c02885..3a9122327 100644 --- a/testdata/public/package/example/0.0.2/kibana/infrastructure-ui-source/default.json +++ b/testdata/public/package/example/0.0.2/kibana/infrastructure-ui-source/default.json @@ -1,35 +1,35 @@ { - "infrastructure-ui-source": { - "description": "", - "fields": { - "container": "container.id", - "host": "host.name", - "pod": "kubernetes.pod.uid", - "tiebreaker": "_doc", - "timestamp": "@timestamp" + "infrastructure-ui-source" : { + "name" : "Default", + "description" : "", + "metricAlias" : "example-*", + "logAlias" : "example-*", + "fields" : { + "container" : "container.id", + "host" : "host.name", + "pod" : "kubernetes.pod.uid", + "tiebreaker" : "_doc", + "timestamp" : "@timestamp" + }, + "logColumns" : [ + { + "timestampColumn" : { + "id" : "5e7f964a-be8a-40d8-88d2-fbcfbdca0e2f" + } + }, + { + "fieldColumn" : { + "id" : " eb9777a8-fcd3-420e-ba7d-172fff6da7a2", + "field" : "event.dataset" + } + }, + { + "messageColumn" : { + "id" : "b645d6da-824b-4723-9a2a-e8cece1645c0" + } + } + ] }, - "logAlias": "example-*", - "logColumns": [ - { - "timestampColumn": { - "id": "5e7f964a-be8a-40d8-88d2-fbcfbdca0e2f" - } - }, - { - "fieldColumn": { - "field": "event.dataset", - "id": " eb9777a8-fcd3-420e-ba7d-172fff6da7a2" - } - }, - { - "messageColumn": { - "id": "b645d6da-824b-4723-9a2a-e8cece1645c0" - } - } - ], - "metricAlias": "example-*", - "name": "Default" - }, - "type": "infrastructure-ui-source", - "updated_at": "2019-09-05T09:42:51.592Z" -} \ No newline at end of file + "type" : "infrastructure-ui-source", + "updated_at" : "2019-09-05T09:42:51.592Z" +}