dnscontrol/pkg/cloudflare-go/api_shield.go
Tom Limoncelli 7fd6a74e0c
CLOUDFLAREAPI: CF_REDIRECT/CF_TEMP_REDIRECT should dtrt using Single Redirects (#3002)
Co-authored-by: Josh Zhang <jzhang1@stackoverflow.com>
2024-06-18 17:38:50 -04:00

72 lines
2.2 KiB
Go

package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
// AuthIdCharacteristics a single option from
// configuration?properties=auth_id_characteristics.
type AuthIdCharacteristics struct {
Type string `json:"type"`
Name string `json:"name"`
}
// APIShield is all the available options under
// configuration?properties=auth_id_characteristics.
type APIShield struct {
AuthIdCharacteristics []AuthIdCharacteristics `json:"auth_id_characteristics"`
}
// APIShieldResponse represents the response from the api_gateway/configuration endpoint.
type APIShieldResponse struct {
Result APIShield `json:"result"`
Response
ResultInfo `json:"result_info"`
}
type UpdateAPIShieldParams struct {
AuthIdCharacteristics []AuthIdCharacteristics `json:"auth_id_characteristics"`
}
// GetAPIShieldConfiguration gets a zone API shield configuration.
//
// API documentation: https://api.cloudflare.com/#api-shield-settings-retrieve-information-about-specific-configuration-properties
func (api *API) GetAPIShieldConfiguration(ctx context.Context, rc *ResourceContainer) (APIShield, ResultInfo, error) {
uri := fmt.Sprintf("/zones/%s/api_gateway/configuration?properties=auth_id_characteristics", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return APIShield{}, ResultInfo{}, err
}
var asResponse APIShieldResponse
err = json.Unmarshal(res, &asResponse)
if err != nil {
return APIShield{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return asResponse.Result, asResponse.ResultInfo, nil
}
// UpdateAPIShieldConfiguration sets a zone API shield configuration.
//
// API documentation: https://api.cloudflare.com/#api-shield-settings-set-configuration-properties
func (api *API) UpdateAPIShieldConfiguration(ctx context.Context, rc *ResourceContainer, params UpdateAPIShieldParams) (Response, error) {
uri := fmt.Sprintf("/zones/%s/api_gateway/configuration", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return Response{}, err
}
var asResponse Response
err = json.Unmarshal(res, &asResponse)
if err != nil {
return Response{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return asResponse, nil
}