-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcontent.go
152 lines (133 loc) · 4.49 KB
/
content.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
package candlepin_client
import (
"context"
"strings"
caliri "github.com/content-services/caliri/release/v4"
)
const OverrideNameBaseUrl = "baseurl"
const OverrideNameCaCert = "sslcacert"
const OverrideSSLVerifyStatus = "sslverifystatus"
const OverrideModuleHotfixes = "module_hotfixes"
func GetContentID(repoConfigUUID string) string {
return strings.Replace(repoConfigUUID, "-", "", -1)
}
func (c *cpClientImpl) ListContents(ctx context.Context, orgID string) ([]string, []string, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, nil, err
}
labels := []string{}
ids := []string{}
contents, httpResp, err := client.OwnerContentAPI.GetContentsByOwner(ctx, OwnerKey(orgID)).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return labels, ids, errorWithResponseBody("could not fetch contents for owner", httpResp, err)
}
for _, c := range contents {
labels = append(labels, *c.Label)
ids = append(ids, *c.Id)
}
return labels, ids, nil
}
func (c *cpClientImpl) CreateContentBatch(ctx context.Context, orgID string, content []caliri.ContentDTO) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
// required contentDTO params: id, name, label, type, vendor
_, httpResp, err := client.OwnerContentAPI.CreateContentBatch(ctx, OwnerKey(orgID)).ContentDTO(content).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("couldn't create content batch", httpResp, err)
}
return nil
}
// CreateContent
// Creates a content set in candlepin. Note that if you try to create the same content set (presummably with the same ID), candlepion simply returns the previously created content set
func (c *cpClientImpl) CreateContent(ctx context.Context, orgID string, content caliri.ContentDTO) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
// required contentDTO params: id, name, label, type, vendor
_, httpResp, err := client.OwnerContentAPI.CreateContent(ctx, OwnerKey(orgID)).ContentDTO(content).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("couldn't create content batch", httpResp, err)
}
return nil
}
func (c *cpClientImpl) UpdateContent(ctx context.Context, orgID string, repoConfigUUID string, content caliri.ContentDTO) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
_, httpResp, err := client.OwnerContentAPI.UpdateContent(ctx, OwnerKey(orgID), GetContentID(repoConfigUUID)).ContentDTO(content).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return errorWithResponseBody("couldn't update content", httpResp, err)
}
return nil
}
func (c *cpClientImpl) FetchContentsByLabel(ctx context.Context, orgID string, labels []string) ([]caliri.ContentDTO, error) {
if len(labels) == 0 {
return []caliri.ContentDTO{}, nil
}
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return []caliri.ContentDTO{}, err
}
contents, httpResp, err := client.OwnerContentAPI.GetContentsByOwner(ctx, OwnerKey(orgID)).Label(labels).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return contents, errorWithResponseBody("could not fetch contents for owner", httpResp, err)
}
for _, c := range contents {
labels = append(labels, *c.Label)
}
return contents, nil
}
func (c *cpClientImpl) FetchContent(ctx context.Context, orgID string, repoConfigUUID string) (*caliri.ContentDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, err
}
content, httpResp, err := client.OwnerContentAPI.GetContentById(ctx, OwnerKey(orgID), GetContentID(repoConfigUUID)).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 content", httpResp, err)
}
return content, nil
}
func (c *cpClientImpl) DeleteContent(ctx context.Context, orgID string, repoConfigUUID string) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
httpResp, err := client.OwnerContentAPI.RemoveContent(ctx, OwnerKey(orgID), GetContentID(repoConfigUUID)).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
return nil
}
return errorWithResponseBody("couldn't delete content", httpResp, err)
}
return nil
}