github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cps/deployments.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 // Deployments is a CPS deployments API interface 14 Deployments interface { 15 // ListDeployments fetches deployments for given enrollment 16 // 17 // See: https://techdocs.akamai.com/cps/reference/get-deployments 18 ListDeployments(context.Context, ListDeploymentsRequest) (*ListDeploymentsResponse, error) 19 20 // GetProductionDeployment fetches production deployment for given enrollment 21 // 22 // See: https://techdocs.akamai.com/cps/reference/get-deployments-production 23 GetProductionDeployment(context.Context, GetDeploymentRequest) (*GetProductionDeploymentResponse, error) 24 25 // GetStagingDeployment fetches staging deployment for given enrollment 26 // 27 // See: https://techdocs.akamai.com/cps/reference/get-deployment-staging 28 GetStagingDeployment(context.Context, GetDeploymentRequest) (*GetStagingDeploymentResponse, error) 29 } 30 31 // ListDeploymentsRequest contains parameters for ListDeployments 32 ListDeploymentsRequest struct { 33 EnrollmentID int 34 } 35 36 // GetDeploymentRequest contains parameters for GetProductionDeployment and GetStagingDeployment 37 GetDeploymentRequest struct { 38 EnrollmentID int 39 } 40 41 // ListDeploymentsResponse contains response for ListDeployments 42 ListDeploymentsResponse struct { 43 Production *Deployment `json:"production"` 44 Staging *Deployment `json:"staging"` 45 } 46 47 // GetProductionDeploymentResponse contains response for GetProductionDeployment 48 GetProductionDeploymentResponse Deployment 49 50 // GetStagingDeploymentResponse contains response for GetStagingDeployment 51 GetStagingDeploymentResponse Deployment 52 53 // Deployment represents details of production or staging deployment 54 Deployment struct { 55 OCSPStapled *bool `json:"ocspStapled"` 56 OCSPURIs []string `json:"ocspUris"` 57 NetworkConfiguration DeploymentNetworkConfiguration `json:"networkConfiguration"` 58 PrimaryCertificate DeploymentCertificate `json:"primaryCertificate"` 59 MultiStackedCertificates []DeploymentCertificate `json:"multiStackedCertificates"` 60 } 61 62 // DeploymentCertificate represents certificate in context of deployment operation 63 DeploymentCertificate struct { 64 Certificate string `json:"certificate"` 65 Expiry string `json:"expiry"` 66 KeyAlgorithm string `json:"keyAlgorithm"` 67 SignatureAlgorithm string `json:"signatureAlgorithm"` 68 TrustChain string `json:"trustChain"` 69 } 70 71 // DeploymentNetworkConfiguration represents network configuration in context of deployment operation 72 DeploymentNetworkConfiguration struct { 73 Geography string `json:"geography"` 74 MustHaveCiphers string `json:"mustHaveCiphers"` 75 OCSPStapling string `json:"ocspStapling"` 76 PreferredCiphers string `json:"preferredCiphers"` 77 QUICEnabled bool `json:"quicEnabled"` 78 SecureNetwork string `json:"secureNetwork"` 79 SNIOnly bool `json:"sniOnly"` 80 DisallowedTLSVersions []string `json:"disallowedTlsVersions"` 81 DNSNames []string `json:"dnsNames"` 82 } 83 ) 84 85 // Validate validates ListDeploymentsRequest 86 func (c ListDeploymentsRequest) Validate() error { 87 return validation.Errors{ 88 "EnrollmentID": validation.Validate(c.EnrollmentID, validation.Required), 89 }.Filter() 90 } 91 92 // Validate validates GetDeploymentsRequest 93 func (c GetDeploymentRequest) Validate() error { 94 return validation.Errors{ 95 "EnrollmentID": validation.Validate(c.EnrollmentID, validation.Required), 96 }.Filter() 97 } 98 99 var ( 100 // ErrListDeployments is returned when ListDeployments fails 101 ErrListDeployments = errors.New("list deployments") 102 // ErrGetProductionDeployment is returned when GetProductionDeployment fails 103 ErrGetProductionDeployment = errors.New("get production deployment") 104 // ErrGetStagingDeployment is returned when GetStagingDeployment fails 105 ErrGetStagingDeployment = errors.New("get staging deployment") 106 ) 107 108 func (c *cps) ListDeployments(ctx context.Context, params ListDeploymentsRequest) (*ListDeploymentsResponse, error) { 109 logger := c.Log(ctx) 110 logger.Debug("ListDeployments") 111 112 if err := params.Validate(); err != nil { 113 return nil, fmt.Errorf("%s: %w: %s", ErrListDeployments, ErrStructValidation, err) 114 } 115 116 uri := fmt.Sprintf("/cps/v2/enrollments/%d/deployments", params.EnrollmentID) 117 118 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 119 if err != nil { 120 return nil, fmt.Errorf("%w: failed to create request: %s", ErrListDeployments, err) 121 } 122 req.Header.Set("Accept", "application/vnd.akamai.cps.deployments.v8+json") 123 124 var result ListDeploymentsResponse 125 resp, err := c.Exec(req, &result) 126 if err != nil { 127 return nil, fmt.Errorf("%w: request failed: %s", ErrListDeployments, err) 128 } 129 130 if resp.StatusCode != http.StatusOK { 131 return nil, fmt.Errorf("%s: %w", ErrListDeployments, c.Error(resp)) 132 } 133 134 return &result, nil 135 } 136 137 func (c *cps) GetProductionDeployment(ctx context.Context, params GetDeploymentRequest) (*GetProductionDeploymentResponse, error) { 138 logger := c.Log(ctx) 139 logger.Debug("GetProductionDeployment") 140 141 if err := params.Validate(); err != nil { 142 return nil, fmt.Errorf("%s: %w: %s", ErrGetProductionDeployment, ErrStructValidation, err) 143 } 144 145 uri := fmt.Sprintf("/cps/v2/enrollments/%d/deployments/production", params.EnrollmentID) 146 147 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 148 if err != nil { 149 return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetProductionDeployment, err) 150 } 151 req.Header.Set("Accept", "application/vnd.akamai.cps.deployment.v8+json") 152 153 var result GetProductionDeploymentResponse 154 resp, err := c.Exec(req, &result) 155 if err != nil { 156 return nil, fmt.Errorf("%w: request failed: %s", ErrGetProductionDeployment, err) 157 } 158 159 if resp.StatusCode != http.StatusOK { 160 return nil, fmt.Errorf("%s: %w", ErrGetProductionDeployment, c.Error(resp)) 161 } 162 163 return &result, nil 164 } 165 166 func (c *cps) GetStagingDeployment(ctx context.Context, params GetDeploymentRequest) (*GetStagingDeploymentResponse, error) { 167 logger := c.Log(ctx) 168 logger.Debug("GetStagingDeployment") 169 170 if err := params.Validate(); err != nil { 171 return nil, fmt.Errorf("%s: %w: %s", ErrGetStagingDeployment, ErrStructValidation, err) 172 } 173 174 uri := fmt.Sprintf("/cps/v2/enrollments/%d/deployments/staging", params.EnrollmentID) 175 176 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 177 if err != nil { 178 return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetStagingDeployment, err) 179 } 180 req.Header.Set("Accept", "application/vnd.akamai.cps.deployment.v8+json") 181 182 var result GetStagingDeploymentResponse 183 resp, err := c.Exec(req, &result) 184 if err != nil { 185 return nil, fmt.Errorf("%w: request failed: %s", ErrGetStagingDeployment, err) 186 } 187 188 if resp.StatusCode != http.StatusOK { 189 return nil, fmt.Errorf("%s: %w", ErrGetStagingDeployment, c.Error(resp)) 190 } 191 192 return &result, nil 193 }