github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/wap_bypass_network_lists.go (about)

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	validation "github.com/go-ozzo/ozzo-validation/v4"
     9  )
    10  
    11  type (
    12  	// The WAPBypassNetworkLists interface supports listing or modifying which network lists are
    13  	// used in the bypass network lists settings.
    14  	//
    15  	// https://developer.akamai.com/api/cloud_security/application_security/v1.html#bypassnetworklist
    16  	WAPBypassNetworkLists interface {
    17  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getbypassnetworklistsforawapconfigversion
    18  		GetWAPBypassNetworkLists(ctx context.Context, params GetWAPBypassNetworkListsRequest) (*GetWAPBypassNetworkListsResponse, error)
    19  
    20  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putbypassnetworklistsforawapconfigversion
    21  		UpdateWAPBypassNetworkLists(ctx context.Context, params UpdateWAPBypassNetworkListsRequest) (*UpdateWAPBypassNetworkListsResponse, error)
    22  
    23  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putbypassnetworklistsforawapconfigversion
    24  		RemoveWAPBypassNetworkLists(ctx context.Context, params RemoveWAPBypassNetworkListsRequest) (*RemoveWAPBypassNetworkListsResponse, error)
    25  	}
    26  
    27  	// GetWAPBypassNetworkListsRequest is used to list which network lists are used in the bypass network lists settings.
    28  	GetWAPBypassNetworkListsRequest struct {
    29  		ConfigID int    `json:"-"`
    30  		Version  int    `json:"-"`
    31  		PolicyID string `json:"policyId"`
    32  	}
    33  
    34  	// NetworkListsStruct is used to define a network list.
    35  	NetworkListsStruct struct {
    36  		Name string `json:"name"`
    37  		ID   string `json:"id"`
    38  	}
    39  
    40  	// GetWAPBypassNetworkListsResponse is returned from a call to GetWAPBypassNetworkLists.
    41  	GetWAPBypassNetworkListsResponse struct {
    42  		NetworkLists []NetworkListsStruct `json:"networkLists"`
    43  	}
    44  
    45  	// UpdateWAPBypassNetworkListsRequest is used to modify which network lists are used in the bypass network lists settings.
    46  	UpdateWAPBypassNetworkListsRequest struct {
    47  		ConfigID     int      `json:"-"`
    48  		Version      int      `json:"-"`
    49  		PolicyID     string   `json:"policyId"`
    50  		NetworkLists []string `json:"networkLists"`
    51  	}
    52  
    53  	// IPNetworkListsList is used to define a list of IP network lists.
    54  	IPNetworkListsList struct {
    55  		NetworkList []string `json:"networkList"`
    56  	}
    57  
    58  	// GeoControlsList is used to define a list of blocked IP network lists.
    59  	GeoControlsList struct {
    60  		BlockedIPNetworkLists IPNetworkListsList `json:"networkList"`
    61  	}
    62  
    63  	// IPControlsLists is used to define lists of allowed and blocked IP network lists.
    64  	IPControlsLists struct {
    65  		AllowedIPNetworkLists IPNetworkListsList `json:"allowedIPNetworkLists"`
    66  		BlockedIPNetworkLists IPNetworkListsList `json:"blockedIPNetworkLists"`
    67  	}
    68  
    69  	// UpdateWAPBypassNetworkListsResponse is returned from a call to UpdateWAPBypassNetworkLists.
    70  	UpdateWAPBypassNetworkListsResponse struct {
    71  		Block       string          `json:"block"`
    72  		GeoControls GeoControlsList `json:"geoControls"`
    73  		IPControls  IPControlsLists `json:"ipControls"`
    74  	}
    75  
    76  	// RemoveWAPBypassNetworkListsRequest is used to modify which network lists are used in the bypass network lists settings.
    77  	// Deprecated: this struct will be removed in a future release.
    78  	RemoveWAPBypassNetworkListsRequest struct {
    79  		ConfigID     int      `json:"-"`
    80  		Version      int      `json:"-"`
    81  		PolicyID     string   `json:"policyId"`
    82  		NetworkLists []string `json:"networkLists"`
    83  	}
    84  
    85  	// RemoveWAPBypassNetworkListsResponse is returned from a call to RemoveWAPBypassNetworkLists.
    86  	// Deprecated: this struct will be removed in a future release.
    87  	RemoveWAPBypassNetworkListsResponse struct {
    88  		NetworkLists []string `json:"networkLists"`
    89  	}
    90  )
    91  
    92  // Validate validates a GetWAPBypassNetworkListsRequest.
    93  func (v GetWAPBypassNetworkListsRequest) Validate() error {
    94  	return validation.Errors{
    95  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    96  		"Version":  validation.Validate(v.Version, validation.Required),
    97  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    98  	}.Filter()
    99  }
   100  
   101  // Validate validates an UpdateWAPBypassNetworkListsRequest.
   102  func (v UpdateWAPBypassNetworkListsRequest) Validate() error {
   103  	return validation.Errors{
   104  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   105  		"Version":  validation.Validate(v.Version, validation.Required),
   106  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   107  	}.Filter()
   108  }
   109  
   110  // Validate validates a RemoveWAPBypassNetworkListsRequest.
   111  // Deprecated: this method will be removed in a future release.
   112  func (v RemoveWAPBypassNetworkListsRequest) Validate() error {
   113  	return validation.Errors{
   114  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   115  		"Version":  validation.Validate(v.Version, validation.Required),
   116  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   117  	}.Filter()
   118  }
   119  
   120  func (p *appsec) GetWAPBypassNetworkLists(ctx context.Context, params GetWAPBypassNetworkListsRequest) (*GetWAPBypassNetworkListsResponse, error) {
   121  	logger := p.Log(ctx)
   122  	logger.Debugf("GetWAPBypassNetworkLists")
   123  
   124  	if err := params.Validate(); err != nil {
   125  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   126  	}
   127  
   128  	uri := fmt.Sprintf(
   129  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bypass-network-lists",
   130  		params.ConfigID,
   131  		params.Version,
   132  		params.PolicyID,
   133  	)
   134  
   135  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   136  	if err != nil {
   137  		return nil, fmt.Errorf("failed to create GetWAPBypassNetworkLists request: %w", err)
   138  	}
   139  
   140  	var result GetWAPBypassNetworkListsResponse
   141  	resp, err := p.Exec(req, &result)
   142  	if err != nil {
   143  		return nil, fmt.Errorf("get WAP bypass network lists request failed: %w", err)
   144  	}
   145  	if resp.StatusCode != http.StatusOK {
   146  		return nil, p.Error(resp)
   147  	}
   148  
   149  	return &result, nil
   150  }
   151  
   152  func (p *appsec) UpdateWAPBypassNetworkLists(ctx context.Context, params UpdateWAPBypassNetworkListsRequest) (*UpdateWAPBypassNetworkListsResponse, error) {
   153  	logger := p.Log(ctx)
   154  	logger.Debugf("UpdateWAPBypassNetworkLists")
   155  
   156  	if err := params.Validate(); err != nil {
   157  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   158  	}
   159  
   160  	uri := fmt.Sprintf(
   161  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bypass-network-lists",
   162  		params.ConfigID,
   163  		params.Version,
   164  		params.PolicyID,
   165  	)
   166  
   167  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   168  	if err != nil {
   169  		return nil, fmt.Errorf("failed to create UpdateWAPBypassNetworkLists request: %w", err)
   170  	}
   171  
   172  	var result UpdateWAPBypassNetworkListsResponse
   173  	resp, err := p.Exec(req, &result, params)
   174  	if err != nil {
   175  		return nil, fmt.Errorf("update WAP bypass network lists request failed: %w", err)
   176  	}
   177  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   178  		return nil, p.Error(resp)
   179  	}
   180  
   181  	return &result, nil
   182  }
   183  
   184  // Deprecated: this method will be removed in a future release.
   185  func (p *appsec) RemoveWAPBypassNetworkLists(ctx context.Context, params RemoveWAPBypassNetworkListsRequest) (*RemoveWAPBypassNetworkListsResponse, error) {
   186  	logger := p.Log(ctx)
   187  	logger.Debugf("RemoveWAPBypassNetworkLists")
   188  
   189  	if err := params.Validate(); err != nil {
   190  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   191  	}
   192  
   193  	uri := fmt.Sprintf(
   194  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bypass-network-lists",
   195  		params.ConfigID,
   196  		params.Version,
   197  		params.PolicyID,
   198  	)
   199  
   200  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   201  	if err != nil {
   202  		return nil, fmt.Errorf("failed to create RemoveWAPBypassNetworkLists request: %w", err)
   203  	}
   204  
   205  	var result RemoveWAPBypassNetworkListsResponse
   206  	resp, err := p.Exec(req, &result, params)
   207  	if err != nil {
   208  		return nil, fmt.Errorf("remove WAP bypass network lists request failed: %w", err)
   209  	}
   210  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   211  		return nil, p.Error(resp)
   212  	}
   213  
   214  	return &result, nil
   215  }