Rainbond/api/region/region.go

265 lines
6.8 KiB
Go
Raw Normal View History

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/>.
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"
"errors"
2017-12-25 19:49:32 +08:00
"fmt"
2018-07-16 19:06:57 +08:00
"io"
"io/ioutil"
2017-11-22 15:09:28 +08:00
"net/http"
"path"
2017-11-22 15:09:28 +08:00
2018-08-02 12:16:50 +08:00
"github.com/Sirupsen/logrus"
"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"
utilhttp "github.com/goodrain/rainbond/util/http"
)
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
Tasks() TaskInterface
Nodes() NodeInterface
Cluster() ClusterInterface
Configs() ConfigsInterface
2018-07-16 19:06:57 +08:00
Version() string
Monitor() MonitorInterface
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 := &regionImpl{
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-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}
}
2018-08-02 12:16:50 +08:00
func (r *regionImpl) GetEndpoint() string {
return r.Endpoints[0]
}
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)
if err != nil {
2018-07-16 19:06:57 +08:00
return 500, err
}
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)
}
2018-08-02 12:16:50 +08:00
res, err := r.Client.Do(request)
if err != nil {
2018-07-16 19:06:57 +08:00
return 500, err
}
if res.Body != nil {
defer res.Body.Close()
}
if decode != nil {
if err := json.NewDecoder(res.Body).Decode(decode); err != nil {
return res.StatusCode, err
}
}
2018-07-16 19:06:57 +08:00
return res.StatusCode, err
}
//LoadConfig load config
func LoadConfig(regionAPI, token string) (map[string]map[string]interface{}, error) {
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
}
return nil, errors.New("wrong region api ")
}
//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 {
if err != nil {
2017-12-25 19:49:32 +08:00
return util.CreateAPIHandleError(code, err)
}
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))
}
return nil
2017-12-25 19:49:32 +08:00
}
//ResourcesInterface ResourcesInterface
type ResourcesInterface interface {
Tenants(tenantName string) ResourcesTenantInterface
}
type resources struct {
2018-07-16 19:06:57 +08:00
regionImpl
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}
}
//ResourcesTenantInterface ResourcesTenantInterface
type ResourcesTenantInterface interface {
Get() (*model.TenantResource, *util.APIHandleError)
}
type resourcesTenant struct {
2018-07-16 19:06:57 +08:00
resources
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)
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
}