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,135 @@ 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+ if elastic .IsNotFound (err ) {
95+ log .Printf ("[WARN] Kibana Object (%s) not found, removing from state" , id )
96+ d .SetId ("" )
97+ return nil
98+ }
99+
100+ return err
101+ }
102+ if result .Found {
103+ // search.Hits.Hits.Fields
104+ // search.Hits.Hits.Source
105+ d .Set ("index" , d .Get ("index" ).(string ))
106+ d .Set ("body" , result .Source )
107+ // already exists
108+ } else {
109+ return fmt .Errorf ("Object not found." )
110+ }
111+
112+ // t := res[d.Id()]
113+ // tj, err := json.Marshal(t)
114+ // if err != nil {
115+ // return err
116+ // }
117+ // d.Set("name", d.Id())
118+ // d.Set("body", string(tj))
119+ return nil
115120}
116121
117122func resourceElasticsearchKibanaObjectUpdate (d * schema.ResourceData , meta interface {}) error {
118- _ , err := resourceElasticsearchPutKibanaObject (d , meta )
119- return err
123+ _ , err := resourceElasticsearchPutKibanaObject (d , meta )
124+ return err
120125}
121126
122127func 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
128+ client := meta .(* elastic.Client )
129+ // _, err := client.IndexDeleteTemplate(d.Id()).Do(context.TODO())
130+
131+ bodyString := d .Get ("body" ).(string )
132+ var body []map [string ]interface {}
133+ if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
134+ return err
135+ }
136+ // TODO handle multiple objects in json
137+ id := body [0 ]["_id" ].(string )
138+ objectType := body [0 ]["_type" ].(string )
139+
140+ res , err := client .Delete ().
141+ Index (d .Get ("index" ).(string )).
142+ Type (objectType ).
143+ Id (id ).
144+ Do (context .TODO ())
145+
146+ if err != nil {
147+ return err
148+ }
149+ if ! res .Found {
150+ // fmt.Print("Document deleted from from index\n")
151+ return fmt .Errorf ("failed to delete the object" )
152+ }
153+
154+ return nil
150155}
151156
152157func 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
158+ client := meta .(* elastic.Client )
159+ // name := d.Get("name").(string)
160+ // body := d.Get("body").(string)
161+ // _, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
162+
163+ bodyString := d .Get ("body" ).(string )
164+ var body []map [string ]interface {}
165+ if err := json .Unmarshal ([]byte (bodyString ), & body ); err != nil {
166+ log .Printf ("[INFO] Failed to unmarshal: %+v" , err )
167+ return "" , err
168+ }
169+ // TODO handle multiple objects in json
170+ id := body [0 ]["_id" ].(string )
171+ objectType := body [0 ]["_type" ].(string )
172+ data := body [0 ]["_source" ]
173+
174+ // will PUT with the given id
175+ _ , err := client .Index ().
176+ Index (d .Get ("index" ).(string )).
177+ Type (objectType ).
178+ Id (id ).
179+ BodyJson (& data ).
180+ Do (context .TODO ())
181+
182+ if err != nil {
183+ return "" , err
184+ }
185+
186+ return id , nil
182187}
0 commit comments