github.com/akamai/AkamaiOPEN-edgegrid-golang/v4@v4.1.0/pkg/appsec/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 BypassNetworkLists interface supports listing or modifying which network lists are
    13  	// used in the bypass network lists settings.
    14  	// Deprecated: this interface will be removed in a future release. Use the WAPBypassNetworkLists interface instead.
    15  	BypassNetworkLists interface {
    16  		// Deprecated: this method will be removed in a future release. Use the GetWAPBypassNetworkLists method of the WAPBypassNetworkLists interface instead.
    17  		//
    18  		// See: https://techdocs.akamai.com/application-security/reference/get-bypass-network-lists
    19  		GetBypassNetworkLists(ctx context.Context, params GetBypassNetworkListsRequest) (*GetBypassNetworkListsResponse, error)
    20  
    21  		// Deprecated: this method will be removed in a future release. Use the UpdateWAPBypassNetworkLists method of the WAPBypassNetworkLists interface instead.
    22  		//
    23  		// See: https://techdocs.akamai.com/application-security/reference/put-bypass-network-lists
    24  		UpdateBypassNetworkLists(ctx context.Context, params UpdateBypassNetworkListsRequest) (*UpdateBypassNetworkListsResponse, error)
    25  
    26  		// Deprecated: this method will be removed in a future release. Use the UpdateWAPBypassNetworkLists method of the WAPBypassNetworkLists interface instead.
    27  		//
    28  		// See: https://techdocs.akamai.com/application-security/reference/put-bypass-network-lists
    29  		RemoveBypassNetworkLists(ctx context.Context, params RemoveBypassNetworkListsRequest) (*RemoveBypassNetworkListsResponse, error)
    30  	}
    31  
    32  	// GetBypassNetworkListsRequest is used to list which network lists are used in the bypass network lists settings.
    33  	GetBypassNetworkListsRequest struct {
    34  		ConfigID int    `json:"-"`
    35  		Version  int    `json:"-"`
    36  		PolicyID string `json:"policyId"`
    37  	}
    38  
    39  	// GetBypassNetworkListsResponse is returned from a call to GetBypassNetworkLists.
    40  	GetBypassNetworkListsResponse struct {
    41  		NetworkLists []NetworkList `json:"networkLists"`
    42  	}
    43  
    44  	// UpdateBypassNetworkListsRequest is used to modify which network lists are used in the bypass network lists settings.
    45  	UpdateBypassNetworkListsRequest struct {
    46  		ConfigID     int      `json:"-"`
    47  		Version      int      `json:"-"`
    48  		PolicyID     string   `json:"policyId"`
    49  		NetworkLists []string `json:"networkLists"`
    50  	}
    51  
    52  	// UpdateBypassNetworkListsResponse is returned from a call to UpdateBypassNetworkLists.
    53  	UpdateBypassNetworkListsResponse struct {
    54  		NetworkLists []string `json:"networkLists"`
    55  	}
    56  
    57  	// RemoveBypassNetworkListsRequest is used to modify which network lists are used in the bypass network lists settings.
    58  	// Deprecated: this struct will be removed in a future release.
    59  	RemoveBypassNetworkListsRequest struct {
    60  		ConfigID     int      `json:"-"`
    61  		Version      int      `json:"-"`
    62  		PolicyID     string   `json:"policyId"`
    63  		NetworkLists []string `json:"networkLists"`
    64  	}
    65  
    66  	// RemoveBypassNetworkListsResponse is returned from a call to RemoveBypassNetworkLists.
    67  	// Deprecated: this struct will be removed in a future release.
    68  	RemoveBypassNetworkListsResponse struct {
    69  		NetworkLists []string `json:"networkLists"`
    70  	}
    71  )
    72  
    73  // Validate validates a GetBypassNetworkListsRequest.
    74  func (v GetBypassNetworkListsRequest) Validate() error {
    75  	return validation.Errors{
    76  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    77  		"Version":  validation.Validate(v.Version, validation.Required),
    78  	}.Filter()
    79  }
    80  
    81  // Validate validates an UpdateBypassNetworkListsRequest.
    82  func (v UpdateBypassNetworkListsRequest) Validate() error {
    83  	return validation.Errors{
    84  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    85  		"Version":  validation.Validate(v.Version, validation.Required),
    86  	}.Filter()
    87  }
    88  
    89  // Validate validates a RemoveBypassNetworkListsRequest.
    90  // Deprecated: this method will be removed in a future release.
    91  func (v RemoveBypassNetworkListsRequest) Validate() error {
    92  	return validation.Errors{
    93  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    94  		"Version":  validation.Validate(v.Version, validation.Required),
    95  	}.Filter()
    96  }
    97  
    98  func (p *appsec) GetBypassNetworkLists(ctx context.Context, params GetBypassNetworkListsRequest) (*GetBypassNetworkListsResponse, error) {
    99  	logger := p.Log(ctx)
   100  	logger.Debugf("GetBypassNetworkLists")
   101  
   102  	if err := params.Validate(); err != nil {
   103  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   104  	}
   105  
   106  	var uri string
   107  	if params.PolicyID == "" {
   108  		uri = fmt.Sprintf(
   109  			"/appsec/v1/configs/%d/versions/%d/bypass-network-lists",
   110  			params.ConfigID,
   111  			params.Version,
   112  		)
   113  	} else {
   114  		uri = fmt.Sprintf(
   115  			"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bypass-network-lists",
   116  			params.ConfigID,
   117  			params.Version,
   118  			params.PolicyID,
   119  		)
   120  	}
   121  
   122  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   123  	if err != nil {
   124  		return nil, fmt.Errorf("failed to create GetBypassNetworkLists request: %w", err)
   125  	}
   126  
   127  	var result GetBypassNetworkListsResponse
   128  	resp, err := p.Exec(req, &result)
   129  	if err != nil {
   130  		return nil, fmt.Errorf("get bypass network lists request failed: %w", err)
   131  	}
   132  	if resp.StatusCode != http.StatusOK {
   133  		return nil, p.Error(resp)
   134  	}
   135  
   136  	return &result, nil
   137  }
   138  
   139  func (p *appsec) UpdateBypassNetworkLists(ctx context.Context, params UpdateBypassNetworkListsRequest) (*UpdateBypassNetworkListsResponse, error) {
   140  	logger := p.Log(ctx)
   141  	logger.Debugf("UpdateBypassNetworkLists")
   142  
   143  	if err := params.Validate(); err != nil {
   144  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   145  	}
   146  
   147  	var uri string
   148  	if params.PolicyID == "" {
   149  		uri = fmt.Sprintf(
   150  			"/appsec/v1/configs/%d/versions/%d/bypass-network-lists",
   151  			params.ConfigID,
   152  			params.Version,
   153  		)
   154  	} else {
   155  		uri = fmt.Sprintf(
   156  			"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bypass-network-lists",
   157  			params.ConfigID,
   158  			params.Version,
   159  			params.PolicyID,
   160  		)
   161  	}
   162  
   163  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   164  	if err != nil {
   165  		return nil, fmt.Errorf("failed to create UpdateBypassNetworkLists request: %w", err)
   166  	}
   167  
   168  	var result UpdateBypassNetworkListsResponse
   169  	resp, err := p.Exec(req, &result, params)
   170  	if err != nil {
   171  		return nil, fmt.Errorf("update bypass network lists request failed: %w", err)
   172  	}
   173  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   174  		return nil, p.Error(resp)
   175  	}
   176  
   177  	return &result, nil
   178  }
   179  
   180  // Deprecated: this method will be removed in a future release.
   181  func (p *appsec) RemoveBypassNetworkLists(ctx context.Context, params RemoveBypassNetworkListsRequest) (*RemoveBypassNetworkListsResponse, error) {
   182  	logger := p.Log(ctx)
   183  	logger.Debugf("RemoveBypassNetworkLists")
   184  
   185  	if err := params.Validate(); err != nil {
   186  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   187  	}
   188  
   189  	var uri string
   190  	if params.PolicyID == "" {
   191  		uri = fmt.Sprintf(
   192  			"/appsec/v1/configs/%d/versions/%d/bypass-network-lists",
   193  			params.ConfigID,
   194  			params.Version,
   195  		)
   196  	} else {
   197  		uri = fmt.Sprintf(
   198  			"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bypass-network-lists",
   199  			params.ConfigID,
   200  			params.Version,
   201  			params.PolicyID,
   202  		)
   203  	}
   204  
   205  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   206  	if err != nil {
   207  		return nil, fmt.Errorf("failed to create RemoveBypassNetworkLists request: %w", err)
   208  	}
   209  
   210  	var result RemoveBypassNetworkListsResponse
   211  	resp, err := p.Exec(req, &result, params)
   212  	if err != nil {
   213  		return nil, fmt.Errorf("remove bypass network lists request failed: %w", err)
   214  	}
   215  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   216  		return nil, p.Error(resp)
   217  	}
   218  
   219  	return &result, nil
   220  }