|
| 1 | +package cloudflare |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// PagesDomain represents a pages domain. |
| 12 | +type PagesDomain struct { |
| 13 | + ID string `json:"id"` |
| 14 | + Name string `json:"name"` |
| 15 | + Status string `json:"status"` |
| 16 | + VerificationData VerificationData `json:"verification_data"` |
| 17 | + ValidationData ValidationData `json:"validation_data"` |
| 18 | + ZoneTag string `json:"zone_tag"` |
| 19 | + CreatedOn *time.Time `json:"created_on"` |
| 20 | +} |
| 21 | + |
| 22 | +// VerificationData represents verification data for a domain. |
| 23 | +type VerificationData struct { |
| 24 | + Status string `json:"status"` |
| 25 | +} |
| 26 | + |
| 27 | +// ValidationData represents validation data for a domain. |
| 28 | +type ValidationData struct { |
| 29 | + Status string `json:"status"` |
| 30 | + Method string `json:"method"` |
| 31 | +} |
| 32 | + |
| 33 | +// PagesDomainsParameters represents parameters for a pages domains request. |
| 34 | +type PagesDomainsParameters struct { |
| 35 | + AccountID string |
| 36 | + ProjectName string |
| 37 | +} |
| 38 | + |
| 39 | +// PagesDomainsResponse represents an API response for a pages domains request. |
| 40 | +type PagesDomainsResponse struct { |
| 41 | + Response |
| 42 | + Result []PagesDomain `json:"result,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +// PagesDomainParameters represents parameters for a pages domain request. |
| 46 | +type PagesDomainParameters struct { |
| 47 | + AccountID string |
| 48 | + ProjectName string |
| 49 | + DomainName string `json:"name,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +// PagesDomainResponse represents an API response for a pages domain request. |
| 53 | +type PagesDomainResponse struct { |
| 54 | + Response |
| 55 | + Result PagesDomain `json:"result,omitempty"` |
| 56 | +} |
| 57 | + |
| 58 | +// GetPagesDomains gets all domains for a pages project. |
| 59 | +// |
| 60 | +// API Reference: https://api.cloudflare.com/#pages-domains-get-domains |
| 61 | +func (api *API) GetPagesDomains(ctx context.Context, params PagesDomainsParameters) ([]PagesDomain, error) { |
| 62 | + if params.AccountID == "" { |
| 63 | + return []PagesDomain{}, ErrMissingAccountID |
| 64 | + } |
| 65 | + |
| 66 | + if params.ProjectName == "" { |
| 67 | + return []PagesDomain{}, ErrMissingProjectName |
| 68 | + } |
| 69 | + |
| 70 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/domains", params.AccountID, params.ProjectName) |
| 71 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 72 | + if err != nil { |
| 73 | + return []PagesDomain{}, err |
| 74 | + } |
| 75 | + |
| 76 | + var pageDomainResponse PagesDomainsResponse |
| 77 | + if err := json.Unmarshal(res, &pageDomainResponse); err != nil { |
| 78 | + return []PagesDomain{}, err |
| 79 | + } |
| 80 | + return pageDomainResponse.Result, nil |
| 81 | +} |
| 82 | + |
| 83 | +// GetPagesDomain gets a single domain. |
| 84 | +// |
| 85 | +// API Reference: https://api.cloudflare.com/#pages-domains-get-domains |
| 86 | +func (api *API) GetPagesDomain(ctx context.Context, params PagesDomainParameters) (PagesDomain, error) { |
| 87 | + if params.AccountID == "" { |
| 88 | + return PagesDomain{}, ErrMissingAccountID |
| 89 | + } |
| 90 | + |
| 91 | + if params.ProjectName == "" { |
| 92 | + return PagesDomain{}, ErrMissingProjectName |
| 93 | + } |
| 94 | + |
| 95 | + if params.DomainName == "" { |
| 96 | + return PagesDomain{}, ErrMissingDomain |
| 97 | + } |
| 98 | + |
| 99 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/domains/%s", params.AccountID, params.ProjectName, params.DomainName) |
| 100 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 101 | + if err != nil { |
| 102 | + return PagesDomain{}, err |
| 103 | + } |
| 104 | + |
| 105 | + var pagesDomainResponse PagesDomainResponse |
| 106 | + if err := json.Unmarshal(res, &pagesDomainResponse); err != nil { |
| 107 | + return PagesDomain{}, err |
| 108 | + } |
| 109 | + return pagesDomainResponse.Result, nil |
| 110 | +} |
| 111 | + |
| 112 | +// PagesPatchDomain retries the validation status of a single domain. |
| 113 | +// |
| 114 | +// API Reference: https://api.cloudflare.com/#pages-domains-patch-domain |
| 115 | +func (api *API) PagesPatchDomain(ctx context.Context, params PagesDomainParameters) (PagesDomain, error) { |
| 116 | + if params.AccountID == "" { |
| 117 | + return PagesDomain{}, ErrMissingAccountID |
| 118 | + } |
| 119 | + |
| 120 | + if params.ProjectName == "" { |
| 121 | + return PagesDomain{}, ErrMissingProjectName |
| 122 | + } |
| 123 | + |
| 124 | + if params.DomainName == "" { |
| 125 | + return PagesDomain{}, ErrMissingDomain |
| 126 | + } |
| 127 | + |
| 128 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/domains/%s", params.AccountID, params.ProjectName, params.DomainName) |
| 129 | + res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, nil) |
| 130 | + if err != nil { |
| 131 | + return PagesDomain{}, err |
| 132 | + } |
| 133 | + |
| 134 | + var pagesDomainResponse PagesDomainResponse |
| 135 | + if err := json.Unmarshal(res, &pagesDomainResponse); err != nil { |
| 136 | + return PagesDomain{}, err |
| 137 | + } |
| 138 | + return pagesDomainResponse.Result, nil |
| 139 | +} |
| 140 | + |
| 141 | +// PagesAddDomain adds a domain to a pages project. |
| 142 | +// |
| 143 | +// API Reference: https://api.cloudflare.com/#pages-domains-add-domain |
| 144 | +func (api *API) PagesAddDomain(ctx context.Context, params PagesDomainParameters) (PagesDomain, error) { |
| 145 | + if params.AccountID == "" { |
| 146 | + return PagesDomain{}, ErrMissingAccountID |
| 147 | + } |
| 148 | + |
| 149 | + if params.ProjectName == "" { |
| 150 | + return PagesDomain{}, ErrMissingProjectName |
| 151 | + } |
| 152 | + |
| 153 | + if params.DomainName == "" { |
| 154 | + return PagesDomain{}, ErrMissingDomain |
| 155 | + } |
| 156 | + |
| 157 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/domains", params.AccountID, params.ProjectName) |
| 158 | + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) |
| 159 | + if err != nil { |
| 160 | + return PagesDomain{}, err |
| 161 | + } |
| 162 | + |
| 163 | + var pagesDomainResponse PagesDomainResponse |
| 164 | + if err := json.Unmarshal(res, &pagesDomainResponse); err != nil { |
| 165 | + return PagesDomain{}, err |
| 166 | + } |
| 167 | + return pagesDomainResponse.Result, nil |
| 168 | +} |
| 169 | + |
| 170 | +// PagesDeleteDomain removes a domain from a pages project. |
| 171 | +// |
| 172 | +// API Reference: https://api.cloudflare.com/#pages-domains-delete-domain |
| 173 | +func (api *API) PagesDeleteDomain(ctx context.Context, params PagesDomainParameters) error { |
| 174 | + if params.AccountID == "" { |
| 175 | + return ErrMissingAccountID |
| 176 | + } |
| 177 | + |
| 178 | + if params.ProjectName == "" { |
| 179 | + return ErrMissingProjectName |
| 180 | + } |
| 181 | + |
| 182 | + if params.DomainName == "" { |
| 183 | + return ErrMissingDomain |
| 184 | + } |
| 185 | + |
| 186 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s/domains/%s", params.AccountID, params.ProjectName, params.DomainName) |
| 187 | + |
| 188 | + _, err := api.makeRequestContext(ctx, http.MethodDelete, uri, params) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + return nil |
| 193 | +} |
0 commit comments