Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support create/delete project and fix other minor issues. #11

Merged
merged 5 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package sls

import (
"encoding/json"
"fmt"
)

// Error defines sls error
type Error struct {
Code string `json:"errorCode"`
Message string `json:"errorMessage"`
RequestID string `json:"requestID"`
}

// NewClientError new client error
func NewClientError(message string) *Error {
err := new(Error)
err.Code = "ClientError"
err.Message = message
return err
}

func (e Error) String() string {
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return ""
}
return string(b)
}

func (e Error) Error() string {
return e.String()
}

// Client ...
type Client struct {
Endpoint string // IP or hostname of SLS endpoint
AccessKeyID string
AccessKeySecret string
SessionToken string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是参照LogProject里的SessionToken,那我一并改掉吧

}

func convert(c *Client, projName string) *LogProject {
return &LogProject{
Name: projName,
Endpoint: c.Endpoint,
AccessKeyID: c.AccessKeyID,
AccessKeySecret: c.AccessKeySecret,
SessionToken: c.SessionToken,
}
}

// CreateProject create a new loghub project.
func (c *Client) CreateProject(name, description string) (*LogProject, error) {
type Body struct {
ProjectName string `json:"projectName"`
Description string `json:"description"`
}
body, err := json.Marshal(Body{
ProjectName: name,
Description: description,
})
if err != nil {
return nil, err
}

h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%d", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}

uri := "/"
proj := convert(c, name)
_, err = request(proj, "POST", uri, h, body)
if err != nil {
return nil, err
}

return proj, nil
}

func (c *Client) GetProject(name string) (*LogProject, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}

uri := "/"
proj := convert(c, name)
_, err := request(proj, "GET", uri, h, nil)
if err != nil {
return nil, err
}

return proj, nil
}

func (c *Client) DeleteProject(name string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
}

proj := convert(c, name)
uri := "/"
_, err := request(proj, "DELETE", uri, h, nil)
if err != nil {
return err
}

return nil
}
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sls

const (
version = "0.5.0" // SDK version
version = "0.6.0" // SDK version
signatureMethod = "hmac-sha1" // Signature method

// OffsetNewest stands for the log head offset, i.e. the offset that will be
Expand All @@ -12,8 +12,8 @@ const (
OffsetOldest = "begin"

// ProgressHeader stands for the progress header in GetLogs response
ProgressHeader = "X-Sls-Progress"
ProgressHeader = "X-Log-Progress"

// GetLogsCountHeader stands for the count header in GetLogs response
GetLogsCountHeader = "X-Sls-Count"
GetLogsCountHeader = "X-Log-Count"
)
2 changes: 2 additions & 0 deletions example/log_config_sample.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main

/*
import (
"fmt"
sls "github.com/galaxydi/go-loghub"
Expand Down Expand Up @@ -119,3 +120,4 @@ func createConfig(configName string, projectName string, logstore string, servic
}
return nil
}
*/
69 changes: 22 additions & 47 deletions log_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,6 @@ import (
"net/http"
)

// Error defines sls error
type Error struct {
Code string `json:"errorCode"`
Message string `json:"errorMessage"`
}

// NewClientError new client error
func NewClientError(message string) *Error {
err := new(Error)
err.Code = "ClientError"
err.Message = message
return err
}

func (e Error) String() string {
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return ""
}
return string(b)
}

func (e Error) Error() string {
return e.String()
}

