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

ip_access_rules: Add ListIPAccessRules api call #1428

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1428.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
ip_access_rules: Add ListIPAccessRules() to list IP Access Rules
```
89 changes: 89 additions & 0 deletions ip_access_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cloudflare

import (
"context"
"fmt"
"net/http"

"github.com/goccy/go-json"
)

type ListIPAccessRulesOrderOption string
type ListIPAccessRulesMatchOption string
type IPAccessRulesModeOption string

const (
IPAccessRulesConfigurationTarget ListIPAccessRulesOrderOption = "configuration.target"
IPAccessRulesConfigurationValue ListIPAccessRulesOrderOption = "configuration.value"
IPAccessRulesMatchOptionAll ListIPAccessRulesMatchOption = "all"
IPAccessRulesMatchOptionAny ListIPAccessRulesMatchOption = "any"
IPAccessRulesModeBlock IPAccessRulesModeOption = "block"
IPAccessRulesModeChallenge IPAccessRulesModeOption = "challenge"
IPAccessRulesModeJsChallenge IPAccessRulesModeOption = "js_challenge"
IPAccessRulesModeManagedChallenge IPAccessRulesModeOption = "managed_challenge"
IPAccessRulesModeWhitelist IPAccessRulesModeOption = "whitelist"
)

type ListIPAccessRulesFilters struct {
Configuration IPAccessRuleConfiguration `json:"configuration,omitempty"`
Match ListIPAccessRulesMatchOption `json:"match,omitempty"`
Mode IPAccessRulesModeOption `json:"mode,omitempty"`
Notes string `json:"notes,omitempty"`
}

type ListIPAccessRulesParams struct {
Direction string `url:"direction,omitempty"`
Filters ListIPAccessRulesFilters `url:"filters,omitempty"`
Order ListIPAccessRulesOrderOption `url:"order,omitempty"`
PaginationOptions
}

type IPAccessRuleConfiguration struct {
Target string `json:"target,omitempty"`
Value string `json:"value,omitempty"`
}

type IPAccessRule struct {
AllowedModes []IPAccessRulesModeOption `json:"allowed_modes"`
Configuration IPAccessRuleConfiguration `json:"configuration"`
CreatedOn string `json:"created_on"`
ID string `json:"id"`
Mode IPAccessRulesModeOption `json:"mode"`
ModifiedOn string `json:"modified_on"`
Notes string `json:"notes"`
}

type ListIPAccessRulesResponse struct {
Result []IPAccessRule `json:"result"`
ResultInfo `json:"result_info"`
Response
}

// ListIPAccessRules fetches IP Access rules of a zone/user/account. You can
// filter the results using several optional parameters.
//
// API references:
// - https://developers.cloudflare.com/api/operations/ip-access-rules-for-a-user-list-ip-access-rules
// - https://developers.cloudflare.com/api/operations/ip-access-rules-for-a-zone-list-ip-access-rules
// - https://developers.cloudflare.com/api/operations/ip-access-rules-for-an-account-list-ip-access-rules
func (api *API) ListIPAccessRules(ctx context.Context, rc *ResourceContainer, params ListIPAccessRulesParams) ([]IPAccessRule, *ResultInfo, error) {
if rc.Identifier == "" {
return []IPAccessRule{}, &ResultInfo{}, ErrMissingResourceIdentifier
}

uri := buildURI(fmt.Sprintf("/%s/%s/firewall/access_rules/rules", rc.Level, rc.Identifier), params)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []IPAccessRule{}, &ResultInfo{}, err
}

result := ListIPAccessRulesResponse{}

err = json.Unmarshal(res, &result)
if err != nil {
return []IPAccessRule{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return result.Result, &result.ResultInfo, nil
}
Loading
Loading