Skip to content

Commit 9237ddf

Browse files
authored
Feature storeview (#283)
* feat: support storeView impl * chore: add const storeType * chore: use const in test * feat: add interface for storeView
1 parent c6f1589 commit 9237ddf

File tree

5 files changed

+500
-0
lines changed

5 files changed

+500
-0
lines changed

client_interface.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ type ClientInterface interface {
170170
// ListEventStore returns all eventStore names of project p.
171171
ListEventStore(project string, offset, size int) ([]string, error)
172172

173+
// #################### StoreView Operations #####################
174+
// CreateStoreView creates a new storeView.
175+
CreateStoreView(project string, storeView *StoreView) error
176+
// UpdateStoreView updates a storeView.
177+
UpdateStoreView(project string, storeView *StoreView) error
178+
// DeleteStoreView deletes a storeView.
179+
DeleteStoreView(project string, storeViewName string) error
180+
// GetStoreView returns storeView.
181+
GetStoreView(project string, storeViewName string) (*StoreView, error)
182+
// ListStoreViews returns all storeView names of a project.
183+
ListStoreViews(project string, req *ListStoreViewsRequest) (*ListStoreViewsResponse, error)
184+
// GetStoreViewIndex returns all index config of logstores in the storeView, only support storeType logstore.
185+
GetStoreViewIndex(project string, storeViewName string) (*GetStoreViewIndexResponse, error)
186+
173187
// #################### Logtail Operations #####################
174188
// ListMachineGroup returns machine group name list and the total number of machine groups.
175189
// The offset starts from 0 and the size is the max number of machine groups could be returned.

client_store_view.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package sls
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
)
8+
9+
func (c *Client) CreateStoreView(project string, storeView *StoreView) error {
10+
body, err := json.Marshal(storeView)
11+
if err != nil {
12+
return NewClientError(err)
13+
}
14+
h := map[string]string{
15+
"Content-Type": "application/json",
16+
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
17+
}
18+
uri := "/storeviews"
19+
r, err := c.request(project, "POST", uri, h, body)
20+
if err != nil {
21+
return err
22+
}
23+
r.Body.Close()
24+
return nil
25+
}
26+
27+
func (c *Client) UpdateStoreView(project string, storeView *StoreView) error {
28+
body, err := json.Marshal(storeView)
29+
if err != nil {
30+
return NewClientError(err)
31+
}
32+
h := map[string]string{
33+
"Content-Type": "application/json",
34+
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
35+
}
36+
uri := "/storeviews/" + storeView.Name
37+
r, err := c.request(project, "PUT", uri, h, body)
38+
if err != nil {
39+
return err
40+
}
41+
r.Body.Close()
42+
return nil
43+
}
44+
45+
func (c *Client) DeleteStoreView(project string, storeViewName string) error {
46+
h := map[string]string{
47+
"Content-Type": "application/json",
48+
"x-log-bodyrawsize": "0",
49+
}
50+
uri := "/storeviews/" + storeViewName
51+
r, err := c.request(project, "DELETE", uri, h, nil)
52+
if err != nil {
53+
return err
54+
}
55+
r.Body.Close()
56+
return nil
57+
}
58+
59+
func (c *Client) GetStoreView(project string, storeViewName string) (*StoreView, error) {
60+
h := map[string]string{
61+
"Content-Type": "application/json",
62+
"x-log-bodyrawsize": "0",
63+
}
64+
uri := "/storeviews/" + storeViewName
65+
r, err := c.request(project, "GET", uri, h, nil)
66+
if err != nil {
67+
return nil, err
68+
}
69+
defer r.Body.Close()
70+
buf, err := ioutil.ReadAll(r.Body)
71+
if err != nil {
72+
return nil, err
73+
}
74+
res := &StoreView{}
75+
if err = json.Unmarshal(buf, res); err != nil {
76+
return nil, NewClientError(err)
77+
}
78+
res.Name = storeViewName
79+
return res, nil
80+
}
81+
82+
func (c *Client) ListStoreViews(project string, req *ListStoreViewsRequest) (*ListStoreViewsResponse, error) {
83+
h := map[string]string{
84+
"Content-Type": "application/json",
85+
"x-log-bodyrawsize": "0",
86+
}
87+
uri := fmt.Sprintf("/storeviews?offset=%d&line=%d", req.Offset, req.Size)
88+
r, err := c.request(project, "GET", uri, h, nil)
89+
if err != nil {
90+
return nil, err
91+
}
92+
defer r.Body.Close()
93+
buf, err := ioutil.ReadAll(r.Body)
94+
if err != nil {
95+
return nil, err
96+
}
97+
res := &ListStoreViewsResponse{}
98+
if err = json.Unmarshal(buf, res); err != nil {
99+
return nil, NewClientError(err)
100+
}
101+
return res, nil
102+
}
103+
104+
func (c *Client) GetStoreViewIndex(project string, storeViewName string) (*GetStoreViewIndexResponse, error) {
105+
h := map[string]string{
106+
"Content-Type": "application/json",
107+
"x-log-bodyrawsize": "0",
108+
}
109+
uri := fmt.Sprintf("/storeviews/%s/index", storeViewName)
110+
r, err := c.request(project, "GET", uri, h, nil)
111+
if err != nil {
112+
return nil, err
113+
}
114+
defer r.Body.Close()
115+
buf, err := ioutil.ReadAll(r.Body)
116+
if err != nil {
117+
return nil, err
118+
}
119+
res := &GetStoreViewIndexResponse{}
120+
if err = json.Unmarshal(buf, res); err != nil {
121+
return nil, NewClientError(err)
122+
}
123+
return res, nil
124+
}

0 commit comments

Comments
 (0)