Skip to content

Commit

Permalink
Feature storeview (#283)
Browse files Browse the repository at this point in the history
* feat: support storeView impl

* chore: add const storeType

* chore: use const in test

* feat: add interface for storeView
  • Loading branch information
crimson-gao committed Jun 13, 2024
1 parent c6f1589 commit 9237ddf
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ type ClientInterface interface {
// ListEventStore returns all eventStore names of project p.
ListEventStore(project string, offset, size int) ([]string, error)

// #################### StoreView Operations #####################
// CreateStoreView creates a new storeView.
CreateStoreView(project string, storeView *StoreView) error
// UpdateStoreView updates a storeView.
UpdateStoreView(project string, storeView *StoreView) error
// DeleteStoreView deletes a storeView.
DeleteStoreView(project string, storeViewName string) error
// GetStoreView returns storeView.
GetStoreView(project string, storeViewName string) (*StoreView, error)
// ListStoreViews returns all storeView names of a project.
ListStoreViews(project string, req *ListStoreViewsRequest) (*ListStoreViewsResponse, error)
// GetStoreViewIndex returns all index config of logstores in the storeView, only support storeType logstore.
GetStoreViewIndex(project string, storeViewName string) (*GetStoreViewIndexResponse, error)

// #################### Logtail Operations #####################
// ListMachineGroup returns machine group name list and the total number of machine groups.
// The offset starts from 0 and the size is the max number of machine groups could be returned.
Expand Down
124 changes: 124 additions & 0 deletions client_store_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package sls

import (
"encoding/json"
"fmt"
"io/ioutil"
)

func (c *Client) CreateStoreView(project string, storeView *StoreView) error {
body, err := json.Marshal(storeView)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
}
uri := "/storeviews"
r, err := c.request(project, "POST", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}

func (c *Client) UpdateStoreView(project string, storeView *StoreView) error {
body, err := json.Marshal(storeView)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
}
uri := "/storeviews/" + storeView.Name
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}

func (c *Client) DeleteStoreView(project string, storeViewName string) error {
h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": "0",
}
uri := "/storeviews/" + storeViewName
r, err := c.request(project, "DELETE", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}

func (c *Client) GetStoreView(project string, storeViewName string) (*StoreView, error) {
h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": "0",
}
uri := "/storeviews/" + storeViewName
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
res := &StoreView{}
if err = json.Unmarshal(buf, res); err != nil {
return nil, NewClientError(err)
}
res.Name = storeViewName
return res, nil
}

func (c *Client) ListStoreViews(project string, req *ListStoreViewsRequest) (*ListStoreViewsResponse, error) {
h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/storeviews?offset=%d&line=%d", req.Offset, req.Size)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
res := &ListStoreViewsResponse{}
if err = json.Unmarshal(buf, res); err != nil {
return nil, NewClientError(err)
}
return res, nil
}

func (c *Client) GetStoreViewIndex(project string, storeViewName string) (*GetStoreViewIndexResponse, error) {
h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/storeviews/%s/index", storeViewName)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
res := &GetStoreViewIndexResponse{}
if err = json.Unmarshal(buf, res); err != nil {
return nil, NewClientError(err)
}
return res, nil
}
Loading

0 comments on commit 9237ddf

Please sign in to comment.