@@ -23,6 +23,8 @@ import (
23
23
"github.com/ctdk/goiardi/search"
24
24
"github.com/ctdk/goiardi/data_bag"
25
25
"github.com/ctdk/goiardi/actor"
26
+ "github.com/ctdk/goiardi/indexer"
27
+ "github.com/ctdk/goiardi/data_store"
26
28
"net/http"
27
29
"encoding/json"
28
30
"fmt"
@@ -186,6 +188,57 @@ func search_handler(w http.ResponseWriter, r *http.Request){
186
188
}
187
189
}
188
190
191
+ func reindexHandler (w http.ResponseWriter , r * http.Request ){
192
+ w .Header ().Set ("Content-Type" , "application/json" )
193
+ reindex_response := make (map [string ]interface {})
194
+ switch r .Method {
195
+ case "POST" :
196
+ reindexObjs := make ([]indexer.Indexable , 0 )
197
+ // We clear the index, *then* do the fetch because if
198
+ // something comes in between the time we fetch the
199
+ // objects to reindex and when it gets done, they'll
200
+ // just be added naturally
201
+ indexer .ClearIndex ()
202
+ // default indices
203
+ defaults := [... ]string { "node" , "client" , "role" , "env" }
204
+ ds := data_store .New ()
205
+ for _ , d := range defaults {
206
+ objList := ds .GetList (d )
207
+ for _ , oname := range objList {
208
+ u , _ := ds .Get (d , oname )
209
+ if u != nil {
210
+ reindexObjs = append (reindexObjs , u .(indexer.Indexable ))
211
+ }
212
+ }
213
+ }
214
+ // data bags have to be done separately
215
+ dbags := data_bag .GetList ()
216
+ for _ , db := range dbags {
217
+ dbag , err := data_bag .Get (db )
218
+ if err != nil {
219
+ continue
220
+ }
221
+ dbis := make ([]indexer.Indexable , len (dbag .DataBagItems ))
222
+ i := 0
223
+ for _ , k := range dbag .DataBagItems {
224
+ n := k
225
+ dbis [i ] = & n
226
+ i ++
227
+ }
228
+ reindexObjs = append (reindexObjs , dbis ... )
229
+ }
230
+ indexer .ReIndex (reindexObjs )
231
+ reindex_response ["reindex" ] = "OK"
232
+ default :
233
+ JsonErrorReport (w , r , "Method not allowed. If you're trying to do something with a data bag named 'reindex', it's not going to work I'm afraid." , http .StatusMethodNotAllowed )
234
+ return
235
+ }
236
+ enc := json .NewEncoder (w )
237
+ if err := enc .Encode (& reindex_response ); err != nil {
238
+ JsonErrorReport (w , r , err .Error (), http .StatusInternalServerError )
239
+ }
240
+ }
241
+
189
242
func partialSearchFormat (results []map [string ]interface {}, partialFormat map [string ]interface {}) ([]map [string ]interface {}, error ) {
190
243
/* regularize partial search keys */
191
244
psearchKeys := make (map [string ][]string , len (partialFormat ))
0 commit comments