/
gateway_upstream_lb_service.go
120 lines (109 loc) · 3.45 KB
/
gateway_upstream_lb_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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"github.com/pkg/errors"
"github.com/xormplus/xorm"
. "github.com/erda-project/erda/modules/hepa/common/vars"
"github.com/erda-project/erda/modules/hepa/repository/orm"
)
type GatewayUpstreamLbServiceImpl struct {
engine *orm.OrmEngine
}
func NewGatewayUpstreamLbServiceImpl() (*GatewayUpstreamLbServiceImpl, error) {
engine, error := orm.GetSingleton()
if error != nil {
return nil, errors.Wrap(error, "new GatewayUpstreamLbServiceImpl failed")
}
return &GatewayUpstreamLbServiceImpl{engine}, nil
}
func (impl GatewayUpstreamLbServiceImpl) Get(cond *orm.GatewayUpstreamLb) (*orm.GatewayUpstreamLb, error) {
if cond == nil {
return nil, errors.New(ERR_INVALID_ARG)
}
dao := &orm.GatewayUpstreamLb{}
succ, err := orm.GetByAny(impl.engine, dao, cond)
if err != nil {
return nil, errors.Wrap(err, ERR_SQL_FAIL)
}
if !succ {
return nil, nil
}
return dao, nil
}
func (impl GatewayUpstreamLbServiceImpl) GetForUpdate(session *xorm.Session, cond *orm.GatewayUpstreamLb) (*orm.GatewayUpstreamLb, error) {
if cond == nil || cond.OrgId == "" || cond.ProjectId == "" || cond.Env == "" || cond.Az == "" || cond.LbName == "" {
return nil, errors.New(ERR_INVALID_ARG)
}
dao := &orm.GatewayUpstreamLb{}
succ, err := orm.GetForUpdate(session, impl.engine, dao, "org_id = ? and project_id = ? and env = ? and az = ? and lb_name = ?", cond.OrgId, cond.ProjectId, cond.Env, cond.Az, cond.LbName)
if err != nil {
return nil, errors.WithStack(err)
}
if !succ {
return nil, nil
}
return dao, nil
}
func (impl GatewayUpstreamLbServiceImpl) Insert(session *xorm.Session, dao *orm.GatewayUpstreamLb) error {
if dao == nil {
return errors.New(ERR_INVALID_ARG)
}
_, err := orm.Insert(session, dao)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (impl GatewayUpstreamLbServiceImpl) UpdateDeploymentId(id string, deploymentId int) error {
if id == "" {
return errors.New(ERR_INVALID_ARG)
}
_, err := orm.Update(impl.engine, &orm.GatewayUpstreamLb{
BaseRow: orm.BaseRow{Id: id},
LastDeploymentId: deploymentId,
}, "last_deployment_id")
if err != nil {
return errors.Wrap(err, ERR_SQL_FAIL)
}
return nil
}
func (impl GatewayUpstreamLbServiceImpl) GetById(id string) (*orm.GatewayUpstreamLb, error) {
if id == "" {
return nil, errors.New(ERR_INVALID_ARG)
}
dao := &orm.GatewayUpstreamLb{}
succ, err := orm.Get(impl.engine, dao, "id = ?", id)
if err != nil {
return nil, errors.Wrap(err, ERR_SQL_FAIL)
}
if !succ {
return nil, nil
}
return dao, nil
}
func (impl GatewayUpstreamLbServiceImpl) GetByKongId(id string) (*orm.GatewayUpstreamLb, error) {
if id == "" {
return nil, errors.New(ERR_INVALID_ARG)
}
dao := &orm.GatewayUpstreamLb{}
succ, err := orm.Get(impl.engine, dao, "kong_upstream_id = ?", id)
if err != nil {
return nil, errors.Wrap(err, ERR_SQL_FAIL)
}
if !succ {
return nil, nil
}
return dao, nil
}