diff --git a/.changelog/1384.txt b/.changelog/1384.txt new file mode 100644 index 00000000000..83a5c51b8ec --- /dev/null +++ b/.changelog/1384.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +dcv_delegation: add GET for DCV Delegation UUID +``` diff --git a/dcv_delegation.go b/dcv_delegation.go new file mode 100644 index 00000000000..601d9f0257f --- /dev/null +++ b/dcv_delegation.go @@ -0,0 +1,39 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + + "github.com/goccy/go-json" +) + +type DCVDelegation struct { + UUID string `json:"uuid"` +} + +// DCVDelegationResponse represents the response from the dcv_delegation/uuid endpoint. +type DCVDelegationResponse struct { + Result DCVDelegation `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +// GetDCVDelegation gets a zone DCV Delegation UUID +// +// TODO: Add API documentation +func (api *API) GetDCVDelegation(ctx context.Context, rc *ResourceContainer) (DCVDelegation, ResultInfo, error) { + uri := fmt.Sprintf("/zones/%s/dcv_delegation/uuid", rc.Identifier) + + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return DCVDelegation{}, ResultInfo{}, err + } + var dcvResponse DCVDelegationResponse + err = json.Unmarshal(res, &dcvResponse) + if err != nil { + return DCVDelegation{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return dcvResponse.Result, dcvResponse.ResultInfo, nil +} diff --git a/dcv_delegation_test.go b/dcv_delegation_test.go new file mode 100644 index 00000000000..0fab7b127b2 --- /dev/null +++ b/dcv_delegation_test.go @@ -0,0 +1,43 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetDCVDelegation(t *testing.T) { + setup() + defer teardown() + + testUuid := "b9ab465427f949ed" + + 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.Fprintf(w, `{ + "success" : true, + "errors": [], + "messages": [], + "result": { + "uuid": "%s" + } +} + `, testUuid) + } + + mux.HandleFunc("/zones/"+testZoneID+"/dcv_delegation/uuid", handler) + + want := DCVDelegation{ + UUID: testUuid, + } + + actual, _, err := client.GetDCVDelegation(context.Background(), ZoneIdentifier(testZoneID)) + + if assert.NoError(t, err) { + assert.Equal(t, want, actual) + } +}