github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/wap_selected_hostnames.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 WAPSelectedHostnames interface supports retrieving and modifying the list of hostnames protected under
    13  	// a configuration and security policy.
    14  	WAPSelectedHostnames interface {
    15  		GetWAPSelectedHostnames(ctx context.Context, params GetWAPSelectedHostnamesRequest) (*GetWAPSelectedHostnamesResponse, error)
    16  		UpdateWAPSelectedHostnames(ctx context.Context, params UpdateWAPSelectedHostnamesRequest) (*UpdateWAPSelectedHostnamesResponse, error)
    17  	}
    18  
    19  	// GetWAPSelectedHostnamesRequest is used to retrieve the WAP selected hostnames and evaluated hostnames.
    20  	GetWAPSelectedHostnamesRequest struct {
    21  		ConfigID         int    `json:"configId"`
    22  		Version          int    `json:"version"`
    23  		SecurityPolicyID string `json:"securityPolicyID"`
    24  	}
    25  
    26  	// GetWAPSelectedHostnamesResponse is returned from a call to GetWAPSelectedHostnames.
    27  	GetWAPSelectedHostnamesResponse struct {
    28  		ProtectedHosts []string `json:"protectedHostnames,omitempty"`
    29  		EvaluatedHosts []string `json:"evalHostnames,omitempty"`
    30  	}
    31  
    32  	// UpdateWAPSelectedHostnamesRequest is used to modify the WAP selected hostnames and evaluated hostnames.
    33  	UpdateWAPSelectedHostnamesRequest struct {
    34  		ConfigID         int      `json:"configId"`
    35  		Version          int      `json:"version"`
    36  		SecurityPolicyID string   `json:"securityPolicyID"`
    37  		ProtectedHosts   []string `json:"protectedHostnames"`
    38  		EvaluatedHosts   []string `json:"evalHostnames"`
    39  	}
    40  
    41  	// UpdateWAPSelectedHostnamesResponse is returned from a call to UpdateWAPSelectedHostnames.
    42  	UpdateWAPSelectedHostnamesResponse struct {
    43  		ProtectedHosts []string `json:"protectedHostnames"`
    44  		EvaluatedHosts []string `json:"evalHostnames"`
    45  	}
    46  )
    47  
    48  // Validate validates a GetWAPSelectedHostnamesRequest.
    49  func (v GetWAPSelectedHostnamesRequest) Validate() error {
    50  	return validation.Errors{
    51  		"ConfigID":         validation.Validate(v.ConfigID, validation.Required),
    52  		"Version":          validation.Validate(v.Version, validation.Required),
    53  		"SecurityPolicyID": validation.Validate(v.Version, validation.Required),
    54  	}.Filter()
    55  }
    56  
    57  // Validate validates an UpdateWAPSelectedHostnamesRequest.
    58  func (v UpdateWAPSelectedHostnamesRequest) Validate() error {
    59  	return validation.Errors{
    60  		"ConfigID":         validation.Validate(v.ConfigID, validation.Required),
    61  		"Version":          validation.Validate(v.Version, validation.Required),
    62  		"SecurityPolicyID": validation.Validate(v.Version, validation.Required),
    63  	}.Filter()
    64  }
    65  
    66  func (p *appsec) GetWAPSelectedHostnames(ctx context.Context, params GetWAPSelectedHostnamesRequest) (*GetWAPSelectedHostnamesResponse, error) {
    67  	logger := p.Log(ctx)
    68  	logger.Debug("GetWAPSelectedHostnames")
    69  
    70  	if err := params.Validate(); err != nil {
    71  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    72  	}
    73  
    74  	uri := fmt.Sprintf(
    75  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/wap-selected-hostnames",
    76  		params.ConfigID,
    77  		params.Version,
    78  		params.SecurityPolicyID)
    79  
    80  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    81  	if err != nil {
    82  		return nil, fmt.Errorf("failed to create GetWAPSelectedHostnames request: %w", err)
    83  	}
    84  
    85  	var result GetWAPSelectedHostnamesResponse
    86  	resp, err := p.Exec(req, &result)
    87  	if err != nil {
    88  		return nil, fmt.Errorf("get WAP selected hostnames request failed: %w", err)
    89  	}
    90  	if resp.StatusCode != http.StatusOK {
    91  		return nil, p.Error(resp)
    92  	}
    93  
    94  	return &result, nil
    95  }
    96  
    97  func (p *appsec) UpdateWAPSelectedHostnames(ctx context.Context, params UpdateWAPSelectedHostnamesRequest) (*UpdateWAPSelectedHostnamesResponse, error) {
    98  	logger := p.Log(ctx)
    99  	logger.Debug("UpdateWAPSelectedHostnames")
   100  
   101  	if err := params.Validate(); err != nil {
   102  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   103  	}
   104  
   105  	uri := fmt.Sprintf(
   106  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/wap-selected-hostnames",
   107  		params.ConfigID,
   108  		params.Version,
   109  		params.SecurityPolicyID,
   110  	)
   111  
   112  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   113  	if err != nil {
   114  		return nil, fmt.Errorf("failed to create UpdateWAPSelectedHostnames request: %w", err)
   115  	}
   116  
   117  	var result UpdateWAPSelectedHostnamesResponse
   118  	resp, err := p.Exec(req, &result, params)
   119  	if err != nil {
   120  		return nil, fmt.Errorf("update WAP selected hostnames request failed: %w", err)
   121  	}
   122  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   123  		return nil, p.Error(resp)
   124  	}
   125  
   126  	return &result, nil
   127  }