Skip to content

Commit 1aa7b31

Browse files
teresaromerojsorianomrodm
authored
Conditional subobjects based on package version (#2911)
* add support for version-based question prompts in data stream creation * Update cmd/create_data_stream.go Co-authored-by: Jaime Soriano Pastor <[email protected]> * extract survey question logic into a separate function and add tests * add tests for createDataStreamDescriptorFromAnswers handling of Subobjects based on spec version * add copyright notice to create_data_stream_test.go * Apply suggestion from @mrodm Co-authored-by: Mario Rodriguez Molins <[email protected]> * Apply suggestion from @mrodm Co-authored-by: Mario Rodriguez Molins <[email protected]> * Apply suggestion from @mrodm Co-authored-by: Mario Rodriguez Molins <[email protected]> * Apply suggestion from @mrodm Co-authored-by: Mario Rodriguez Molins <[email protected]> * Apply suggestion from @mrodm Co-authored-by: Mario Rodriguez Molins <[email protected]> * Apply suggestion from @mrodm Co-authored-by: Mario Rodriguez Molins <[email protected]> * Add missing require import in create_data_stream_test.go * Fix assertion for subobjects true case --------- Co-authored-by: Jaime Soriano Pastor <[email protected]> Co-authored-by: Mario Rodriguez Molins <[email protected]>
1 parent a7f4296 commit 1aa7b31

File tree

2 files changed

+150
-38
lines changed

2 files changed

+150
-38
lines changed

cmd/create_data_stream.go

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"slices"
1212

1313
"github.com/AlecAivazis/survey/v2"
14+
"github.com/Masterminds/semver/v3"
1415

1516
"github.com/spf13/cobra"
1617

@@ -19,6 +20,8 @@ import (
1920
"github.com/elastic/elastic-package/internal/surveyext"
2021
)
2122

23+
var semver3_2_0 = semver.MustParse("3.2.0")
24+
2225
const createDataStreamLongDescription = `Use this command to create a new data stream.
2326
2427
The command can extend the package with a new data stream using embedded data stream template and wizard.`
@@ -53,42 +56,13 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
5356
return fmt.Errorf("data-streams are not supported in input packages")
5457
}
5558

56-
validator := surveyext.Validator{Cwd: "."}
57-
qs := []*survey.Question{
58-
{
59-
Name: "name",
60-
Prompt: &survey.Input{
61-
Message: "Data stream name:",
62-
Default: "new_data_stream",
63-
},
64-
Validate: survey.ComposeValidators(survey.Required, validator.DataStreamDoesNotExist, validator.DataStreamName),
65-
},
66-
{
67-
Name: "title",
68-
Prompt: &survey.Input{
69-
Message: "Data stream title:",
70-
Default: "New Data Stream",
71-
},
72-
Validate: survey.Required,
73-
},
74-
{
75-
Name: "type",
76-
Prompt: &survey.Select{
77-
Message: "Type:",
78-
Options: []string{"logs", "metrics"},
79-
Default: "logs",
80-
},
81-
Validate: survey.Required,
82-
},
83-
{
84-
Name: "subobjects",
85-
Prompt: &survey.Confirm{
86-
Message: "Enable creation of subobjects for fields with dots in their names?",
87-
Default: false,
88-
},
89-
Validate: survey.Required,
90-
},
59+
sv, err := semver.NewVersion(manifest.SpecVersion)
60+
if err != nil {
61+
return fmt.Errorf("failed to obtain spec version from package manifest in \"%s\": %w", packageRoot, err)
9162
}
63+
64+
qs := getInitialSurveyQuestionsForVersion(sv)
65+
9266
var answers newDataStreamAnswers
9367
err = survey.Ask(qs, &answers)
9468
if err != nil {
@@ -173,7 +147,7 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
173147
}
174148
}
175149

176-
descriptor := createDataStreamDescriptorFromAnswers(answers, packageRoot)
150+
descriptor := createDataStreamDescriptorFromAnswers(answers, packageRoot, sv)
177151
err = archetype.CreateDataStream(descriptor)
178152
if err != nil {
179153
return fmt.Errorf("can't create new data stream: %w", err)
@@ -183,14 +157,14 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
183157
return nil
184158
}
185159

