From 00ce52144fb034d095dd9b19adfdad5bb8e499f3 Mon Sep 17 00:00:00 2001 From: Andrei Lukyanchyk Date: Tue, 30 Apr 2024 15:32:10 +0200 Subject: [PATCH 1/4] CDI-696: add presets --- client.go | 8 ++++++ presets/presets.go | 25 ++++++++++++++++++ presets/service.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 presets/presets.go create mode 100644 presets/service.go diff --git a/client.go b/client.go index 44b4241..a07a24e 100644 --- a/client.go +++ b/client.go @@ -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" @@ -15,6 +16,7 @@ type ClientService interface { OriginGroups() origingroups.OriginGroupService OriginShielding() originshielding.OriginShieldingService SSLCerts() sslcerts.SSLCertService + Presets() presets.PresetsService } var _ ClientService = (*Service)(nil) @@ -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 { @@ -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), } } @@ -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 +} diff --git a/presets/presets.go b/presets/presets.go new file mode 100644 index 0000000..43924e6 --- /dev/null +++ b/presets/presets.go @@ -0,0 +1,25 @@ +package presets + +import ( + "context" +) + +type PresetsService interface { + Create(ctx context.Context, presetID int64, req *CreateRequest) (*Preset, error) + Get(ctx context.Context, presetID, objectID int64) (*Preset, error) + Delete(ctx context.Context, presetID, objectID int64) error +} + +type CreateRequest struct { + ObjectID int `json:"object_id"` +} + +type GetResponse struct { + ObjectType *string `json:"object_type"` + ObjectIDs []int `json:"object_ids"` +} + +type Preset struct { + ID int + ObjectID int +} diff --git a/presets/service.go b/presets/service.go new file mode 100644 index 0000000..58e655d --- /dev/null +++ b/presets/service.go @@ -0,0 +1,65 @@ +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) Create(ctx context.Context, presetID int64, req *CreateRequest) (*Preset, error) { + preset := Preset{ + ID: int(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) Get(ctx context.Context, presetID, objectID int64) (*Preset, error) { + var response GetResponse + preset := Preset{ + ID: int(presetID), + ObjectID: int(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 == int(objectID) { + return &preset, nil + } + } + + return nil, fmt.Errorf("error: preset with id %d is not applied to the object with id %d", presetID, objectID) +} + +func (s *Service) Delete(ctx context.Context, presetID, objectID int64) 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 +} From acf83e0b797bbfd9ed9115b8aa849745e85b71eb Mon Sep 17 00:00:00 2001 From: Andrei Lukyanchyk Date: Thu, 9 May 2024 11:24:11 +0200 Subject: [PATCH 2/4] CDI-696: refactoring --- presets/presets.go | 14 +++++++------- presets/service.go | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/presets/presets.go b/presets/presets.go index 43924e6..d2b222c 100644 --- a/presets/presets.go +++ b/presets/presets.go @@ -5,21 +5,21 @@ import ( ) type PresetsService interface { - Create(ctx context.Context, presetID int64, req *CreateRequest) (*Preset, error) - Get(ctx context.Context, presetID, objectID int64) (*Preset, error) - Delete(ctx context.Context, presetID, objectID int64) error + Apply(ctx context.Context, presetID int64, req *ApplyRequest) (*AppliedPreset, error) + GetAppliedPreset(ctx context.Context, presetID, objectID int64) (*AppliedPreset, error) + Unapply(ctx context.Context, presetID, objectID int64) error } -type CreateRequest struct { +type ApplyRequest struct { ObjectID int `json:"object_id"` } -type GetResponse struct { +type GetAppliedPresetResponse struct { ObjectType *string `json:"object_type"` ObjectIDs []int `json:"object_ids"` } -type Preset struct { - ID int +type AppliedPreset struct { + PresetID int ObjectID int } diff --git a/presets/service.go b/presets/service.go index 58e655d..c576a89 100644 --- a/presets/service.go +++ b/presets/service.go @@ -18,9 +18,9 @@ func NewService(r gcore.Requester) *Service { return &Service{r: r} } -func (s *Service) Create(ctx context.Context, presetID int64, req *CreateRequest) (*Preset, error) { - preset := Preset{ - ID: int(presetID), +func (s *Service) Apply(ctx context.Context, presetID int64, req *ApplyRequest) (*AppliedPreset, error) { + preset := AppliedPreset{ + PresetID: int(presetID), ObjectID: req.ObjectID, } @@ -32,10 +32,10 @@ func (s *Service) Create(ctx context.Context, presetID int64, req *CreateRequest return &preset, nil } -func (s *Service) Get(ctx context.Context, presetID, objectID int64) (*Preset, error) { - var response GetResponse - preset := Preset{ - ID: int(presetID), +func (s *Service) GetAppliedPreset(ctx context.Context, presetID, objectID int64) (*AppliedPreset, error) { + var response GetAppliedPresetResponse + preset := AppliedPreset{ + PresetID: int(presetID), ObjectID: int(objectID), } @@ -54,7 +54,7 @@ func (s *Service) Get(ctx context.Context, presetID, objectID int64) (*Preset, e return nil, fmt.Errorf("error: preset with id %d is not applied to the object with id %d", presetID, objectID) } -func (s *Service) Delete(ctx context.Context, presetID, objectID int64) error { +func (s *Service) Unapply(ctx context.Context, presetID, objectID int64) error { path := fmt.Sprintf("/cdn/presets/%d/applied/%d", presetID, objectID) if err := s.r.Request(ctx, http.MethodDelete, path, nil, nil); err != nil { From 3cdf8fe97c27a5ad0070a7a7d6ce3a1c82fcaa7a Mon Sep 17 00:00:00 2001 From: Andrei Lukyanchyk Date: Fri, 10 May 2024 10:08:33 +0200 Subject: [PATCH 3/4] CDI-696: refactor types in Presets service --- presets/presets.go | 6 +++--- presets/service.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/presets/presets.go b/presets/presets.go index d2b222c..863fef9 100644 --- a/presets/presets.go +++ b/presets/presets.go @@ -5,9 +5,9 @@ import ( ) type PresetsService interface { - Apply(ctx context.Context, presetID int64, req *ApplyRequest) (*AppliedPreset, error) - GetAppliedPreset(ctx context.Context, presetID, objectID int64) (*AppliedPreset, error) - Unapply(ctx context.Context, presetID, objectID int64) error + 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 } type ApplyRequest struct { diff --git a/presets/service.go b/presets/service.go index c576a89..a32de7d 100644 --- a/presets/service.go +++ b/presets/service.go @@ -18,9 +18,9 @@ func NewService(r gcore.Requester) *Service { return &Service{r: r} } -func (s *Service) Apply(ctx context.Context, presetID int64, req *ApplyRequest) (*AppliedPreset, error) { +func (s *Service) Apply(ctx context.Context, presetID int, req *ApplyRequest) (*AppliedPreset, error) { preset := AppliedPreset{ - PresetID: int(presetID), + PresetID: presetID, ObjectID: req.ObjectID, } @@ -32,11 +32,11 @@ func (s *Service) Apply(ctx context.Context, presetID int64, req *ApplyRequest) return &preset, nil } -func (s *Service) GetAppliedPreset(ctx context.Context, presetID, objectID int64) (*AppliedPreset, error) { +func (s *Service) GetAppliedPreset(ctx context.Context, presetID, objectID int) (*AppliedPreset, error) { var response GetAppliedPresetResponse preset := AppliedPreset{ - PresetID: int(presetID), - ObjectID: int(objectID), + PresetID: presetID, + ObjectID: objectID, } path := fmt.Sprintf("/cdn/presets/%d/applied", presetID) @@ -46,15 +46,15 @@ func (s *Service) GetAppliedPreset(ctx context.Context, presetID, objectID int64 // check if objectID is in response.ObjectIDs list, if no - return error for _, id := range response.ObjectIDs { - if id == int(objectID) { + if id == objectID { return &preset, nil } } - return nil, fmt.Errorf("error: preset with id %d is not applied to the object with id %d", presetID, objectID) + return nil, nil } -func (s *Service) Unapply(ctx context.Context, presetID, objectID int64) error { +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 { From 43d7c2719cbb907d1dab923206543bfe63d0438e Mon Sep 17 00:00:00 2001 From: Andrei Lukyanchyk Date: Fri, 10 May 2024 15:12:29 +0200 Subject: [PATCH 4/4] CDI-696: add the Get method to retrieve Preset by ID --- presets/presets.go | 6 ++++++ presets/service.go | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/presets/presets.go b/presets/presets.go index 863fef9..7c9e1b9 100644 --- a/presets/presets.go +++ b/presets/presets.go @@ -8,6 +8,7 @@ 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 { @@ -23,3 +24,8 @@ type AppliedPreset struct { PresetID int ObjectID int } + +type Preset struct { + ID int `json:"id"` + Name string `json:"name"` +} diff --git a/presets/service.go b/presets/service.go index a32de7d..ba3c9db 100644 --- a/presets/service.go +++ b/presets/service.go @@ -63,3 +63,14 @@ func (s *Service) Unapply(ctx context.Context, presetID, objectID int) error { 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 +}