From e805918016ae5e4682eb04e1fa187fc20bb15db1 Mon Sep 17 00:00:00 2001 From: Aleksei Iakovlev Date: Thu, 14 Mar 2024 14:03:34 +0800 Subject: [PATCH] CDP-617 support 2-step deletion for rules and resources --- resources/resources.go | 4 ++++ resources/service.go | 11 +++++++++++ rules/rules.go | 4 ++++ rules/service.go | 11 +++++++++++ 4 files changed, 30 insertions(+) diff --git a/resources/resources.go b/resources/resources.go index b8b6386..98f2d0c 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -53,6 +53,10 @@ type UpdateRequest struct { Options *gcore.Options `json:"options,omitempty"` } +type ActivateRequest struct { + Active bool `json:"active"` +} + type Resource struct { ID int64 `json:"id"` Name string `json:"name"` diff --git a/resources/service.go b/resources/service.go index b92ec3b..4b3f8cc 100644 --- a/resources/service.go +++ b/resources/service.go @@ -47,6 +47,17 @@ func (s *Service) Update(ctx context.Context, id int64, req *UpdateRequest) (*Re func (s *Service) Delete(ctx context.Context, resourceID int64) error { path := fmt.Sprintf("/cdn/resources/%d", resourceID) + + var deactivateRequestBody ActivateRequest = ActivateRequest{ + Active: false, + } + var resource Resource + + // deactivate the resource instance before deletion + if err := s.r.Request(ctx, http.MethodPatch, path, &deactivateRequestBody, &resource); err != nil { + return fmt.Errorf("request: %w", err) + } + if err := s.r.Request(ctx, http.MethodDelete, path, nil, nil); err != nil { return fmt.Errorf("request: %w", err) } diff --git a/rules/rules.go b/rules/rules.go index 01173c5..4667996 100644 --- a/rules/rules.go +++ b/rules/rules.go @@ -35,6 +35,10 @@ type UpdateRequest struct { Options *gcore.Options `json:"options,omitempty"` } +type ActivateRequest struct { + Active bool `json:"active"` +} + type Rule struct { ID int64 `json:"id"` Name string `json:"name"` diff --git a/rules/service.go b/rules/service.go index 947e9f4..7f422ad 100644 --- a/rules/service.go +++ b/rules/service.go @@ -53,6 +53,17 @@ func (s *Service) Update(ctx context.Context, resourceID, ruleID int64, req *Upd func (s *Service) Delete(ctx context.Context, resourceID, ruleID int64) error { path := fmt.Sprintf("/cdn/resources/%d/rules/%d", resourceID, ruleID) + + var deactivateRequestBody ActivateRequest = ActivateRequest{ + Active: false, + } + var rule Rule + + // deactivate the rule instance before deletion + if err := s.r.Request(ctx, http.MethodPatch, path, &deactivateRequestBody, &rule); err != nil { + return fmt.Errorf("request: %w", err) + } + if err := s.r.Request(ctx, http.MethodDelete, path, nil, nil); err != nil { return fmt.Errorf("request: %w", err) }