-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.go
More file actions
53 lines (47 loc) · 1.43 KB
/
Copy pathaction.go
File metadata and controls
53 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package compute
import (
"encoding/json"
"errors"
"net/http"
"github.com/JSYoo5B/SandStack/internal/api/respond"
appcompute "github.com/JSYoo5B/SandStack/internal/app/compute"
"github.com/go-chi/chi/v5"
)
func (h Handler) actionServer(w http.ResponseWriter, r *http.Request) {
var request serverActionRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
respond.Error(w, http.StatusBadRequest, "invalid JSON request body")
return
}
var err error
switch {
case request.Has("os-start"):
err = h.service.StartServer(chi.URLParam(r, "server_id"))
case request.Has("os-stop"):
err = h.service.StopServer(chi.URLParam(r, "server_id"))
case request.Has("reboot"):
err = h.service.RebootServer(chi.URLParam(r, "server_id"))
case request.Has("addSecurityGroup"):
err = h.service.AddServerSecurityGroup(
chi.URLParam(r, "server_id"),
request.SecurityGroupName("addSecurityGroup"),
)
case request.Has("removeSecurityGroup"):
err = h.service.RemoveServerSecurityGroup(
chi.URLParam(r, "server_id"),
request.SecurityGroupName("removeSecurityGroup"),
)
default:
respond.Error(w, http.StatusBadRequest, "unsupported server action")
return
}
if errors.Is(err, appcompute.ErrServerNotFound) {
respond.Error(w, http.StatusNotFound, "server not found")
return
}
if err != nil {
respond.Error(w, http.StatusInternalServerError, "server action failed")
return
}
w.WriteHeader(http.StatusAccepted)
}