/
consumer_group_consumer_service.go
99 lines (85 loc) · 2.97 KB
/
consumer_group_consumer_service.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
package kong
import (
"context"
"fmt"
"net/url"
)
// AbstractConsumerGroupConsumerService handles ConsumerGroups' Consumers in Kong.
type AbstractConsumerGroupConsumerService interface {
// Create creates a ConsumerGroupConsumer in Kong.
Create(ctx context.Context, consumerGroupNameOrID *string, consumerNameOrID *string) (*ConsumerGroupObject, error)
// Delete deletes a ConsumerGroupConsumer in Kong
Delete(ctx context.Context, consumerGroupNameOrID *string, consumerNameOrID *string) error
// ListAll fetches all ConsumerGroup's Consumers in Kong.
ListAll(ctx context.Context, consumerGroupNameOrID *string) (*ConsumerGroupObject, error)
}
// ConsumerGroupService handles ConsumerGroup in Kong.
type ConsumerGroupConsumerService service
// Create creates a ConsumerGroupConsumer in Kong.
// If an ID is specified, it will be used to
// create a consumer for a consumer group in Kong, otherwise an ID
// is auto-generated.
func (s *ConsumerGroupConsumerService) Create(ctx context.Context,
consumerGroupNameOrID *string, consumerNameOrID *string,
) (*ConsumerGroupObject, error) {
if isEmptyString(consumerGroupNameOrID) {
return nil, fmt.Errorf("consumerGroupNameOrID cannot be nil for Create operation")
}
if isEmptyString(consumerNameOrID) {
return nil, fmt.Errorf("consumerNameOrID cannot be nil for Create operation")
}
queryPath := "/consumer_groups/" + *consumerGroupNameOrID + "/consumers"
data := url.Values{}
data.Set("consumer", *consumerNameOrID)
req, err := s.client.NewRequest("POST", queryPath, nil, data)
if err != nil {
return nil, err
}
var cg ConsumerGroupObject
_, err = s.client.Do(ctx, req, &cg)
if err != nil {
return nil, err
}
return &cg, nil
}
// Delete deletes a ConsumerGroupConsumer in Kong
func (s *ConsumerGroupConsumerService) Delete(ctx context.Context,
consumerGroupNameOrID *string, consumerNameOrID *string,
) error {
if isEmptyString(consumerGroupNameOrID) {
return fmt.Errorf("consumerGroupNameOrID cannot be nil for Delete operation")
}
if isEmptyString(consumerNameOrID) {
return fmt.Errorf("consumerNameOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf(
"/consumer_groups/%v/consumers/%v", *consumerGroupNameOrID, *consumerNameOrID,
)
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
// List fetches a list all of ConsumerGroup's consumers in Kong.
func (s *ConsumerGroupConsumerService) ListAll(
ctx context.Context, consumerGroupNameOrID *string,
) (*ConsumerGroupObject, error) {
if isEmptyString(consumerGroupNameOrID) {
return nil, fmt.Errorf("consumerGroupNameOrID cannot be nil for ListAll operation")
}
endpoint := fmt.Sprintf(
"/consumer_groups/%v/consumers", *consumerGroupNameOrID,
)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var cg ConsumerGroupObject
_, err = s.client.Do(ctx, req, &cg)
if err != nil {
return nil, err
}
return &cg, nil
}