| 1 | package sls |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strconv" |
| 6 | ) |
| 7 | |
| 8 | type LogtailInput struct { |
| 9 | LogType string `json:"logType,omitempty"` |
| 10 | LogPath string `json:"logPath,omitempty"` |
| 11 | FilePattern string `json:"filePattern,omitempty"` |
| 12 | LocalStorage bool `json:"localStorage"` |
| 13 | TimeFormat string `json:"timeFormat"` |
| 14 | EnableTag bool `json:"enable_tag,omitempty"` |
| 15 | TimeKey string `json:"timeKey"` |
| 16 | LogBeginRegex string `json:"logBeginRegex,omitempty"` |
| 17 | Regex string `json:"regex,omitempty"` |
| 18 | Key []string `json:"key,omitempty"` |
| 19 | FilterKey []string `json:"filterKey,omitempty"` |
| 20 | FilterRegex []string `json:"filterRegex,omitempty"` |
| 21 | TopicFormat string `json:"topicFormat,omitempty"` |
| 22 | Separator string `json:"separator,omitempty"` |
| 23 | Quote string `json:"quote,omitempty"` |
| 24 | AutoExtend bool `json:"autoExtend,omitempty"` |
| 25 | } |
| 26 | |
| 27 | type LogtailOutput struct { |
| 28 | LogstoreName string `json:"logstoreName,omitempty"` |
| 29 | } |
| 30 | |
| 31 | type LogtailConfig struct { |
| 32 | Name string `json:"configName,omitempty"` |
| 33 | InputType string `json:"inputType,omitempty"` |
| 34 | InputDetail LogtailInput `json:"inputDetail,omitempty"` |
| 35 | OutputType string `json:"outputType,omitempty"` |
| 36 | Sample string `json:"logSample,omitempty"` |
| 37 | OutputDetail LogtailOutput `json:"outputDetail,omitempty"` |
| 38 | } |
| 39 | |
| 40 | func (proj *Project) CreateConfig(config *LogtailConfig) error { |
| 41 | data, err := json.Marshal(config) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | req := &request{ |
| 47 | method: METHOD_POST, |
| 48 | path: "/configs", |
| 49 | payload: data, |
| 50 | contentType: "application/json", |
| 51 | } |
| 52 | |
| 53 | return proj.client.requestWithClose(req) |
| 54 | } |
| 55 | |
| 56 | type LogtailConfigList struct { |
| 57 | Count int `json:"count,omitempty"` |
| 58 | Total int `json:"total,omitempty"` |
| 59 | Configs []string `json:"configs,omitempty"` |
| 60 | } |
| 61 | |
| 62 | func (proj *Project) ListConfig(offset, size int) (*LogtailConfigList, error) { |
| 63 | req := &request{ |
| 64 | method: METHOD_GET, |
| 65 | path: "/configs", |
| 66 | params: map[string]string{ |
| 67 | "size": strconv.Itoa(size), |
| 68 | "offset": strconv.Itoa(offset), |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | list := &LogtailConfigList{} |
| 73 | if err := proj.client.requestWithJsonResponse(req, list); err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | return list, nil |
| 77 | } |
| 78 | |
| 79 | func (proj *Project) GetConfig(name string) (*LogtailConfig, error) { |
| 80 | req := &request{ |
| 81 | method: METHOD_GET, |
| 82 | path: "/configs/" + name, |
| 83 | } |
| 84 | |
| 85 | config := &LogtailConfig{} |
| 86 | if err := proj.client.requestWithJsonResponse(req, config); err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | |
| 90 | return config, nil |
| 91 | } |
| 92 | |
| 93 | func (proj *Project) GetAppliedMachineGroups(configName string) ([]string, error) { |
| 94 | type appliedMachineGroups struct { |
| 95 | Machinegroups []string `json:"machinegroups,omitempty"` |
| 96 | } |
| 97 | |
| 98 | req := &request{ |
| 99 | method: METHOD_GET, |
| 100 | path: "/configs/" + configName + "/machinegroups", |
| 101 | } |
| 102 | |
| 103 | group := &appliedMachineGroups{} |
| 104 | |
| 105 | if err := proj.client.requestWithJsonResponse(req, group); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | return group.Machinegroups, nil |
| 110 | } |
| 111 | |
| 112 | func (proj *Project) DeleteConfig(configName string) error { |
| 113 | req := &request{ |
| 114 | method: METHOD_DELETE, |
| 115 | path: "/configs/" + configName, |
| 116 | } |
| 117 | |
| 118 | return proj.client.requestWithClose(req) |
| 119 | } |
| 120 | |
| 121 | func (proj *Project) UpdateConfig(config *LogtailConfig) error { |
| 122 | data, err := json.Marshal(config) |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | req := &request{ |
| 128 | method: METHOD_PUT, |
| 129 | path: "/configs/" + config.Name, |
| 130 | payload: data, |
| 131 | contentType: "application/json", |
| 132 | } |
| 133 | |
| 134 | return proj.client.requestWithClose(req) |
| 135 | } |