github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/selected_hostname.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 SelectedHostname interface supports retrieving and modifying the list of hostnames protected under
    13  	// a configuration.
    14  	// Deprecated: this interface will be removed in a future release. Use the WAPSelectedHostnames interface instead.
    15  	SelectedHostname interface {
    16  		// GetSelectedHostnames lists the hostnames that the configuration version selects as candidates of protected hostnames,
    17  		// which you can use in match targets.
    18  		//
    19  		// See: https://techdocs.akamai.com/application-security/reference/get-selected-hostnames
    20  		// Deprecated: this method will be removed in a future release. Use the GetWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    21  		GetSelectedHostnames(ctx context.Context, params GetSelectedHostnamesRequest) (*GetSelectedHostnamesResponse, error)
    22  
    23  		// GetSelectedHostname returns the hostname that the configuration version selects as a candidate of protected hostname,
    24  		// which you can use in match targets.
    25  		//
    26  		// See: https://techdocs.akamai.com/application-security/reference/get-selected-hostnames
    27  		// Deprecated: this method will be removed in a future release. Use the GetWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    28  		GetSelectedHostname(ctx context.Context, params GetSelectedHostnameRequest) (*GetSelectedHostnameResponse, error)
    29  
    30  		// UpdateSelectedHostname updates the selected hostname for a configuration version.
    31  		//
    32  		// See: https://techdocs.akamai.com/application-security/reference/put-selected-hostnames
    33  		// Deprecated: this method will be removed in a future release. Use the UpdateWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    34  		UpdateSelectedHostname(ctx context.Context, params UpdateSelectedHostnameRequest) (*UpdateSelectedHostnameResponse, error)
    35  
    36  		// UpdateSelectedHostnames updates the list of selected hostnames for a configuration version.
    37  		//
    38  		// See: https://techdocs.akamai.com/application-security/reference/put-selected-hostnames
    39  		// Deprecated: this method will be removed in a future release. Use the UpdateWAPSelectedHostnames method of the WAPSelectedHostnames interface instead.
    40  		UpdateSelectedHostnames(ctx context.Context, params UpdateSelectedHostnamesRequest) (*UpdateSelectedHostnamesResponse, error)
    41  	}
    42  
    43  	// GetSelectedHostnamesRequest is used to retrieve the selected hostnames for a configuration.
    44  	GetSelectedHostnamesRequest struct {
    45  		ConfigID     int        `json:"configId"`
    46  		Version      int        `json:"version"`
    47  		HostnameList []Hostname `json:"hostnameList"`
    48  	}
    49  
    50  	// GetSelectedHostnamesResponse is returned from a call to GetSelectedHostnames.
    51  	GetSelectedHostnamesResponse struct {
    52  		HostnameList []Hostname `json:"hostnameList,omitempty"`
    53  	}
    54  
    55  	// GetSelectedHostnameRequest is used to retrieve the selected hostnames for a configuration.
    56  	// Deprecated: this struct will be removed in a future release.
    57  	GetSelectedHostnameRequest struct {
    58  		ConfigID     int        `json:"configId"`
    59  		Version      int        `json:"version"`
    60  		HostnameList []Hostname `json:"hostnameList"`
    61  	}
    62  
    63  	// GetSelectedHostnameResponse is returned from a call to GetSelectedHostname.
    64  	// Deprecated: this struct will be removed in a future release.
    65  	GetSelectedHostnameResponse struct {
    66  		HostnameList []Hostname `json:"hostnameList"`
    67  	}
    68  
    69  	// UpdateSelectedHostnamesRequest is used to modify the selected hostnames for a configuration.
    70  	UpdateSelectedHostnamesRequest struct {
    71  		ConfigID     int        `json:"configId"`
    72  		Version      int        `json:"version"`
    73  		HostnameList []Hostname `json:"hostnameList"`
    74  	}
    75  
    76  	// UpdateSelectedHostnamesResponse is returned from a call to UpdateSelectedHostnames.
    77  	UpdateSelectedHostnamesResponse struct {
    78  		HostnameList []Hostname `json:"hostnameList"`
    79  	}
    80  
    81  	// UpdateSelectedHostnameRequest is used to modify the selected hostnames for a configuration.
    82  	// Deprecated: this struct will be removed in a future release.
    83  	UpdateSelectedHostnameRequest struct {
    84  		ConfigID     int        `json:"configId"`
    85  		Version      int        `json:"version"`
    86  		HostnameList []Hostname `json:"hostnameList"`
    87  	}
    88  
    89  	// UpdateSelectedHostnameResponse is returned from a call to UpdateSelectedHostname.
    90  	// Deprecated: this struct will be removed in a future release.
    91  	UpdateSelectedHostnameResponse struct {
    92  		HostnameList []Hostname `json:"hostnameList"`
    93  	}
    94  
    95  	// Hostname describes a hostname that may be protected.
    96  	Hostname struct {
    97  		Hostname string `json:"hostname"`
    98  	}
    99  )
   100  
   101  // Validate validates a GetSelectedHostnameRequest.
   102  // Deprecated: this method will be removed in a future release.
   103  func (v GetSelectedHostnameRequest) Validate() error {
   104  	return validation.Errors{
   105  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   106  		"Version":  validation.Validate(v.Version, validation.Required),
   107  	}.Filter()
   108  }
   109  
   110  // Validate validates a GetSelectedHostnamesRequest.
   111  func (v GetSelectedHostnamesRequest) Validate() error {
   112  	return validation.Errors{
   113  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   114  		"Version":  validation.Validate(v.Version, validation.Required),
   115  	}.Filter()
   116  }
   117  
   118  // Validate validates an UpdateSelectedHostnamesRequest.
   119  func (v UpdateSelectedHostnamesRequest) Validate() error {
   120  	return validation.Errors{
   121  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   122  		"Version":  validation.Validate(v.Version, validation.Required),
   123  	}.Filter()
   124  }
   125  
   126  // Validate validates an UpdateSelectedHostnameRequest.
   127  // Deprecated: this method will be removed in a future release.
   128  func (v UpdateSelectedHostnameRequest) Validate() error {
   129  	return validation.Errors{
   130  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   131  		"Version":  validation.Validate(v.Version, validation.Required),
   132  	}.Filter()
   133  }
   134  
   135  // Deprecated: this method will be removed in a future release.
   136  func (p *appsec) GetSelectedHostname(ctx context.Context, params GetSelectedHostnameRequest) (*GetSelectedHostnameResponse, error) {
   137  	logger := p.Log(ctx)
   138  	logger.Debug("GetSelectedHostname")
   139  
   140  	if err := params.Validate(); err != nil {
   141  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   142  	}
   143  
   144  	uri := fmt.Sprintf(
   145  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames",
   146  		params.ConfigID,
   147  		params.Version)
   148  
   149  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   150  	if err != nil {
   151  		return nil, fmt.Errorf("failed to create GetSelectedHostname request: %w", err)
   152  	}
   153  
   154  	var result GetSelectedHostnameResponse
   155  	resp, err := p.Exec(req, &result)
   156  	if err != nil {
   157  		return nil, fmt.Errorf("get selected hostname request failed: %w", err)
   158  	}
   159  	if resp.StatusCode != http.StatusOK {
   160  		return nil, p.Error(resp)
   161  	}
   162  
   163  	return &result, nil
   164  }
   165  
   166  func (p *appsec) GetSelectedHostnames(ctx context.Context, params GetSelectedHostnamesRequest) (*GetSelectedHostnamesResponse, error) {
   167  	logger := p.Log(ctx)
   168  	logger.Debug("GetSelectedHostnames")
   169  
   170  	if err := params.Validate(); err != nil {
   171  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   172  	}
   173  
   174  	uri := fmt.Sprintf(
   175  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames",
   176  		params.ConfigID,
   177  		params.Version)
   178  
   179  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   180  	if err != nil {
   181  		return nil, fmt.Errorf("failed to create GetSelectedHostnames request: %w", err)
   182  	}
   183  
   184  	var result GetSelectedHostnamesResponse
   185  	resp, err := p.Exec(req, &result)
   186  	if err != nil {
   187  		return nil, fmt.Errorf("get selected hostnames request failed: %w", err)
   188  	}
   189  	if resp.StatusCode != http.StatusOK {
   190  		return nil, p.Error(resp)
   191  	}
   192  
   193  	return &result, nil
   194  }
   195  
   196  func (p *appsec) UpdateSelectedHostnames(ctx context.Context, params UpdateSelectedHostnamesRequest) (*UpdateSelectedHostnamesResponse, error) {
   197  	logger := p.Log(ctx)
   198  	logger.Debug("UpdateSelectedHostnames")
   199  
   200  	if err := params.Validate(); err != nil {
   201  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   202  	}
   203  
   204  	uri := fmt.Sprintf(
   205  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames",
   206  		params.ConfigID,
   207  		params.Version,
   208  	)
   209  
   210  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   211  	if err != nil {
   212  		return nil, fmt.Errorf("failed to create UpdateSelectedHostnames request: %w", err)
   213  	}
   214  
   215  	var result UpdateSelectedHostnamesResponse
   216  	resp, err := p.Exec(req, &result, params)
   217  	if err != nil {
   218  		return nil, fmt.Errorf("update selected hostnames request failed: %w", err)
   219  	}
   220  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   221  		return nil, p.Error(resp)
   222  	}
   223  
   224  	return &result, nil
   225  }
   226  
   227  // Deprecated: this method will be removed in a future release.
   228  func (p *appsec) UpdateSelectedHostname(ctx context.Context, params UpdateSelectedHostnameRequest) (*UpdateSelectedHostnameResponse, error) {
   229  	logger := p.Log(ctx)
   230  	logger.Debug("UpdateSelectedHostname")
   231  
   232  	if err := params.Validate(); err != nil {
   233  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   234  	}
   235  
   236  	uri := fmt.Sprintf(
   237  		"/appsec/v1/configs/%d/versions/%d/selected-hostnames",
   238  		params.ConfigID,
   239  		params.Version,
   240  	)
   241  
   242  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   243  	if err != nil {
   244  		return nil, fmt.Errorf("failed to create UpdateSelectedHostname request: %w", err)
   245  	}
   246  
   247  	var result UpdateSelectedHostnameResponse
   248  	resp, err := p.Exec(req, &result, params)
   249  	if err != nil {
   250  		return nil, fmt.Errorf("update selected hostname request failed: %w", err)
   251  	}
   252  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   253  		return nil, p.Error(resp)
   254  	}
   255  
   256  	return &result, nil
   257  }