From 86eb210b28d0f91deff4af23f960d6e4c0be564c Mon Sep 17 00:00:00 2001 From: Andrei Lukyanchyk Date: Tue, 31 Dec 2024 12:48:59 +0100 Subject: [PATCH] CDI-916: add Rule Templates --- client.go | 8 +++++ ruletemplates/ruletemplates.go | 61 ++++++++++++++++++++++++++++++++++ ruletemplates/service.go | 53 +++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 ruletemplates/ruletemplates.go create mode 100644 ruletemplates/service.go diff --git a/client.go b/client.go index a07a24e..b9dc0d0 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,7 @@ import ( "github.com/G-Core/gcorelabscdn-go/presets" "github.com/G-Core/gcorelabscdn-go/resources" "github.com/G-Core/gcorelabscdn-go/rules" + "github.com/G-Core/gcorelabscdn-go/ruletemplates" "github.com/G-Core/gcorelabscdn-go/sslcerts" ) @@ -17,6 +18,7 @@ type ClientService interface { OriginShielding() originshielding.OriginShieldingService SSLCerts() sslcerts.SSLCertService Presets() presets.PresetsService + RuleTemplates() ruletemplates.RuleTemplateService } var _ ClientService = (*Service)(nil) @@ -29,6 +31,7 @@ type Service struct { originShieldingService originshielding.OriginShieldingService sslCertsService sslcerts.SSLCertService presetsService presets.PresetsService + ruleTemplatesService ruletemplates.RuleTemplateService } func NewService(r gcore.Requester) *Service { @@ -40,6 +43,7 @@ func NewService(r gcore.Requester) *Service { originShieldingService: originshielding.NewService(r), sslCertsService: sslcerts.NewService(r), presetsService: presets.NewService(r), + ruleTemplatesService: ruletemplates.NewService(r), } } @@ -66,3 +70,7 @@ func (s *Service) SSLCerts() sslcerts.SSLCertService { func (s *Service) Presets() presets.PresetsService { return s.presetsService } + +func (s *Service) RuleTemplates() ruletemplates.RuleTemplateService { + return s.ruleTemplatesService +} diff --git a/ruletemplates/ruletemplates.go b/ruletemplates/ruletemplates.go new file mode 100644 index 0000000..6790678 --- /dev/null +++ b/ruletemplates/ruletemplates.go @@ -0,0 +1,61 @@ +package ruletemplates + +import ( + "context" + + "github.com/G-Core/gcorelabscdn-go/gcore" +) + +type RuleTemplateService interface { + Create(ctx context.Context, req *CreateRequest) (*RuleTemplate, error) + Get(ctx context.Context, id int64) (*RuleTemplate, error) + Update(ctx context.Context, id int64, req *UpdateRequest) (*RuleTemplate, error) + Delete(ctx context.Context, id int64) error +} + +type Protocol string + +const ( + HTTPProtocol Protocol = "HTTP" + HTTPSProtocol Protocol = "HTTPS" + MatchProtocol Protocol = "MATCH" +) + +type RuleType int + +const ( + RuleType0 RuleType = 0 + RuleType1 RuleType = 1 +) + +type RuleTemplate struct { + ID int64 `json:"id"` + Name string `json:"name"` + Client int64 `json:"client"` + Deleted bool `json:"deleted"` + Rule string `json:"rule"` + RuleType RuleType `json:"ruleType"` + Weight int `json:"weight"` + Template bool `json:"template"` + Default bool `json:"default"` + OverrideOriginProtocol *Protocol `json:"overrideOriginProtocol"` + Options *gcore.Options `json:"options,omitempty"` +} + +type CreateRequest struct { + Name string `json:"name,omitempty"` + Rule string `json:"rule"` + RuleType RuleType `json:"ruleType"` + Weight int `json:"weight,omitempty"` + OverrideOriginProtocol *Protocol `json:"overrideOriginProtocol"` + Options *gcore.Options `json:"options,omitempty"` +} + +type UpdateRequest struct { + Name string `json:"name,omitempty"` + Rule string `json:"rule,omitempty"` + RuleType RuleType `json:"ruleType,omitempty"` + Weight int `json:"weight,omitempty"` + OverrideOriginProtocol *Protocol `json:"overrideOriginProtocol"` + Options *gcore.Options `json:"options,omitempty"` +} diff --git a/ruletemplates/service.go b/ruletemplates/service.go new file mode 100644 index 0000000..1579f92 --- /dev/null +++ b/ruletemplates/service.go @@ -0,0 +1,53 @@ +package ruletemplates + +import ( + "context" + "fmt" + "net/http" + + "github.com/G-Core/gcorelabscdn-go/gcore" +) + +type Service struct { + r gcore.Requester +} + +func NewService(r gcore.Requester) *Service { + return &Service{r: r} +} + +func (s *Service) Create(ctx context.Context, req *CreateRequest) (*RuleTemplate, error) { + var ruleTemplate RuleTemplate + + if err := s.r.Request(ctx, http.MethodPost, "/cdn/resources/rule_templates", req, &ruleTemplate); err != nil { + return nil, fmt.Errorf("request: %w", err) + } + + return &ruleTemplate, nil +} + +func (s *Service) Get(ctx context.Context, id int64) (*RuleTemplate, error) { + var ruleTemplate RuleTemplate + if err := s.r.Request(ctx, http.MethodGet, fmt.Sprintf("/cdn/resources/rule_templates/%d", id), nil, &ruleTemplate); err != nil { + return nil, fmt.Errorf("request: %w", err) + } + + return &ruleTemplate, nil +} + +func (s *Service) Update(ctx context.Context, id int64, req *UpdateRequest) (*RuleTemplate, error) { + var ruleTemplate RuleTemplate + if err := s.r.Request(ctx, http.MethodPatch, fmt.Sprintf("/cdn/resources/rule_templates/%d", id), req, &ruleTemplate); err != nil { + return nil, fmt.Errorf("request: %w", err) + } + + return &ruleTemplate, nil +} + +func (s *Service) Delete(ctx context.Context, id int64) error { + if err := s.r.Request(ctx, http.MethodDelete, fmt.Sprintf("/cdn/resources/rule_templates/%d", id), nil, nil); err != nil { + return fmt.Errorf("request: %w", err) + } + + return nil +}