11package main
22
33import (
4- "fmt "
5- "log "
6- "context "
7- "encoding/json "
4+ "context "
5+ "encoding/json "
6+ "fmt "
7+ "log "
88
9- "github.com/hashicorp/terraform/helper/schema"
10- elastic "gopkg.in/olivere/elastic.v5"
9+ "github.com/hashicorp/terraform/helper/schema"
10+ elastic "gopkg.in/olivere/elastic.v5"
1111)
1212
1313func resourceElasticsearchKibanaObject () * schema.Resource {
14- return & schema.Resource {
15- Create : resourceElasticsearchKibanaObjectCreate ,
16- Read : resourceElasticsearchKibanaObjectRead ,
17- Update : resourceElasticsearchKibanaObjectUpdate ,
18- Delete : resourceElasticsearchKibanaObjectDelete ,
19- Schema : map [string ]* schema.Schema {
20- "body" : & schema.Schema {
21- Type : schema .TypeString ,
22- Required : true ,
23- // ForceNew: true,
24- // DiffSuppressFunc: diffSuppressKibanaObject,
25- },
26- "index" : & schema.Schema {
27- Type : schema .TypeString ,
28- Optional : true ,
29- Default : ".kibana" ,
30- },
31- },
32- }
14+ return & schema.Resource {
15+ Create : resourceElasticsearchKibanaObjectCreate ,
16+ Read : resourceElasticsearchKibanaObjectRead ,
17+ Update : resourceElasticsearchKibanaObjectUpdate ,
18+ Delete : resourceElasticsearchKibanaObjectDelete ,
19+ Schema : map [string ]* schema.Schema {
20+ "body" : & schema.Schema {
21+ Type : schema .TypeString ,
22+ Required : true ,
23+ // ForceNew: true,
24+ // DiffSuppressFunc: diffSuppressKibanaObject,
25+ },
26+ "index" : & schema.Schema {
27+ Type : schema .TypeString ,
28+ Optional : true ,
29+ Default : ".kibana" ,
30+ },
31+ },
32+ }
3333}
3434
3535func resourceElasticsearchKibanaObjectCreate (d * schema.ResourceData , meta interface {}) error {
36- client := meta .(* elastic.Client )
37- // Use the IndexExists service to check if a specified index exists.
38- exists , err := client .IndexExists (d .Get ("index" ).(string )).Do (context .TODO ())
39- if err != nil {
40- return err
41- }
42- if ! exists {
43- log .Printf ("[INFO] Creating new kibana index" )
44- mapping := `{
36+ client := meta .(* elastic.Client )
37+ // Use the IndexExists service to check if a specified index exists.
38+ exists , err := client .IndexExists (d .Get ("index" ).(string )).Do (context .TODO ())
39+ if err != nil {
40+ return err
41+ }
42+ if ! exists {
43+ log .Printf ("[INFO] Creating new kibana index" )
44+ mapping := `{
4545 "search": {
4646 "properties": {
4747 "hits": {
@@ -53,130 +53,130 @@ func resourceElasticsearchKibanaObjectCreate(d *schema.ResourceData, meta interf
5353 }
5454 }
5555 }`
56- createIndex , err := client .CreateIndex (d .Get ("index" ).(string )+ "/_mapping/search" ).Body (mapping ).Do (context .TODO ())
57- if err != nil {
58- log .Printf ("[INFO] Failed to creating new kibana index: %+v" , err )
59- return err
60- }
61- if ! createIndex .Acknowledged {
62- return fmt .Errorf ("fail to create the Elasticsearch index" )
63- }
64- }
65-
66- id , err := resourceElasticsearchPutKibanaObject (d , meta )
67-
68- if err != nil {
69- log .Printf ("[INFO] Failed to put kibana object: %+v" , err )
70- return err
71- }
72-
73- d .SetId (id )
74- log .Printf ("[INFO] Object ID: %s" , d .Id ())
75-
76- return nil
56+ createIndex , err := client .CreateIndex (d .Get ("index" ).(string ) + "/_mapping/search" ).Body (mapping ).Do (context .TODO ())
57+ if err != nil {
58+ log .Printf ("[INFO] Failed to creating new kibana index: %+v" , err )
59+ return err
60+ }
61+ if ! createIndex .Acknowledged {
62+ return fmt .Errorf ("fail to create the Elasticsearch index" )
63+ }
64+ }
65+
66+ id , err := resourceElasticsearchPutKibanaObject (d , meta )
67+
68+ if err != nil {
69+ log .Printf ("[INFO] Failed to put kibana object: %+v" , err )
70+ return err
71+ }
72+
73+ d .SetId (id )
74+ log .Printf ("[INFO] Object ID: %s" , d .Id ())
75+
76+ return nil
7777}
7878
7979func resourceElasticsearchKibanaObjectRead (d * schema.ResourceData , meta interface {}) error {
80- client := meta .(* elastic.Client )
81- // res, err := client.IndexGetTemplate(d.Id()).Do(context.TODO())
82- bodyString := d .Get ("body" ).(string )
83- var body []map [string ]interface {}
84- if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
85- return err
86- }
87- // TODO handle multiple objects in json
88- id := body [0 ]["_id" ].(string )
89- objectType := body [0 ]["_type" ].(string )
90-
91- // termQuery := elastic.Query(elastic.NewTermQuery("title", id))
92- result , err := client .Get ().Index (d .Get ("index" ).(string )).Type (objectType ).Id (id ).Do (context .TODO ())
93- if err != nil {
94- log .Printf ("[INFO] Not found: %s %s %s" , id , objectType , d .Get ("index" ).(string ))
95- return err
96- }
97- if result .Found {
98- // search.Hits.Hits.Fields
99- // search.Hits.Hits.Source
100- d .Set ("index" , d .Get ("index" ).(string ))
101- d .Set ("body" , result .Source )
102- // already exists
103- } else {
104- return fmt .Errorf ("Object not found." )
105- }
106-
107- // t := res[d.Id()]
108- // tj, err := json.Marshal(t)
109- // if err != nil {
110- // return err
111- // }
112- // d.Set("name", d.Id())
113- // d.Set("body", string(tj))
114- return nil
80+ client := meta .(* elastic.Client )
81+ // res, err := client.IndexGetTemplate(d.Id()).Do(context.TODO())
82+ bodyString := d .Get ("body" ).(string )
83+ var body []map [string ]interface {}
84+ if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
85+ return err
86+ }
87+ // TODO handle multiple objects in json
88+ id := body [0 ]["_id" ].(string )
89+ objectType := body [0 ]["_type" ].(string )
90+
91+ // termQuery := elastic.Query(elastic.NewTermQuery("title", id))
92+ result , err := client .Get ().Index (d .Get ("index" ).(string )).Type (objectType ).Id (id ).Do (context .TODO ())
93+ if err != nil {
94+ log .Printf ("[INFO] Not found: %s %s %s" , id , objectType , d .Get ("index" ).(string ))
95+ return err
96+ }
97+ if result .Found {
98+ // search.Hits.Hits.Fields
99+ // search.Hits.Hits.Source
100+ d .Set ("index" , d .Get ("index" ).(string ))
101+ d .Set ("body" , result .Source )
102+ // already exists
103+ } else {
104+ return fmt .Errorf ("Object not found." )
105+ }
106+
107+ // t := res[d.Id()]
108+ // tj, err := json.Marshal(t)
109+ // if err != nil {
110+ // return err
111+ // }
112+ // d.Set("name", d.Id())
113+ // d.Set("body", string(tj))
114+ return nil
115115}
116116
117117func resourceElasticsearchKibanaObjectUpdate (d * schema.ResourceData , meta interface {}) error {
118- _ , err := resourceElasticsearchPutKibanaObject (d , meta )
119- return err
118+ _ , err := resourceElasticsearchPutKibanaObject (d , meta )
119+ return err
120120}
121121
122122func resourceElasticsearchKibanaObjectDelete (d * schema.ResourceData , meta interface {}) error {
123- client := meta .(* elastic.Client )
124- // _, err := client.IndexDeleteTemplate(d.Id()).Do(context.TODO())
125-
126- bodyString := d .Get ("body" ).(string )
127- var body []map [string ]interface {}
128- if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
129- return err
130- }
131- // TODO handle multiple objects in json
132- id := body [0 ]["_id" ].(string )
133- objectType := body [0 ]["_type" ].(string )
134-
135- res , err := client .Delete ().
136- Index (d .Get ("index" ).(string )).
137- Type (objectType ).
138- Id (id ).
139- Do (context .TODO ())
140-
141- if err != nil {
142- return err
143- }
144- if ! res .Found {
145- // fmt.Print("Document deleted from from index\n")
146- return fmt .Errorf ("failed to delete the object" )
147- }
148-
149- return nil
123+ client := meta .(* elastic.Client )
124+ // _, err := client.IndexDeleteTemplate(d.Id()).Do(context.TODO())
125+
126+ bodyString := d .Get ("body" ).(string )
127+ var body []map [string ]interface {}
128+ if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
129+ return err
130+ }
131+ // TODO handle multiple objects in json
132+ id := body [0 ]["_id" ].(string )
133+ objectType := body [0 ]["_type" ].(string )
134+
135+ res , err := client .Delete ().
136+ Index (d .Get ("index" ).(string )).
137+ Type (objectType ).
138+ Id (id ).
139+ Do (context .TODO ())
140+
141+ if err != nil {
142+ return err
143+ }
144+ if ! res .Found {
145+ // fmt.Print("Document deleted from from index\n")
146+ return fmt .Errorf ("failed to delete the object" )
147+ }
148+
149+ return nil
150150}
151151
152152func resourceElasticsearchPutKibanaObject (d * schema.ResourceData , meta interface {}) (string , error ) {
153- client := meta .(* elastic.Client )
154- // name := d.Get("name").(string)
155- // body := d.Get("body").(string)
156- // _, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
157-
158- bodyString := d .Get ("body" ).(string )
159- var body []map [string ]interface {}
160- if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
161- log .Printf ("[INFO] Failed to unmarshal: %+v" , err )
162- return "" , err
163- }
164- // TODO handle multiple objects in json
165- id := body [0 ]["_id" ].(string )
166- objectType := body [0 ]["_type" ].(string )
167- data := body [0 ]["_source" ]
168-
169- // will PUT with the given id
170- _ , err := client .Index ().
171- Index (d .Get ("index" ).(string )).
172- Type (objectType ).
173- Id (id ).
174- BodyJson (& data ).
175- Do (context .TODO ())
176-
177- if err != nil {
178- return "" , err
179- }
180-
181- return id , nil
153+ client := meta .(* elastic.Client )
154+ // name := d.Get("name").(string)
155+ // body := d.Get("body").(string)
156+ // _, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
157+
158+ bodyString := d .Get ("body" ).(string )
159+ var body []map [string ]interface {}
160+ if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
161+ log .Printf ("[INFO] Failed to unmarshal: %+v" , err )
162+ return "" , err
163+ }
164+ // TODO handle multiple objects in json
165+ id := body [0 ]["_id" ].(string )
166+ objectType := body [0 ]["_type" ].(string )
167+ data := body [0 ]["_source" ]
168+
169+ // will PUT with the given id
170+ _ , err := client .Index ().
171+ Index (d .Get ("index" ).(string )).
172+ Type (objectType ).
173+ Id (id ).
174+ BodyJson (& data ).
175+ Do (context .TODO ())
176+
177+ if err != nil {
178+ return "" , err
179+ }
180+
181+ return id , nil
182182}
0 commit comments