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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package sls
import (
"encoding/json"
"strconv"
)
type LogtailInput struct {
LogType string `json:"logType,omitempty"`
LogPath string `json:"logPath,omitempty"`
FilePattern string `json:"filePattern,omitempty"`
LocalStorage bool `json:"localStorage"`
TimeFormat string `json:"timeFormat"`
EnableTag bool `json:"enable_tag,omitempty"`
TimeKey string `json:"timeKey"`
LogBeginRegex string `json:"logBeginRegex,omitempty"`
Regex string `json:"regex,omitempty"`
Key []string `json:"key,omitempty"`
FilterKey []string `json:"filterKey,omitempty"`
FilterRegex []string `json:"filterRegex,omitempty"`
TopicFormat string `json:"topicFormat,omitempty"`
Separator string `json:"separator,omitempty"`
Quote string `json:"quote,omitempty"`
AutoExtend bool `json:"autoExtend,omitempty"`
}
type LogtailOutput struct {
LogstoreName string `json:"logstoreName,omitempty"`
}
type LogtailConfig struct {
Name string `json:"configName,omitempty"`
InputType string `json:"inputType,omitempty"`
InputDetail LogtailInput `json:"inputDetail,omitempty"`
OutputType string `json:"outputType,omitempty"`
Sample string `json:"logSample,omitempty"`
OutputDetail LogtailOutput `json:"outputDetail,omitempty"`
}
func (proj *Project) CreateConfig(config *LogtailConfig) error {
data, err := json.Marshal(config)
if err != nil {
return err
}
req := &request{
method: METHOD_POST,
path: "/configs",
payload: data,
contentType: "application/json",
}
return proj.client.requestWithClose(req)
}
type LogtailConfigList struct {
Count int `json:"count,omitempty"`
Total int `json:"total,omitempty"`
Configs []string `json:"configs,omitempty"`
}
func (proj *Project) ListConfig(offset, size int) (*LogtailConfigList, error) {
req := &request{
method: METHOD_GET,
path: "/configs",
params: map[string]string{
"size": strconv.Itoa(size),
"offset": strconv.Itoa(offset),
},
}
list := &LogtailConfigList{}
if err := proj.client.requestWithJsonResponse(req, list); err != nil {
return nil, err
}
return list, nil
}
func (proj *Project) GetConfig(name string) (*LogtailConfig, error) {
req := &request{
method: METHOD_GET,
path: "/configs/" + name,
}
config := &LogtailConfig{}
if err := proj.client.requestWithJsonResponse(req, config); err != nil {
return nil, err
}
return config, nil
}
func (proj *Project) GetAppliedMachineGroups(configName string) ([]string, error) {
type appliedMachineGroups struct {
Machinegroups []string `json:"machinegroups,omitempty"`
}
req := &request{
method: METHOD_GET,
path: "/configs/" + configName + "/machinegroups",
}
group := &appliedMachineGroups{}
if err := proj.client.requestWithJsonResponse(req, group); err != nil {
return nil, err
}
return group.Machinegroups, nil
}
func (proj *Project) DeleteConfig(configName string) error {
req := &request{
method: METHOD_DELETE,
path: "/configs/" + configName,
}
return proj.client.requestWithClose(req)
}
func (proj *Project) UpdateConfig(config *LogtailConfig) error {
data, err := json.Marshal(config)
if err != nil {
return err
}
req := &request{
method: METHOD_PUT,
path: "/configs/" + config.Name,
payload: data,
contentType: "application/json",
}
return proj.client.requestWithClose(req)
}
|