-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathenvironment.go
228 lines (199 loc) · 7.18 KB
/
environment.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
package candlepin_client
import (
"context"
"strings"
caliri "github.com/content-services/caliri/release/v4"
"github.com/content-services/content-sources-backend/pkg/utils"
)
const ENVIRONMENT_TYPE = "content-template"
func GetEnvironmentID(templateUUID string) string {
return strings.Replace(templateUUID, "-", "", -1)
}
func (c *cpClientImpl) AssociateEnvironment(ctx context.Context, _ string, templateName string, consumerUuid string) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
envs := []caliri.EnvironmentDTO{}
if templateName != "" {
tempName := GetEnvironmentID(templateName)
envs = []caliri.EnvironmentDTO{{Id: &tempName}}
}
httpResp, err := client.ConsumerAPI.UpdateConsumer(ctx, consumerUuid).ConsumerDTO(caliri.ConsumerDTO{Environments: envs}).Execute()
// env, httpResp, err := client.OwnerAPI.CreateEnv(ctx, ownerKey).EnvironmentDTO(caliri.EnvironmentDTO{Id: &id, Name: &name, ContentPrefix: &prefix}).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("couldn't add consumer to environment", httpResp, err)
}
return nil
}
func (c *cpClientImpl) CreateEnvironment(ctx context.Context, orgID string, name string, templateUUID string, prefix string) (*caliri.EnvironmentDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, err
}
envId := GetEnvironmentID(templateUUID)
env, httpResp, err := client.OwnerAPI.CreateEnvironment(ctx, OwnerKey(orgID)).EnvironmentDTO(caliri.EnvironmentDTO{Id: &envId, Name: &name, ContentPrefix: &prefix, Type: utils.Ptr(ENVIRONMENT_TYPE)}).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return nil, errorWithResponseBody("couldn't create environment", httpResp, err)
}
return env, nil
}
func (c *cpClientImpl) FetchEnvironment(ctx context.Context, templateID string) (*caliri.EnvironmentDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, err
}
resp, httpResp, err := client.EnvironmentAPI.GetEnvironment(ctx, GetEnvironmentID(templateID)).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
return nil, nil
}
return nil, errorWithResponseBody("couldn't fetch environment", httpResp, err)
}
return resp, nil
}
func (c *cpClientImpl) PromoteContentToEnvironment(ctx context.Context, templateID string, repoConfigUUIDs []string) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
contentIDs := []string{}
for _, repoUUID := range repoConfigUUIDs {
contentIDs = append(contentIDs, GetContentID(repoUUID))
}
var contentToPromote []caliri.ContentToPromoteDTO
for _, id := range contentIDs {
contentID := id
contentToPromote = append(contentToPromote, caliri.ContentToPromoteDTO{
EnvironmentId: utils.Ptr(GetEnvironmentID(templateID)),
ContentId: &contentID,
Enabled: utils.Ptr(true),
})
}
_, httpResp, err := client.EnvironmentAPI.PromoteContent(ctx, GetEnvironmentID(templateID)).ContentToPromoteDTO(contentToPromote).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("couldn't promote content to environment", httpResp, err)
}
return nil
}
func (c *cpClientImpl) DemoteContentFromEnvironment(ctx context.Context, templateID string, repoConfigUUIDs []string) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
contentIDs := []string{}
for _, repoUUID := range repoConfigUUIDs {
contentIDs = append(contentIDs, GetContentID(repoUUID))
}
_, httpResp, err := client.EnvironmentAPI.DemoteContent(ctx, GetEnvironmentID(templateID)).Content(contentIDs).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("couldn't promote content to environment", httpResp, err)
}
return nil
}
func (c *cpClientImpl) UpdateContentOverrides(ctx context.Context, templateID string, dtos []caliri.ContentOverrideDTO) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
_, httpResp, err := client.EnvironmentAPI.PutEnvironmentContentOverrides(ctx, GetEnvironmentID(templateID)).ContentOverrideDTO(dtos).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("could not override environment contents", httpResp, err)
}
return nil
}
func (c *cpClientImpl) RemoveContentOverrides(ctx context.Context, templateUUID string, toRemove []caliri.ContentOverrideDTO) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
_, httpResp, err := client.EnvironmentAPI.DeleteEnvironmentContentOverrides(ctx, GetEnvironmentID(templateUUID)).ContentOverrideDTO(toRemove).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("could not remove overrides", httpResp, err)
}
return nil
}
func (c *cpClientImpl) FetchContentOverrides(ctx context.Context, templateUUID string) ([]caliri.ContentOverrideDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return []caliri.ContentOverrideDTO{}, err
}
overrides, httpResp, err := client.EnvironmentAPI.GetEnvironmentContentOverrides(ctx, GetEnvironmentID(templateUUID)).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return []caliri.ContentOverrideDTO{}, errorWithResponseBody("could not fetch environment contents", httpResp, err)
}
return overrides, nil
}
// FetchContentOverridesForRepo Behaves just like FetchContentOverrides but returns a subset of overrides only for a given repo
func (c *cpClientImpl) FetchContentOverridesForRepo(ctx context.Context, templateUUID string, label string) ([]caliri.ContentOverrideDTO, error) {
overrides, err := c.FetchContentOverrides(ctx, GetEnvironmentID(templateUUID))
if err != nil {
return []caliri.ContentOverrideDTO{}, err
}
subset := []caliri.ContentOverrideDTO{}
for _, override := range overrides {
if *override.ContentLabel == label {
subset = append(subset, override)
}
}
return subset, nil
}
func (c *cpClientImpl) DeleteEnvironment(ctx context.Context, templateUUID string) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
httpResp, err := client.EnvironmentAPI.DeleteEnvironment(ctx, GetEnvironmentID(templateUUID)).RetainConsumers(true).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
return nil
}
return errorWithResponseBody("couldn't delete environment", httpResp, err)
}
return nil
}
func (c *cpClientImpl) RenameEnvironment(ctx context.Context, templateUUID, name string) (*caliri.EnvironmentDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, err
}
envDto := caliri.EnvironmentDTO{Name: &name}
env, httpResp, err := client.EnvironmentAPI.
UpdateEnvironment(ctx, GetEnvironmentID(templateUUID)).
EnvironmentDTO(envDto).
Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return nil, errorWithResponseBody("couldn't update environment", httpResp, err)
}
return env, nil
}