Skip to content

Commit

Permalink
rename methods to match <verb><noun> format
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Nov 20, 2022
1 parent 160d6d2 commit c6a103d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
42 changes: 33 additions & 9 deletions workers_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ var (
ErrMissingEnvironment = errors.New("required environment missing")
)

type AttachWorkersDomainParams struct {
ID string `json:"id,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
ZoneName string `json:"zone_name,omitempty"`
Hostname string `json:"hostname,omitempty"`
Service string `json:"service,omitempty"`
Environment string `json:"environment,omitempty"`
}

type WorkersDomain struct {
ID string `json:"id,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
Expand All @@ -28,7 +37,7 @@ type WorkersDomainResponse struct {
Result WorkersDomain `json:"result"`
}

type WorkersDomainListParams struct {
type ListWorkersDomainParams struct {
ZoneID string `url:"zone_id,omitempty"`
ZoneName string `url:"zone_name,omitempty"`
Hostname string `url:"hostname,omitempty"`
Expand All @@ -41,86 +50,101 @@ type WorkersDomainListResponse struct {
Result []WorkersDomain `json:"result"`
}

// WorkersListDomains lists all Worker Domains.
// ListWorkersDomains lists all Worker Domains.
//
// API reference: https://api.cloudflare.com/#worker-domain-list-domains
func (api *API) WorkersListDomains(ctx context.Context, rc *ResourceContainer, params WorkersDomainListParams) ([]WorkersDomain, error) {
func (api *API) ListWorkersDomains(ctx context.Context, rc *ResourceContainer, params ListWorkersDomainParams) ([]WorkersDomain, error) {
if rc.Identifier == "" {
return []WorkersDomain{}, ErrMissingAccountID
}

uri := buildURI(fmt.Sprintf("/accounts/%s/workers/domains", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []WorkersDomain{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

var r WorkersDomainListResponse
if err := json.Unmarshal(res, &r); err != nil {
return []WorkersDomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return r.Result, nil
}

// WorkersAttachDomain attaches a worker to a zone and hostname.
// AttachWorkersDomain attaches a worker to a zone and hostname.
//
// API reference: https://api.cloudflare.com/#worker-domain-attach-to-domain
func (api *API) WorkersAttachDomain(ctx context.Context, rc *ResourceContainer, domain WorkersDomain) (WorkersDomain, error) {
func (api *API) AttachWorkersDomain(ctx context.Context, rc *ResourceContainer, domain AttachWorkersDomainParams) (WorkersDomain, error) {
if rc.Identifier == "" {
return WorkersDomain{}, ErrMissingAccountID
}

if domain.ZoneID == "" {
return WorkersDomain{}, ErrMissingZoneID
}

if domain.Hostname == "" {
return WorkersDomain{}, ErrMissingHostname
}

if domain.Service == "" {
return WorkersDomain{}, ErrMissingService
}

if domain.Environment == "" {
return WorkersDomain{}, ErrMissingEnvironment
}

uri := fmt.Sprintf("/accounts/%s/workers/domains", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, domain)
if err != nil {
return WorkersDomain{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

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

return r.Result, nil
}

// WorkersGetDomain gets a Worker Domain.
// GetWorkersDomain gets a single Worker Domain.
//
// API reference: https://api.cloudflare.com/#worker-domain-get-a-domain
func (api *API) WorkersGetDomain(ctx context.Context, rc *ResourceContainer, domainID string) (WorkersDomain, error) {
func (api *API) GetWorkersDomain(ctx context.Context, rc *ResourceContainer, domainID string) (WorkersDomain, error) {
if rc.Identifier == "" {
return WorkersDomain{}, ErrMissingAccountID
}

uri := fmt.Sprintf("/accounts/%s/workers/domains/%s", rc.Identifier, domainID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return WorkersDomain{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

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

return r.Result, nil
}

// WorkersDetachDomain detaches a worker from a zone and hostname.
// DetachWorkersDomain detaches a worker from a zone and hostname.
//
// API reference: https://api.cloudflare.com/#worker-domain-detach-from-domain
func (api *API) WorkersDetachDomain(ctx context.Context, rc *ResourceContainer, domainID string) error {
func (api *API) DetachWorkersDomain(ctx context.Context, rc *ResourceContainer, domainID string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}

uri := fmt.Sprintf("/accounts/%s/workers/domains/%s", rc.Identifier, domainID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("%s: %w", errMakeRequestError, err)
}

return nil
}
24 changes: 12 additions & 12 deletions workers_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func TestWorkersDomain_GetDomain(t *testing.T) {
}
}`)
})
_, err := client.WorkersGetDomain(context.Background(), AccountIdentifier(""), "")
_, err := client.GetWorkersDomain(context.Background(), AccountIdentifier(""), "")
if assert.Error(t, err) {
assert.Equal(t, ErrMissingAccountID, err)
}
res, err := client.WorkersGetDomain(context.Background(), AccountIdentifier(testAccountID), testWorkerDomainID)
res, err := client.GetWorkersDomain(context.Background(), AccountIdentifier(testAccountID), testWorkerDomainID)
if assert.NoError(t, err) {
assert.Equal(t, expectedWorkerDomain, res)
}
Expand Down Expand Up @@ -74,11 +74,11 @@ func TestWorkersDomain_ListDomains(t *testing.T) {
]
}`)
})
_, err := client.WorkersListDomains(context.Background(), AccountIdentifier(""), WorkersDomainListParams{})
_, err := client.ListWorkersDomains(context.Background(), AccountIdentifier(""), ListWorkersDomainParams{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingAccountID, err)
}
res, err := client.WorkersListDomains(context.Background(), AccountIdentifier(testAccountID), WorkersDomainListParams{})
res, err := client.ListWorkersDomains(context.Background(), AccountIdentifier(testAccountID), ListWorkersDomainParams{})
if assert.NoError(t, err) {
assert.Equal(t, 1, len(res))
assert.Equal(t, expectedWorkerDomain, res[0])
Expand Down Expand Up @@ -106,32 +106,32 @@ func TestWorkersDomain_AttachDomain(t *testing.T) {
}
}`)
})
_, err := client.WorkersAttachDomain(context.Background(), AccountIdentifier(""), WorkersDomain{})
_, err := client.AttachWorkersDomain(context.Background(), AccountIdentifier(""), AttachWorkersDomainParams{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingAccountID, err)
}

_, err = client.WorkersAttachDomain(context.Background(), AccountIdentifier(testAccountID), WorkersDomain{})
_, err = client.AttachWorkersDomain(context.Background(), AccountIdentifier(testAccountID), AttachWorkersDomainParams{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingZoneID, err)
}

_, err = client.WorkersAttachDomain(context.Background(), AccountIdentifier(testAccountID), WorkersDomain{
_, err = client.AttachWorkersDomain(context.Background(), AccountIdentifier(testAccountID), AttachWorkersDomainParams{
ZoneID: testZoneID,
})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingHostname, err)
}

_, err = client.WorkersAttachDomain(context.Background(), AccountIdentifier(testAccountID), WorkersDomain{
_, err = client.AttachWorkersDomain(context.Background(), AccountIdentifier(testAccountID), AttachWorkersDomainParams{
ZoneID: testZoneID,
Hostname: "foo.example.com",
})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingService, err)
}

_, err = client.WorkersAttachDomain(context.Background(), AccountIdentifier(testAccountID), WorkersDomain{
_, err = client.AttachWorkersDomain(context.Background(), AccountIdentifier(testAccountID), AttachWorkersDomainParams{
ZoneID: testZoneID,
Hostname: "foo.example.com",
Service: "foo",
Expand All @@ -140,7 +140,7 @@ func TestWorkersDomain_AttachDomain(t *testing.T) {
assert.Equal(t, ErrMissingEnvironment, err)
}

res, err := client.WorkersAttachDomain(context.Background(), AccountIdentifier(testAccountID), WorkersDomain{
res, err := client.AttachWorkersDomain(context.Background(), AccountIdentifier(testAccountID), AttachWorkersDomainParams{
ZoneID: testZoneID,
Hostname: "foo.example.com",
Service: "foo",
Expand Down Expand Up @@ -172,10 +172,10 @@ func TestWorkersDomain_DetachDomain(t *testing.T) {
}
}`)
})
err := client.WorkersDetachDomain(context.Background(), AccountIdentifier(""), testWorkerDomainID)
err := client.DetachWorkersDomain(context.Background(), AccountIdentifier(""), testWorkerDomainID)
if assert.Error(t, err) {
assert.Equal(t, ErrMissingAccountID, err)
}
err = client.WorkersDetachDomain(context.Background(), AccountIdentifier(testAccountID), testWorkerDomainID)
err = client.DetachWorkersDomain(context.Background(), AccountIdentifier(testAccountID), testWorkerDomainID)
assert.NoError(t, err)
}

0 comments on commit c6a103d

Please sign in to comment.