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

FLPROD-796: Extend snippet interface with DELETE and GET a single sni… #3529

Merged
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
3 changes: 3 additions & 0 deletions .changelog/3529.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
snippets: add missing delete and get a single snippet methods
```
48 changes: 48 additions & 0 deletions snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloudflare
import (
"bytes"
"context"
"errors"
"fmt"
"mime/multipart"
"net/http"
Expand All @@ -16,6 +17,11 @@ type SnippetsResponse struct {
Result []Snippet `json:"result"`
}

type SnippetResponse struct {
Response
Result *Snippet `json:"result"`
}

type Snippet struct {
CreatedOn *time.Time `json:"created_on"`
ModifiedOn *time.Time `json:"modified_on"`
Expand All @@ -33,6 +39,48 @@ type SnippetRequest struct {
Files []SnippetFile `json:"files"`
}

func (api *API) DeleteZoneSnippet(ctx context.Context, rc *ResourceContainer, snippetName string) error {
if rc.Identifier == "" {
return ErrMissingZoneID
}

uri := buildURI(fmt.Sprintf("/zones/%s/snippets/%s", rc.Identifier, snippetName), nil)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}

result := SnippetResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return fmt.Errorf("%s: %w", errUnmarshalError, err)
}

if !result.Success {
return errors.New(result.Messages[0].Message)
}

return nil
}

func (api *API) GetZoneSnippet(ctx context.Context, rc *ResourceContainer, snippetName string) (*Snippet, error) {
if rc.Identifier == "" {
return nil, ErrMissingZoneID
}

uri := buildURI(fmt.Sprintf("/zones/%s/snippets/%s", rc.Identifier, snippetName), nil)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}

result := SnippetResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return result.Result, nil
}

func (api *API) ListZoneSnippets(ctx context.Context, rc *ResourceContainer) ([]Snippet, error) {
if rc.Identifier == "" {
return nil, ErrMissingZoneID
Expand Down
52 changes: 52 additions & 0 deletions snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,58 @@ func TestSnippets(t *testing.T) {
}
}

func TestGetSnippet(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"result": {
"snippet_name": "some_id_1",
"created_on":"0001-01-01T00:00:00Z",
"modified_on":"0001-01-01T00:00:00Z"
},
"success": true,
"errors": [],
"messages": []
}`)
}
mux.HandleFunc("/zones/"+testZoneID+"/snippets/some_id_1", handler)

want := Snippet{
SnippetName: "some_id_1",
CreatedOn: &time.Time{},
ModifiedOn: &time.Time{},
}

zoneActual, err := client.GetZoneSnippet(context.Background(), ZoneIdentifier(testZoneID), "some_id_1")
if assert.NoError(t, err) {
assert.Equal(t, want, *zoneActual)
}
}

func TestDeleteSnippet(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"result": null,
"success": true,
"errors": [],
"messages": []
}`)
}
mux.HandleFunc("/zones/"+testZoneID+"/snippets/some_id_1", handler)

err := client.DeleteZoneSnippet(context.Background(), ZoneIdentifier(testZoneID), "some_id_1")
assert.NoError(t, err)
}

func TestUpdateSnippets(t *testing.T) {
setup()
defer teardown()
Expand Down
Loading