Rainbond/api/region/service.go

135 lines
4.5 KiB
Go
Raw Normal View History

2018-12-08 12:58:40 +08:00
// 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 {
2018-12-25 19:24:48 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get pods code %d", code))
2018-12-08 12:58:40 +08:00
}
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 {
2018-12-25 19:24:48 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get event log code %d", code))
2018-12-08 12:58:40 +08:00
}
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 + `"}`)
2018-12-08 17:19:53 +08:00
var res utilhttp.ResponseBody
2018-12-08 12:58:40 +08:00
code, err := s.DoRequest(s.prefix+"/stop", "POST", bytes.NewBuffer(data), nil)
2018-12-08 17:19:53 +08:00
if err != nil {
return "", handleErrAndCode(err, code)
}
return eventID, handleAPIResult(code, res)
2018-12-08 12:58:40 +08:00
}
func (s *services) Start(eventID string) (string, *util.APIHandleError) {
if eventID == "" {
eventID = coreutil.NewUUID()
}
2018-12-08 17:19:53 +08:00
var res utilhttp.ResponseBody
2018-12-08 12:58:40 +08:00
data := []byte(`{"event_id":"` + eventID + `"}`)
code, err := s.DoRequest(s.prefix+"/start", "POST", bytes.NewBuffer(data), nil)
2018-12-08 17:19:53 +08:00
if err != nil {
return "", handleErrAndCode(err, code)
}
return eventID, handleAPIResult(code, res)
2018-12-08 12:58:40 +08:00
}
//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)
}