balance.go 4.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5
package balance

import (
	"errors"
	"fmt"
黄孟柱 已提交
6 7 8
	"github.com/eolinker/goku-api-gateway/console/module/service"
	driver2 "github.com/eolinker/goku-api-gateway/server/driver"
	entity "github.com/eolinker/goku-api-gateway/server/entity/balance-entity-service"
E
eoLinker API Management 已提交
9 10
	"time"

黄孟柱 已提交
11 12
	"github.com/eolinker/goku-api-gateway/server/dao"
	dao_balance "github.com/eolinker/goku-api-gateway/server/dao/dao-balance"
E
eoLinker API Management 已提交
13 14
)

Y
Your Name 已提交
15
//Add 新增负载
E
eoLinker API Management 已提交
16 17
func Add(info *Param) (string, error) {
	const TableName = "goku_balance"
Y
Your Name 已提交
18 19 20
	serviceInfo, err := service.Get(info.ServiceName)
	if err != nil {
		return fmt.Sprintf("serviceName:%s", err.Error()), err
E
eoLinker API Management 已提交
21 22
	}
	switch serviceInfo.Type {
Y
Your Name 已提交
23 24 25 26 27 28 29 30 31 32 33
	case driver2.Static:
		{
			if info.Static == "" && info.StaticCluster == "" {
				return "param:static 和 staticCluster 不能同时为空", errors.New("param:static 和 staticCluster 不能同时为空")
			}
			now := time.Now().Format("2006-01-02 15:04:05")
			result, err := dao_balance.AddStatic(info.Name, info.ServiceName, info.Static, info.StaticCluster, info.Desc, now)
			if err == nil {
				dao.UpdateTable(TableName)
			}
			return result, err
E
eoLinker API Management 已提交
34
		}
Y
Your Name 已提交
35 36 37 38 39 40 41 42 43 44 45
	case driver2.Discovery:
		{
			if info.AppName == "" {
				return "param:appName 不能为空", errors.New("param:appName 不能为空")
			}
			now := time.Now().Format("2006-01-02 15:04:05")
			result, err := dao_balance.AddDiscovery(info.Name, info.ServiceName, info.AppName, info.Desc, now)
			if err == nil {
				dao.UpdateTable(TableName)
			}
			return result, err
E
eoLinker API Management 已提交
46 47 48 49 50 51 52
		}

	}

	return "无效serviceName", errors.New("invalid serviceName")
}

Y
Your Name 已提交
53
//Save 保存负载
E
eoLinker API Management 已提交
54 55
func Save(info *Param) (string, error) {
	const TableName = "goku_balance"
Y
Your Name 已提交
56 57 58
	serviceInfo, err := service.Get(info.ServiceName)
	if err != nil {
		return fmt.Sprintf("serviceName:%s", err.Error()), err
E
eoLinker API Management 已提交
59 60
	}
	switch serviceInfo.Type {
Y
Your Name 已提交
61 62 63 64 65 66 67 68 69 70 71
	case driver2.Static:
		{
			if info.Static == "" && info.StaticCluster == "" {
				return "param:static 和 staticCluster 不能同时为空", errors.New("param:static 和 staticCluster 不能同时为空")
			}
			now := time.Now().Format("2006-01-02 15:04:05")
			result, err := dao_balance.SaveStatic(info.Name, info.ServiceName, info.Static, info.StaticCluster, info.Desc, now)
			if err == nil {
				dao.UpdateTable(TableName)
			}
			return result, err
E
eoLinker API Management 已提交
72
		}
Y
Your Name 已提交
73 74 75 76 77 78 79 80 81 82 83
	case driver2.Discovery:
		{
			if info.AppName == "" {
				return "param:appName 不能为空", errors.New("param:appName 不能为空")
			}
			now := time.Now().Format("2006-01-02 15:04:05")
			result, err := dao_balance.SaveDiscover(info.Name, info.ServiceName, info.AppName, info.Desc, now)
			if err == nil {
				dao.UpdateTable(TableName)
			}
			return result, err
E
eoLinker API Management 已提交
84 85 86 87 88 89
		}

	}

	return "无效serviceName", errors.New("invalid serviceName")
}
Y
Your Name 已提交
90 91

//Get 获取负载
E
eoLinker API Management 已提交
92 93 94 95 96 97
func Get(name string) (*Info, error) {
	b, e := dao_balance.Get(name)
	if e != nil {
		return nil, e
	}

Y
Your Name 已提交
98
	return ReadInfo(b), nil
E
eoLinker API Management 已提交
99
}
Y
Your Name 已提交
100 101

//Search 查询
Y
Your Name 已提交
102
func Search(keyworkd string) ([]*Info, error) {
E
eoLinker API Management 已提交
103
	var entities []*entity.Balance
Y
Your Name 已提交
104 105
	if keyworkd == "" {
		es, e := dao_balance.GetAll()
E
eoLinker API Management 已提交
106 107 108 109
		if e != nil {
			return nil, e
		}
		entities = es
Y
Your Name 已提交
110 111
	} else {
		es, e := dao_balance.Search(keyworkd)
E
eoLinker API Management 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124
		if e != nil {
			return nil, e
		}
		entities = es
	}

	infos := make([]*Info, 0, len(entities))

	for _, ent := range entities {
		infos = append(infos, ReadInfo(ent))
	}
	return infos, nil
}
Y
Your Name 已提交
125 126

//GetAll 获取所有负载信息
E
eoLinker API Management 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
func GetAll() ([]*Info, error) {

	entities, e := dao_balance.GetAll()
	if e != nil {
		return nil, e
	}
	infos := make([]*Info, 0, len(entities))

	for _, ent := range entities {
		infos = append(infos, ReadInfo(ent))
	}
	return infos, nil
}

Y
Your Name 已提交
141
//Delete 删除
E
eoLinker API Management 已提交
142 143 144 145 146 147 148 149 150
func Delete(name string) (string, error) {
	tableName := "goku_balance"
	result, err := dao_balance.Delete(name)
	if err == nil {
		dao.UpdateTable(tableName)
	}
	return result, err
}

Y
Your Name 已提交
151
//GetBalancNames 获取所有负载名称
E
eoLinker API Management 已提交
152 153 154 155
func GetBalancNames() (bool, []string, error) {
	return dao_balance.GetBalanceNames()
}

Y
Your Name 已提交
156
//BatchDeleteBalance 批量删除负载
E
eoLinker API Management 已提交
157 158 159 160 161 162 163 164
func BatchDeleteBalance(balanceNames []string) (string, error) {
	tableName := "goku_balance"
	result, err := dao_balance.BatchDelete(balanceNames)
	if err == nil {
		dao.UpdateTable(tableName)
	}
	return result, err
}