forked from goodrain/rainbond
/
service.go
134 lines (124 loc) · 4.48 KB
/
service.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package region
import (
"bytes"
"fmt"
"github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util"
dbmodel "github.com/goodrain/rainbond/db/model"
coreutil "github.com/goodrain/rainbond/util"
utilhttp "github.com/goodrain/rainbond/util/http"
)
type services struct {
tenant
prefix string
model model.ServiceStruct
}
//ServiceInterface ServiceInterface
type ServiceInterface interface {
Get() (*serviceInfo, *util.APIHandleError)
GetDeployInfo() (*ServiceDeployInfo, *util.APIHandleError)
Pods() ([]*podInfo, *util.APIHandleError)
List() ([]*dbmodel.TenantServices, *util.APIHandleError)
Stop(eventID string) (string, *util.APIHandleError)
Start(eventID string) (string, *util.APIHandleError)
EventLog(eventID, level string) ([]*model.MessageData, *util.APIHandleError)
}
func (s *services) Pods() ([]*podInfo, *util.APIHandleError) {
var gc []*podInfo
var decode utilhttp.ResponseBody
decode.List = &gc
code, err := s.DoRequest(s.prefix+"/pods", "GET", nil, &decode)
if err != nil {
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get pods code %d", code))
}
return gc, nil
}
func (s *services) Get() (*serviceInfo, *util.APIHandleError) {
var service serviceInfo
var decode utilhttp.ResponseBody
decode.Bean = &service
code, err := s.DoRequest(s.prefix, "GET", nil, &decode)
if err != nil {
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get err with code %d", code))
}
return &service, nil
}
func (s *services) EventLog(eventID, level string) ([]*model.MessageData, *util.APIHandleError) {
data := []byte(`{"event_id":"` + eventID + `","level":"` + level + `"}`)
var message []*model.MessageData
var decode utilhttp.ResponseBody
decode.List = &message
code, err := s.DoRequest(s.prefix+"/event-log", "POST", bytes.NewBuffer(data), &decode)
if err != nil {
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get event log code %d", code))
}
return message, nil
}
func (s *services) List() ([]*dbmodel.TenantServices, *util.APIHandleError) {
var gc []*dbmodel.TenantServices
var decode utilhttp.ResponseBody
decode.List = &gc
code, err := s.DoRequest(s.prefix, "GET", nil, &decode)
if err != nil {
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get with code %d", code))
}
return gc, nil
}
func (s *services) Stop(eventID string) (string, *util.APIHandleError) {
if eventID == "" {
eventID = coreutil.NewUUID()
}
data := []byte(`{"event_id":"` + eventID + `"}`)
var res utilhttp.ResponseBody
code, err := s.DoRequest(s.prefix+"/stop", "POST", bytes.NewBuffer(data), nil)
if err != nil {
return "", handleErrAndCode(err, code)
}
return eventID, handleAPIResult(code, res)
}
func (s *services) Start(eventID string) (string, *util.APIHandleError) {
if eventID == "" {
eventID = coreutil.NewUUID()
}
var res utilhttp.ResponseBody
data := []byte(`{"event_id":"` + eventID + `"}`)
code, err := s.DoRequest(s.prefix+"/start", "POST", bytes.NewBuffer(data), nil)
if err != nil {
return "", handleErrAndCode(err, code)
}
return eventID, handleAPIResult(code, res)
}
//GetDeployInfo get service deploy info
func (s *services) GetDeployInfo() (*ServiceDeployInfo, *util.APIHandleError) {
var deployInfo ServiceDeployInfo
var decode utilhttp.ResponseBody
decode.Bean = &deployInfo
code, err := s.DoRequest(s.prefix+"/deploy-info", "GET", nil, &decode)
return &deployInfo, handleErrAndCode(err, code)
}