This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 875
/
ip_pool.go
181 lines (169 loc) · 4.4 KB
/
ip_pool.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package service
import (
"github.com/KubeOperator/KubeOperator/pkg/constant"
"github.com/KubeOperator/KubeOperator/pkg/controller/condition"
"github.com/KubeOperator/KubeOperator/pkg/controller/page"
"github.com/KubeOperator/KubeOperator/pkg/db"
"github.com/KubeOperator/KubeOperator/pkg/dto"
"github.com/KubeOperator/KubeOperator/pkg/model"
"github.com/KubeOperator/KubeOperator/pkg/model/common"
"github.com/KubeOperator/KubeOperator/pkg/repository"
dbUtil "github.com/KubeOperator/KubeOperator/pkg/util/db"
)
type IpPoolService interface {
Get(name string) (dto.IpPool, error)
Page(num, size int, conditions condition.Conditions) (*page.Page, error)
Create(creation dto.IpPoolCreate) (dto.IpPool, error)
Batch(op dto.IpPoolOp) error
List(conditions condition.Conditions) ([]dto.IpPool, error)
Delete(name string) error
}
type ipPoolService struct {
ipPoolRepo repository.IpPoolRepository
ipService IpService
}
func NewIpPoolService() IpPoolService {
return &ipPoolService{
ipPoolRepo: repository.NewIpPoolRepository(),
ipService: NewIpService(),
}
}
func (i ipPoolService) Get(name string) (dto.IpPool, error) {
var ipPoolDTO dto.IpPool
ipPool, err := i.ipPoolRepo.Get(name)
if err != nil {
return ipPoolDTO, err
}
ipPoolDTO.IpPool = ipPool
return ipPoolDTO, nil
}
func (i ipPoolService) Page(num, size int, conditions condition.Conditions) (*page.Page, error) {
var (
p page.Page
ipPoolDTOS []dto.IpPool
ipPools []model.IpPool
)
d := db.DB.Model(model.IpPool{})
if err := dbUtil.WithConditions(&d, model.IpPool{}, conditions); err != nil {
return nil, err
}
if err := d.Count(&p.Total).Preload("Ips").Offset((num - 1) * size).Limit(size).Find(&ipPools).Error; err != nil {
return nil, err
}
for _, mo := range ipPools {
ipUsed := 0
for _, ip := range mo.Ips {
if ip.Status != constant.IpAvailable {
ipUsed++
}
}
ipPoolDTOS = append(ipPoolDTOS, dto.IpPool{
IpPool: mo,
IpUsed: ipUsed,
})
}
p.Items = ipPoolDTOS
return &p, nil
}
func (i ipPoolService) List(conditions condition.Conditions) ([]dto.IpPool, error) {
var ipPoolDTOS []dto.IpPool
var ipPools []model.IpPool
d := db.DB.Model(model.IpPool{})
if err := dbUtil.WithConditions(&d, model.IpPool{}, conditions); err != nil {
return nil, err
}
err := d.Preload("Ips").Find(&ipPools).Error
if err != nil {
return ipPoolDTOS, err
}
for _, mo := range ipPools {
ipUsed := 0
for _, ip := range mo.Ips {
if ip.Status != constant.IpAvailable {
ipUsed++
}
}
ipPoolDTOS = append(ipPoolDTOS, dto.IpPool{
IpPool: mo,
IpUsed: ipUsed,
})
}
return ipPoolDTOS, nil
}
func (i ipPoolService) Create(creation dto.IpPoolCreate) (dto.IpPool, error) {
var ipPoolDTO dto.IpPool
ipPool := model.IpPool{
BaseModel: common.BaseModel{},
Name: creation.Name,
Description: creation.Description,
Subnet: creation.Subnet,
}
tx := db.DB.Begin()
err := tx.Create(&ipPool).Error
if err != nil {
tx.Rollback()
return ipPoolDTO, err
}
err = i.ipService.Create(dto.IpCreate{
IpStart: creation.IpStart,
IpEnd: creation.IpEnd,
Gateway: creation.Gateway,
IpPoolName: ipPool.Name,
DNS1: creation.DNS1,
DNS2: creation.DNS2,
}, tx)
if err != nil {
tx.Rollback()
return ipPoolDTO, err
}
tx.Commit()
ipPoolDTO.IpPool = ipPool
return ipPoolDTO, err
}
func (i ipPoolService) Delete(name string) error {
ipPool, err := i.Get(name)
if err != nil {
return err
}
tx := db.DB.Begin()
if err := tx.Delete(&ipPool).Error; err != nil {
return err
}
if err := tx.Where("ip_pool_id = ?", ipPool.ID).Delete(&model.Ip{}).Error; err != nil {
return err
}
tx.Commit()
return nil
}
func (i ipPoolService) Batch(op dto.IpPoolOp) error {
var opItems []model.IpPool
for _, item := range op.Items {
opItems = append(opItems, model.IpPool{
BaseModel: common.BaseModel{},
Name: item.Name,
})
}
tx := db.DB.Begin()
switch op.Operation {
case constant.BatchOperationDelete:
for i := range opItems {
var ipPool model.IpPool
if err := tx.Where("name = ?", opItems[i].Name).First(&ipPool).Error; err != nil {
tx.Rollback()
return err
}
if err := tx.Delete(&ipPool).Error; err != nil {
tx.Rollback()
return err
}
if err := tx.Where("ip_pool_id = ?", ipPool.ID).Delete(&model.Ip{}).Error; err != nil {
tx.Rollback()
return err
}
}
default:
return constant.NotSupportedBatchOperation
}
tx.Commit()
return nil
}