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