/
env_config.go
231 lines (206 loc) · 6.52 KB
/
env_config.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// 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.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package bundle
import (
"fmt"
"github.com/pkg/errors"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle/apierrors"
"github.com/erda-project/erda/pkg/http/httputil"
)
// CreateNamespace 创建配置中心 Namespace
func (b *Bundle) CreateNamespace(createReq apistructs.NamespaceCreateRequest) error {
host, err := b.urls.DOP()
if err != nil {
return err
}
hc := b.hc
var createResp apistructs.NamespaceCreateResponse
resp, err := hc.Post(host).Path("/api/config/namespace").
Header(httputil.InternalHeader, "bundle").
JSONBody(createReq).
Do().JSON(&createResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !createResp.Success {
return toAPIError(resp.StatusCode(), createResp.Error)
}
return nil
}
// CreateNamespaceRelations 创建 Namespace 关联关系
func (b *Bundle) CreateNamespaceRelations(createReq apistructs.NamespaceRelationCreateRequest) error {
host, err := b.urls.DOP()
if err != nil {
return err
}
hc := b.hc
var createResp apistructs.NamespaceRelationCreateResponse
resp, err := hc.Post(host).Path("/api/config/namespace/relation").
Header(httputil.InternalHeader, "bundle").
JSONBody(createReq).
Do().JSON(&createResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !createResp.Success {
return toAPIError(resp.StatusCode(), createResp.Error)
}
return nil
}
// DeleteNamespace 删除配置中心 Namespace
func (b *Bundle) DeleteNamespace(name string) error {
host, err := b.urls.DOP()
if err != nil {
return err
}
hc := b.hc
var deleteResp apistructs.NamespaceDeleteResponse
resp, err := hc.Delete(host).Path("/api/config/namespace").
Header(httputil.InternalHeader, "bundle").
Param("name", name).Do().JSON(&deleteResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !deleteResp.Success {
return toAPIError(resp.StatusCode(), deleteResp.Error)
}
return nil
}
// DeleteNamespaceRelation 删除配置中心 namespace relation
func (b *Bundle) DeleteNamespaceRelation(defaultNamespace string) error {
host, err := b.urls.DOP()
if err != nil {
return err
}
hc := b.hc
var deleteResp apistructs.NamespaceRelationDeleteResponse
resp, err := hc.Delete(host).Path("/api/config/namespace/relation").
Header(httputil.InternalHeader, "bundle").
Param("default_namespace", defaultNamespace).Do().JSON(&deleteResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !deleteResp.Success {
return toAPIError(resp.StatusCode(), deleteResp.Error)
}
return nil
}
// FetchDeploymentConfig 通过 namespace 查询部署配置
// return (ENV, FILE, error)
func (b *Bundle) FetchDeploymentConfig(namespace string) (map[string]string, map[string]string, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, nil, err
}
hc := b.hc
var fetchResp apistructs.EnvConfigFetchResponse
resp, err := hc.Get(host).Path("/api/config/deployment").
Param("namespace_name", namespace).Header(httputil.InternalHeader, "bundle").Do().JSON(&fetchResp)
if err != nil {
return nil, nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !fetchResp.Success {
return nil, nil, toAPIError(resp.StatusCode(), fetchResp.Error)
}
envs := make(map[string]string)
files := make(map[string]string)
for _, c := range fetchResp.Data {
if c.ConfigType == "FILE" {
files[c.Key] = c.Value
} else {
envs[c.Key] = c.Value
}
}
return envs, files, nil
}
// FetchNamespaceConfig 通过 namespace 查询配置.
func (b *Bundle) FetchNamespaceConfig(fetchReq apistructs.EnvConfigFetchRequest) (map[string]string, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc
// auto create namespace if not exist to avoid error [namespace.not.exist].
if fetchReq.AutoCreateIfNotExist {
if fetchReq.CreateReq.ProjectID <= 0 {
return nil, errors.Errorf("invalid projectID: %d", fetchReq.CreateReq.ProjectID)
}
if err := b.CreateNamespace(fetchReq.CreateReq); err != nil {
return nil, err
}
}
var fetchResp apistructs.EnvConfigFetchResponse
resp, err := hc.Get(host).Path("/api/config").
Header(httputil.InternalHeader, "bundle").
Param("namespace_name", fetchReq.Namespace).Param("decrypt", fmt.Sprintf("%t", fetchReq.Decrypt)).
Do().JSON(&fetchResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !fetchResp.Success {
return nil, toAPIError(resp.StatusCode(), fetchResp.Error)
}
kvs := make(map[string]string, 0)
for _, item := range fetchResp.Data {
if item.Key != "" {
kvs[item.Key] = item.Value
}
}
return kvs, nil
}
// AddOrUpdateNamespaceConfig 新增或更新 namespace config .
func (b *Bundle) AddOrUpdateNamespaceConfig(namespace string, items []apistructs.EnvConfig, encrypt bool) error {
host, err := b.urls.DOP()
if err != nil {
return err
}
hc := b.hc
var updateResp apistructs.Header
resp, err := hc.Post(host).
Path("/api/config").
Header(httputil.InternalHeader, "bundle").
Param("namespace_name", namespace).
Param("encrypt", fmt.Sprintf("%t", encrypt)).
JSONBody(&apistructs.EnvConfigAddOrUpdateRequest{Configs: items}).
Do().JSON(&updateResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !updateResp.Success {
return toAPIError(resp.StatusCode(), updateResp.Error)
}
return nil
}
// DeleteNamespaceConfig 删除 namespace 配置.
func (b *Bundle) DeleteNamespaceConfig(namespace string, key string) error {
host, err := b.urls.DOP()
if err != nil {
return err
}
hc := b.hc
var updateResp apistructs.Header
resp, err := hc.Delete(host).
Path("/api/config").
Header(httputil.InternalHeader, "bundle").
Param("namespace_name", namespace).
Param("key", key).
Do().JSON(&updateResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !updateResp.Success {
return toAPIError(resp.StatusCode(), updateResp.Error)
}
return nil
}