diff --git a/.changelog/3529.txt b/.changelog/3529.txt new file mode 100644 index 00000000000..32da23d64c3 --- /dev/null +++ b/.changelog/3529.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +snippets: add missing delete and get a single snippet methods +``` diff --git a/snippets.go b/snippets.go index 870b8f73a9a..d21ea6885f3 100644 --- a/snippets.go +++ b/snippets.go @@ -3,6 +3,7 @@ package cloudflare import ( "bytes" "context" + "errors" "fmt" "mime/multipart" "net/http" @@ -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"` @@ -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 diff --git a/snippets_test.go b/snippets_test.go index 627b3cb55da..805a9f21fdc 100644 --- a/snippets_test.go +++ b/snippets_test.go @@ -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()