Skip to content

Commit

Permalink
CDP-617 support 2-step deletion for rules and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
freenoth committed Mar 14, 2024
1 parent 866d385 commit e805918
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
11 changes: 11 additions & 0 deletions resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
11 changes: 11 additions & 0 deletions rules/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e805918

Please sign in to comment.