-
Notifications
You must be signed in to change notification settings - Fork 21
/
internalrequests.go
99 lines (81 loc) · 3.37 KB
/
internalrequests.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package manageserver
import (
"encoding/json"
"net/http"
"github.com/golang/glog"
"golang.org/x/net/context"
"github.com/cloudstax/firecamp/manage"
)
func (s *ManageHTTPServer) internalGetOp(ctx context.Context, w http.ResponseWriter,
r *http.Request, trimURL string, requuid string) (errmsg string, errcode int) {
switch trimURL {
case manage.InternalGetServiceTaskOp:
return s.internalGetServiceTask(ctx, w, r, requuid)
case manage.InternalListActiveServiceTasksOp:
return s.internalListActiveServiceTasks(ctx, w, r, requuid)
default:
return http.StatusText(http.StatusBadRequest), http.StatusBadRequest
}
}
func (s *ManageHTTPServer) internalGetServiceTask(ctx context.Context, w http.ResponseWriter,
r *http.Request, requuid string) (errmsg string, errcode int) {
req := &manage.InternalGetServiceTaskRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
glog.Errorln("InternalGetServiceTask decode request error", err, "requuid", requuid)
return http.StatusText(http.StatusBadRequest), http.StatusBadRequest
}
if req.Cluster != s.cluster || req.Region != s.region {
glog.Errorln("InternalGetServiceTask invalid request, local cluster", s.cluster,
"region", s.region, "requuid", requuid, req)
return http.StatusText(http.StatusBadRequest), http.StatusBadRequest
}
taskID, err := s.containersvcIns.GetServiceTask(ctx, req.Cluster, req.ServiceName, req.ContainerInstanceID)
if err != nil {
glog.Errorln("GetServiceTask error", err, "requuid", requuid, req)
return manage.ConvertToHTTPError(err)
}
glog.Infoln("GetServiceTask", taskID, "requuid", requuid, req)
resp := &manage.InternalGetServiceTaskResponse{
ServiceTaskID: taskID,
}
b, err := json.Marshal(resp)
if err != nil {
glog.Errorln("Marshal InternalGetServiceTaskResponse error", err, "requuid", requuid, req)
return http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError
}
w.WriteHeader(http.StatusOK)
w.Write(b)
return "", http.StatusOK
}
func (s *ManageHTTPServer) internalListActiveServiceTasks(ctx context.Context, w http.ResponseWriter,
r *http.Request, requuid string) (errmsg string, errcode int) {
req := &manage.InternalListActiveServiceTasksRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
glog.Errorln("InternalListActiveServiceTasks decode request error", err, "requuid", requuid)
return http.StatusText(http.StatusBadRequest), http.StatusBadRequest
}
if req.Cluster != s.cluster || req.Region != s.region {
glog.Errorln("InternalListActiveServiceTasks invalid request, local cluster", s.cluster,
"region", s.region, "requuid", requuid, req)
return http.StatusText(http.StatusBadRequest), http.StatusBadRequest
}
taskIDs, err := s.containersvcIns.ListActiveServiceTasks(ctx, req.Cluster, req.ServiceName)
if err != nil {
glog.Errorln("ListActiveServiceTasks error", err, "requuid", requuid, req)
return manage.ConvertToHTTPError(err)
}
glog.Infoln("ListActiveServiceTasks", len(taskIDs), "requuid", requuid, req)
resp := &manage.InternalListActiveServiceTasksResponse{
ServiceTaskIDs: taskIDs,
}
b, err := json.Marshal(resp)
if err != nil {
glog.Errorln("Marshal InternalListActiveServiceTasksResponse error", err, "requuid", requuid, req)
return http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError
}
w.WriteHeader(http.StatusOK)
w.Write(b)
return "", http.StatusOK
}