github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudwrapper/multi_cdn.go (about) 1 package cloudwrapper 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 "net/url" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/edgegriderr" 11 validation "github.com/go-ozzo/ozzo-validation/v4" 12 ) 13 14 type ( 15 // MultiCDN is the cloudwrapper Multi-CDN API interface 16 MultiCDN interface { 17 // ListAuthKeys lists the cdnAuthKeys for a specified contractId and cdnCode 18 // 19 // See: https://techdocs.akamai.com/cloud-wrapper/reference/get-auth-keys 20 ListAuthKeys(context.Context, ListAuthKeysRequest) (*ListAuthKeysResponse, error) 21 22 // ListCDNProviders lists CDN providers 23 // 24 // See: https://techdocs.akamai.com/cloud-wrapper/reference/get-providers 25 ListCDNProviders(context.Context) (*ListCDNProvidersResponse, error) 26 } 27 28 // ListAuthKeysRequest is a request struct 29 ListAuthKeysRequest struct { 30 ContractID string 31 CDNCode string 32 } 33 34 // ListAuthKeysResponse contains response list of CDN auth keys 35 ListAuthKeysResponse struct { 36 CDNAuthKeys []MultiCDNAuthKey `json:"cdnAuthKeys"` 37 } 38 39 // MultiCDNAuthKey contains CDN auth key information 40 MultiCDNAuthKey struct { 41 AuthKeyName string `json:"authKeyName"` 42 ExpiryDate string `json:"expiryDate"` 43 HeaderName string `json:"headerName"` 44 } 45 46 // ListCDNProvidersResponse contains response list of CDN providers 47 ListCDNProvidersResponse struct { 48 CDNProviders []CDNProvider `json:"cdnProviders"` 49 } 50 51 // CDNProvider contains CDN provider information 52 CDNProvider struct { 53 CDNCode string `json:"cdnCode"` 54 CDNName string `json:"cdnName"` 55 } 56 ) 57 58 // Validate validates ListAuthKeysRequest 59 func (r ListAuthKeysRequest) Validate() error { 60 return edgegriderr.ParseValidationErrors(validation.Errors{ 61 "ContractID": validation.Validate(r.ContractID, validation.Required), 62 "CDNCode": validation.Validate(r.CDNCode, validation.Required), 63 }) 64 } 65 66 var ( 67 // ErrListAuthKeys is returned in case an error occurs on ListAuthKeys operation 68 ErrListAuthKeys = errors.New("list auth keys") 69 // ErrListCDNProviders is returned in case an error occurs on ListCDNProviders operation 70 ErrListCDNProviders = errors.New("list CDN providers") 71 ) 72 73 func (c *cloudwrapper) ListAuthKeys(ctx context.Context, params ListAuthKeysRequest) (*ListAuthKeysResponse, error) { 74 logger := c.Log(ctx) 75 logger.Debug("ListAuthKeys") 76 77 if err := params.Validate(); err != nil { 78 return nil, fmt.Errorf("%s: %w: %s", ErrListAuthKeys, ErrStructValidation, err) 79 } 80 81 uri, err := url.Parse("/cloud-wrapper/v1/multi-cdn/auth-keys") 82 if err != nil { 83 return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListAuthKeys, err) 84 } 85 q := uri.Query() 86 q.Add("contractId", params.ContractID) 87 q.Add("cdnCode", params.CDNCode) 88 uri.RawQuery = q.Encode() 89 90 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil) 91 if err != nil { 92 return nil, fmt.Errorf("%w: failed to create request: %s", ErrListAuthKeys, err) 93 } 94 95 var result ListAuthKeysResponse 96 resp, err := c.Exec(req, &result) 97 if err != nil { 98 return nil, fmt.Errorf("%w: request failed: %s", ErrListAuthKeys, err) 99 } 100 101 if resp.StatusCode != http.StatusOK { 102 return nil, fmt.Errorf("%s: %w", ErrListAuthKeys, c.Error(resp)) 103 } 104 105 return &result, nil 106 } 107 108 func (c *cloudwrapper) ListCDNProviders(ctx context.Context) (*ListCDNProvidersResponse, error) { 109 logger := c.Log(ctx) 110 logger.Debug("ListCDNProviders") 111 112 uri := "/cloud-wrapper/v1/multi-cdn/providers" 113 114 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 115 if err != nil { 116 return nil, fmt.Errorf("%w: failed to create request: %s", ErrListCDNProviders, err) 117 } 118 119 var result ListCDNProvidersResponse 120 resp, err := c.Exec(req, &result) 121 if err != nil { 122 return nil, fmt.Errorf("%w: request failed: %s", ErrListCDNProviders, err) 123 } 124 125 if resp.StatusCode != http.StatusOK { 126 return nil, fmt.Errorf("%s: %w", ErrListCDNProviders, c.Error(resp)) 127 } 128 129 return &result, nil 130 }