Skip to content

Commit 4fd1b58

Browse files
authored
Add synthetic mode as default when creating a datastream (#1261)
Added questions to set synthetic or time series modes in the data stream manifest when using the CLI command elastic-package create data-stream
1 parent 5ad8bd8 commit 4fd1b58

File tree

3 files changed

+86
-22
lines changed

3 files changed

+86
-22
lines changed

cmd/create_data_stream.go

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ const createDataStreamLongDescription = `Use this command to create a new data s
1919
The command can extend the package with a new data stream using embedded data stream template and wizard.`
2020

2121
type newDataStreamAnswers struct {
22-
Name string
23-
Title string
24-
Type string
22+
Name string
23+
Title string
24+
Type string
25+
SyntheticAndTimeSeries bool
26+
Synthetic bool
2527
}
2628

2729
func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
@@ -68,6 +70,40 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
6870
return errors.Wrap(err, "prompt failed")
6971
}
7072

73+
if answers.Type == "metrics" {
74+
qs := []*survey.Question{
75+
{
76+
Name: "syntheticAndTimeSeries",
77+
Prompt: &survey.Confirm{
78+
Message: "Enable time series and synthetic source?",
79+
Default: true,
80+
},
81+
Validate: survey.Required,
82+
},
83+
}
84+
err = survey.Ask(qs, &answers)
85+
if err != nil {
86+
return errors.Wrap(err, "prompt failed")
87+
}
88+
89+
if !answers.SyntheticAndTimeSeries {
90+
qs := []*survey.Question{
91+
{
92+
Name: "synthetic",
93+
Prompt: &survey.Confirm{
94+
Message: "Enable synthetic source?",
95+
Default: true,
96+
},
97+
Validate: survey.Required,
98+
},
99+
}
100+
err = survey.Ask(qs, &answers)
101+
if err != nil {
102+
return errors.Wrap(err, "prompt failed")
103+
}
104+
}
105+
}
106+
71107
descriptor := createDataStreamDescriptorFromAnswers(answers, packageRoot)
72108
err = archetype.CreateDataStream(descriptor)
73109
if err != nil {
@@ -79,12 +115,27 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
79115
}
80116

81117
func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, packageRoot string) archetype.DataStreamDescriptor {
118+
manifest := packages.DataStreamManifest{
119+
Name: answers.Name,
120+
Title: answers.Title,
121+
Type: answers.Type,
122+
}
123+
124+
if !answers.SyntheticAndTimeSeries && !answers.Synthetic {
125+
return archetype.DataStreamDescriptor{
126+
Manifest: manifest,
127+
PackageRoot: packageRoot,
128+
}
129+
}
130+
elasticsearch := packages.Elasticsearch{
131+
SourceMode: "synthetic",
132+
}
133+
if answers.SyntheticAndTimeSeries {
134+
elasticsearch.IndexMode = "time_series"
135+
}
136+
manifest.Elasticsearch = &elasticsearch
82137
return archetype.DataStreamDescriptor{
83-
Manifest: packages.DataStreamManifest{
84-
Name: answers.Name,
85-
Title: answers.Title,
86-
Type: answers.Type,
87-
},
138+
Manifest: manifest,
88139
PackageRoot: packageRoot,
89140
}
90141
}

internal/packages/archetype/data_stream_manifest.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,14 @@ streams:{{if eq .Manifest.Type "logs" }}
2626
type: text
2727
title: Period
2828
default: 10s
29+
{{ if .Manifest.Elasticsearch }}
30+
elasticsearch:
31+
{{ if .Manifest.Elasticsearch.SourceMode }}
32+
source_mode: {{ .Manifest.Elasticsearch.SourceMode }}
33+
{{- end}}
34+
{{ if .Manifest.Elasticsearch.IndexMode }}
35+
index_mode: {{ .Manifest.Elasticsearch.IndexMode }}
36+
{{- end}}
37+
{{- end}}
2938
{{- end}}
3039
`

internal/packages/packages.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,26 @@ type PackageManifest struct {
132132
Categories []string `config:"categories" json:"categories" yaml:"categories"`
133133
}
134134

135+
type Elasticsearch struct {
136+
IndexTemplate *struct {
137+
IngestPipeline *struct {
138+
Name string `config:"name" json:"name" yaml:"name"`
139+
} `config:"ingest_pipeline" json:"ingest_pipeline" yaml:"ingest_pipeline"`
140+
} `config:"index_template" json:"index_template" yaml:"index_template"`
141+
SourceMode string `config:"source_mode" json:"source_mode" yaml:"source_mode"`
142+
IndexMode string `config:"index_mode" json:"index_mode" yaml:"index_mode"`
143+
}
144+
135145
// DataStreamManifest represents the structure of a data stream's manifest
136146
type DataStreamManifest struct {
137-
Name string `config:"name" json:"name" yaml:"name"`
138-
Title string `config:"title" json:"title" yaml:"title"`
139-
Type string `config:"type" json:"type" yaml:"type"`
140-
Dataset string `config:"dataset" json:"dataset" yaml:"dataset"`
141-
Hidden bool `config:"hidden" json:"hidden" yaml:"hidden"`
142-
Release string `config:"release" json:"release" yaml:"release"`
143-
Elasticsearch *struct {
144-
IndexTemplate *struct {
145-
IngestPipeline *struct {
146-
Name string `config:"name" json:"name" yaml:"name"`
147-
} `config:"ingest_pipeline" json:"ingest_pipeline" yaml:"ingest_pipeline"`
148-
} `config:"index_template" json:"index_template" yaml:"index_template"`
149-
} `config:"elasticsearch" json:"elasticsearch" yaml:"elasticsearch"`
150-
Streams []struct {
147+
Name string `config:"name" json:"name" yaml:"name"`
148+
Title string `config:"title" json:"title" yaml:"title"`
149+
Type string `config:"type" json:"type" yaml:"type"`
150+
Dataset string `config:"dataset" json:"dataset" yaml:"dataset"`
151+
Hidden bool `config:"hidden" json:"hidden" yaml:"hidden"`
152+
Release string `config:"release" json:"release" yaml:"release"`
153+
Elasticsearch *Elasticsearch `config:"elasticsearch" json:"elasticsearch" yaml:"elasticsearch"`
154+
Streams []struct {
151155
Input string `config:"input" json:"input" yaml:"input"`
152156
Vars []Variable `config:"vars" json:"vars" yaml:"vars"`
153157
} `config:"streams" json:"streams" yaml:"streams"`

0 commit comments

Comments
 (0)