Skip to content

Commit

Permalink
Merge pull request #33 from Archirk/master
Browse files Browse the repository at this point in the history
Add method to toggle protection state
  • Loading branch information
Archirk authored Jun 6, 2024
2 parents 7f4f523 + e112727 commit 6b87a8d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
12 changes: 7 additions & 5 deletions pkg/v2/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
)

const (
rootPath = "/zones"
zonePath = "/zones/%v"
zonePathUpdateState = "/zones/%v/state"
rrsetPath = "/zones/%v/rrset"
singleRRSetPath = "/zones/%v/rrset/%v"
rootPath = "/zones"
zonePath = "/zones/%v"
zonePathUpdateState = "/zones/%v/state"
zonePathUpdateProtection = "/zones/%v/protection"
rrsetPath = "/zones/%v/rrset"
singleRRSetPath = "/zones/%v/rrset/%v"
)

type (
Expand All @@ -31,6 +32,7 @@ type (
DeleteZone(ctx context.Context, zoneID string) error
UpdateZoneState(ctx context.Context, zoneID string, disabled bool) error
UpdateZoneComment(ctx context.Context, zoneID string, comment string) error
UpdateProtectionState(ctx context.Context, zoneID string, protected bool) error
}

RRSetManager[S any] interface {
Expand Down
11 changes: 6 additions & 5 deletions pkg/v2/testing/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const (
)

const (
rootPath = "/zones"
zonePath = "/zones/%v"
zonePathUpdateState = "/zones/%v/state"
rrsetPath = "/zones/%v/rrset"
singleRRSetPath = "/zones/%v/rrset/%v"
rootPath = "/zones"
zonePath = "/zones/%v"
zonePathUpdateState = "/zones/%v/state"
zonePathUpdateProtection = "/zones/%v/protection"
rrsetPath = "/zones/%v/rrset"
singleRRSetPath = "/zones/%v/rrset/%v"
)

var (
Expand Down
13 changes: 13 additions & 0 deletions pkg/v2/testing/zoneManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,16 @@ func (s *ZoneManageSuite) TestUpdateZoneState_state_updated() {

s.Nil(err)
}

func (s *ZoneManageSuite) TestUpdateProtectionState_OK() {
path := fmt.Sprintf(zonePathUpdateProtection, testID)
httpmock.RegisterResponder(
http.MethodPatch,
fmt.Sprintf("%s%s", testAPIURL, path),
httpmock.NewBytesResponder(http.StatusNoContent, []byte{}),
)

err := testClient.UpdateProtectionState(testCtx, testID, true)

s.Nil(err)
}
20 changes: 20 additions & 0 deletions pkg/v2/zoneManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type (
zoneUpdateState struct {
Disabled bool `json:"disabled"`
}
zoneProtectionState struct {
Protected bool `json:"protected"`
}
)

func (z *Zone) CreationForm() (io.Reader, error) {
Expand Down Expand Up @@ -123,3 +126,20 @@ func (c *Client) UpdateZoneState(ctx context.Context, zoneID string, disabled bo

return err
}

// UpdateProtectionState request to enable/disable zone protection from delete operation.
func (c *Client) UpdateProtectionState(ctx context.Context, zoneID string, protected bool) error {
updateState, err := json.Marshal(zoneProtectionState{
Protected: protected,
})
if err != nil {
return fmt.Errorf("zone marshal: %w", err)
}
form := bytes.NewReader(updateState)
r, e := c.prepareRequest(
ctx, http.MethodPatch, fmt.Sprintf(zonePathUpdateProtection, zoneID), form, nil, nil,
)
_, err = processRequest[Zone](c.httpClient, r, e)

return err
}

0 comments on commit 6b87a8d

Please sign in to comment.