mirror of
https://github.com/datarhei/core.git
synced 2025-10-04 15:42:57 +08:00
Add /v1/core/config endpoint to cluster API
This commit is contained in:
@@ -16,6 +16,7 @@ package cluster
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -125,6 +126,7 @@ func NewAPI(config APIConfig) (API, error) {
|
||||
a.router.DELETE("/v1/kv/:key", a.UnsetKV)
|
||||
|
||||
a.router.GET("/v1/core", a.CoreAPIAddress)
|
||||
a.router.GET("/v1/core/config", a.CoreConfig)
|
||||
|
||||
return a, nil
|
||||
}
|
||||
@@ -157,7 +159,7 @@ func (a *api) AddServer(c echo.Context) error {
|
||||
r := client.JoinRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "Invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
a.logger.Debug().WithFields(log.Fields{
|
||||
@@ -174,7 +176,7 @@ func (a *api) AddServer(c echo.Context) error {
|
||||
err := a.cluster.Join(origin, r.ID, r.RaftAddress, "")
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("id", r.ID).Log("Unable to join cluster")
|
||||
return Err(http.StatusInternalServerError, "unable to join cluster", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to join cluster: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -208,7 +210,7 @@ func (a *api) RemoveServer(c echo.Context) error {
|
||||
err := a.cluster.Leave(origin, id)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("id", id).Log("Unable to leave cluster")
|
||||
return Err(http.StatusInternalServerError, "unable to leave cluster", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to leave cluster%s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -227,7 +229,7 @@ func (a *api) Snapshot(c echo.Context) error {
|
||||
data, err := a.cluster.Snapshot()
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).Log("Unable to create snaphot")
|
||||
return Err(http.StatusInternalServerError, "unable to create snapshot", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to create snapshot: %s", err.Error())
|
||||
}
|
||||
|
||||
defer data.Close()
|
||||
@@ -253,7 +255,7 @@ func (a *api) AddProcess(c echo.Context) error {
|
||||
r := client.AddProcessRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -267,7 +269,7 @@ func (a *api) AddProcess(c echo.Context) error {
|
||||
err := a.cluster.AddProcess(origin, &r.Config)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("id", r.Config.ID).Log("Unable to add process")
|
||||
return Err(http.StatusInternalServerError, "unable to add process", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to add process: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -303,7 +305,7 @@ func (a *api) RemoveProcess(c echo.Context) error {
|
||||
err := a.cluster.RemoveProcess(origin, pid)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("id", pid).Log("Unable to remove process")
|
||||
return Err(http.StatusInternalServerError, "unable to remove process", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to remove process: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -330,7 +332,7 @@ func (a *api) UpdateProcess(c echo.Context) error {
|
||||
r := client.UpdateProcessRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -349,7 +351,7 @@ func (a *api) UpdateProcess(c echo.Context) error {
|
||||
err := a.cluster.UpdateProcess(origin, pid, &r.Config)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("id", pid).Log("Unable to update process")
|
||||
return Err(http.StatusInternalServerError, "unable to update process", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to update process: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -377,7 +379,7 @@ func (a *api) SetProcessMetadata(c echo.Context) error {
|
||||
r := client.SetProcessMetadataRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -391,7 +393,7 @@ func (a *api) SetProcessMetadata(c echo.Context) error {
|
||||
err := a.cluster.SetProcessMetadata(origin, pid, key, r.Metadata)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("id", pid).Log("Unable to update metadata")
|
||||
return Err(http.StatusInternalServerError, "unable to update metadata", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to update metadata: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -415,7 +417,7 @@ func (a *api) AddIdentity(c echo.Context) error {
|
||||
r := client.AddIdentityRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -429,7 +431,7 @@ func (a *api) AddIdentity(c echo.Context) error {
|
||||
err := a.cluster.AddIdentity(origin, r.Identity)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("identity", r.Identity).Log("Unable to add identity")
|
||||
return Err(http.StatusInternalServerError, "unable to add identity", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to add identity: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -454,7 +456,7 @@ func (a *api) UpdateIdentity(c echo.Context) error {
|
||||
r := client.UpdateIdentityRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -474,7 +476,7 @@ func (a *api) UpdateIdentity(c echo.Context) error {
|
||||
"name": name,
|
||||
"identity": r.Identity,
|
||||
}).Log("Unable to add identity")
|
||||
return Err(http.StatusInternalServerError, "unable to update identity", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to update identity: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -499,7 +501,7 @@ func (a *api) SetIdentityPolicies(c echo.Context) error {
|
||||
r := client.SetPoliciesRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -513,7 +515,7 @@ func (a *api) SetIdentityPolicies(c echo.Context) error {
|
||||
err := a.cluster.SetPolicies(origin, name, r.Policies)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("policies", r.Policies).Log("Unable to set policies")
|
||||
return Err(http.StatusInternalServerError, "unable to add identity", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to add identity: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -545,7 +547,7 @@ func (a *api) RemoveIdentity(c echo.Context) error {
|
||||
err := a.cluster.RemoveIdentity(origin, name)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("identity", name).Log("Unable to remove identity")
|
||||
return Err(http.StatusInternalServerError, "unable to remove identity", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to remove identity: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -558,13 +560,25 @@ func (a *api) RemoveIdentity(c echo.Context) error {
|
||||
// @ID cluster-1-core-api-address
|
||||
// @Produce json
|
||||
// @Success 200 {string} string
|
||||
// @Success 500 {object} Error
|
||||
// @Router /v1/core [get]
|
||||
func (a *api) CoreAPIAddress(c echo.Context) error {
|
||||
address, _ := a.cluster.CoreAPIAddress("")
|
||||
return c.JSON(http.StatusOK, address)
|
||||
}
|
||||
|
||||
// CoreConfig returns the Core config of this node
|
||||
// @Summary Core config
|
||||
// @Description Core config of this node
|
||||
// @Tags v1.0.0
|
||||
// @ID cluster-1-core-config
|
||||
// @Produce json
|
||||
// @Success 200 {object} config.Config
|
||||
// @Router /v1/core/config [get]
|
||||
func (a *api) CoreConfig(c echo.Context) error {
|
||||
config := a.cluster.CoreConfig()
|
||||
return c.JSON(http.StatusOK, config)
|
||||
}
|
||||
|
||||
// Lock tries to acquire a named lock
|
||||
// @Summary Acquire a named lock
|
||||
// @Description Acquire a named lock
|
||||
@@ -574,14 +588,14 @@ func (a *api) CoreAPIAddress(c echo.Context) error {
|
||||
// @Param data body client.LockRequest true "Lock request"
|
||||
// @Param X-Cluster-Origin header string false "Origin ID of request"
|
||||
// @Success 200 {string} string
|
||||
// @Success 500 {object} Error
|
||||
// @Failure 500 {object} Error
|
||||
// @Failure 508 {object} Error
|
||||
// @Router /v1/lock [post]
|
||||
func (a *api) Lock(c echo.Context) error {
|
||||
r := client.LockRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -595,7 +609,7 @@ func (a *api) Lock(c echo.Context) error {
|
||||
err := a.cluster.CreateLock(origin, r.Name, r.ValidUntil)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("name", r.Name).Log("Unable to acquire lock")
|
||||
return Err(http.StatusInternalServerError, "unable to acquire lock", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to acquire lock: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -611,6 +625,7 @@ func (a *api) Lock(c echo.Context) error {
|
||||
// @Param X-Cluster-Origin header string false "Origin ID of request"
|
||||
// @Success 200 {string} string
|
||||
// @Failure 404 {object} Error
|
||||
// @Failure 500 {object} Error
|
||||
// @Failure 508 {object} Error
|
||||
// @Router /v1/lock/{name} [delete]
|
||||
func (a *api) Unlock(c echo.Context) error {
|
||||
@@ -627,7 +642,7 @@ func (a *api) Unlock(c echo.Context) error {
|
||||
err := a.cluster.DeleteLock(origin, name)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("name", name).Log("Unable to remove lock")
|
||||
return Err(http.StatusInternalServerError, "unable to remove lock", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to remove lock: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -642,14 +657,14 @@ func (a *api) Unlock(c echo.Context) error {
|
||||
// @Param data body client.SetKVRequest true "Set KV request"
|
||||
// @Param X-Cluster-Origin header string false "Origin ID of request"
|
||||
// @Success 200 {string} string
|
||||
// @Success 500 {object} Error
|
||||
// @Failure 500 {object} Error
|
||||
// @Failure 508 {object} Error
|
||||
// @Router /v1/kv [post]
|
||||
func (a *api) SetKV(c echo.Context) error {
|
||||
r := client.SetKVRequest{}
|
||||
|
||||
if err := util.ShouldBindJSON(c, &r); err != nil {
|
||||
return Err(http.StatusBadRequest, "Invalid JSON", "%s", err.Error())
|
||||
return Err(http.StatusBadRequest, "", "invalid JSON: %s", err.Error())
|
||||
}
|
||||
|
||||
origin := c.Request().Header.Get("X-Cluster-Origin")
|
||||
@@ -663,7 +678,7 @@ func (a *api) SetKV(c echo.Context) error {
|
||||
err := a.cluster.SetKV(origin, r.Key, r.Value)
|
||||
if err != nil {
|
||||
a.logger.Debug().WithError(err).WithField("key", r.Key).Log("Unable to store value")
|
||||
return Err(http.StatusInternalServerError, "unable to store value", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to store value: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
@@ -679,6 +694,7 @@ func (a *api) SetKV(c echo.Context) error {
|
||||
// @Param X-Cluster-Origin header string false "Origin ID of request"
|
||||
// @Success 200 {string} string
|
||||
// @Failure 404 {object} Error
|
||||
// @Failure 500 {object} Error
|
||||
// @Failure 508 {object} Error
|
||||
// @Router /v1/kv/{key} [delete]
|
||||
func (a *api) UnsetKV(c echo.Context) error {
|
||||
@@ -694,8 +710,11 @@ func (a *api) UnsetKV(c echo.Context) error {
|
||||
|
||||
err := a.cluster.UnsetKV(origin, key)
|
||||
if err != nil {
|
||||
if err == fs.ErrNotExist {
|
||||
return Err(http.StatusNotFound, "", "%s", err.Error())
|
||||
}
|
||||
a.logger.Debug().WithError(err).WithField("key", key).Log("Unable to remove key")
|
||||
return Err(http.StatusInternalServerError, "unable to remove key", "%s", err.Error())
|
||||
return Err(http.StatusInternalServerError, "", "unable to remove key: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
|
Reference in New Issue
Block a user