Skip to content

Commit d564886

Browse files
kwonkwonnwind5052ga111o
authored
Migration build error resolved (#44)
* file change : api * migrate ambigious directory name request into client * Add pkg directory for containing lower bound dependancy-free packages (#42) Merge pkg structure migration to Control logic. None of functional changes has applied but, structural refactoring for reduce of dependency has been applied. * resolve previous build error --------- Co-authored-by: wind5052 <wind5052@naver.com> Co-authored-by: Minhyeok <ga111o@proton.me>
1 parent d7be868 commit d564886

25 files changed

Lines changed: 520 additions & 454 deletions

api/connect_vm.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/easy-cloud-Knet/KWS_Control/service"
8+
"github.com/easy-cloud-Knet/KWS_Control/structure"
9+
"github.com/easy-cloud-Knet/KWS_Control/util"
10+
)
11+
12+
type ApiVmConnectRequest struct {
13+
UUID structure.UUID `json:"uuid"`
14+
}
15+
16+
type ApiVmConnectResponse struct {
17+
AuthToken string `json:"authToken"`
18+
}
19+
20+
func (c *handlerContext) vmConnect(w http.ResponseWriter, r *http.Request) {
21+
log := util.GetLogger()
22+
23+
uuidStr := r.URL.Query().Get("uuid")
24+
if uuidStr == "" {
25+
http.Error(w, "Missing 'uuid' query parameter", http.StatusBadRequest)
26+
log.Error("Missing 'uuid' query parameter", nil, true)
27+
return
28+
}
29+
30+
uuid := structure.UUID(uuidStr)
31+
authToken, err := service.GetGuacamoleToken(uuid, c.context)
32+
if err != nil {
33+
http.Error(w, err.Error(), http.StatusInternalServerError)
34+
log.Error("Failed to get Guacamole token: %v", err, true)
35+
return
36+
}
37+
38+
w.Header().Set("Content-Type", "application/json")
39+
if err := json.NewEncoder(w).Encode(ApiVmConnectResponse{AuthToken: authToken}); err != nil {
40+
log.Error("Failed to encode response: %v", err, true)
41+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
42+
return
43+
}
44+
}

api/create_vm.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/easy-cloud-Knet/KWS_Control/service"
7+
"github.com/easy-cloud-Knet/KWS_Control/util"
8+
)
9+
10+
func (c *handlerContext) createVm(w http.ResponseWriter, r *http.Request) {
11+
log := util.GetLogger()
12+
13+
err := service.CreateVM(w, r, c.context, c.rdb)
14+
if err != nil {
15+
h := w.Header()
16+
h.Del("Content-Length")
17+
h.Set("Content-Type", "text/plain; charset=utf-8")
18+
h.Set("X-Content-Type-Options", "nosniff")
19+
w.WriteHeader(http.StatusMethodNotAllowed)
20+
21+
log.Error("Failed to create VM: %v", err, true)
22+
return
23+
}
24+
defer r.Body.Close()
25+
26+
w.WriteHeader(http.StatusCreated)
27+
_, _ = w.Write([]byte("VM created successfully"))
28+
}

api/delete_vm.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/easy-cloud-Knet/KWS_Control/service"
8+
"github.com/easy-cloud-Knet/KWS_Control/structure"
9+
)
10+
11+
type ApiDeleteVmRequest struct {
12+
UUID structure.UUID `json:"uuid"`
13+
}
14+
15+
func (c *handlerContext) deleteVm(w http.ResponseWriter, r *http.Request) {
16+
var req ApiDeleteVmRequest
17+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
18+
w.WriteHeader(http.StatusBadRequest)
19+
return
20+
}
21+
defer r.Body.Close()
22+
23+
err := service.DeleteVM(req.UUID, c.context, c.rdb)
24+
if err != nil {
25+
w.WriteHeader(http.StatusInternalServerError)
26+
return
27+
}
28+
29+
w.WriteHeader(http.StatusOK)
30+
}

api/get_vm_info.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/easy-cloud-Knet/KWS_Control/service"
8+
"github.com/easy-cloud-Knet/KWS_Control/structure"
9+
"github.com/easy-cloud-Knet/KWS_Control/util"
10+
)
11+
12+
type ApiVmInfoRequest struct {
13+
UUID structure.UUID `json:"uuid"`
14+
}
15+
16+
type ApiVmInfoResponse struct {
17+
UUID structure.UUID `json:"uuid"`
18+
CPU uint32 `json:"cpu"`
19+
Memory uint32 `json:"memory"` // MiB
20+
Disk uint32 `json:"disk"` // MiB
21+
IP string `json:"ip"`
22+
}
23+
24+
func (c *handlerContext) vmInfo(w http.ResponseWriter, r *http.Request) {
25+
log := util.GetLogger()
26+
27+
var req ApiVmInfoRequest
28+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
29+
http.Error(w, "invalid request body", http.StatusBadRequest)
30+
log.Error("Invalid request body: %v", err, true)
31+
return
32+
}
33+
defer r.Body.Close()
34+
35+
vmInfo, err := service.GetVMInfoFromRedis(r.Context(), c.rdb, req.UUID)
36+
if err != nil {
37+
http.Error(w, err.Error(), http.StatusNotFound)
38+
log.Error("failed to get vm info from redis: %v", err, true)
39+
return
40+
}
41+
42+
response := ApiVmInfoResponse{
43+
UUID: vmInfo.UUID,
44+
CPU: vmInfo.CPU,
45+
Memory: vmInfo.Memory,
46+
Disk: vmInfo.Disk,
47+
IP: vmInfo.IP,
48+
}
49+
50+
w.Header().Set("Content-Type", "application/json")
51+
if err := json.NewEncoder(w).Encode(response); err != nil {
52+
log.Error("failed to encode vm info response: %v", err, true)
53+
http.Error(w, "failed to encode response", http.StatusInternalServerError)
54+
return
55+
}
56+
57+
log.Info("retrieved vm info from redis: UUID=%s", string(req.UUID), true)
58+
}

