-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1428 from onrocketdotcom/dkoston/add_list_ip_acce…
…ss_rules ip_access_rules: Add ListIPAccessRules api call
- Loading branch information
Showing
3 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.