Skip to content

Commit

Permalink
CDI-916: add Rule Templates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-lukyanchyk committed Dec 31, 2024
1 parent ca153b1 commit 86eb210
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -17,6 +18,7 @@ type ClientService interface {
OriginShielding() originshielding.OriginShieldingService
SSLCerts() sslcerts.SSLCertService
Presets() presets.PresetsService
RuleTemplates() ruletemplates.RuleTemplateService
}

var _ ClientService = (*Service)(nil)
Expand All @@ -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 {
Expand All @@ -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),
}
}

Expand All @@ -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
}
61 changes: 61 additions & 0 deletions ruletemplates/ruletemplates.go
Original file line number Diff line number Diff line change
@@ -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"`
}
53 changes: 53 additions & 0 deletions ruletemplates/service.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 86eb210

Please sign in to comment.