2015-04-20 18:59:24 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-12-12 00:50:19 +00:00
|
|
|
"time"
|
2015-04-20 18:59:24 +00:00
|
|
|
|
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
|
2017-07-31 22:25:27 +00:00
|
|
|
isLeader, address, clusterAddr, 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
|
|
|
|
}
|
2018-08-27 17:01:07 +00:00
|
|
|
resp := &LeaderResponse{
|
2017-07-31 22:25:27 +00:00
|
|
|
HAEnabled: haEnabled,
|
|
|
|
IsSelf: isLeader,
|
|
|
|
LeaderAddress: address,
|
|
|
|
LeaderClusterAddress: clusterAddr,
|
2018-08-27 17:01:07 +00:00
|
|
|
PerfStandby: core.PerfStandby(),
|
|
|
|
}
|
2020-12-12 00:50:19 +00:00
|
|
|
if isLeader {
|
|
|
|
resp.ActiveTime = core.ActiveTime()
|
|
|
|
}
|
2018-08-27 17:01:07 +00:00
|
|
|
if resp.PerfStandby {
|
|
|
|
resp.PerfStandbyLastRemoteWAL = vault.LastRemoteWAL(core)
|
2018-10-16 13:38:44 +00:00
|
|
|
} else if isLeader || !haEnabled {
|
|
|
|
resp.LastWAL = vault.LastWAL(core)
|
2018-08-27 17:01:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 23:07:27 +00:00
|
|
|
resp.RaftCommittedIndex, resp.RaftAppliedIndex = core.GetRaftIndexes()
|
|
|
|
|
2018-08-27 17:01:07 +00:00
|
|
|
respondOk(w, resp)
|
2015-04-20 18:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LeaderResponse struct {
|
2020-12-12 00:50:19 +00:00
|
|
|
HAEnabled bool `json:"ha_enabled"`
|
|
|
|
IsSelf bool `json:"is_self"`
|
|
|
|
ActiveTime time.Time `json:"active_time,omitempty"`
|
|
|
|
LeaderAddress string `json:"leader_address"`
|
|
|
|
LeaderClusterAddress string `json:"leader_cluster_address"`
|
|
|
|
PerfStandby bool `json:"performance_standby"`
|
|
|
|
PerfStandbyLastRemoteWAL uint64 `json:"performance_standby_last_remote_wal"`
|
|
|
|
LastWAL uint64 `json:"last_wal,omitempty"`
|
2020-05-18 23:07:27 +00:00
|
|
|
|
|
|
|
// Raft Indexes for this node
|
|
|
|
RaftCommittedIndex uint64 `json:"raft_committed_index,omitempty"`
|
|
|
|
RaftAppliedIndex uint64 `json:"raft_applied_index,omitempty"`
|
2015-04-20 18:59:24 +00:00
|
|
|
}
|