186-
func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, packageRoot string) archetype.DataStreamDescriptor {
160+
func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, packageRoot string, specVersion *semver.Version) archetype.DataStreamDescriptor {
187161
manifest := packages.DataStreamManifest{
188162
Name: answers.Name,
189163
Title: answers.Title,
190164
Type: answers.Type,
191165
}
192166

193-
if !answers.Subobjects {
167+
if !specVersion.LessThan(semver3_2_0) && !answers.Subobjects {
194168
manifest.Elasticsearch = &packages.Elasticsearch{
195169
IndexTemplate: &packages.ManifestIndexTemplate{
196170
Mappings: &packages.ManifestMappings{
@@ -231,3 +205,47 @@ func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, package
231205
PackageRoot: packageRoot,
232206
}
233207
}
208+
209+
func getInitialSurveyQuestionsForVersion(specVersion *semver.Version) []*survey.Question {
210+
validator := surveyext.Validator{Cwd: "."}
211+
qs := []*survey.Question{
212+
{
213+
Name: "name",
214+
Prompt: &survey.Input{
215+
Message: "Data stream name:",
216+
Default: "new_data_stream",
217+
},
218+
Validate: survey.ComposeValidators(survey.Required, validator.DataStreamDoesNotExist, validator.DataStreamName),
219+
},
220+
{
221+
Name: "title",
222+
Prompt: &survey.Input{
223+
Message: "Data stream title:",
224+
Default: "New Data Stream",
225+
},
226+
Validate: survey.Required,
227+
},
228+
{
229+
Name: "type",
230+
Prompt: &survey.Select{
231+
Message: "Type:",
232+
Options: []string{"logs", "metrics"},
233+
Default: "logs",
234+
},
235+
Validate: survey.Required,
236+
},
237+
}
238+
239+
if !specVersion.LessThan(semver3_2_0) {
240+
qs = append(qs, &survey.Question{
241+
Name: "subobjects",
242+
Prompt: &survey.Confirm{
243+
Message: "Enable creation of subobjects for fields with dots in their names?",
244+
Default: false,
245+
},
246+
Validate: survey.Required,
247+
})
248+
}
249+
250+
return qs
251+
}

cmd/create_data_stream_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package cmd
6+
7+
import (
8+
"testing"
9+
10+
"github.com/AlecAivazis/survey/v2"
11+
"github.com/Masterminds/semver/v3"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestGetSurveyQuestionsForVersion_BelowSemver3_2_0(t *testing.T) {
17+
version := semver.MustParse("3.1.9")
18+
questions := getInitialSurveyQuestionsForVersion(version)
19+
20+
require.Len(t, questions, 3, "should return 3 questions for spec version < 3.2.0")
21+
22+
assert.Equal(t, "name", questions[0].Name)
23+
assert.IsType(t, &survey.Input{}, questions[0].Prompt)
24+
assert.Equal(t, "title", questions[1].Name)
25+
assert.IsType(t, &survey.Input{}, questions[1].Prompt)
26+
assert.Equal(t, "type", questions[2].Name)
27+
assert.IsType(t, &survey.Select{}, questions[2].Prompt)
28+
}
29+
30+
func TestGetSurveyQuestionsForVersion_EqualSemver3_2_0(t *testing.T) {
31+
version := semver.MustParse("3.2.0")
32+
questions := getInitialSurveyQuestionsForVersion(version)
33+
34+
require.Len(t, questions, 4, "should return 4 questions for spec version >= 3.2.0")
35+
36+
assert.Equal(t, "subobjects", questions[3].Name)
37+
assert.IsType(t, &survey.Confirm{}, questions[3].Prompt)
38+
}
39+
40+
func TestGetSurveyQuestionsForVersion_AboveSemver3_2_0(t *testing.T) {
41+
version := semver.MustParse("3.3.0")
42+
questions := getInitialSurveyQuestionsForVersion(version)
43+
44+
require.Len(t, questions, 4, "should return 4 questions for spec version > 3.2.0")
45+
46+
assert.Equal(t, "subobjects", questions[3].Name)
47+
assert.IsType(t, &survey.Confirm{}, questions[3].Prompt)
48+
}
49+
50+
func TestCreateDataStreamDescriptorFromAnswers_SubobjectsFalseForSpecVersionBelow3_2_0(t *testing.T) {
51+
specVersion := semver.MustParse("3.1.0")
52+
answers := newDataStreamAnswers{
53+
Name: "test_stream",
54+
Title: "Test Stream",
55+
Type: "logs",
56+
Subobjects: false,
57+
}
58+
descriptor := createDataStreamDescriptorFromAnswers(answers, "/tmp/package", specVersion)
59+
60+
assert.Equal(t, "test_stream", descriptor.Manifest.Name)
61+
assert.Equal(t, "Test Stream", descriptor.Manifest.Title)
62+
assert.Equal(t, "logs", descriptor.Manifest.Type)
63+
assert.Equal(t, "/tmp/package", descriptor.PackageRoot)
64+
assert.Nil(t, descriptor.Manifest.Elasticsearch)
65+
}
66+
67+
func TestCreateDataStreamDescriptorFromAnswers_SubobjectsFalseForSpecVersionGTE3_2_0(t *testing.T) {
68+
specVersion := semver.MustParse("3.2.0")
69+
answers := newDataStreamAnswers{
70+
Name: "test_stream",
71+
Title: "Test Stream",
72+
Type: "logs",
73+
Subobjects: false,
74+
}
75+
descriptor := createDataStreamDescriptorFromAnswers(answers, "/tmp/package", specVersion)
76+
77+
require.NotNil(t, descriptor.Manifest.Elasticsearch)
78+
require.NotNil(t, descriptor.Manifest.Elasticsearch.IndexTemplate)
79+
require.NotNil(t, descriptor.Manifest.Elasticsearch.IndexTemplate.Mappings)
80+
assert.False(t, descriptor.Manifest.Elasticsearch.IndexTemplate.Mappings.Subobjects)
81+
}
82+
83+
func TestCreateDataStreamDescriptorFromAnswers_SubobjectsTrueForSpecVersionGTE3_2_0(t *testing.T) {
84+
specVersion := semver.MustParse("3.2.0")
85+
answers := newDataStreamAnswers{
86+
Name: "test_stream",
87+
Title: "Test Stream",
88+
Type: "logs",
89+
Subobjects: true,
90+
}
91+
descriptor := createDataStreamDescriptorFromAnswers(answers, "/tmp/package", specVersion)
92+
93+
require.Nil(t, descriptor.Manifest.Elasticsearch)
94+
}

0 commit comments

Comments
 (0)