Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDI-696: add preset support #59

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/G-Core/gcorelabscdn-go/gcore"
"github.com/G-Core/gcorelabscdn-go/origingroups"
"github.com/G-Core/gcorelabscdn-go/originshielding"
"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/sslcerts"
Expand All @@ -15,6 +16,7 @@ type ClientService interface {
OriginGroups() origingroups.OriginGroupService
OriginShielding() originshielding.OriginShieldingService
SSLCerts() sslcerts.SSLCertService
Presets() presets.PresetsService
}

var _ ClientService = (*Service)(nil)
Expand All @@ -26,6 +28,7 @@ type Service struct {
originGroupsService origingroups.OriginGroupService
originShieldingService originshielding.OriginShieldingService
sslCertsService sslcerts.SSLCertService
presetsService presets.PresetsService
}

func NewService(r gcore.Requester) *Service {
Expand All @@ -36,6 +39,7 @@ func NewService(r gcore.Requester) *Service {
originGroupsService: origingroups.NewService(r),
originShieldingService: originshielding.NewService(r),
sslCertsService: sslcerts.NewService(r),
presetsService: presets.NewService(r),
}
}

Expand All @@ -58,3 +62,7 @@ func (s *Service) OriginShielding() originshielding.OriginShieldingService {
func (s *Service) SSLCerts() sslcerts.SSLCertService {
return s.sslCertsService
}

func (s *Service) Presets() presets.PresetsService {
return s.presetsService
}
31 changes: 31 additions & 0 deletions presets/presets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package presets

import (
"context"
)

type PresetsService interface {
Apply(ctx context.Context, presetID int, req *ApplyRequest) (*AppliedPreset, error)
GetAppliedPreset(ctx context.Context, presetID, objectID int) (*AppliedPreset, error)
Unapply(ctx context.Context, presetID, objectID int) error
Get(ctx context.Context, presetID int) (*Preset, error)
}

type ApplyRequest struct {
ObjectID int `json:"object_id"`
}

type GetAppliedPresetResponse struct {
ObjectType *string `json:"object_type"`
ObjectIDs []int `json:"object_ids"`
}

type AppliedPreset struct {
PresetID int
ObjectID int
}

type Preset struct {
ID int `json:"id"`
Name string `json:"name"`
}
76 changes: 76 additions & 0 deletions presets/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package presets

import (
"context"
"fmt"
"net/http"

"github.com/G-Core/gcorelabscdn-go/gcore"
)

var _ PresetsService = (*Service)(nil)

type Service struct {
r gcore.Requester
}

func NewService(r gcore.Requester) *Service {
return &Service{r: r}
}

func (s *Service) Apply(ctx context.Context, presetID int, req *ApplyRequest) (*AppliedPreset, error) {
preset := AppliedPreset{
PresetID: presetID,
ObjectID: req.ObjectID,
}

path := fmt.Sprintf("/cdn/presets/%d/applied", presetID)
if err := s.r.Request(ctx, http.MethodPost, path, req, nil); err != nil {
return nil, fmt.Errorf("request: %w", err)
}

return &preset, nil
}

func (s *Service) GetAppliedPreset(ctx context.Context, presetID, objectID int) (*AppliedPreset, error) {
var response GetAppliedPresetResponse
preset := AppliedPreset{
PresetID: presetID,
ObjectID: objectID,
}

path := fmt.Sprintf("/cdn/presets/%d/applied", presetID)
if err := s.r.Request(ctx, http.MethodGet, path, nil, &response); err != nil {
return nil, fmt.Errorf("request: %w", err)
}

// check if objectID is in response.ObjectIDs list, if no - return error
for _, id := range response.ObjectIDs {
if id == objectID {
return &preset, nil
}
}

return nil, nil
}

func (s *Service) Unapply(ctx context.Context, presetID, objectID int) error {
path := fmt.Sprintf("/cdn/presets/%d/applied/%d", presetID, objectID)

if err := s.r.Request(ctx, http.MethodDelete, path, nil, nil); err != nil {
return fmt.Errorf("request: %w", err)
}

return nil
}

func (s *Service) Get(ctx context.Context, presetID int) (*Preset, error) {
path := fmt.Sprintf("/cdn/presets/%d", presetID)
var preset Preset

if err := s.r.Request(ctx, http.MethodGet, path, nil, &preset); err != nil {
return nil, fmt.Errorf("unable to retrieve preset with id=%d. Make sure the ID provided is correct. Details: %w", presetID, err)
}

return &preset, nil
}
Loading