api/get_vm_status.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/easy-cloud-Knet/KWS_Control/service"
8+
"github.com/easy-cloud-Knet/KWS_Control/structure"
9+
"github.com/easy-cloud-Knet/KWS_Control/util"
10+
)
11+
12+
type ApiVmStatusRequest struct {
13+
UUID structure.UUID `json:"uuid"`
14+
Type string `json:"type"` // "cpu", "memory", or "disk"
15+
}
16+
17+
func (c *handlerContext) vmStatus(w http.ResponseWriter, r *http.Request) {
18+
log := util.GetLogger()
19+
20+
var req ApiVmStatusRequest
21+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
22+
http.Error(w, "Invalid request body", http.StatusBadRequest)
23+
log.Error("Failed to decode request body: %v", err, true)
24+
return
25+
}
26+
defer r.Body.Close()
27+
28+
statusType := req.Type
29+
if statusType != "cpu" && statusType != "memory" && statusType != "disk" {
30+
http.Error(w, "Invalid status type. Must be 'cpu', 'memory', or 'disk'", http.StatusBadRequest)
31+
return
32+
}
33+
34+
var data any
35+
var err error
36+
37+
switch statusType {
38+
case "cpu":
39+
data, err = service.GetVMCpuInfo(req.UUID, c.context)
40+
case "memory":
41+
data, err = service.GetVMMemoryInfo(req.UUID, c.context)
42+
case "disk":
43+
data, err = service.GetVMDiskInfo(req.UUID, c.context)
44+
}
45+
46+
if err != nil {
47+
http.Error(w, err.Error(), http.StatusInternalServerError)
48+
log.Error("Failed to get VM status: %v", err, true)
49+
return
50+
}
51+
52+
w.Header().Set("Content-Type", "application/json")
53+
if err := json.NewEncoder(w).Encode(data); err != nil {
54+
log.Error("Failed to encode response: %v", err, true)
55+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
56+
return
57+
}
58+
}

api/model/vm.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

api/shutdown_vm.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/easy-cloud-Knet/KWS_Control/service"
8+
"github.com/easy-cloud-Knet/KWS_Control/structure"
9+
)
10+
11+
type ApiShutdownVmRequest struct {
12+
UUID structure.UUID `json:"uuid"`
13+
}
14+
15+
type ApiShutdownVmResponse struct {
16+
Message string `json:"message"`
17+
}
18+
19+
type ApiForceShutdownVmRequest struct {
20+
UUID structure.UUID `json:"uuid"`
21+
}
22+
23+
type ApiForceShutdownVmResponse struct {
24+
Message string `json:"message"`
25+
}
26+
27+
func (c *handlerContext) shutdownVm(w http.ResponseWriter, r *http.Request) {
28+
var req ApiShutdownVmRequest
29+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
30+
http.Error(w, "Invalid request body", http.StatusBadRequest)
31+
return
32+
}
33+
defer r.Body.Close()
34+
35+
err := service.ShutdownVM(req.UUID, c.context, c.rdb)
36+
if err != nil {
37+
http.Error(w, err.Error(), http.StatusInternalServerError)
38+
return
39+
}
40+
41+
w.WriteHeader(http.StatusOK)
42+
}

api/start_vm.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/easy-cloud-Knet/KWS_Control/service"
8+
"github.com/easy-cloud-Knet/KWS_Control/structure"
9+
)
10+
11+
type ApiStartVmRequest struct {
12+
UUID structure.UUID `json:"uuid"`
13+
}
14+
15+
func (c *handlerContext) startVm(w http.ResponseWriter, r *http.Request) {
16+
var req ApiStartVmRequest
17+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
18+
http.Error(w, "Invalid request body", http.StatusBadRequest)
19+
return
20+
}
21+
defer r.Body.Close()
22+
23+
err := service.StartVM(req.UUID, c.context)
24+
if err != nil {
25+
http.Error(w, err.Error(), http.StatusInternalServerError)
26+
return
27+
}
28+
29+
w.WriteHeader(http.StatusOK)
30+
}

0 commit comments

Comments
 (0)