// LogProject defines log project
type LogProject struct {
Name string // Project name
Expand Down Expand Up @@ -62,7 +36,7 @@ func (p *LogProject) WithToken(token string) (*LogProject, error) {
// ListLogStore returns all logstore names of project p.
func (p *LogProject) ListLogStore() ([]string, error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}

uri := fmt.Sprintf("/logstores")
Expand Down Expand Up @@ -91,7 +65,7 @@ func (p *LogProject) ListLogStore() ([]string, error) {
// GetLogStore returns logstore according by logstore name.
func (p *LogProject) GetLogStore(name string) (*LogStore, error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}

r, err := request(p, "GET", "/logstores/"+name, h, nil)
Expand All @@ -108,6 +82,7 @@ func (p *LogProject) GetLogStore(name string) (*LogStore, error) {

s := &LogStore{}
json.Unmarshal(buf, s)
s.Name = name
s.project = p
return s, nil
}
Expand All @@ -133,7 +108,7 @@ func (p *LogProject) CreateLogStore(name string, ttl, shardCnt int) error {
}

h := map[string]string{
"x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
Expand All @@ -155,7 +130,7 @@ func (p *LogProject) CreateLogStore(name string, ttl, shardCnt int) error {
// DeleteLogStore deletes a logstore according by logstore name.
func (p *LogProject) DeleteLogStore(name string) (err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}

r, err := request(p, "DELETE", "/logstores/"+name, h, nil)
Expand Down Expand Up @@ -191,7 +166,7 @@ func (p *LogProject) UpdateLogStore(name string, ttl, shardCnt int) (err error)
}

h := map[string]string{
"x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
Expand All @@ -213,7 +188,7 @@ func (p *LogProject) UpdateLogStore(name string, ttl, shardCnt int) (err error)
// The offset starts from 0 and the size is the max number of machine groups could be returned.
func (p *LogProject) ListMachineGroup(offset, size int) (m []string, total int, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
if size <= 0 {
size = 500
Expand Down Expand Up @@ -246,7 +221,7 @@ func (p *LogProject) ListMachineGroup(offset, size int) (m []string, total int,
// CheckMachineGroupExist check machine group exist or not
func (p *LogProject) CheckMachineGroupExist(name string) (exist bool, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
r, err := request(p, "GET", "/machinegroups/"+name, h, nil)
if err != nil {
Expand All @@ -270,7 +245,7 @@ func (p *LogProject) CheckMachineGroupExist(name string) (exist bool, err error)
// GetMachineGroup retruns machine group according by machine group name.
func (p *LogProject) GetMachineGroup(name string) (m *MachineGroup, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
resp, err := request(p, "GET", "/machinegroups/"+name, h, nil)
if err != nil {
Expand Down Expand Up @@ -298,7 +273,7 @@ func (p *LogProject) CreateMachineGroup(m *MachineGroup) error {
}

h := map[string]string{
"x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
Expand All @@ -324,7 +299,7 @@ func (p *LogProject) UpdateMachineGroup(m *MachineGroup) (err error) {
}

h := map[string]string{
"x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
Expand All @@ -345,7 +320,7 @@ func (p *LogProject) UpdateMachineGroup(m *MachineGroup) (err error) {
// DeleteMachineGroup deletes machine group according machine group name.
func (p *LogProject) DeleteMachineGroup(name string) (err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
r, err := request(p, "DELETE", "/machinegroups/"+name, h, nil)
if err != nil {
Expand All @@ -366,7 +341,7 @@ func (p *LogProject) DeleteMachineGroup(name string) (err error) {
// The offset starts from 0 and the size is the max number of configs could be returned.
func (p *LogProject) ListConfig(offset, size int) (cfgNames []string, total int, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
if size <= 0 {
size = 100
Expand Down Expand Up @@ -398,7 +373,7 @@ func (p *LogProject) ListConfig(offset, size int) (cfgNames []string, total int,
// CheckConfigExist check config exist or not
func (p *LogProject) CheckConfigExist(name string) (exist bool, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
r, err := request(p, "GET", "/configs/"+name, h, nil)
if err != nil {
Expand All @@ -423,7 +398,7 @@ func (p *LogProject) CheckConfigExist(name string) (exist bool, err error) {
// GetConfig returns config according by config name.
func (p *LogProject) GetConfig(name string) (c *LogConfig, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
r, err := request(p, "GET", "/configs/"+name, h, nil)
if err != nil {
Expand Down Expand Up @@ -451,7 +426,7 @@ func (p *LogProject) UpdateConfig(c *LogConfig) (err error) {
}

h := map[string]string{
"x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
Expand All @@ -477,7 +452,7 @@ func (p *LogProject) CreateConfig(c *LogConfig) (err error) {
}

h := map[string]string{
"x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
Expand All @@ -498,7 +473,7 @@ func (p *LogProject) CreateConfig(c *LogConfig) (err error) {
// DeleteConfig deletes a config according by config name.
func (p *LogProject) DeleteConfig(name string) (err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
r, err := request(p, "DELETE", "/configs/"+name, h, nil)
if err != nil {
Expand All @@ -518,7 +493,7 @@ func (p *LogProject) DeleteConfig(name string) (err error) {
// GetAppliedMachineGroups returns applied machine group names list according config name.
func (p *LogProject) GetAppliedMachineGroups(confName string) (groupNames []string, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/configs/%v/machinegroups", confName)
r, err := request(p, "GET", uri, h, nil)
Expand Down Expand Up @@ -546,7 +521,7 @@ func (p *LogProject) GetAppliedMachineGroups(confName string) (groupNames []stri
// GetAppliedConfigs returns applied config names list according machine group name groupName.
func (p *LogProject) GetAppliedConfigs(groupName string) (confNames []string, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/configs", groupName)
r, err := request(p, "GET", uri, h, nil)
Expand Down Expand Up @@ -574,7 +549,7 @@ func (p *LogProject) GetAppliedConfigs(groupName string) (confNames []string, er
// ApplyConfigToMachineGroup applies config to machine group.
func (p *LogProject) ApplyConfigToMachineGroup(confName, groupName string) (err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/configs/%v", groupName, confName)
r, err := request(p, "PUT", uri, h, nil)
Expand All @@ -595,7 +570,7 @@ func (p *LogProject) ApplyConfigToMachineGroup(confName, groupName string) (err
// RemoveConfigFromMachineGroup removes config from machine group.
func (p *LogProject) RemoveConfigFromMachineGroup(confName, groupName string) (err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/configs/%v", groupName, confName)
r, err := request(p, "DELETE", uri, h, nil)
Expand Down
Loading