Skip to content

Commit f1c7180

Browse files
authored
Merge pull request #77 from G-Core/feature/CDM-1397-add-logs-uploader-support
CDM-1397 Add logs uploader service
2 parents f356c8f + 3774445 commit f1c7180

File tree

6 files changed

+367
-0
lines changed

6 files changed

+367
-0
lines changed

client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/G-Core/gcorelabscdn-go/cacerts"
55
"github.com/G-Core/gcorelabscdn-go/clients"
66
"github.com/G-Core/gcorelabscdn-go/gcore"
7+
"github.com/G-Core/gcorelabscdn-go/logsuploader"
78
"github.com/G-Core/gcorelabscdn-go/origingroups"
89
"github.com/G-Core/gcorelabscdn-go/originshielding"
910
"github.com/G-Core/gcorelabscdn-go/presets"
@@ -23,6 +24,7 @@ type ClientService interface {
2324
CACerts() cacerts.CACertService
2425
RuleTemplates() ruletemplates.RuleTemplateService
2526
ClientsMe() clients.ClientsMeService
27+
LogsUploader() logsuploader.LogsUploaderService
2628
}
2729

2830
var _ ClientService = (*Service)(nil)
@@ -38,6 +40,7 @@ type Service struct {
3840
caCertsService cacerts.CACertService
3941
ruleTemplatesService ruletemplates.RuleTemplateService
4042
clientsMeService clients.ClientsMeService
43+
logsUploaderService logsuploader.LogsUploaderService
4144
}
4245

4346
func NewService(r gcore.Requester) *Service {
@@ -52,6 +55,7 @@ func NewService(r gcore.Requester) *Service {
5255
caCertsService: cacerts.NewService(r),
5356
ruleTemplatesService: ruletemplates.NewService(r),
5457
clientsMeService: clients.NewService(r),
58+
logsUploaderService: logsuploader.NewService(r),
5559
}
5660
}
5761

