github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/papi/propertyhostname.go (about)

     1  package papi
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	validation "github.com/go-ozzo/ozzo-validation/v4"
    10  )
    11  
    12  type (
    13  	// PropertyVersionHostnames contains operations available on PropertyVersionHostnames resource
    14  	// See: https://developer.akamai.com/api/core_features/property_manager/v1.html#propertyversionhostnamesgroup
    15  	PropertyVersionHostnames interface {
    16  		// GetPropertyVersionHostnames lists all the hostnames assigned to a property version
    17  		// See: https://developer.akamai.com/api/core_features/property_manager/v1.html#getpropertyversionhostnames
    18  		GetPropertyVersionHostnames(context.Context, GetPropertyVersionHostnamesRequest) (*GetPropertyVersionHostnamesResponse, error)
    19  
    20  		// CreatePropertyVersionHostnames lists all the hostnames assigned to a property version
    21  		// See: https://developer.akamai.com/api/core_features/property_manager/v1.html#putpropertyversionhostnames
    22  		UpdatePropertyVersionHostnames(context.Context, UpdatePropertyVersionHostnamesRequest) (*UpdatePropertyVersionHostnamesResponse, error)
    23  	}
    24  
    25  	// GetPropertyVersionHostnamesRequest contains parameters required to list property version hostnames
    26  	GetPropertyVersionHostnamesRequest struct {
    27  		PropertyID        string
    28  		PropertyVersion   int
    29  		ContractID        string
    30  		GroupID           string
    31  		ValidateHostnames bool
    32  		IncludeCertStatus bool
    33  	}
    34  
    35  	// GetPropertyVersionHostnamesResponse contains all property version hostnames associated to the given parameters
    36  	GetPropertyVersionHostnamesResponse struct {
    37  		AccountID       string                `json:"accountId"`
    38  		ContractID      string                `json:"contractId"`
    39  		GroupID         string                `json:"groupId"`
    40  		PropertyID      string                `json:"propertyId"`
    41  		PropertyVersion int                   `json:"propertyVersion"`
    42  		Etag            string                `json:"etag"`
    43  		Hostnames       HostnameResponseItems `json:"hostnames"`
    44  	}
    45  
    46  	// HostnameResponseItems contains the response body for GetPropertyVersionHostnamesResponse
    47  	HostnameResponseItems struct {
    48  		Items []Hostname `json:"items"`
    49  	}
    50  
    51  	// Hostname contains information about each of the HostnameResponseItems
    52  	Hostname struct {
    53  		CnameType            HostnameCnameType `json:"cnameType"`
    54  		EdgeHostnameID       string            `json:"edgeHostnameId,omitempty"`
    55  		CnameFrom            string            `json:"cnameFrom"`
    56  		CnameTo              string            `json:"cnameTo,omitempty"`
    57  		CertProvisioningType string            `json:"certProvisioningType"`
    58  		CertStatus           CertStatusItem    `json:"certStatus,omitempty"`
    59  	}
    60  
    61  	// CertStatusItem contains information about certificate status for specific Hostname
    62  	CertStatusItem struct {
    63  		ValidationCname ValidationCname `json:"validationCname,omitempty"`
    64  		Staging         []StatusItem    `json:"staging,omitempty"`
    65  		Production      []StatusItem    `json:"production,omitempty"`
    66  	}
    67  
    68  	// ValidationCname is the CNAME record used to validate the certificate’s domain
    69  	ValidationCname struct {
    70  		Hostname string `json:"hostname,omitempty"`
    71  		Target   string `json:"target,omitempty"`
    72  	}
    73  
    74  	// StatusItem determines whether a hostname is capable of serving secure content over the staging or production network.
    75  	StatusItem struct {
    76  		Status string `json:"status,omitempty"`
    77  	}
    78  
    79  	// UpdatePropertyVersionHostnamesRequest contains parameters required to update the set of hostname entries for a property version
    80  	UpdatePropertyVersionHostnamesRequest struct {
    81  		PropertyID        string
    82  		PropertyVersion   int
    83  		ContractID        string
    84  		GroupID           string
    85  		ValidateHostnames bool
    86  		IncludeCertStatus bool
    87  		Hostnames         []Hostname
    88  	}
    89  
    90  	// UpdatePropertyVersionHostnamesResponse contains information about each of the HostnameRequestItems
    91  	UpdatePropertyVersionHostnamesResponse struct {
    92  		AccountID       string                `json:"accountId"`
    93  		ContractID      string                `json:"contractId"`
    94  		GroupID         string                `json:"groupId"`
    95  		PropertyID      string                `json:"propertyId"`
    96  		PropertyVersion int                   `json:"propertyVersion"`
    97  		Etag            string                `json:"etag"`
    98  		Hostnames       HostnameResponseItems `json:"hostnames"`
    99  	}
   100  
   101  	// HostnameCnameType represents HostnameCnameType enum
   102  	HostnameCnameType string
   103  )
   104  
   105  const (
   106  	// HostnameCnameTypeEdgeHostname const
   107  	HostnameCnameTypeEdgeHostname HostnameCnameType = "EDGE_HOSTNAME"
   108  )
   109  
   110  // Validate validates GetPropertyVersionHostnamesRequest
   111  func (ph GetPropertyVersionHostnamesRequest) Validate() error {
   112  	return validation.Errors{
   113  		"PropertyID":      validation.Validate(ph.PropertyID, validation.Required),
   114  		"PropertyVersion": validation.Validate(ph.PropertyVersion, validation.Required),
   115  	}.Filter()
   116  }
   117  
   118  // Validate validates UpdatePropertyVersionHostnamesRequest
   119  func (ch UpdatePropertyVersionHostnamesRequest) Validate() error {
   120  	return validation.Errors{
   121  		"PropertyID":      validation.Validate(ch.PropertyID, validation.Required),
   122  		"PropertyVersion": validation.Validate(ch.PropertyVersion, validation.Required),
   123  	}.Filter()
   124  }
   125  
   126  var (
   127  	// ErrGetPropertyVersionHostnames represents error when fetching hostnames fails
   128  	ErrGetPropertyVersionHostnames = errors.New("fetching hostnames")
   129  	// ErrUpdatePropertyVersionHostnames represents error when updating hostnames fails
   130  	ErrUpdatePropertyVersionHostnames = errors.New("updating hostnames")
   131  )
   132  
   133  func (p *papi) GetPropertyVersionHostnames(ctx context.Context, params GetPropertyVersionHostnamesRequest) (*GetPropertyVersionHostnamesResponse, error) {
   134  	if err := params.Validate(); err != nil {
   135  		return nil, fmt.Errorf("%s: %w: %s", ErrGetPropertyVersionHostnames, ErrStructValidation, err)
   136  	}
   137  
   138  	logger := p.Log(ctx)
   139  	logger.Debug("GetPropertyVersionHostnames")
   140  
   141  	getURL := fmt.Sprintf(
   142  		"/papi/v1/properties/%s/versions/%d/hostnames?contractId=%s&groupId=%s&validateHostnames=%t&includeCertStatus=%t",
   143  		params.PropertyID,
   144  		params.PropertyVersion,
   145  		params.ContractID,
   146  		params.GroupID,
   147  		params.ValidateHostnames,
   148  		params.IncludeCertStatus)
   149  
   150  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   151  	if err != nil {
   152  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPropertyVersionHostnames, err)
   153  	}
   154  
   155  	var hostnames GetPropertyVersionHostnamesResponse
   156  	resp, err := p.Exec(req, &hostnames)
   157  	if err != nil {
   158  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetPropertyVersionHostnames, err)
   159  	}
   160  	if resp.StatusCode != http.StatusOK {
   161  		return nil, fmt.Errorf("%s: %w", ErrGetPropertyVersionHostnames, p.Error(resp))
   162  	}
   163  
   164  	return &hostnames, nil
   165  }
   166  
   167  func (p *papi) UpdatePropertyVersionHostnames(ctx context.Context, params UpdatePropertyVersionHostnamesRequest) (*UpdatePropertyVersionHostnamesResponse, error) {
   168  	if err := params.Validate(); err != nil {
   169  		return nil, fmt.Errorf("%s: %w: %s", ErrUpdatePropertyVersionHostnames, ErrStructValidation, err)
   170  	}
   171  
   172  	logger := p.Log(ctx)
   173  	logger.Debug("UpdatePropertyVersionHostnames")
   174  
   175  	putURL := fmt.Sprintf(
   176  		"/papi/v1/properties/%s/versions/%v/hostnames?contractId=%s&groupId=%s&validateHostnames=%t&includeCertStatus=%t",
   177  		params.PropertyID,
   178  		params.PropertyVersion,
   179  		params.ContractID,
   180  		params.GroupID,
   181  		params.ValidateHostnames,
   182  		params.IncludeCertStatus,
   183  	)
   184  
   185  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   186  	if err != nil {
   187  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdatePropertyVersionHostnames, err)
   188  	}
   189  
   190  	var hostnames UpdatePropertyVersionHostnamesResponse
   191  	newHostnames := params.Hostnames
   192  	if newHostnames == nil {
   193  		newHostnames = []Hostname{}
   194  	}
   195  	resp, err := p.Exec(req, &hostnames, newHostnames)
   196  	if err != nil {
   197  		return nil, fmt.Errorf("%w: request failed: %s", ErrUpdatePropertyVersionHostnames, err)
   198  	}
   199  
   200  	if resp.StatusCode != http.StatusOK {
   201  		return nil, fmt.Errorf("%s: %w", ErrUpdatePropertyVersionHostnames, p.Error(resp))
   202  	}
   203  
   204  	return &hostnames, nil
   205  }