Skip to content

Commit

Permalink
Merge pull request #3529 from WowVeryLogin/denis/FLPROD-796-missing
Browse files Browse the repository at this point in the history
FLPROD-796: Extend snippet interface with DELETE and GET a single sni…
  • Loading branch information
jacobbednarz authored Oct 28, 2024
2 parents 4f23792 + a83b510 commit d45ab25
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
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

0 comments on commit d45ab25

Please sign in to comment.