-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
115 lines (95 loc) · 3.51 KB
/
requests.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
package topics
import (
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
)
// CreateOpsBuilder is an interface which is used for creating a kafka topic
type CreateOpsBuilder interface {
ToTopicCreateMap() (map[string]interface{}, error)
}
// CreateOps is a struct that contains all the parameters of create function
type CreateOps struct {
// the name/ID of a topic
Name string `json:"id" required:"true"`
// topic partitions, value range: 1-100
Partition int `json:"partition,omitempty"`
// topic replications, value range: 1-3
Replication int `json:"replication,omitempty"`
// aging time in hours, value range: 1-168, defaults to 72
RetentionTime int `json:"retention_time,omitempty"`
SyncMessageFlush bool `json:"sync_message_flush,omitempty"`
SyncReplication bool `json:"sync_replication,omitempty"`
}
// ToTopicCreateMap is used for type convert
func (ops CreateOps) ToTopicCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(ops, "")
}
// Create a kafka topic with given parameters
func Create(client *golangsdk.ServiceClient, instanceID string, ops CreateOpsBuilder) (r CreateResult) {
b, err := ops.ToTopicCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client, instanceID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// UpdateOptsBuilder is an interface which can build the map paramter of update function
type UpdateOptsBuilder interface {
ToTopicUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts is a struct which represents the parameters of update function
type UpdateOpts struct {
Topics []UpdateItem `json:"topics" required:"true"`
}
// UpdateItem represents the object of one topic in update function
type UpdateItem struct {
// Name can not be updated
Name string `json:"id" required:"true"`
Partition *int `json:"new_partition_numbers,omitempty"`
RetentionTime *int `json:"retention_time,omitempty"`
SyncMessageFlush *bool `json:"sync_message_flush,omitempty"`
SyncReplication *bool `json:"sync_replication,omitempty"`
}
// ToTopicUpdateMap is used for type convert
func (opts UpdateOpts) ToTopicUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update is a method which can be able to update topics
func Update(client *golangsdk.ServiceClient, instanceID string, opts UpdateOptsBuilder) (r UpdateResult) {
body, err := opts.ToTopicUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(rootURL(client, instanceID), body, nil, &golangsdk.RequestOpts{
OkCodes: []int{204},
})
return
}
// Get an topic with detailed information by instance id and topic name
func Get(client *golangsdk.ServiceClient, instanceID, topicName string) (r GetResult) {
_, r.Err = client.Get(getURL(client, instanceID, topicName), &r.Body, nil)
return
}
// List all topics belong to the instance id
func List(client *golangsdk.ServiceClient, instanceID string) (r ListResult) {
_, r.Err = client.Get(rootURL(client, instanceID), &r.Body, nil)
return
}
// Delete given topics belong to the instance id
func Delete(client *golangsdk.ServiceClient, instanceID string, topics []string) (r DeleteResult) {
var delOpts = struct {
Topics []string `json:"topics" required:"true"`
}{Topics: topics}
b, err := golangsdk.BuildRequestBody(delOpts, "")
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(deleteURL(client, instanceID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}