2015-04-20 18:59:24 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2016-05-16 20:11:33 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-04-20 18:59:24 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSysLeader(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
handleSysLeaderGet(core, w, r)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysLeaderGet(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
haEnabled := true
|
|
|
|
isLeader, address, err := core.Leader()
|
2016-05-16 20:11:33 +00:00
|
|
|
if errwrap.Contains(err, vault.ErrHANotEnabled.Error()) {
|
2015-04-20 18:59:24 +00:00
|
|
|
haEnabled = false
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, &LeaderResponse{
|
|
|
|
HAEnabled: haEnabled,
|
|
|
|
IsSelf: isLeader,
|
|
|
|
LeaderAddress: address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type LeaderResponse struct {
|
|
|
|
HAEnabled bool `json:"ha_enabled"`
|
|
|
|
IsSelf bool `json:"is_self"`
|
|
|
|
LeaderAddress string `json:"leader_address"`
|
|
|
|
}
|