github.com/akamai/AkamaiOPEN-edgegrid-golang/v4@v4.1.0/pkg/appsec/eval_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 EvalHost interface supports retrieving and modifying list of evaluation hostnames for a configuration.
    13  	// Deprecated: this interface will be removed in a future release. Use the WAPSelectedHostnames interface instead.
    14  	EvalHost interface {
    15  		// GetEvalHosts lists the evaluation hostnames for a configuration version.
    16  		//
    17  		// See: https://techdocs.akamai.com/application-security/reference/get-selected-hostnames-eval-hostnames
    18  		// Deprecated: this method will be removed in a future release. Use the GetWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    19  		GetEvalHosts(ctx context.Context, params GetEvalHostsRequest) (*GetEvalHostsResponse, error)
    20  
    21  		// GetEvalHost return the specified evaluation hostname for a configuration version.
    22  		//
    23  		// See: https://techdocs.akamai.com/application-security/reference/get-selected-hostnames-eval-hostnames
    24  		// Deprecated: this method will be removed in a future release. Use the GetWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    25  		GetEvalHost(ctx context.Context, params GetEvalHostRequest) (*GetEvalHostResponse, error)
    26  
    27  		// UpdateEvalHost updates the list of hostnames you want to evaluate for a configuration version.
    28  		//
    29  		// See: https://techdocs.akamai.com/application-security/reference/put-selected-eval-hostnames
    30  		// Deprecated: this method will be removed in a future release. Use the UpdateWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    31  		UpdateEvalHost(ctx context.Context, params UpdateEvalHostRequest) (*UpdateEvalHostResponse, error)
    32  
    33  		// RemoveEvalHost removed the specified evaluation hostname.
    34  		//
    35  		// See: https://techdocs.akamai.com/application-security/reference/put-selected-eval-hostnames
    36  		// Deprecated: this method will be removed in a future release. Use the WAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    37  		RemoveEvalHost(ctx context.Context, params RemoveEvalHostRequest) (*RemoveEvalHostResponse, error)
    38  	}
    39  
    40  	// GetEvalHostRequest is used to retrieve the evaluation hostnames for a configuration.
    41  	// Deprecated: this struct will be removed in a future release.
    42  	GetEvalHostRequest struct {
    43  		ConfigID int `json:"-"`
    44  		Version  int `json:"-"`
    45  	}
    46  
    47  	// GetEvalHostResponse is returned from a call to GetEvalHost.
    48  	// Deprecated: this struct will be removed in a future release.
    49  	GetEvalHostResponse struct {
    50  		Hostnames []string `json:"hostnames"`
    51  	}
    52  
    53  	// GetEvalHostsRequest is used to retrieve the evaluation hostnames for a configuration.
    54  	GetEvalHostsRequest struct {
    55  		ConfigID int `json:"-"`
    56  		Version  int `json:"-"`
    57  	}
    58  
    59  	// GetEvalHostsResponse is returned from a call to GetEvalHosts.
    60  	GetEvalHostsResponse struct {
    61  		Hostnames []string `json:"hostnames"`
    62  	}
    63  
    64  	// UpdateEvalHostRequest is used to modify the evaluation hostnames for a configuration.
    65  	UpdateEvalHostRequest struct {
    66  		ConfigID  int      `json:"-"`
    67  		Version   int      `json:"-"`
    68  		Hostnames []string `json:"hostnames"`
    69  	}
    70  
    71  	// UpdateEvalHostResponse is returned from a call to UpdateEvalHost.
    72  	UpdateEvalHostResponse struct {
    73  		HostnameList []struct {
    74  			Hostname string `json:"hostname"`
    75  		} `json:"hostnameList"`
    76  	}
    77  
    78  	// RemoveEvalHostRequest is used to remove the evaluation hostnames for a configuration.
    79  	RemoveEvalHostRequest struct {
    80  		ConfigID  int      `json:"-"`
    81  		Version   int      `json:"-"`
    82  		Hostnames []string `json:"hostnames"`
    83  	}
    84  
    85  	// RemoveEvalHostResponse is returned from a call to RemoveEvalHost.
    86  	RemoveEvalHostResponse struct {
    87  		Hostnames []string `json:"hostnames"`
    88  	}
    89  )
    90  
    91  // Validate validates a GetEvalHostRequest.
    92  // Deprecated: this method will be removed in a future release.
    93  func (v GetEvalHostRequest) Validate() error {
    94  	return validation.Errors{
    95  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    96  		"Version":  validation.Validate(v.Version, validation.Required),
    97  	}.Filter()
    98  }
    99  
   100  // Validate validates a GetEvalHostsRequest.
   101  func (v GetEvalHostsRequest) Validate() error {
   102  	return validation.Errors{
   103  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   104  		"Version":  validation.Validate(v.Version, validation.Required),
   105  	}.Filter()
   106  }
   107  
   108  // Validate validates an UpdateEvalHostRequest.
   109  func (v UpdateEvalHostRequest) Validate() error {
   110  	return validation.Errors{
   111  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   112  		"Version":  validation.Validate(v.Version, validation.Required),
   113  	}.Filter()
   114  }
   115  
   116  // Validate validates a RemoveEvalHostRequest.
   117  func (v RemoveEvalHostRequest) Validate() error {
   118  	return validation.Errors{
   119  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   120  		"Version":  validation.Validate(v.Version, validation.Required),
   121  	}.Filter()
   122  }
   123  
   124  // Deprecated: this method will be removed in a future release.
   125  func (p *appsec) GetEvalHost(ctx context.Context, params GetEvalHostRequest) (*GetEvalHostResponse, error) {
   126  	logger := p.Log(ctx)
   127  	logger.Debug("GetEvalHost")
   128  
   129  	if err := params.Validate(); err != nil {
   130  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   131  	}
   132  
   133  	uri := fmt.Sprintf(
   134  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames/eval-hostnames",
   135  		params.ConfigID,
   136  		params.Version)
   137  
   138  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   139  	if err != nil {
   140  		return nil, fmt.Errorf("failed to create GetEvalHost request: %w", err)
   141  	}
   142  
   143  	var result GetEvalHostResponse
   144  	resp, err := p.Exec(req, &result)
   145  	if err != nil {
   146  		return nil, fmt.Errorf("get eval host 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) GetEvalHosts(ctx context.Context, params GetEvalHostsRequest) (*GetEvalHostsResponse, error) {
   156  	logger := p.Log(ctx)
   157  	logger.Debug("GetEvalHosts")
   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/selected-hostnames/eval-hostnames",
   165  		params.ConfigID,
   166  		params.Version)
   167  
   168  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   169  	if err != nil {
   170  		return nil, fmt.Errorf("failed to create GetEvalHosts request: %w", err)
   171  	}
   172  
   173  	var result GetEvalHostsResponse
   174  	resp, err := p.Exec(req, &result)
   175  	if err != nil {
   176  		return nil, fmt.Errorf("get eval hosts request failed: %w", err)
   177  	}
   178  	if resp.StatusCode != http.StatusOK {
   179  		return nil, p.Error(resp)
   180  	}
   181  
   182  	return &result, nil
   183  }
   184  
   185  func (p *appsec) UpdateEvalHost(ctx context.Context, params UpdateEvalHostRequest) (*UpdateEvalHostResponse, error) {
   186  	logger := p.Log(ctx)
   187  	logger.Debug("UpdateEvalHost")
   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/selected-hostnames/eval-hostnames",
   195  		params.ConfigID,
   196  		params.Version)
   197  
   198  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   199  	if err != nil {
   200  		return nil, fmt.Errorf("failed to create UpdateEvalHost request: %w", err)
   201  	}
   202  
   203  	var result UpdateEvalHostResponse
   204  	resp, err := p.Exec(req, &result, params)
   205  	if err != nil {
   206  		return nil, fmt.Errorf("update eval host request failed: %w", err)
   207  	}
   208  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   209  		return nil, p.Error(resp)
   210  	}
   211  
   212  	return &result, nil
   213  }
   214  
   215  func (p *appsec) RemoveEvalHost(ctx context.Context, params RemoveEvalHostRequest) (*RemoveEvalHostResponse, error) {
   216  	logger := p.Log(ctx)
   217  	logger.Debug("RemoveEvalHost")
   218  
   219  	if err := params.Validate(); err != nil {
   220  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   221  	}
   222  
   223  	uri := fmt.Sprintf(
   224  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames/eval-hostnames",
   225  		params.ConfigID,
   226  		params.Version)
   227  
   228  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   229  	if err != nil {
   230  		return nil, fmt.Errorf("failed to create RemoveEvalHost request: %w", err)
   231  	}
   232  
   233  	var result RemoveEvalHostResponse
   234  	resp, err := p.Exec(req, &result, params)
   235  	if err != nil {
   236  		return nil, fmt.Errorf("remove eval host request failed: %w", err)
   237  	}
   238  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   239  		return nil, p.Error(resp)
   240  	}
   241  
   242  	return &result, nil
   243  }