github.com/akamai/AkamaiOPEN-edgegrid-golang/v4@v4.1.0/pkg/appsec/eval_protect_host.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 EvalProtectHost interface supports retrieving the evaluation hostnames for a configuration and
    13  	// moving hostnames from evaluating to protected status.
    14  	// Deprecated: this interface will be removed in a future release. Use the WAPSelectedHostnames interface instead.
    15  	EvalProtectHost interface {
    16  		// GetEvalProtectHosts lists the evaluation hostnames for a configuration version.
    17  		//
    18  		// See: https://techdocs.akamai.com/application-security/reference/get-selected-hostnames-eval-hostnames
    19  		// Deprecated: this method will be removed in a future release. Use the GetWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    20  		GetEvalProtectHosts(ctx context.Context, params GetEvalProtectHostsRequest) (*GetEvalProtectHostsResponse, error)
    21  
    22  		// GetEvalProtectHost return the evaluation hostname for a configuration version.
    23  		//
    24  		// See: https://techdocs.akamai.com/application-security/reference/get-selected-hostnames-eval-hostnames
    25  		// Deprecated: this method will be removed in a future release. Use the GetWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    26  		GetEvalProtectHost(ctx context.Context, params GetEvalProtectHostRequest) (*GetEvalProtectHostResponse, error)
    27  
    28  		// UpdateEvalProtectHost updates the list of hostnames you want to evaluate for a configuration version.
    29  		//
    30  		// See: https://techdocs.akamai.com/application-security/reference/put-selected-eval-hostnames
    31  		// Deprecated: this method will be removed in a future release. Use the UpdateWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    32  		UpdateEvalProtectHost(ctx context.Context, params UpdateEvalProtectHostRequest) (*UpdateEvalProtectHostResponse, error)
    33  	}
    34  
    35  	// GetEvalProtectHostRequest is used to call GetEvalProtectHost.
    36  	// Deprecated: this struct will be removed in a future release.
    37  	GetEvalProtectHostRequest struct {
    38  		ConfigID int `json:"-"`
    39  		Version  int `json:"-"`
    40  	}
    41  
    42  	// GetEvalProtectHostResponse is returned from a call to GetEvalProtectHost.
    43  	// Deprecated: this struct will be removed in a future release.
    44  	GetEvalProtectHostResponse struct {
    45  		Hostnames []string `json:"hostnames"`
    46  	}
    47  
    48  	// GetEvalProtectHostsRequest is used to call GetEvalProtectHosts.
    49  	GetEvalProtectHostsRequest struct {
    50  		ConfigID int `json:"-"`
    51  		Version  int `json:"-"`
    52  	}
    53  
    54  	// GetEvalProtectHostsResponse is returned from a call to GetEvalProtectHosts.
    55  	GetEvalProtectHostsResponse struct {
    56  		Hostnames []string `json:"hostnames"`
    57  	}
    58  
    59  	// UpdateEvalProtectHostRequest is used to call UpdateEvalProtectHost.
    60  	UpdateEvalProtectHostRequest struct {
    61  		ConfigID  int      `json:"-"`
    62  		Version   int      `json:"-"`
    63  		Hostnames []string `json:"hostnames"`
    64  	}
    65  
    66  	// UpdateEvalProtectHostResponse is returned from a call to UpdateEvalProtectHost.
    67  	UpdateEvalProtectHostResponse struct {
    68  		HostnameList []struct {
    69  			Hostname string `json:"hostname"`
    70  		} `json:"hostnameList"`
    71  	}
    72  )
    73  
    74  // Validate validates a GetEvalProtectHostRequest.
    75  // Deprecated: this method will be removed in a future release.
    76  func (v GetEvalProtectHostRequest) Validate() error {
    77  	return validation.Errors{
    78  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    79  		"Version":  validation.Validate(v.Version, validation.Required),
    80  	}.Filter()
    81  }
    82  
    83  // Validate validates a GetEvalProtectHostsRequest.
    84  func (v GetEvalProtectHostsRequest) Validate() error {
    85  	return validation.Errors{
    86  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    87  		"Version":  validation.Validate(v.Version, validation.Required),
    88  	}.Filter()
    89  }
    90  
    91  // Validate validates an UpdateEvalProtectHostRequest.
    92  func (v UpdateEvalProtectHostRequest) Validate() error {
    93  	return validation.Errors{
    94  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    95  		"Version":  validation.Validate(v.Version, validation.Required),
    96  	}.Filter()
    97  }
    98  
    99  // Deprecated: this method will be removed in a future release.
   100  func (p *appsec) GetEvalProtectHost(ctx context.Context, params GetEvalProtectHostRequest) (*GetEvalProtectHostResponse, error) {
   101  	logger := p.Log(ctx)
   102  	logger.Debug("GetEvalProtectHost")
   103  
   104  	if err := params.Validate(); err != nil {
   105  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   106  	}
   107  
   108  	uri := fmt.Sprintf(
   109  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames/eval-hostnames",
   110  		params.ConfigID,
   111  		params.Version)
   112  
   113  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   114  	if err != nil {
   115  		return nil, fmt.Errorf("failed to create GetEvalProtectHost request: %w", err)
   116  	}
   117  
   118  	var result GetEvalProtectHostResponse
   119  	resp, err := p.Exec(req, &result)
   120  	if err != nil {
   121  		return nil, fmt.Errorf("get eval protect host request failed: %w", err)
   122  	}
   123  	if resp.StatusCode != http.StatusOK {
   124  		return nil, p.Error(resp)
   125  	}
   126  
   127  	return &result, nil
   128  }
   129  
   130  func (p *appsec) GetEvalProtectHosts(ctx context.Context, params GetEvalProtectHostsRequest) (*GetEvalProtectHostsResponse, error) {
   131  	logger := p.Log(ctx)
   132  	logger.Debug("GetEvalProtectHosts")
   133  
   134  	if err := params.Validate(); err != nil {
   135  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   136  	}
   137  
   138  	uri := fmt.Sprintf(
   139  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames/eval-hostnames",
   140  		params.ConfigID,
   141  		params.Version)
   142  
   143  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   144  	if err != nil {
   145  		return nil, fmt.Errorf("failed to create GetEvalProtectHosts request: %w", err)
   146  	}
   147  
   148  	var result GetEvalProtectHostsResponse
   149  	resp, err := p.Exec(req, &result)
   150  	if err != nil {
   151  		return nil, fmt.Errorf("get eval protect hosts request failed: %w", err)
   152  	}
   153  	if resp.StatusCode != http.StatusOK {
   154  		return nil, p.Error(resp)
   155  	}
   156  
   157  	return &result, nil
   158  }
   159  
   160  func (p *appsec) UpdateEvalProtectHost(ctx context.Context, params UpdateEvalProtectHostRequest) (*UpdateEvalProtectHostResponse, error) {
   161  	logger := p.Log(ctx)
   162  	logger.Debug("UpdateEvalProtectHost")
   163  
   164  	if err := params.Validate(); err != nil {
   165  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   166  	}
   167  
   168  	uri := fmt.Sprintf(
   169  		"/appsec/v1/configs/%d/versions/%d/protect-eval-hostnames",
   170  		params.ConfigID,
   171  		params.Version,
   172  	)
   173  
   174  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   175  	if err != nil {
   176  		return nil, fmt.Errorf("failed to create UpdateEvalProtectHost request: %w", err)
   177  	}
   178  
   179  	var result UpdateEvalProtectHostResponse
   180  	resp, err := p.Exec(req, &result, params)
   181  	if err != nil {
   182  		return nil, fmt.Errorf("update eval protect host request failed: %w", err)
   183  	}
   184  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   185  		return nil, p.Error(resp)
   186  	}
   187  
   188  	return &result, nil
   189  }