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

Generalize IP lists to lists of arbitrary kind. #918

Merged
merged 2 commits into from Jun 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions errors.go
Expand Up @@ -26,8 +26,9 @@ const (
)

var (
ErrMissingAccountID = errors.New("required missing account ID")
ErrMissingZoneID = errors.New("required missing zone ID")
ErrMissingAccountID = errors.New("required missing account ID")
ErrMissingZoneID = errors.New("required missing zone ID")
ErrMissingResourceIdentifier = errors.New("required missing resource identifier")
)

type ErrorType string
Expand Down
88 changes: 66 additions & 22 deletions ip_list.go
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/pkg/errors"
)

// The definitions in this file are deprecated and should be removed after
// enough time is given for users to migrate. Use the more general `List`
// methods instead.

const (
// IPListTypeIP specifies a list containing IP addresses.
IPListTypeIP = "ip"
Expand Down Expand Up @@ -127,9 +131,11 @@ type IPListItemsGetResponse struct {
Result IPListItem `json:"result"`
}

// ListIPLists lists all IP Lists
// ListIPLists lists all IP Lists.
//
// API reference: https://api.cloudflare.com/#rules-lists-list-lists
//
// Deprecated: Use `ListLists` instead.
func (api *API) ListIPLists(ctx context.Context, accountID string) ([]IPList, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
Expand All @@ -145,9 +151,11 @@ func (api *API) ListIPLists(ctx context.Context, accountID string) ([]IPList, er
return result.Result, nil
}

// CreateIPList creates a new IP List
// CreateIPList creates a new IP List.
//
// API reference: https://api.cloudflare.com/#rules-lists-create-list
//
// Deprecated: Use `CreateList` instead.
func (api *API) CreateIPList(ctx context.Context, accountID, name, description, kind string) (IPList,
error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists", accountID)
Expand All @@ -168,6 +176,8 @@ func (api *API) CreateIPList(ctx context.Context, accountID, name, description,
// GetIPList returns a single IP List
//
// API reference: https://api.cloudflare.com/#rules-lists-get-list
//
// Deprecated: Use `GetList` instead.
func (api *API) GetIPList(ctx context.Context, accountID, ID string) (IPList, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
Expand All @@ -183,9 +193,11 @@ func (api *API) GetIPList(ctx context.Context, accountID, ID string) (IPList, er
return result.Result, nil
}

// UpdateIPList updates the description of an existing IP List
// UpdateIPList updates the description of an existing IP List.
//
// API reference: https://api.cloudflare.com/#rules-lists-update-list
//
// Deprecated: Use `UpdateList` instead.
func (api *API) UpdateIPList(ctx context.Context, accountID, ID, description string) (IPList, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, IPListUpdateRequest{Description: description})
Expand All @@ -201,9 +213,11 @@ func (api *API) UpdateIPList(ctx context.Context, accountID, ID, description str
return result.Result, nil
}

// DeleteIPList deletes an IP List
// DeleteIPList deletes an IP List.
//
// API reference: https://api.cloudflare.com/#rules-lists-delete-list
//
// Deprecated: Use `DeleteList` instead.
func (api *API) DeleteIPList(ctx context.Context, accountID, ID string) (IPListDeleteResponse, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
Expand All @@ -219,9 +233,11 @@ func (api *API) DeleteIPList(ctx context.Context, accountID, ID string) (IPListD
return result, nil
}

// ListIPListItems returns a list with all items in an IP List
// ListIPListItems returns a list with all items in an IP List.
//
// API reference: https://api.cloudflare.com/#rules-lists-list-list-items
//
// Deprecated: Use `ListListItems` instead.
func (api *API) ListIPListItems(ctx context.Context, accountID, ID string) ([]IPListItem, error) {
var list []IPListItem
var cursor string
Expand Down Expand Up @@ -251,10 +267,13 @@ func (api *API) ListIPListItems(ctx context.Context, accountID, ID string) ([]IP
return list, nil
}

// CreateIPListItemAsync creates a new IP List Item asynchronously. Users have to poll the operation status by
// using the operation_id returned by this function.
// CreateIPListItemAsync creates a new IP List Item asynchronously. Users have
// to poll the operation status by using the operation_id returned by this
// function.
//
// API reference: https://api.cloudflare.com/#rules-lists-create-list-items
//
// Deprecated: Use `CreateListItemAsync` instead.
func (api *API) CreateIPListItemAsync(ctx context.Context, accountID, ID, ip, comment string) (IPListItemCreateResponse, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, []IPListItemCreateRequest{{IP: ip, Comment: comment}})
Expand All @@ -270,7 +289,10 @@ func (api *API) CreateIPListItemAsync(ctx context.Context, accountID, ID, ip, co
return result, nil
}

// CreateIPListItem creates a new IP List Item synchronously and returns the current set of IP List Items.
// CreateIPListItem creates a new IP List Item synchronously and returns the
// current set of IP List Items.
//
// Deprecated: Use `CreateListItem` instead.
func (api *API) CreateIPListItem(ctx context.Context, accountID, ID, ip, comment string) ([]IPListItem, error) {
result, err := api.CreateIPListItemAsync(ctx, accountID, ID, ip, comment)

Expand All @@ -286,10 +308,13 @@ func (api *API) CreateIPListItem(ctx context.Context, accountID, ID, ip, comment
return api.ListIPListItems(ctx, accountID, ID)
}

// CreateIPListItemsAsync bulk creates many IP List Items asynchronously. Users have to poll the operation status by
// using the operation_id returned by this function.
// CreateIPListItemsAsync bulk creates many IP List Items asynchronously. Users
// have to poll the operation status by using the operation_id returned by this
// function.
//
// API reference: https://api.cloudflare.com/#rules-lists-create-list-items
//
// Deprecated: Use `CreateListItemsAsync` instead.
func (api *API) CreateIPListItemsAsync(ctx context.Context, accountID, ID string, items []IPListItemCreateRequest) (
IPListItemCreateResponse, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", accountID, ID)
Expand All @@ -306,7 +331,10 @@ func (api *API) CreateIPListItemsAsync(ctx context.Context, accountID, ID string
return result, nil
}

// CreateIPListItems bulk creates many IP List Items synchronously and returns the current set of IP List Items.
// CreateIPListItems bulk creates many IP List Items synchronously and returns
// the current set of IP List Items.
//
// Deprecated: Use `CreateListItems` instead.
func (api *API) CreateIPListItems(ctx context.Context, accountID, ID string, items []IPListItemCreateRequest) (
[]IPListItem, error) {
result, err := api.CreateIPListItemsAsync(ctx, accountID, ID, items)
Expand All @@ -322,10 +350,13 @@ func (api *API) CreateIPListItems(ctx context.Context, accountID, ID string, ite
return api.ListIPListItems(ctx, accountID, ID)
}

// ReplaceIPListItemsAsync replaces all IP List Items asynchronously. Users have to poll the operation status by
// using the operation_id returned by this function.
// ReplaceIPListItemsAsync replaces all IP List Items asynchronously. Users have
// to poll the operation status by using the operation_id returned by this
// function.
//
// API reference: https://api.cloudflare.com/#rules-lists-replace-list-items
//
// Deprecated: Use `ReplaceListItemsAsync` instead.
func (api *API) ReplaceIPListItemsAsync(ctx context.Context, accountID, ID string, items []IPListItemCreateRequest) (
IPListItemCreateResponse, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", accountID, ID)
Expand All @@ -342,7 +373,10 @@ func (api *API) ReplaceIPListItemsAsync(ctx context.Context, accountID, ID strin
return result, nil
}

// ReplaceIPListItems replaces all IP List Items synchronously and returns the current set of IP List Items.
// ReplaceIPListItems replaces all IP List Items synchronously and returns the
// current set of IP List Items.
//
// Deprecated: Use `ReplaceListItems` instead.
func (api *API) ReplaceIPListItems(ctx context.Context, accountID, ID string, items []IPListItemCreateRequest) (
[]IPListItem, error) {
result, err := api.ReplaceIPListItemsAsync(ctx, accountID, ID, items)
Expand All @@ -358,10 +392,13 @@ func (api *API) ReplaceIPListItems(ctx context.Context, accountID, ID string, it
return api.ListIPListItems(ctx, accountID, ID)
}

// DeleteIPListItemsAsync removes specific Items of an IP List by their ID asynchronously. Users have to poll the
// operation status by using the operation_id returned by this function.
// DeleteIPListItemsAsync removes specific Items of an IP List by their ID
// asynchronously. Users have to poll the operation status by using the
// operation_id returned by this function.
//
// API reference: https://api.cloudflare.com/#rules-lists-delete-list-items
//
// Deprecated: Use `DeleteListItemsAsync` instead.
func (api *API) DeleteIPListItemsAsync(ctx context.Context, accountID, ID string, items IPListItemDeleteRequest) (
IPListItemDeleteResponse, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", accountID, ID)
Expand All @@ -378,8 +415,10 @@ func (api *API) DeleteIPListItemsAsync(ctx context.Context, accountID, ID string
return result, nil
}

// DeleteIPListItems removes specific Items of an IP List by their ID synchronously and returns the current set
// of IP List Items.
// DeleteIPListItems removes specific Items of an IP List by their ID
// synchronously and returns the current set of IP List Items.
//
// Deprecated: Use `DeleteListItems` instead.
func (api *API) DeleteIPListItems(ctx context.Context, accountID, ID string, items IPListItemDeleteRequest) (
[]IPListItem, error) {
result, err := api.DeleteIPListItemsAsync(ctx, accountID, ID, items)
Expand All @@ -395,9 +434,11 @@ func (api *API) DeleteIPListItems(ctx context.Context, accountID, ID string, ite
return api.ListIPListItems(ctx, accountID, ID)
}

// GetIPListItem returns a single IP List Item
// GetIPListItem returns a single IP List Item.
//
// API reference: https://api.cloudflare.com/#rules-lists-get-list-item
//
// Deprecated: Use `GetListItem` instead.
func (api *API) GetIPListItem(ctx context.Context, accountID, listID, id string) (IPListItem, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items/%s", accountID, listID, id)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
Expand All @@ -413,9 +454,11 @@ func (api *API) GetIPListItem(ctx context.Context, accountID, listID, id string)
return result.Result, nil
}

// GetIPListBulkOperation returns the status of a bulk operation
// GetIPListBulkOperation returns the status of a bulk operation.
//
// API reference: https://api.cloudflare.com/#rules-lists-get-bulk-operation
//
// Deprecated: Use `GetListBulkOperation` instead.
func (api *API) GetIPListBulkOperation(ctx context.Context, accountID, ID string) (IPListBulkOperation, error) {
uri := fmt.Sprintf("/accounts/%s/rules/lists/bulk_operations/%s", accountID, ID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
Expand All @@ -431,8 +474,9 @@ func (api *API) GetIPListBulkOperation(ctx context.Context, accountID, ID string
return result.Result, nil
}

// pollIPListBulkOperation implements synchronous behaviour for some asynchronous endpoints.
// bulk-operation status can be either pending, running, failed or completed.
// pollIPListBulkOperation implements synchronous behaviour for some
// asynchronous endpoints. bulk-operation status can be either pending, running,
// failed or completed.
func (api *API) pollIPListBulkOperation(ctx context.Context, accountID, ID string) error {
for i := uint8(0); i < 16; i++ {
sleepDuration := 1 << (i / 2) * time.Second
Expand Down