@@ -18,20 +18,14 @@ import (
1818 "time"
1919
2020 "github.com/pkg/errors"
21- "gopkg.in/yaml.v3"
2221
2322 "github.com/elastic/elastic-package/internal/elasticsearch"
23+ "github.com/elastic/elastic-package/internal/elasticsearch/ingest"
2424 "github.com/elastic/elastic-package/internal/packages"
2525)
2626
2727var ingestPipelineTag = regexp .MustCompile (`{{\s*IngestPipeline.+}}` )
2828
29- type pipelineResource struct {
30- name string
31- format string
32- content []byte
33- }
34-
3529type simulatePipelineRequest struct {
3630 Docs []pipelineDocument `json:"docs"`
3731}
@@ -48,7 +42,7 @@ type pipelineIngestedDocument struct {
4842 Doc pipelineDocument `json:"doc"`
4943}
5044
51- func installIngestPipelines (api * elasticsearch.API , dataStreamPath string ) (string , []pipelineResource , error ) {
45+ func installIngestPipelines (api * elasticsearch.API , dataStreamPath string ) (string , []ingest. Pipeline , error ) {
5246 dataStreamManifest , err := packages .ReadDataStreamManifest (filepath .Join (dataStreamPath , packages .DataStreamManifestFile ))
5347 if err != nil {
5448 return "" , nil , errors .Wrap (err , "reading data stream manifest failed" )
@@ -62,19 +56,15 @@ func installIngestPipelines(api *elasticsearch.API, dataStreamPath string) (stri
6256 return "" , nil , errors .Wrap (err , "loading ingest pipeline files failed" )
6357 }
6458
65- jsonPipelines , err := convertPipelineToJSON (pipelines )
66- if err != nil {
67- return "" , nil , errors .Wrap (err , "converting pipelines failed" )
68- }
59+ err = installPipelinesInElasticsearch (api , pipelines )
6960
70- err = installPipelinesInElasticsearch (api , jsonPipelines )
7161 if err != nil {
7262 return "" , nil , errors .Wrap (err , "installing pipelines failed" )
7363 }
74- return mainPipeline , jsonPipelines , nil
64+ return mainPipeline , pipelines , nil
7565}
7666
77- func loadIngestPipelineFiles (dataStreamPath string , nonce int64 ) ([]pipelineResource , error ) {
67+ func loadIngestPipelineFiles (dataStreamPath string , nonce int64 ) ([]ingest. Pipeline , error ) {
7868 elasticsearchPath := filepath .Join (dataStreamPath , "elasticsearch" , "ingest_pipeline" )
7969
8070 var pipelineFiles []string
@@ -86,7 +76,7 @@ func loadIngestPipelineFiles(dataStreamPath string, nonce int64) ([]pipelineReso
8676 pipelineFiles = append (pipelineFiles , files ... )
8777 }
8878
89- var pipelines []pipelineResource
79+ var pipelines []ingest. Pipeline
9080 for _ , path := range pipelineFiles {
9181 c , err := os .ReadFile (path )
9282 if err != nil {
@@ -102,75 +92,52 @@ func loadIngestPipelineFiles(dataStreamPath string, nonce int64) ([]pipelineReso
10292 return []byte (getWithPipelineNameWithNonce (pipelineTag , nonce ))
10393 })
10494 name := filepath .Base (path )
105- pipelines = append (pipelines , pipelineResource {
106- name : getWithPipelineNameWithNonce (name [:strings .Index (name , "." )], nonce ),
107- format : filepath .Ext (path )[1 :],
108- content : c ,
95+ pipelines = append (pipelines , ingest. Pipeline {
96+ Name : getWithPipelineNameWithNonce (name [:strings .Index (name , "." )], nonce ),
97+ Format : filepath .Ext (path )[1 :],
98+ Content : c ,
10999 })
110100 }
111101 return pipelines , nil
112102}
113103
114- func convertPipelineToJSON (pipelines []pipelineResource ) ([]pipelineResource , error ) {
115- var jsonPipelines []pipelineResource
116- for _ , pipeline := range pipelines {
117- if pipeline .format == "json" {
118- jsonPipelines = append (jsonPipelines , pipeline )
119- continue
120- }
121-
122- var node map [string ]interface {}
123- err := yaml .Unmarshal (pipeline .content , & node )
124- if err != nil {
125- return nil , errors .Wrapf (err , "unmarshalling pipeline content failed (pipeline: %s)" , pipeline .name )
126- }
127-
128- c , err := json .Marshal (& node )
129- if err != nil {
130- return nil , errors .Wrapf (err , "marshalling pipeline content failed (pipeline: %s)" , pipeline .name )
131- }
132-
133- jsonPipelines = append (jsonPipelines , pipelineResource {
134- name : pipeline .name ,
135- format : "json" ,
136- content : c ,
137- })
138- }
139- return jsonPipelines , nil
140- }
141-
142- func installPipelinesInElasticsearch (api * elasticsearch.API , pipelines []pipelineResource ) error {
143- for _ , pipeline := range pipelines {
144- if err := installPipeline (api , pipeline ); err != nil {
104+ func installPipelinesInElasticsearch (api * elasticsearch.API , pipelines []ingest.Pipeline ) error {
105+ for _ , p := range pipelines {
106+ if err := installPipeline (api , p ); err != nil {
145107 return err
146108 }
147109 }
148110 return nil
149111}
150112
151- func installPipeline (api * elasticsearch.API , pipeline pipelineResource ) error {
113+ func installPipeline (api * elasticsearch.API , pipeline ingest. Pipeline ) error {
152114 if err := putIngestPipeline (api , pipeline ); err != nil {
153115 return err
154116 }
155117 // Just to be sure the pipeline has been uploaded.
156- return getIngestPipeline (api , pipeline .name )
118+ return getIngestPipeline (api , pipeline .Name )
157119}
158120
159- func putIngestPipeline (api * elasticsearch.API , pipeline pipelineResource ) error {
160- r , err := api . Ingest . PutPipeline ( pipeline .name , bytes . NewReader ( pipeline . content ) )
121+ func putIngestPipeline (api * elasticsearch.API , pipeline ingest. Pipeline ) error {
122+ source , err := pipeline .MarshalJSON ( )
161123 if err != nil {
162- return errors .Wrapf (err , "PutPipeline API call failed (pipelineName: %s)" , pipeline .name )
124+ return err
125+ }
126+ r , err := api .Ingest .PutPipeline (pipeline .Name , bytes .NewReader (source ))
127+ if err != nil {
128+ return errors .Wrapf (err , "PutPipeline API call failed (pipelineName: %s)" , pipeline .Name )
163129 }
164130 defer r .Body .Close ()
165131
166132 body , err := io .ReadAll (r .Body )
167133 if err != nil {
168- return errors .Wrapf (err , "failed to read PutPipeline API response body (pipelineName: %s)" , pipeline .name )
134+ return errors .Wrapf (err , "failed to read PutPipeline API response body (pipelineName: %s)" , pipeline .Name )
169135 }
170136
171137 if r .StatusCode != http .StatusOK {
138+
172139 return errors .Wrapf (elasticsearch .NewError (body ), "unexpected response status for PutPipeline (%d): %s (pipelineName: %s)" ,
173- r .StatusCode , r .Status (), pipeline .name )
140+ r .StatusCode , r .Status (), pipeline .Name )
174141 }
175142 return nil
176143}
@@ -196,12 +163,13 @@ func getIngestPipeline(api *elasticsearch.API, pipelineName string) error {
196163 return nil
197164}
198165
199- func uninstallIngestPipelines (api * elasticsearch.API , pipelines []pipelineResource ) error {
166+ func uninstallIngestPipelines (api * elasticsearch.API , pipelines []ingest. Pipeline ) error {
200167 for _ , pipeline := range pipelines {
201- _ , err := api .Ingest .DeletePipeline (pipeline .name )
168+ resp , err := api .Ingest .DeletePipeline (pipeline .Name )
202169 if err != nil {
203- return errors .Wrapf (err , "DeletePipeline API call failed (pipelineName: %s)" , pipeline .name )
170+ return errors .Wrapf (err , "DeletePipeline API call failed (pipelineName: %s)" , pipeline .Name )
204171 }
172+ resp .Body .Close ()
205173 }
206174 return nil
207175}
0 commit comments