github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/selectable_hostnames.go (about)

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  type (
    10  	// The SelectableHostnames interface supports retrieving the hostnames that a given configuration version
    11  	// has the ability to protect. Hostnames may show as error hosts when they aren’t currently available. for
    12  	// example, when a contract expires.
    13  	SelectableHostnames interface {
    14  		// GetSelectableHostnames lists the hostnames that a given configuration version has the ability to protect.
    15  		//
    16  		// See: https://techdocs.akamai.com/application-security/reference/get-selectable-hostnames
    17  		GetSelectableHostnames(ctx context.Context, params GetSelectableHostnamesRequest) (*GetSelectableHostnamesResponse, error)
    18  	}
    19  
    20  	// GetSelectableHostnamesRequest is used to retrieve the selectable hostnames for a configuration.
    21  	GetSelectableHostnamesRequest struct {
    22  		ConfigID   int    `json:"configId"`
    23  		Version    int    `json:"version"`
    24  		ContractID string `json:"-"`
    25  		GroupID    int    `json:"-"`
    26  	}
    27  
    28  	// GetSelectableHostnamesResponse is returned from a call to GetSelectableHostnames.
    29  	GetSelectableHostnamesResponse struct {
    30  		AvailableSet []struct {
    31  			ActiveInProduction     bool   `json:"activeInProduction,omitempty"`
    32  			ActiveInStaging        bool   `json:"activeInStaging,omitempty"`
    33  			ArlInclusion           bool   `json:"arlInclusion,omitempty"`
    34  			Hostname               string `json:"hostname,omitempty"`
    35  			ConfigIDInProduction   int    `json:"configIdInProduction,omitempty"`
    36  			ConfigNameInProduction string `json:"configNameInProduction,omitempty"`
    37  		} `json:"availableSet,omitempty"`
    38  		ConfigID                int  `json:"configId,omitempty"`
    39  		ConfigVersion           int  `json:"configVersion,omitempty"`
    40  		ProtectARLInclusionHost bool `json:"protectARLInclusionHost,omitempty"`
    41  	}
    42  )
    43  
    44  func (p *appsec) GetSelectableHostnames(ctx context.Context, params GetSelectableHostnamesRequest) (*GetSelectableHostnamesResponse, error) {
    45  	logger := p.Log(ctx)
    46  	logger.Debug("GetSelectableHostnamess")
    47  
    48  	var uri string
    49  
    50  	if params.ConfigID != 0 {
    51  		uri = fmt.Sprintf(
    52  			"/appsec/v1/configs/%d/versions/%d/selectable-hostnames",
    53  			params.ConfigID,
    54  			params.Version)
    55  	} else {
    56  		uri = fmt.Sprintf(
    57  			"/appsec/v1/contracts/%s/groups/%d/selectable-hostnames",
    58  			params.ContractID,
    59  			params.GroupID)
    60  	}
    61  
    62  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    63  	if err != nil {
    64  		return nil, fmt.Errorf("failed to create GetSelectableHostnames request: %w", err)
    65  	}
    66  
    67  	var result GetSelectableHostnamesResponse
    68  	resp, err := p.Exec(req, &result)
    69  	if err != nil {
    70  		return nil, fmt.Errorf("get selectable hostnames request failed: %w", err)
    71  	}
    72  	if resp.StatusCode != http.StatusOK {
    73  		return nil, p.Error(resp)
    74  	}
    75  
    76  	return &result, nil
    77  }