diff --git a/client_interface.go b/client_interface.go index eef5f910..ad1c24ab 100644 --- a/client_interface.go +++ b/client_interface.go @@ -133,6 +133,12 @@ type ClientInterface interface { UpdateLogStoreV2(project string, logstore *LogStore) error // CheckLogstoreExist check logstore exist or not CheckLogstoreExist(project string, logstore string) (bool, error) + // GetLogStoreMeteringMode get the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest + GetLogStoreMeteringMode(project string, logstore string) (*GetMeteringModeResponse, error) + // GetLogStoreMeteringMode update the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest + // + // Warning: this method may affect your billings, for more details ref: https://www.aliyun.com/price/detail/sls + UpdateLogStoreMeteringMode(project string, logstore string, meteringMode string) error // #################### MetricStore Operations ##################### // CreateMetricStore creates a new metric store in SLS. diff --git a/client_project.go b/client_project.go index ea164138..3ef6ff46 100644 --- a/client_project.go +++ b/client_project.go @@ -18,9 +18,10 @@ func (c *Client) ListLogStore(project string) ([]string, error) { } // ListLogStoreV2 list logstores with params : -// offset: start offset -// size: max return size -// telemetryType : telemetry type filter +// +// offset: start offset +// size: max return size +// telemetryType : telemetry type filter func (c *Client) ListLogStoreV2(project string, offset, size int, telemetryType string) ([]string, error) { proj := convert(c, project) return proj.ListLogStoreV2(offset, size, telemetryType) @@ -69,6 +70,19 @@ func (c *Client) UpdateLogStoreV2(project string, logstore *LogStore) (err error return proj.UpdateLogStoreV2(logstore) } +// GetLogStoreMeteringMode get the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest +func (c *Client) GetLogStoreMeteringMode(project string, logstore string) (*GetMeteringModeResponse, error) { + ls := convertLogstore(c, project, logstore) + return ls.GetMeteringMode() +} + +// GetLogStoreMeteringMode update the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest +// Warning: this method may affect your billings, for more details ref: https://www.aliyun.com/price/detail/sls +func (c *Client) UpdateLogStoreMeteringMode(project string, logstore string, meteringMode string) error { + ls := convertLogstore(c, project, logstore) + return ls.UpdateMeteringMode(meteringMode) +} + // 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. func (c *Client) ListMachineGroup(project string, offset, size int) (m []string, total int, err error) { diff --git a/client_test.go b/client_test.go index 5d5052e5..bc0aa2b3 100644 --- a/client_test.go +++ b/client_test.go @@ -88,3 +88,22 @@ func (s *ClientTestSuite) TestClientCommonHeader() { s.Require().NoError(err) fmt.Println(len(stores)) } + +func (s *ClientTestSuite) TestMeteringMode() { + + res, err := s.client.GetLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName) + s.Require().NoError(err) + s.Require().Equal(CHARGE_BY_FUNCTION, res.MeteringMode) + // change to data ingest + err = s.client.UpdateLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName, CHARGE_BY_DATA_INGEST) + s.Require().NoError(err) + res, err = s.client.GetLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName) + s.Require().NoError(err) + s.Require().Equal(CHARGE_BY_DATA_INGEST, res.MeteringMode) + // change back + err = s.client.UpdateLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName, CHARGE_BY_FUNCTION) + s.Require().NoError(err) + res, err = s.client.GetLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName) + s.Require().NoError(err) + s.Require().Equal(CHARGE_BY_FUNCTION, res.MeteringMode) +} diff --git a/error_code.go b/error_code.go index cb83ea45..9d105e1c 100644 --- a/error_code.go +++ b/error_code.go @@ -52,3 +52,4 @@ const CONFIG_NOT_EXIST = "ConfigNotExist" const CONFIG_ALREADY_EXIST = "ConfigAlreadyExist" const BAD_REQUEST = "BadRequest" const INVALID_PARAMETER = "InvalidParameter" +const NOT_SUPPORTED = "NotSupported" diff --git a/log_store.go b/log_store.go index 60bd84c5..a7be28be 100644 --- a/log_store.go +++ b/log_store.go @@ -1076,3 +1076,53 @@ func (s *LogStore) CheckIndexExist() (bool, error) { return true, nil } + +func (s *LogStore) GetMeteringMode() (*GetMeteringModeResponse, error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + uri := fmt.Sprintf("/logstores/%s/meteringmode", s.Name) + r, err := request(s.project, "GET", uri, h, nil) + if err != nil { + return nil, err + } + defer r.Body.Close() + data, err := ioutil.ReadAll(r.Body) + if err != nil { + return nil, NewBadResponseError("", r.Header, r.StatusCode) + } + res := GetMeteringModeResponse{} + err = json.Unmarshal(data, &res) + if err != nil { + return nil, NewBadResponseError(string(data), r.Header, r.StatusCode) + + } + return &res, nil + +} + +func (s *LogStore) UpdateMeteringMode(meteringMode string) error { + + body := map[string]string{ + "meteringMode": meteringMode, + } + uri := fmt.Sprintf("/logstores/%s/meteringmode", s.Name) + requestBody, err := json.Marshal(body) + if err != nil { + return fmt.Errorf("cant marshal body:%w", err) + } + + h := map[string]string{ + "Content-Type": "application/json", + "x-log-bodyrawsize": strconv.Itoa(len(requestBody)), + } + + r, err := request(s.project, "PUT", uri, h, requestBody) + + if err != nil { + r.Body.Close() + return err + } + return nil +} diff --git a/model.go b/model.go index d214bdef..f62ef229 100644 --- a/model.go +++ b/model.go @@ -293,3 +293,12 @@ func CreateDefaultIndex() *Index { }, } } + +type GetMeteringModeResponse struct { + MeteringMode string `json:"meteringMode"` +} + +const ( + CHARGE_BY_FUNCTION = "ChargeByFunction" + CHARGE_BY_DATA_INGEST = "ChargeByDataIngest" +) diff --git a/token_auto_update_client.go b/token_auto_update_client.go index 020ad48d..d76bb132 100644 --- a/token_auto_update_client.go +++ b/token_auto_update_client.go @@ -326,6 +326,26 @@ func (c *TokenAutoUpdateClient) ListMachineGroup(project string, offset, size in return } +func (c *TokenAutoUpdateClient) GetLogStoreMeteringMode(project string, logstore string) (res *GetMeteringModeResponse, err error) { + for i := 0; i < c.maxTryTimes; i++ { + res, err = c.logClient.GetLogStoreMeteringMode(project, logstore) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) UpdateLogStoreMeteringMode(project string, logstore string, meteringMode string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.UpdateLogStoreMeteringMode(project, logstore, meteringMode) + if !c.processError(err) { + return + } + } + return +} + func (c *TokenAutoUpdateClient) ListMachines(project, machineGroupName string) (ms []*Machine, total int, err error) { for i := 0; i < c.maxTryTimes; i++ { ms, total, err = c.logClient.ListMachines(project, machineGroupName)