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

     1  package cps
     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  	// History is a CPS interface for History management
    14  	History interface {
    15  		// GetDVHistory is a domain name validation history for the enrollment
    16  		//
    17  		// See: https://techdocs.akamai.com/cps/reference/get-dv-history
    18  		GetDVHistory(context.Context, GetDVHistoryRequest) (*GetDVHistoryResponse, error)
    19  
    20  		// GetCertificateHistory views the certificate history.
    21  		//
    22  		// See: https://techdocs.akamai.com/cps/reference/get-history-certificates
    23  		GetCertificateHistory(context.Context, GetCertificateHistoryRequest) (*GetCertificateHistoryResponse, error)
    24  
    25  		// GetChangeHistory views the change history for enrollment.
    26  		//
    27  		// See: https://techdocs.akamai.com/cps/reference/get-history-changes
    28  		GetChangeHistory(context.Context, GetChangeHistoryRequest) (*GetChangeHistoryResponse, error)
    29  	}
    30  
    31  	// GetDVHistoryRequest represents request for GetDVHistory operation
    32  	GetDVHistoryRequest struct {
    33  		EnrollmentID int
    34  	}
    35  
    36  	// GetDVHistoryResponse represents response for GetDVHistory operation
    37  	GetDVHistoryResponse struct {
    38  		Results []HistoryResult `json:"results"`
    39  	}
    40  
    41  	// HistoryResult represents a piece of history for GetDVHistory operation
    42  	HistoryResult struct {
    43  		Domain        string          `json:"domain"`
    44  		DomainHistory []DomainHistory `json:"domainHistory"`
    45  	}
    46  
    47  	// DomainHistory represents a history for single domain for GetDVHistory operation
    48  	DomainHistory struct {
    49  		Domain             string             `json:"domain"`
    50  		Challenges         []Challenge        `json:"challenges"`
    51  		Error              string             `json:"error"`
    52  		Expires            string             `json:"expires"`
    53  		FullPath           string             `json:"fullPath"`
    54  		RedirectFullPath   string             `json:"redirectFullPath"`
    55  		RequestTimestamp   string             `json:"requestTimestamp"`
    56  		ResponseBody       string             `json:"responseBody"`
    57  		Status             string             `json:"status"`
    58  		Token              string             `json:"token"`
    59  		ValidatedTimestamp string             `json:"validatedTimestamp"`
    60  		ValidationRecords  []ValidationRecord `json:"validationRecords"`
    61  		ValidationStatus   string             `json:"validationStatus"`
    62  	}
    63  
    64  	// GetCertificateHistoryRequest represents request for GetCertificateHistory operation
    65  	GetCertificateHistoryRequest struct {
    66  		EnrollmentID int
    67  	}
    68  
    69  	// GetCertificateHistoryResponse represents response for GetCertificateHistory operation
    70  	GetCertificateHistoryResponse struct {
    71  		Certificates []HistoryCertificate `json:"certificates"`
    72  	}
    73  
    74  	// HistoryCertificate represents a piece of enrollment's certificate history for GetCertificateHistory operation
    75  	HistoryCertificate struct {
    76  		DeploymentStatus         string              `json:"deploymentStatus"`
    77  		Geography                string              `json:"geography"`
    78  		MultiStackedCertificates []CertificateObject `json:"multiStackedCertificates"`
    79  		PrimaryCertificate       CertificateObject   `json:"primaryCertificate"`
    80  		RA                       string              `json:"ra"`
    81  		Slots                    []int               `json:"slots"`
    82  		StagingStatus            string              `json:"stagingStatus"`
    83  		Type                     string              `json:"type"`
    84  	}
    85  
    86  	//CertificateObject represent certificate for enrollment
    87  	CertificateObject struct {
    88  		Certificate  string `json:"certificate"`
    89  		Expiry       string `json:"expiry"`
    90  		KeyAlgorithm string `json:"keyAlgorithm"`
    91  		TrustChain   string `json:"trustChain"`
    92  	}
    93  
    94  	// GetChangeHistoryRequest represents request for GetChangeHistory operation
    95  	GetChangeHistoryRequest struct {
    96  		EnrollmentID int
    97  	}
    98  
    99  	// GetChangeHistoryResponse represents response for GetChangeHistory operation
   100  	GetChangeHistoryResponse struct {
   101  		Changes []ChangeHistory `json:"changes"`
   102  	}
   103  
   104  	// ChangeHistory represents a piece of enrollment's history for a single change for GetChangeHistory operation
   105  	ChangeHistory struct {
   106  		Action                         string                     `json:"action"`
   107  		ActionDescription              string                     `json:"actionDescription"`
   108  		BusinessCaseID                 string                     `json:"businessCaseId"`
   109  		CreatedBy                      string                     `json:"createdBy"`
   110  		CreatedOn                      string                     `json:"createdOn"`
   111  		LastUpdated                    string                     `json:"lastUpdated"`
   112  		MultiStackedCertificates       []CertificateChangeHistory `json:"multiStackedCertificates"`
   113  		PrimaryCertificate             CertificateChangeHistory   `json:"primaryCertificate"`
   114  		PrimaryCertificateOrderDetails CertificateOrderDetails    `json:"primaryCertificateOrderDetails"`
   115  		RA                             string                     `json:"ra"`
   116  		Status                         string                     `json:"status"`
   117  	}
   118  
   119  	// CertificateChangeHistory represents certificate returned in GetChangeHistory operation
   120  	CertificateChangeHistory struct {
   121  		Certificate  string `json:"certificate"`
   122  		TrustChain   string `json:"trustChain"`
   123  		CSR          string `json:"csr"`
   124  		KeyAlgorithm string `json:"keyAlgorithm"`
   125  	}
   126  
   127  	// CertificateOrderDetails represents CA order details for a Change
   128  	CertificateOrderDetails struct {
   129  		OrderID string `json:"orderId"`
   130  	}
   131  )
   132  
   133  var (
   134  	// ErrGetDVHistory is returned when GetDVHistory fails
   135  	ErrGetDVHistory = errors.New("get dv history")
   136  	// ErrGetCertificateHistory is returned when GetDVHistory fails
   137  	ErrGetCertificateHistory = errors.New("get certificate history")
   138  	// ErrGetChangeHistory is returned when GetDVHistory fails
   139  	ErrGetChangeHistory = errors.New("get change history")
   140  )
   141  
   142  // Validate validates GetDVHistoryRequest
   143  func (r GetDVHistoryRequest) Validate() error {
   144  	return validation.Errors{
   145  		"EnrollmentID": validation.Validate(r.EnrollmentID, validation.Required),
   146  	}.Filter()
   147  }
   148  
   149  // Validate validates GetCertificateHistoryRequest
   150  func (r GetCertificateHistoryRequest) Validate() error {
   151  	return validation.Errors{
   152  		"EnrollmentID": validation.Validate(r.EnrollmentID, validation.Required),
   153  	}.Filter()
   154  }
   155  
   156  // Validate validates GetChangeHistoryRequest
   157  func (r GetChangeHistoryRequest) Validate() error {
   158  	return validation.Errors{
   159  		"EnrollmentID": validation.Validate(r.EnrollmentID, validation.Required),
   160  	}.Filter()
   161  }
   162  
   163  func (c *cps) GetDVHistory(ctx context.Context, params GetDVHistoryRequest) (*GetDVHistoryResponse, error) {
   164  	logger := c.Log(ctx)
   165  	logger.Debug("GetDVHistory")
   166  
   167  	if err := params.Validate(); err != nil {
   168  		return nil, fmt.Errorf("%s: %w: %s", ErrGetDVHistory, ErrStructValidation, err)
   169  	}
   170  
   171  	url := fmt.Sprintf("/cps/v2/enrollments/%d/dv-history", params.EnrollmentID)
   172  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
   173  	if err != nil {
   174  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetDVHistory, err)
   175  	}
   176  	var result GetDVHistoryResponse
   177  	req.Header.Set("Accept", "application/vnd.akamai.cps.dv-history.v1+json")
   178  	resp, err := c.Exec(req, &result)
   179  	if err != nil {
   180  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetDVHistory, err)
   181  	}
   182  
   183  	if resp.StatusCode != http.StatusOK {
   184  		return nil, fmt.Errorf("%s: %w", ErrGetDVHistory, c.Error(resp))
   185  	}
   186  
   187  	return &result, nil
   188  }
   189  
   190  func (c *cps) GetCertificateHistory(ctx context.Context, params GetCertificateHistoryRequest) (*GetCertificateHistoryResponse, error) {
   191  	logger := c.Log(ctx)
   192  	logger.Debug("GetCertificateHistory")
   193  
   194  	if err := params.Validate(); err != nil {
   195  		return nil, fmt.Errorf("%s: %w: %s", ErrGetCertificateHistory, ErrStructValidation, err)
   196  	}
   197  
   198  	url := fmt.Sprintf("/cps/v2/enrollments/%d/history/certificates", params.EnrollmentID)
   199  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
   200  	if err != nil {
   201  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetCertificateHistory, err)
   202  	}
   203  	var result GetCertificateHistoryResponse
   204  	req.Header.Set("Accept", "application/vnd.akamai.cps.certificate-history.v2+json")
   205  	resp, err := c.Exec(req, &result)
   206  	if err != nil {
   207  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetCertificateHistory, err)
   208  	}
   209  
   210  	if resp.StatusCode != http.StatusOK {
   211  		return nil, fmt.Errorf("%s: %w", ErrGetCertificateHistory, c.Error(resp))
   212  	}
   213  
   214  	return &result, nil
   215  }
   216  
   217  func (c *cps) GetChangeHistory(ctx context.Context, params GetChangeHistoryRequest) (*GetChangeHistoryResponse, error) {
   218  	logger := c.Log(ctx)
   219  	logger.Debug("GetChangeHistory")
   220  
   221  	if err := params.Validate(); err != nil {
   222  		return nil, fmt.Errorf("%s: %w: %s", ErrGetChangeHistory, ErrStructValidation, err)
   223  	}
   224  
   225  	url := fmt.Sprintf("/cps/v2/enrollments/%d/history/changes", params.EnrollmentID)
   226  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
   227  	if err != nil {
   228  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetChangeHistory, err)
   229  	}
   230  	var result GetChangeHistoryResponse
   231  	req.Header.Set("Accept", "application/vnd.akamai.cps.change-history.v5+json")
   232  	resp, err := c.Exec(req, &result)
   233  	if err != nil {
   234  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetChangeHistory, err)
   235  	}
   236  
   237  	if resp.StatusCode != http.StatusOK {
   238  		return nil, fmt.Errorf("%s: %w", ErrGetChangeHistory, c.Error(resp))
   239  	}
   240  
   241  	return &result, nil
   242  }