2020package v0
2121
2222import (
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
3436func (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-
4748func (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+
6884func (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
196212func (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
208236func (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
214254func (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}
0 commit comments