Skip to content

Commit

Permalink
add get containers handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentius15 committed Aug 1, 2018
1 parent 67431b1 commit cb0be2c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *scheduler) initialize(user, password, dbname, host, port, sslmode strin

s.Router = mux.NewRouter()
s.Router.HandleFunc("/api/v1/container", s.createNewLxcHandler).Methods("POST")
s.Router.HandleFunc("/api/v1/container", s.getContainerHandler).Methods("GET")
s.client = agentClient{}
s.metricsDB = prometheusMetricsDB{}

Expand All @@ -81,6 +82,33 @@ func (s *scheduler) run(port string) {
log.Fatal(http.ListenAndServe(port, s.Router))
}

func (s *scheduler) getContainerHandler(w http.ResponseWriter, r *http.Request) {
type resp struct {
ID string `json:"id" db:"id"`
LXDName string `json:"lxd_name" db:"lxd_name"`
LXCName string `json:"lxc_name" db:"lxc_name"`
Image string `json:"image" db:"image"`
Status string `json:"status" db:"status"`
}

var result []resp
rows, err := s.DB.Queryx(`SELECT c.id as "id", c.name as "lxc_name", d.name as "lxd_name", c.alias as "image", c.status as "status" FROM lxc c JOIN lxd d ON c.lxd_id = d.id`)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
}

for rows.Next() {
var temp resp
err = rows.StructScan(&temp)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
}
result = append(result, temp)
}

respondWithJSON(w, http.StatusOK, result)
}

func (s *scheduler) createNewLxcHandler(w http.ResponseWriter, r *http.Request) {
var data createContainerRequestData
decoder := json.NewDecoder(r.Body)
Expand Down

0 comments on commit cb0be2c

Please sign in to comment.