github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/network_layer_protection.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 NetworkLayerProtection interface supports retrieving and updating network layer protection for a configuration and policy.
    13  	//
    14  	// https://developer.akamai.com/api/cloud_security/application_security/v1.html#protections
    15  	NetworkLayerProtection interface {
    16  		// GetNetworkLayerProtections retrieves the current network layer protection setting for a configuration and policy.
    17  		//
    18  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getprotections
    19  		// Deprecated: this method will be removed in a future release. Use GetNetworkLayerProtection instead.
    20  		GetNetworkLayerProtections(ctx context.Context, params GetNetworkLayerProtectionsRequest) (*GetNetworkLayerProtectionsResponse, error)
    21  
    22  		// GetNetworkLayerProtection retrieves the current network layer protection setting for a configuration and policy.
    23  		//
    24  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getprotections
    25  		GetNetworkLayerProtection(ctx context.Context, params GetNetworkLayerProtectionRequest) (*GetNetworkLayerProtectionResponse, error)
    26  
    27  		// UpdateNetworkLayerProtection updates the network layer protection setting for a configuration and policy.
    28  		//
    29  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putprotections
    30  		UpdateNetworkLayerProtection(ctx context.Context, params UpdateNetworkLayerProtectionRequest) (*UpdateNetworkLayerProtectionResponse, error)
    31  
    32  		// UpdateNetworkLayerProtection removes network layer protection for a configuration and policy.
    33  		//
    34  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putprotections
    35  		// Deprecated: this method will be removed in a future release. Use UpdateNetworkLayerProtection instead.
    36  		RemoveNetworkLayerProtection(ctx context.Context, params RemoveNetworkLayerProtectionRequest) (*RemoveNetworkLayerProtectionResponse, error)
    37  	}
    38  
    39  	// GetNetworkLayerProtectionRequest is used to retrieve the network layer protection setting.
    40  	GetNetworkLayerProtectionRequest struct {
    41  		ConfigID                  int    `json:"-"`
    42  		Version                   int    `json:"-"`
    43  		PolicyID                  string `json:"-"`
    44  		ApplyNetworkLayerControls bool   `json:"applyNetworkLayerControls"`
    45  	}
    46  
    47  	// GetNetworkLayerProtectionResponse is returned from a call to GetNetworkLayerProtection.
    48  	GetNetworkLayerProtectionResponse ProtectionsResponse
    49  
    50  	// GetNetworkLayerProtectionsRequest is used to retrieve the network layer protection setting.
    51  	// Deprecated: this struct will be removed in a future release.
    52  	GetNetworkLayerProtectionsRequest struct {
    53  		ConfigID                  int    `json:"-"`
    54  		Version                   int    `json:"-"`
    55  		PolicyID                  string `json:"-"`
    56  		ApplyNetworkLayerControls bool   `json:"applyNetworkLayerControls"`
    57  	}
    58  
    59  	// GetNetworkLayerProtectionsResponse is returned from a call to GetNetworkLayerProtection.
    60  	// Deprecated: this struct will be removed in a future release.
    61  	GetNetworkLayerProtectionsResponse ProtectionsResponse
    62  
    63  	// UpdateNetworkLayerProtectionRequest is used to modify the network layer protection setting.
    64  	UpdateNetworkLayerProtectionRequest struct {
    65  		ConfigID                  int    `json:"-"`
    66  		Version                   int    `json:"-"`
    67  		PolicyID                  string `json:"-"`
    68  		ApplyNetworkLayerControls bool   `json:"applyNetworkLayerControls"`
    69  	}
    70  
    71  	// UpdateNetworkLayerProtectionResponse is returned from a call to UpdateNetworkLayerProtection
    72  	UpdateNetworkLayerProtectionResponse ProtectionsResponse
    73  
    74  	// RemoveNetworkLayerProtectionRequest is used to remove the network layer protection setting.
    75  	// Deprecated: this struct will be removed in a future release.
    76  	RemoveNetworkLayerProtectionRequest struct {
    77  		ConfigID                  int    `json:"-"`
    78  		Version                   int    `json:"-"`
    79  		PolicyID                  string `json:"-"`
    80  		ApplyNetworkLayerControls bool   `json:"applyNetworkLayerControls"`
    81  	}
    82  
    83  	// RemoveNetworkLayerProtectionResponse is returned from a call to RemoveNetworkLayerProtection.
    84  	// Deprecated: this struct will be removed in a future release.
    85  	RemoveNetworkLayerProtectionResponse ProtectionsResponse
    86  )
    87  
    88  // Validate validates a GetNetworkLayerProtectionRequest.
    89  func (v GetNetworkLayerProtectionRequest) Validate() error {
    90  	return validation.Errors{
    91  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    92  		"Version":  validation.Validate(v.Version, validation.Required),
    93  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    94  	}.Filter()
    95  }
    96  
    97  // Validate validates a GetNetworkLayerProtectionsRequest.
    98  func (v GetNetworkLayerProtectionsRequest) Validate() error {
    99  	return validation.Errors{
   100  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   101  		"Version":  validation.Validate(v.Version, validation.Required),
   102  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   103  	}.Filter()
   104  }
   105  
   106  // Validate validates an UpdateNetworkLayerProtectionRequest.
   107  func (v UpdateNetworkLayerProtectionRequest) Validate() error {
   108  	return validation.Errors{
   109  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   110  		"Version":  validation.Validate(v.Version, validation.Required),
   111  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   112  	}.Filter()
   113  }
   114  
   115  // Validate validates a RemoveNetworkLayerProtectionRequest.
   116  func (v RemoveNetworkLayerProtectionRequest) Validate() error {
   117  	return validation.Errors{
   118  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   119  		"Version":  validation.Validate(v.Version, validation.Required),
   120  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   121  	}.Filter()
   122  }
   123  
   124  func (p *appsec) GetNetworkLayerProtection(ctx context.Context, params GetNetworkLayerProtectionRequest) (*GetNetworkLayerProtectionResponse, error) {
   125  	logger := p.Log(ctx)
   126  	logger.Debug("GetNetworkLayerProtection")
   127  
   128  	if err := params.Validate(); err != nil {
   129  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   130  	}
   131  
   132  	uri := fmt.Sprintf(
   133  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   134  		params.ConfigID,
   135  		params.Version,
   136  		params.PolicyID)
   137  
   138  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   139  	if err != nil {
   140  		return nil, fmt.Errorf("failed to create GetNetworkLayerProtection request: %w", err)
   141  	}
   142  
   143  	var result GetNetworkLayerProtectionResponse
   144  	resp, err := p.Exec(req, &result)
   145  	if err != nil {
   146  		return nil, fmt.Errorf("get network layer protection request failed: %w", err)
   147  	}
   148  	if resp.StatusCode != http.StatusOK {
   149  		return nil, p.Error(resp)
   150  	}
   151  
   152  	return &result, nil
   153  }
   154  
   155  func (p *appsec) GetNetworkLayerProtections(ctx context.Context, params GetNetworkLayerProtectionsRequest) (*GetNetworkLayerProtectionsResponse, error) {
   156  	logger := p.Log(ctx)
   157  	logger.Debug("GetNetworkLayerProtections")
   158  
   159  	if err := params.Validate(); err != nil {
   160  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   161  	}
   162  
   163  	uri := fmt.Sprintf(
   164  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   165  		params.ConfigID,
   166  		params.Version,
   167  		params.PolicyID)
   168  
   169  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   170  	if err != nil {
   171  		return nil, fmt.Errorf("failed to create GetNetworkLayerProtections request: %w", err)
   172  	}
   173  
   174  	var result GetNetworkLayerProtectionsResponse
   175  	resp, err := p.Exec(req, &result)
   176  	if err != nil {
   177  		return nil, fmt.Errorf("get network layer protections request failed: %w", err)
   178  	}
   179  	if resp.StatusCode != http.StatusOK {
   180  		return nil, p.Error(resp)
   181  	}
   182  
   183  	return &result, nil
   184  }
   185  
   186  func (p *appsec) UpdateNetworkLayerProtection(ctx context.Context, params UpdateNetworkLayerProtectionRequest) (*UpdateNetworkLayerProtectionResponse, error) {
   187  	logger := p.Log(ctx)
   188  	logger.Debug("UpdateNetworkLayerProtection")
   189  
   190  	if err := params.Validate(); err != nil {
   191  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   192  	}
   193  
   194  	uri := fmt.Sprintf(
   195  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   196  		params.ConfigID,
   197  		params.Version,
   198  		params.PolicyID,
   199  	)
   200  
   201  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   202  	if err != nil {
   203  		return nil, fmt.Errorf("failed to create UpdateNetworkLayerProtection request: %w", err)
   204  	}
   205  
   206  	var result UpdateNetworkLayerProtectionResponse
   207  	resp, err := p.Exec(req, &result, params)
   208  	if err != nil {
   209  		return nil, fmt.Errorf("update network layer protection request failed: %w", err)
   210  	}
   211  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   212  		return nil, p.Error(resp)
   213  	}
   214  
   215  	return &result, nil
   216  }
   217  
   218  func (p *appsec) RemoveNetworkLayerProtection(ctx context.Context, params RemoveNetworkLayerProtectionRequest) (*RemoveNetworkLayerProtectionResponse, error) {
   219  	logger := p.Log(ctx)
   220  	logger.Debug("RemoveNetworkLayerProtection")
   221  
   222  	if err := params.Validate(); err != nil {
   223  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   224  	}
   225  
   226  	uri := fmt.Sprintf(
   227  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   228  		params.ConfigID,
   229  		params.Version,
   230  		params.PolicyID,
   231  	)
   232  
   233  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   234  	if err != nil {
   235  		return nil, fmt.Errorf("failed to create RemoveNetworkLayerProtection request: %w", err)
   236  	}
   237  
   238  	var result RemoveNetworkLayerProtectionResponse
   239  	resp, err := p.Exec(req, &result, params)
   240  	if err != nil {
   241  		return nil, fmt.Errorf("remove network layer protection request failed: %w", err)
   242  	}
   243  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   244  		return nil, p.Error(resp)
   245  	}
   246  
   247  	return &result, nil
   248  }