Skip to content

Commit 3b8f765

Browse files
author
Jacqui Maher
committed
elastigo doesn't properly test if indices exist - added workaround, resolves issue #7
1 parent 0ccdec6 commit 3b8f765

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

hive/hive.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,25 +3206,48 @@ func (s *Server) UserAssignmentHandler(w http.ResponseWriter, r *http.Request) {
32063206
func (s *Server) AdminSetupHandler(w http.ResponseWriter, r *http.Request) {
32073207
vars := mux.Vars(r)
32083208

3209-
indexExists, _ := s.EsConn.IndicesExists(s.Index)
3209+
log.Println("Importing data into hive...")
3210+
3211+
log.Println("Step 1: configuring elasticsearch.")
3212+
indexExists, possible404 := s.EsConn.IndicesExists(s.Index)
3213+
3214+
// for reasons mysterious to me, elastigo wraps all of the http pkg's functions
3215+
// and does not check if the response to IndicesExists is a 404.
3216+
// Elasticsearch will respond with a 404 if the index does not exist.
3217+
// Here we check for this and correctly set the value of indexExists to false
3218+
if possible404 != nil && possible404.Error() == "record not found" {
3219+
indexExists = false
3220+
3221+
// otherwise some other error was thrown, so just 500 and give up here.
3222+
} else if possible404 != nil {
3223+
s.wrapResponse(w, r, 500, s.wrapError(possible404))
3224+
return
3225+
}
3226+
32103227
if vars["DELETE_MY_DATABASE"] == "YES_I_AM_SURE" && indexExists {
32113228
// Delete existing hive index (was: curl -XDELETE localhost:9200/hive >/dev/null 2>&1)
32123229
_, err := s.EsConn.DeleteIndex(s.Index)
32133230
if err != nil {
3231+
log.Println("Failed to delete index:", err)
32143232
s.wrapResponse(w, r, 500, s.wrapError(err))
32153233
return
32163234
}
3235+
log.Println("Deleted index", s.Index, ". I hope that was ok - you said you were sure!")
32173236
indexExists = false
3237+
} else if indexExists {
3238+
giveUpErr := fmt.Errorf("Index '%s' exists. Use a different value or add 'YES_I_AM_SURE' to delete it: /admin/setup/YES_I_AM_SURE.", s.Index)
3239+
s.wrapResponse(w, r, 500, s.wrapError(giveUpErr))
3240+
return
32183241
}
32193242

32203243
if !indexExists {
3244+
log.Println("Creating index", s.Index)
32213245
// Create hive index (was: curl -XPOST localhost:9200/hive >/dev/null 2>&1)
32223246
_, err := s.EsConn.CreateIndex(s.Index)
32233247
if err != nil {
32243248
s.wrapResponse(w, r, 500, s.wrapError(err))
32253249
return
32263250
}
3227-
log.Println("Created", s.Index, "index")
32283251
}
32293252

32303253
assignmentsBody := `{
@@ -3274,6 +3297,11 @@ func (s *Server) AdminSetupHandler(w http.ResponseWriter, r *http.Request) {
32743297
s.wrapResponse(w, r, 500, s.wrapError(err))
32753298
return
32763299
}
3300+
3301+
log.Println("Done configuring elasticsearch")
3302+
3303+
log.Println("Step 2: creating project.")
3304+
32773305
body, err := ioutil.ReadAll(r.Body)
32783306
if err != nil {
32793307
s.wrapResponse(w, r, 500, s.wrapError(err))
@@ -3299,14 +3327,18 @@ func (s *Server) AdminSetupHandler(w http.ResponseWriter, r *http.Request) {
32993327
s.wrapResponse(w, r, 500, s.wrapError(err))
33003328
return
33013329
}
3302-
log.Println("Created project:", s.ActiveProjectId)
3330+
log.Println("Done creating project:", s.ActiveProjectId)
3331+
3332+
log.Println("Step 3: importing tasks.")
33033333

33043334
tasks, _, err := s.importTasks(importedJson.Tasks)
33053335
if err != nil {
33063336
s.wrapResponse(w, r, 500, s.wrapError(err))
33073337
return
33083338
}
3309-
log.Println("Created tasks:", len(tasks))
3339+
log.Println("Done creating tasks:", len(tasks))
3340+
3341+
log.Println("Step 4: adding assets.")
33103342

33113343
assetsBody := `{
33123344
"assets": {
@@ -3366,7 +3398,7 @@ func (s *Server) AdminSetupHandler(w http.ResponseWriter, r *http.Request) {
33663398
s.wrapResponse(w, r, 500, s.wrapError(err))
33673399
return
33683400
}
3369-
log.Println("Created", len(assets), "assets")
3401+
log.Println("Done adding", len(assets), "assets")
33703402

33713403
report := []byte(fmt.Sprintf(`{"status":"200 OK", "Project": "%s", "Tasks": "%d", "Assets": "%d"}`, s.ActiveProjectId, len(tasks), len(assets)))
33723404
s.wrapResponse(w, r, 200, report)
@@ -3390,6 +3422,7 @@ func (s *Server) Run() {
33903422

33913423
// ANY /admin/setup - clears out db, configures elasticsearch and creates a project
33923424
r.HandleFunc("/admin/setup", s.AdminSetupHandler)
3425+
r.HandleFunc("/admin/setup/{DELETE_MY_DATABASE}", s.AdminSetupHandler)
33933426

33943427
// GET /admin/projects - returns all projects in Hive
33953428
r.HandleFunc("/admin/projects", s.AdminProjectsHandler).Methods("GET")

0 commit comments

Comments
 (0)