-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconsumer.go
70 lines (61 loc) · 1.87 KB
/
consumer.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
package candlepin_client
import (
"context"
"net/http"
caliri "github.com/content-services/caliri/release/v4"
"github.com/content-services/content-sources-backend/pkg/utils"
)
func (c *cpClientImpl) CreateConsumer(ctx context.Context, orgID string, name string) (*caliri.ConsumerDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, err
}
consumerDTO := caliri.ConsumerDTO{
Name: utils.Ptr(name),
Type: &caliri.ConsumerTypeDTO{
Label: utils.Ptr("system"),
},
}
consumer, httpResp, err := client.ConsumerAPI.CreateConsumer(ctx).Owner(OwnerKey(orgID)).ConsumerDTO(consumerDTO).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
return nil, errorWithResponseBody("couldn't create consumer", httpResp, err)
}
return consumer, nil
}
func (c *cpClientImpl) FetchConsumer(ctx context.Context, consumerUUID string) (*caliri.ConsumerDTO, error) {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return nil, err
}
consumer, httpResp, err := client.ConsumerAPI.GetConsumer(ctx, consumerUUID).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, errorWithResponseBody("couldn't list consumers", httpResp, err)
}
return consumer, nil
}
func (c *cpClientImpl) DeleteConsumer(ctx context.Context, consumerUUID string) error {
ctx, client, err := getCandlepinClient(ctx)
if err != nil {
return err
}
httpResp, err := client.ConsumerAPI.DeleteConsumer(ctx, consumerUUID).Execute()
if httpResp != nil {
defer httpResp.Body.Close()
}
if err != nil {
if httpResp != nil && (httpResp.StatusCode == http.StatusNotFound || httpResp.StatusCode == http.StatusGone) {
return nil
}
return errorWithResponseBody("couldn't delete consumer", httpResp, err)
}
return nil
}