Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit bd5497c

Browse files
committed
add cluster status API
1 parent 4a0fc2f commit bd5497c

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

api/cluster.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package api
2121

2222
import (
2323
"context"
24+
25+
"github.com/LiterMC/go-openbmclapi/config"
2426
)
2527

2628
type ClusterStatus int32
@@ -61,14 +63,19 @@ type Cluster interface {
6163
Host() string
6264
Port() uint16
6365
PublicHosts() []string
66+
Options() *config.ClusterOptions
6467

6568
Status() ClusterStatus
6669
Connect(context.Context) error
6770
Disconnect(context.Context) error
6871
Enable(context.Context) error
69-
Disable(ctx context.Context) error
72+
Disable(context.Context) error
7073
}
7174

7275
type ClusterManager interface {
73-
//
76+
GetClusters() []Cluster
77+
GetCluster(name string) Cluster
78+
AddCluster(name string, opts *config.ClusterOptions) error
79+
UpdateCluster(name string, opts *config.ClusterOptions) error
80+
RemoveCluster(name string) bool
7481
}

api/v0/cluster.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package v0
2121

2222
import (
23+
"context"
2324
"encoding/json"
2425
"fmt"
2526
"io"
@@ -29,10 +30,12 @@ import (
2930

3031
"github.com/LiterMC/go-openbmclapi/api"
3132
"github.com/LiterMC/go-openbmclapi/config"
33+
"github.com/LiterMC/go-openbmclapi/log"
3234
)
3335

3436
func (h *Handler) buildClusterRoute(mux *http.ServeMux) {
3537
mux.Handle("GET /cluster/list", permHandleFunc(api.ClusterPerm, h.routeClusterList))
38+
mux.Handle("GET /cluster/status", permHandleFunc(api.ClusterPerm, h.routeClusterStatus))
3639
mux.Handle("GET /cluster/config", permHandleFunc(api.ClusterPerm, h.routeClusterConfigGET))
3740
mux.Handle("PUT /cluster/config", permHandleFunc(api.ClusterPerm, h.routeClusterConfigPUT))
3841
mux.Handle("DELETE /cluster/config", permHandleFunc(api.ClusterPerm, h.routeClusterConfigDELETE))
@@ -42,8 +45,6 @@ func (h *Handler) buildClusterRoute(mux *http.ServeMux) {
4245
mux.Handle("POST /cluster/disable", permHandleFunc(api.ClusterPerm, h.routeClusterDisable))
4346
}
4447

45-
const configClustersPath = "clusters"
46-
4748
func (h *Handler) routeClusterList(rw http.ResponseWriter, req *http.Request) {
4849
data, err := json.Marshal(h.config.GetConfig().Clusters)
4950
if err != nil {
@@ -65,6 +66,21 @@ func (h *Handler) routeClusterList(rw http.ResponseWriter, req *http.Request) {
6566
rw.Write(data)
6667
}
6768

69+
func (h *Handler) routeClusterStatus(rw http.ResponseWriter, req *http.Request) {
70+
type clusterStatus struct {
71+
Status api.ClusterStatus `json:"status"`
72+
Sync bool `json:"sync"`
73+
}
74+
data := make(map[string]*clusterStatus)
75+
for _, cluster := range h.clusters.GetClusters() {
76+
data[cluster.Name()] = &clusterStatus{
77+
Status: cluster.Status(),
78+
Sync: false, // TODO
79+
}
80+
}
81+
writeJson(rw, http.StatusOK, data)
82+
}
83+
6884
func (h *Handler) routeClusterConfigGET(rw http.ResponseWriter, req *http.Request) {
6985
clusterId := req.URL.Query().Get("cluster_id")
7086
clusterCfg, ok := h.config.GetConfig().Clusters[clusterId]
@@ -195,7 +211,19 @@ func (h *Handler) routeClusterConfigDELETE(rw http.ResponseWriter, req *http.Req
195211

196212
func (h *Handler) routeClusterConnect(rw http.ResponseWriter, req *http.Request) {
197213
clusterId := req.URL.Query().Get("cluster_id")
198-
_ = clusterId
214+
cluster := h.clusters.GetCluster(clusterId)
215+
if cluster == nil {
216+
writeJson(rw, http.StatusNotFound, Map{
217+
"error": "ClusterNotFound",
218+
})
219+
return
220+
}
221+
go func() {
222+
err := cluster.Connect(context.Background())
223+
if err != nil {
224+
log.Errorf("API Connect Error: %v", err)
225+
}
226+
}()
199227
rw.WriteHeader(http.StatusNoContent)
200228
}
201229

@@ -207,12 +235,37 @@ func (h *Handler) routeClusterSync(rw http.ResponseWriter, req *http.Request) {
207235

208236
func (h *Handler) routeClusterEnable(rw http.ResponseWriter, req *http.Request) {
209237
clusterId := req.URL.Query().Get("cluster_id")
210-
_ = clusterId
238+
cluster := h.clusters.GetCluster(clusterId)
239+
if cluster == nil {
240+
writeJson(rw, http.StatusNotFound, Map{
241+
"error": "ClusterNotFound",
242+
})
243+
return
244+
}
245+
go func() {
246+
err := cluster.Enable(context.Background())
247+
if err != nil {
248+
log.Errorf("API Enable Error: %v", err)
249+
}
250+
}()
211251
rw.WriteHeader(http.StatusNoContent)
212252
}
213253

214254
func (h *Handler) routeClusterDisable(rw http.ResponseWriter, req *http.Request) {
215255
clusterId := req.URL.Query().Get("cluster_id")
216-
_ = clusterId
256+
cluster := h.clusters.GetCluster(clusterId)
257+
if cluster == nil {
258+
writeJson(rw, http.StatusNotFound, Map{
259+
"error": "ClusterNotFound",
260+
})
261+
return
262+
}
263+
go func() {
264+
err := cluster.Disable(context.Background())
265+
if err != nil {
266+
log.Errorf("API Disable Error: %v", err)
267+
}
268+
cluster.Disconnect(context.Background())
269+
}()
217270
rw.WriteHeader(http.StatusNoContent)
218271
}

cluster/cluster.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ type Cluster struct {
7070
fileListLastMod int64
7171
}
7272

73+
var _ api.Cluster = (*Cluster)(nil)
74+
7375
func NewCluster(
7476
name string, opts config.ClusterOptions, gcfg config.ClusterGeneralConfig,
7577
storageManager *storage.Manager,

cluster/socket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (cr *Cluster) Connect(ctx context.Context) error {
124124
// Disconnect will not disable the cluster
125125
//
126126
// See Connect
127-
func (cr *Cluster) Disconnect() error {
127+
func (cr *Cluster) Disconnect(context.Context) error {
128128
if cr.Status().Disconnected() {
129129
return nil
130130
}

0 commit comments

Comments
 (0)