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