2018-03-14 14:12:26 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2017-11-10 11:53:05 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-10 11:53:05 +08:00
|
|
|
// 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.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-10 11:53:05 +08:00
|
|
|
// 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.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-10 11:53:05 +08:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-11-09 17:56:12 +08:00
|
|
|
package region
|
|
|
|
|
|
|
|
import (
|
2018-08-02 12:16:50 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-11-22 15:09:28 +08:00
|
|
|
"encoding/json"
|
2017-11-09 17:56:12 +08:00
|
|
|
"errors"
|
2017-12-25 19:49:32 +08:00
|
|
|
"fmt"
|
2018-07-16 19:06:57 +08:00
|
|
|
"io"
|
2017-11-09 17:56:12 +08:00
|
|
|
"io/ioutil"
|
2017-11-22 15:09:28 +08:00
|
|
|
"net/http"
|
2018-07-16 15:30:53 +08:00
|
|
|
"path"
|
2017-11-22 15:09:28 +08:00
|
|
|
|
2018-08-02 12:16:50 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/api/model"
|
|
|
|
"github.com/goodrain/rainbond/api/util"
|
2018-07-16 19:06:57 +08:00
|
|
|
"github.com/goodrain/rainbond/cmd"
|
2018-04-24 16:44:59 +08:00
|
|
|
utilhttp "github.com/goodrain/rainbond/util/http"
|
2017-11-09 17:56:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var regionAPI, token string
|
|
|
|
|
2018-07-16 19:06:57 +08:00
|
|
|
var region Region
|
|
|
|
|
|
|
|
//AllTenant AllTenant
|
|
|
|
var AllTenant string
|
|
|
|
|
|
|
|
//Region region api
|
|
|
|
type Region interface {
|
|
|
|
Tenants(name string) TenantInterface
|
|
|
|
Resources() ResourcesInterface
|
2018-07-17 16:18:19 +08:00
|
|
|
Tasks() TaskInterface
|
|
|
|
Nodes() NodeInterface
|
|
|
|
Cluster() ClusterInterface
|
|
|
|
Configs() ConfigsInterface
|
2018-07-16 19:06:57 +08:00
|
|
|
Version() string
|
2018-08-01 12:29:06 +08:00
|
|
|
Monitor() MonitorInterface
|
2018-08-07 14:28:15 +08:00
|
|
|
Notification() NotificationInterface
|
2018-07-16 19:06:57 +08:00
|
|
|
DoRequest(path, method string, body io.Reader, decode *utilhttp.ResponseBody) (int, error)
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:16:50 +08:00
|
|
|
//APIConf region api config
|
|
|
|
type APIConf struct {
|
|
|
|
Endpoints []string `yaml:"endpoints"`
|
|
|
|
Token string `yaml:"token"`
|
|
|
|
AuthType string `yaml:"auth_type"`
|
|
|
|
Cacert string `yaml:"client-ca-file"`
|
|
|
|
Cert string `yaml:"tls-cert-file"`
|
|
|
|
CertKey string `yaml:"tls-private-key-file"`
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:44:58 +08:00
|
|
|
type serviceInfo struct {
|
|
|
|
ServicesAlias string `json:"serviceAlias"`
|
|
|
|
TenantName string `json:"tenantName"`
|
|
|
|
ServiceId string `json:"serviceId"`
|
|
|
|
TenantId string `json:"tenantId"`
|
|
|
|
}
|
|
|
|
|
2018-08-29 11:08:27 +08:00
|
|
|
type podInfo struct {
|
|
|
|
ServiceID string `json:"service_id"`
|
|
|
|
//部署资源的ID ,例如rc ,deploment, statefulset
|
|
|
|
ReplicationID string `json:"rc_id"`
|
|
|
|
ReplicationType string `json:"rc_type"`
|
|
|
|
PodName string `json:"pod_name"`
|
|
|
|
PodIP string `json:"pod_ip"`
|
|
|
|
Container map[string]map[string]string `json:"container"`
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:06:57 +08:00
|
|
|
//NewRegion NewRegion
|
2018-08-02 12:16:50 +08:00
|
|
|
func NewRegion(c APIConf) (Region, error) {
|
2018-07-16 19:06:57 +08:00
|
|
|
if region == nil {
|
2018-08-02 12:16:50 +08:00
|
|
|
re := ®ionImpl{
|
|
|
|
APIConf: c,
|
|
|
|
}
|
|
|
|
if c.Cacert != "" && c.Cert != "" && c.CertKey != "" {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
caCrt, err := ioutil.ReadFile(c.Cacert)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("read ca file err: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pool.AppendCertsFromPEM(caCrt)
|
|
|
|
|
|
|
|
cliCrt, err := tls.LoadX509KeyPair(c.Cert, c.CertKey)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Loadx509keypair err: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: pool,
|
|
|
|
Certificates: []tls.Certificate{cliCrt},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
re.Client = &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
re.Client = http.DefaultClient
|
2018-07-16 19:06:57 +08:00
|
|
|
}
|
2018-08-02 12:16:50 +08:00
|
|
|
region = re
|
2018-07-16 19:06:57 +08:00
|
|
|
}
|
2018-08-02 12:16:50 +08:00
|
|
|
return region, nil
|
2018-07-16 19:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//GetRegion GetRegion
|
|
|
|
func GetRegion() Region {
|
|
|
|
return region
|
|
|
|
}
|
|
|
|
|
|
|
|
type regionImpl struct {
|
2018-08-02 12:16:50 +08:00
|
|
|
APIConf
|
|
|
|
Client *http.Client
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
2017-11-22 15:09:28 +08:00
|
|
|
|
2018-07-16 19:06:57 +08:00
|
|
|
//Tenants Tenants
|
|
|
|
func (r *regionImpl) Tenants(tenantName string) TenantInterface {
|
|
|
|
return &tenant{prefix: path.Join("/v2/tenants", tenantName), tenantName: tenantName, regionImpl: *r}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Version Version
|
|
|
|
func (r *regionImpl) Version() string {
|
|
|
|
return cmd.GetVersion()
|
|
|
|
}
|
|
|
|
|
|
|
|
//Resources about resources
|
|
|
|
func (r *regionImpl) Resources() ResourcesInterface {
|
|
|
|
return &resources{prefix: "/v2/resources", regionImpl: *r}
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
2018-08-02 12:16:50 +08:00
|
|
|
func (r *regionImpl) GetEndpoint() string {
|
|
|
|
return r.Endpoints[0]
|
|
|
|
}
|
2017-11-09 17:56:12 +08:00
|
|
|
|
2018-07-16 19:06:57 +08:00
|
|
|
//DoRequest do request
|
|
|
|
func (r *regionImpl) DoRequest(path, method string, body io.Reader, decode *utilhttp.ResponseBody) (int, error) {
|
2018-08-02 12:16:50 +08:00
|
|
|
request, err := http.NewRequest(method, r.GetEndpoint()+path, body)
|
2017-11-09 17:56:12 +08:00
|
|
|
if err != nil {
|
2018-07-16 19:06:57 +08:00
|
|
|
return 500, err
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
2018-08-02 12:16:50 +08:00
|
|
|
if r.Token != "" {
|
|
|
|
request.Header.Set("Authorization", "Token "+r.Token)
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
2018-08-02 12:16:50 +08:00
|
|
|
res, err := r.Client.Do(request)
|
2017-11-09 17:56:12 +08:00
|
|
|
if err != nil {
|
2018-07-16 19:06:57 +08:00
|
|
|
return 500, err
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
2018-07-16 16:35:42 +08:00
|
|
|
if res.Body != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
if decode != nil {
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(decode); err != nil {
|
|
|
|
return res.StatusCode, err
|
|
|
|
}
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
2018-07-16 19:06:57 +08:00
|
|
|
return res.StatusCode, err
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
|
|
|
|
2018-12-08 18:23:25 +08:00
|
|
|
//LoadConfig load config
|
2017-11-09 17:56:12 +08:00
|
|
|
func LoadConfig(regionAPI, token string) (map[string]map[string]interface{}, error) {
|
2017-11-22 11:40:14 +08:00
|
|
|
if regionAPI != "" {
|
|
|
|
//return nil, errors.New("region api url can not be empty")
|
|
|
|
//return nil, errors.New("region api url can not be empty")
|
|
|
|
//todo
|
|
|
|
request, err := http.NewRequest("GET", regionAPI+"/v1/config", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
if token != "" {
|
|
|
|
request.Header.Set("Authorization", "Token "+token)
|
|
|
|
}
|
|
|
|
res, err := http.DefaultClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := make(map[string]map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(data), &config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
2017-11-22 15:17:33 +08:00
|
|
|
return nil, errors.New("wrong region api ")
|
2017-11-22 11:40:14 +08:00
|
|
|
|
2017-11-09 17:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//SetInfo 设置
|
|
|
|
func SetInfo(region, t string) {
|
|
|
|
regionAPI = region
|
|
|
|
token = t
|
|
|
|
}
|
2017-12-25 19:49:32 +08:00
|
|
|
func handleErrAndCode(err error, code int) *util.APIHandleError {
|
2017-12-21 18:33:41 +08:00
|
|
|
if err != nil {
|
2017-12-25 19:49:32 +08:00
|
|
|
return util.CreateAPIHandleError(code, err)
|
2017-12-21 18:33:41 +08:00
|
|
|
}
|
2018-12-08 17:19:53 +08:00
|
|
|
if code >= 300 {
|
2017-12-25 19:49:32 +08:00
|
|
|
return util.CreateAPIHandleError(code, fmt.Errorf("error with code %d", code))
|
2017-12-21 18:33:41 +08:00
|
|
|
}
|
|
|
|
return nil
|
2017-12-25 19:49:32 +08:00
|
|
|
}
|
2018-07-16 15:30:53 +08:00
|
|
|
|
|
|
|
//ResourcesInterface ResourcesInterface
|
|
|
|
type ResourcesInterface interface {
|
|
|
|
Tenants(tenantName string) ResourcesTenantInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
type resources struct {
|
2018-07-16 19:06:57 +08:00
|
|
|
regionImpl
|
2018-07-16 15:30:53 +08:00
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resources) Tenants(tenantName string) ResourcesTenantInterface {
|
2018-07-16 19:06:57 +08:00
|
|
|
return &resourcesTenant{prefix: path.Join(r.prefix, "tenants", tenantName), resources: *r}
|
2018-07-16 15:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//ResourcesTenantInterface ResourcesTenantInterface
|
|
|
|
type ResourcesTenantInterface interface {
|
|
|
|
Get() (*model.TenantResource, *util.APIHandleError)
|
|
|
|
}
|
|
|
|
type resourcesTenant struct {
|
2018-07-16 19:06:57 +08:00
|
|
|
resources
|
2018-07-16 15:30:53 +08:00
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resourcesTenant) Get() (*model.TenantResource, *util.APIHandleError) {
|
2018-07-16 19:06:57 +08:00
|
|
|
var rt model.TenantResource
|
|
|
|
var decode utilhttp.ResponseBody
|
|
|
|
decode.Bean = &rt
|
|
|
|
code, err := r.DoRequest(r.prefix+"/res", "GET", nil, &decode)
|
2018-07-16 15:30:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, handleErrAndCode(err, code)
|
|
|
|
}
|
|
|
|
return &rt, nil
|
|
|
|
}
|
2018-12-08 17:19:53 +08:00
|
|
|
|
|
|
|
func handleAPIResult(code int, res utilhttp.ResponseBody) *util.APIHandleError {
|
|
|
|
if code >= 300 {
|
|
|
|
return util.CreateAPIHandleErrorf(code, res.Msg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|