@@ -90,3 +94,7 @@ func (s *Service) RuleTemplates() ruletemplates.RuleTemplateService {
9094
func (s *Service) ClientsMe() clients.ClientsMeService {
9195
return s.clientsMeService
9296
}
97+
98+
func (s *Service) LogsUploader() logsuploader.LogsUploaderService {
99+
return s.logsUploaderService
100+
}

logsuploader/configs.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package logsuploader
2+
3+
import (
4+
"time"
5+
)
6+
7+
type Config struct {
8+
ID int64 `json:"id"`
9+
ClientID int64 `json:"client_id"`
10+
Created time.Time `json:"created"`
11+
Updated time.Time `json:"updated"`
12+
Name string `json:"name"`
13+
Enabled bool `json:"enabled"`
14+
Policy int64 `json:"policy"`
15+
Target int64 `json:"target"`
16+
ForAllResources bool `json:"for_all_resources"`
17+
Resources []int64 `json:"resources"`
18+
}
19+
20+
type ConfigCreateRequest struct {
21+
Name string `json:"name"`
22+
Enabled bool `json:"enabled"`
23+
Policy int64 `json:"policy"`
24+
Target int64 `json:"target"`
25+
ForAllResources bool `json:"for_all_resources"`
26+
Resources []int64 `json:"resources"`
27+
}
28+
29+
type ConfigUpdateRequest struct {
30+
Name string `json:"name"`
31+
Enabled bool `json:"enabled"`
32+
Policy int64 `json:"policy"`
33+
Target int64 `json:"target"`
34+
ForAllResources bool `json:"for_all_resources"`
35+
Resources []int64 `json:"resources"`
36+
}

logsuploader/logsuploader.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package logsuploader
2+
3+
import "context"
4+
5+
type LogsUploaderService interface {
6+
PolicyCreate(ctx context.Context, req *PolicyCreateRequest) (*Policy, error)
7+
PolicyList(ctx context.Context, limit, offset int) ([]Policy, error)
8+
PolicyGet(ctx context.Context, id int64) (*Policy, error)
9+
PolicyUpdate(ctx context.Context, id int64, req *PolicyUpdateRequest) (*Policy, error)
10+
PolicyDelete(ctx context.Context, policyID int64) error
11+
TargetCreate(ctx context.Context, req *TargetCreateRequest) (*Target, error)
12+
TargetList(ctx context.Context, limit, offset int) ([]Target, error)
13+
TargetGet(ctx context.Context, id int64) (*Target, error)
14+
TargetUpdate(ctx context.Context, id int64, req *TargetUpdateRequest) (*Target, error)
15+
TargetDelete(ctx context.Context, targetID int64) error
16+
ConfigCreate(ctx context.Context, req *ConfigCreateRequest) (*Config, error)
17+
ConfigList(ctx context.Context, limit, offset int) ([]Config, error)
18+
ConfigGet(ctx context.Context, id int64) (*Config, error)
19+
ConfigUpdate(ctx context.Context, id int64, req *ConfigUpdateRequest) (*Config, error)
20+
ConfigDelete(ctx context.Context, configID int64) error
21+
}

logsuploader/policies.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package logsuploader
2+
3+
import (
4+
"time"
5+
)
6+
7+
type Policy struct {
8+
ID int64 `json:"id"`
9+
ClientID int64 `json:"client_id"`
10+
Created time.Time `json:"created"`
11+
Updated time.Time `json:"updated"`
12+
IncludeEmptyLogs bool `json:"include_empty_logs"`
13+
IncludeShieldLogs bool `json:"include_shield_logs"`
14+
Name string `json:"name"`
15+
Description string `json:"description"`
16+
RetryIntervalMinutes int `json:"retry_interval_minutes"`
17+
RotateIntervalMinutes int `json:"rotate_interval_minutes"`
18+
RotateThresholdMB *int `json:"rotate_threshold_mb"`
19+
RotateThresholdLines int `json:"rotate_threshold_lines"`
20+
DateFormat string `json:"date_format"`
21+
FieldDelimiter string `json:"field_delimiter"`
22+
FieldSeparator string `json:"field_separator"`
23+
Fields []string `json:"fields"`
24+
FileNameTemplate string `json:"file_name_template"`
25+
FormatType string `json:"format_type"`
26+
Tags map[string]string `json:"tags"`
27+
RelatedUploaderConfigs []int64 `json:"related_uploader_configs"`
28+
}
29+
30+
type PolicyCreateRequest struct {
31+
IncludeEmptyLogs bool `json:"include_empty_logs,omitempty"`
32+
IncludeShieldLogs bool `json:"include_shield_logs,omitempty"`
33+
Name string `json:"name,omitempty"`
34+
Description string `json:"description,omitempty"`
35+
RetryIntervalMinutes int `json:"retry_interval_minutes,omitempty"`
36+
RotateIntervalMinutes int `json:"rotate_interval_minutes,omitempty"`
37+
RotateThresholdMB *int `json:"rotate_threshold_mb"`
38+
RotateThresholdLines int `json:"rotate_threshold_lines,omitempty"`
39+
DateFormat string `json:"date_format,omitempty"`
40+
FieldDelimiter string `json:"field_delimiter,omitempty"`
41+
FieldSeparator string `json:"field_separator,omitempty"`
42+
Fields []string `json:"fields,omitempty"`
43+
FileNameTemplate string `json:"file_name_template,omitempty"`
44+
FormatType string `json:"format_type,omitempty"`
45+
Tags map[string]string `json:"tags,omitempty"`
46+
}
47+
48+
type PolicyUpdateRequest struct {
49+
IncludeEmptyLogs bool `json:"include_empty_logs"`
50+
IncludeShieldLogs bool `json:"include_shield_logs"`
51+
Name string `json:"name,omitempty"`
52+
Description string `json:"description"`
53+
RetryIntervalMinutes int `json:"retry_interval_minutes,omitempty"`
54+
RotateIntervalMinutes int `json:"rotate_interval_minutes,omitempty"`
55+
RotateThresholdMB *int `json:"rotate_threshold_mb"`
56+
RotateThresholdLines int `json:"rotate_threshold_lines"`
57+
DateFormat string `json:"date_format"`
58+
FieldDelimiter string `json:"field_delimiter,omitempty"`
59+
FieldSeparator string `json:"field_separator,omitempty"`
60+
Fields []string `json:"fields,omitempty"`
61+
FileNameTemplate string `json:"file_name_template,omitempty"`
62+
FormatType string `json:"format_type"`
63+
Tags map[string]string `json:"tags"`
64+
}

logsuploader/service.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package logsuploader
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
9+
"github.com/G-Core/gcorelabscdn-go/gcore"
10+
)
11+
12+
var _ LogsUploaderService = (*Service)(nil)
13+
14+
type Service struct {
15+
r gcore.Requester
16+
}
17+
18+
func NewService(r gcore.Requester) *Service {
19+
return &Service{r: r}
20+
}
21+
22+
func (s *Service) PolicyCreate(ctx context.Context, req *PolicyCreateRequest) (*Policy, error) {
23+
var policy Policy
24+
25+
if err := s.r.Request(ctx, http.MethodPost, "/cdn/logs_uploader/policies", req, &policy); err != nil {
26+
return nil, fmt.Errorf("request: %w", err)
27+
}
28+
29+
return &policy, nil
30+
}
31+
32+
func (s *Service) PolicyList(ctx context.Context, limit, offset int) ([]Policy, error) {
33+
var policies []Policy
34+
params := url.Values{}
35+
if limit > 0 {
36+
params.Add("limit", fmt.Sprintf("%d", limit))
37+
}
38+
if offset > 0 {
39+
params.Add("offset", fmt.Sprintf("%d", offset))
40+
}
41+
42+
path := "/cdn/logs_uploader/policies"
43+
if len(params) > 0 {
44+
path = fmt.Sprintf("%s?%s", path, params.Encode())
45+
}
46+
47+
if err := s.r.Request(ctx, http.MethodGet, path, nil, &policies); err != nil {
48+
return nil, fmt.Errorf("request: %w", err)
49+
}
50+
51+
return policies, nil
52+
}
53+
54+
func (s *Service) PolicyGet(ctx context.Context, id int64) (*Policy, error) {
55+
var policy Policy
56+
if err := s.r.Request(ctx, http.MethodGet, fmt.Sprintf("/cdn/logs_uploader/policies/%d", id), nil, &policy); err != nil {
57+
return nil, fmt.Errorf("request: %w", err)
58+
}
59+
60+
return &policy, nil
61+
}
62+
63+
func (s *Service) PolicyUpdate(ctx context.Context, id int64, req *PolicyUpdateRequest) (*Policy, error) {
64+
var policy Policy
65+
if err := s.r.Request(ctx, http.MethodPatch, fmt.Sprintf("/cdn/logs_uploader/policies/%d", id), req, &policy); err != nil {
66+
return nil, fmt.Errorf("request: %w", err)
67+
}
68+
69+
return &policy, nil
70+
}
71+
72+
func (s *Service) PolicyDelete(ctx context.Context, id int64) error {
73+
if err := s.r.Request(ctx, http.MethodDelete, fmt.Sprintf("/cdn/logs_uploader/policies/%d", id), nil, nil); err != nil {
74+
return fmt.Errorf("request: %w", err)
75+
}
76+
77+
return nil
78+
}
79+
80+
func (s *Service) TargetCreate(ctx context.Context, req *TargetCreateRequest) (*Target, error) {
81+
var target Target
82+
83+
if err := s.r.Request(ctx, http.MethodPost, "/cdn/logs_uploader/targets", req, &target); err != nil {
84+
return nil, fmt.Errorf("request: %w", err)
85+
}
86+
87+
return &target, nil
88+
}
89+
90+
func (s *Service) TargetList(ctx context.Context, limit, offset int) ([]Target, error) {
91+
var targets []Target
92+
params := url.Values{}
93+
if limit > 0 {
94+
params.Add("limit", fmt.Sprintf("%d", limit))
95+
}
96+
if offset > 0 {
97+
params.Add("offset", fmt.Sprintf("%d", offset))
98+
}
99+
100+
path := "/cdn/logs_uploader/targets"
101+
if len(params) > 0 {
102+
path = fmt.Sprintf("%s?%s", path, params.Encode())
103+
}
104+
105+
if err := s.r.Request(ctx, http.MethodGet, path, nil, &targets); err != nil {
106+
return nil, fmt.Errorf("request: %w", err)
107+
}
108+
109+
return targets, nil
110+
}
111+
112+
func (s *Service) TargetGet(ctx context.Context, id int64) (*Target, error) {
113+
var target Target
114+
if err := s.r.Request(ctx, http.MethodGet, fmt.Sprintf("/cdn/logs_uploader/targets/%d", id), nil, &target); err != nil {
115+
return nil, fmt.Errorf("request: %w", err)
116+
}
117+
118+
return &target, nil
119+
}
120+
121+
func (s *Service) TargetUpdate(ctx context.Context, id int64, req *TargetUpdateRequest) (*Target, error) {
122+
var target Target
123+
if err := s.r.Request(ctx, http.MethodPatch, fmt.Sprintf("/cdn/logs_uploader/targets/%d", id), req, &target); err != nil {
124+
return nil, fmt.Errorf("request: %w", err)
125+
}
126+
127+
return &target, nil
128+
}
129+
130+
func (s *Service) TargetDelete(ctx context.Context, id int64) error {
131+
if err := s.r.Request(ctx, http.MethodDelete, fmt.Sprintf("/cdn/logs_uploader/targets/%d", id), nil, nil); err != nil {
132+
return fmt.Errorf("request: %w", err)
133+
}
134+
135+
return nil
136+
}
137+
138+
func (s *Service) ConfigCreate(ctx context.Context, req *ConfigCreateRequest) (*Config, error) {
139+
var config Config
140+
141+
if err := s.r.Request(ctx, http.MethodPost, "/cdn/logs_uploader/configs", req, &config); err != nil {
142+
return nil, fmt.Errorf("request: %w", err)
143+
}
144+
145+
return &config, nil
146+
}
147+
148+
func (s *Service) ConfigList(ctx context.Context, limit, offset int) ([]Config, error) {
149+
var configs []Config
150+
params := url.Values{}
151+
if limit > 0 {
152+
params.Add("limit", fmt.Sprintf("%d", limit))
153+
}
154+
if offset > 0 {
155+
params.Add("offset", fmt.Sprintf("%d", offset))
156+
}
157+
158+
path := "/cdn/logs_uploader/configs"
159+
if len(params) > 0 {
160+
path = fmt.Sprintf("%s?%s", path, params.Encode())
161+
}
162+
163+
if err := s.r.Request(ctx, http.MethodGet, path, nil, &configs); err != nil {
164+
return nil, fmt.Errorf("request: %w", err)
165+
}
166+
167+
return configs, nil
168+
}
169+
170+
func (s *Service) ConfigGet(ctx context.Context, id int64) (*Config, error) {
171+
var config Config
172+
if err := s.r.Request(ctx, http.MethodGet, fmt.Sprintf("/cdn/logs_uploader/configs/%d", id), nil, &config); err != nil {
173+
return nil, fmt.Errorf("request: %w", err)
174+
}
175+
176+
return &config, nil
177+
}
178+
179+
func (s *Service) ConfigUpdate(ctx context.Context, id int64, req *ConfigUpdateRequest) (*Config, error) {
180+
var config Config
181+
if err := s.r.Request(ctx, http.MethodPatch, fmt.Sprintf("/cdn/logs_uploader/configs/%d", id), req, &config); err != nil {
182+
return nil, fmt.Errorf("request: %w", err)
183+
}
184+
185+
return &config, nil
186+
}
187+
188+
func (s *Service) ConfigDelete(ctx context.Context, id int64) error {
189+
if err := s.r.Request(ctx, http.MethodDelete, fmt.Sprintf("/cdn/logs_uploader/configs/%d", id), nil, nil); err != nil {
190+
return fmt.Errorf("request: %w", err)
191+
}
192+
193+
return nil
194+
}

0 commit comments

Comments
 (0)