-
Notifications
You must be signed in to change notification settings - Fork 465
/
ClusterRepository.go
190 lines (171 loc) · 5.81 KB
/
ClusterRepository.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
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2020 Devtron Labs
*
* 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 repository
import (
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type Cluster struct {
tableName struct{} `sql:"cluster" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
ClusterName string `sql:"cluster_name"`
ServerUrl string `sql:"server_url"`
PrometheusEndpoint string `sql:"prometheus_endpoint"`
Active bool `sql:"active,notnull"`
CdArgoSetup bool `sql:"cd_argo_setup,notnull"`
Config map[string]string `sql:"config"`
PUserName string `sql:"p_username"`
PPassword string `sql:"p_password"`
PTlsClientCert string `sql:"p_tls_client_cert"`
PTlsClientKey string `sql:"p_tls_client_key"`
AgentInstallationStage int `sql:"agent_installation_stage"`
K8sVersion string `sql:"k8s_version"`
ErrorInConnecting string `sql:"error_in_connecting"`
IsVirtualCluster bool `sql:"is_virtual_cluster"`
InsecureSkipTlsVerify bool `sql:"insecure_skip_tls_verify"`
sql.AuditLog
}
type ClusterRepository interface {
Save(model *Cluster) error
FindOne(clusterName string) (*Cluster, error)
FindOneActive(clusterName string) (*Cluster, error)
FindAll() ([]Cluster, error)
FindAllActive() ([]Cluster, error)
FindAllActiveExceptVirtual() ([]Cluster, error)
FindById(id int) (*Cluster, error)
FindByIds(id []int) ([]Cluster, error)
Update(model *Cluster) error
Delete(model *Cluster) error
MarkClusterDeleted(model *Cluster) error
UpdateClusterConnectionStatus(clusterId int, errorInConnecting string) error
FindActiveClusters() ([]Cluster, error)
SaveAll(models []*Cluster) error
FindByNames(clusterNames []string) ([]*Cluster, error)
}
func NewClusterRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *ClusterRepositoryImpl {
return &ClusterRepositoryImpl{
dbConnection: dbConnection,
logger: logger,
}
}
type ClusterRepositoryImpl struct {
dbConnection *pg.DB
logger *zap.SugaredLogger
}
func (impl ClusterRepositoryImpl) Save(model *Cluster) error {
return impl.dbConnection.Insert(model)
}
func (impl ClusterRepositoryImpl) FindOne(clusterName string) (*Cluster, error) {
cluster := &Cluster{}
err := impl.dbConnection.
Model(cluster).
Where("cluster_name =?", clusterName).
Where("active =?", true).
Limit(1).
Select()
return cluster, err
}
func (impl ClusterRepositoryImpl) SaveAll(models []*Cluster) error {
return impl.dbConnection.Insert(models)
}
func (impl ClusterRepositoryImpl) FindOneActive(clusterName string) (*Cluster, error) {
cluster := &Cluster{}
err := impl.dbConnection.
Model(cluster).
Where("cluster_name =?", clusterName).
Where("active=?", true).
Limit(1).
Select()
return cluster, err
}
func (impl ClusterRepositoryImpl) FindAll() ([]Cluster, error) {
var clusters []Cluster
err := impl.dbConnection.
Model(&clusters).
Where("active =?", true).
Select()
return clusters, err
}
func (impl ClusterRepositoryImpl) FindActiveClusters() ([]Cluster, error) {
activeClusters := make([]Cluster, 0)
query := "select id, cluster_name, active from cluster where active = true"
_, err := impl.dbConnection.Query(&activeClusters, query)
return activeClusters, err
}
func (impl ClusterRepositoryImpl) FindAllActive() ([]Cluster, error) {
var clusters []Cluster
err := impl.dbConnection.
Model(&clusters).
Where("active=?", true).
Select()
return clusters, err
}
func (impl ClusterRepositoryImpl) FindAllActiveExceptVirtual() ([]Cluster, error) {
var clusters []Cluster
err := impl.dbConnection.
Model(&clusters).
Where("active=?", true).
Where("is_virtual_cluster=?", false).
Select()
return clusters, err
}
func (impl ClusterRepositoryImpl) FindById(id int) (*Cluster, error) {
cluster := &Cluster{}
err := impl.dbConnection.
Model(cluster).
Where("id =?", id).
Where("active =?", true).
Limit(1).
Select()
return cluster, err
}
func (impl ClusterRepositoryImpl) FindByNames(clusterNames []string) ([]*Cluster, error) {
var cluster []*Cluster
err := impl.dbConnection.
Model(&cluster).
Where("cluster_name in (?)", pg.In(clusterNames)).
Where("active = ?", true).
Select()
return cluster, err
}
func (impl ClusterRepositoryImpl) FindByIds(id []int) ([]Cluster, error) {
var cluster []Cluster
err := impl.dbConnection.
Model(&cluster).
Where("id in(?)", pg.In(id)).
Where("active =?", true).
Select()
return cluster, err
}
func (impl ClusterRepositoryImpl) Update(model *Cluster) error {
return impl.dbConnection.Update(model)
}
func (impl ClusterRepositoryImpl) Delete(model *Cluster) error {
return impl.dbConnection.Delete(model)
}
func (impl ClusterRepositoryImpl) MarkClusterDeleted(model *Cluster) error {
model.Active = false
return impl.dbConnection.Update(model)
}
func (impl ClusterRepositoryImpl) UpdateClusterConnectionStatus(clusterId int, errorInConnecting string) error {
cluster := &Cluster{}
_, err := impl.dbConnection.Model(cluster).
Set("error_in_connecting = ?", errorInConnecting).Where("id = ?", clusterId).
Update()
return err
}