|
| 1 | +package cloudflare |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strconv" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +// PagesProject represents a Pages project. |
| 16 | +type PagesProject struct { |
| 17 | + Name string `json:"name"` |
| 18 | + ID string `json:"id"` |
| 19 | + CreatedOn *time.Time `json:"created_on"` |
| 20 | + SubDomain string `json:"subdomain"` |
| 21 | + Domains []string `json:"domains,omitempty"` |
| 22 | + Source PagesProjectSource `json:"source"` |
| 23 | + BuildConfig PagesProjectBuildConfig `json:"build_config"` |
| 24 | + DeploymentConfigs PagesProjectDeploymentConfigs `json:"deployment_configs"` |
| 25 | + LatestDeployment PagesProjectDeployment `json:"latest_deployment"` |
| 26 | + CanonicalDeployment PagesProjectDeployment `json:"canonical_deployment"` |
| 27 | +} |
| 28 | + |
| 29 | +// PagesProjectSource represents the configuration of a Pages project source. |
| 30 | +type PagesProjectSource struct { |
| 31 | + Type string `json:"type"` |
| 32 | + Config *PagesProjectSourceConfig `json:"config"` |
| 33 | +} |
| 34 | + |
| 35 | +// PagesProjectSourceConfig represents the properties use to configure a Pages project source. |
| 36 | +type PagesProjectSourceConfig struct { |
| 37 | + Owner string `json:"owner"` |
| 38 | + RepoName string `json:"repo_name"` |
| 39 | + ProductionBranch string `json:"production_branch"` |
| 40 | + PRCommentsEnabled bool `json:"pr_comments_enabled"` |
| 41 | + DeploymentsEnabled bool `json:"deployments_enabled"` |
| 42 | +} |
| 43 | + |
| 44 | +// PagesProjectBuildConfig represents the configuration of a Pages project build process. |
| 45 | +type PagesProjectBuildConfig struct { |
| 46 | + BuildCommand string `json:"build_command"` |
| 47 | + DestinationDir string `json:"destination_dir"` |
| 48 | + RootDir string `json:"root_dir"` |
| 49 | + WebAnalyticsTag string `json:"web_analytics_tag"` |
| 50 | + WebAnalyticsToken string `json:"web_analytics_token"` |
| 51 | +} |
| 52 | + |
| 53 | +// PagesProjectDeploymentConfigs represents the configuration for deployments in a Pages project. |
| 54 | +type PagesProjectDeploymentConfigs struct { |
| 55 | + Preview PagesProjectDeploymentConfigEnvironment `json:"preview"` |
| 56 | + Production PagesProjectDeploymentConfigEnvironment `json:"production"` |
| 57 | +} |
| 58 | + |
| 59 | +// PagesProjectDeploymentConfigEnvironment represents the configuration for preview or production deploys. |
| 60 | +type PagesProjectDeploymentConfigEnvironment struct { |
| 61 | + EnvVars PagesProjectDeploymentConfigEnvVars `json:"env_vars"` |
| 62 | +} |
| 63 | + |
| 64 | +// PagesProjectDeploymentConfigEnvVars represents the BUILD_VERSION environment variables for a specific build config. |
| 65 | +type PagesProjectDeploymentConfigEnvVars struct { |
| 66 | + BuildVersion PagesProjectDeploymentConfigBuildVersion `json:"BUILD_VERSION"` |
| 67 | +} |
| 68 | + |
| 69 | +// PagesProjectDeploymentConfigBuildVersion represents a value for a BUILD_VERSION. |
| 70 | +type PagesProjectDeploymentConfigBuildVersion struct { |
| 71 | + Value string `json:"value"` |
| 72 | +} |
| 73 | + |
| 74 | +// PagesProjectDeployment represents a deployment to a Pages project. |
| 75 | +type PagesProjectDeployment struct { |
| 76 | + ID string `json:"id"` |
| 77 | + ShortID string `json:"short_id"` |
| 78 | + ProjectID string `json:"project_id"` |
| 79 | + ProjectName string `json:"project_name"` |
| 80 | + Environment string `json:"environment"` |
| 81 | + URL string `json:"url"` |
| 82 | + CreatedOn *time.Time `json:"created_on"` |
| 83 | + ModifiedOn *time.Time `json:"modified_on"` |
| 84 | + Aliases []string `json:"aliases,omitempty"` |
| 85 | + LatestStage PagesProjectDeploymentStage `json:"latest_stage"` |
| 86 | + EnvVars map[string]map[string]string `json:"env_vars"` |
| 87 | + DeploymentTrigger PagesProjectDeploymentTrigger `json:"deployment_trigger"` |
| 88 | + Stages []PagesProjectDeploymentStage `json:"stages"` |
| 89 | + BuildConfig PagesProjectBuildConfig `json:"build_config"` |
| 90 | + Source PagesProjectSource `json:"source"` |
| 91 | +} |
| 92 | + |
| 93 | +// PagesProjectDeploymentStage represents an individual stage in a Pages project deployment. |
| 94 | +type PagesProjectDeploymentStage struct { |
| 95 | + Name string `json:"name"` |
| 96 | + StartedOn *time.Time `json:"started_on,omitempty"` |
| 97 | + EndedOn *time.Time `json:"ended_on,omitempty"` |
| 98 | + Status string `json:"status"` |
| 99 | +} |
| 100 | + |
| 101 | +// PagesProjectDeploymentTrigger represents information about what caused a deployment. |
| 102 | +type PagesProjectDeploymentTrigger struct { |
| 103 | + Type string `json:"type"` |
| 104 | + Metadata *PagesProjectDeploymentTriggerMetadata `json:"metadata"` |
| 105 | +} |
| 106 | + |
| 107 | +// PagesProjectDeploymentTriggerMetadata represents additional information about the cause of a deployment. |
| 108 | +type PagesProjectDeploymentTriggerMetadata struct { |
| 109 | + Branch string `json:"branch"` |
| 110 | + CommitHash string `json:"commit_hash"` |
| 111 | + CommitMessage string `json:"commit_message"` |
| 112 | +} |
| 113 | + |
| 114 | +type pagesProjectResponse struct { |
| 115 | + Response |
| 116 | + Result PagesProject `json:"result"` |
| 117 | +} |
| 118 | + |
| 119 | +type pagesProjectListResponse struct { |
| 120 | + Response |
| 121 | + Result []PagesProject `json:"result"` |
| 122 | + ResultInfo `json:"result_info"` |
| 123 | +} |
| 124 | + |
| 125 | +// ListPagesProjects returns all Pages projects for an account. |
| 126 | +// |
| 127 | +// API reference: https://api.cloudflare.com/#pages-project-get-projects |
| 128 | +func (api *API) ListPagesProjects(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]PagesProject, ResultInfo, error) { |
| 129 | + v := url.Values{} |
| 130 | + if pageOpts.PerPage > 0 { |
| 131 | + v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) |
| 132 | + } |
| 133 | + if pageOpts.Page > 0 { |
| 134 | + v.Set("page", strconv.Itoa(pageOpts.Page)) |
| 135 | + } |
| 136 | + |
| 137 | + uri := fmt.Sprintf("/accounts/%s/pages/projects", accountID) |
| 138 | + if len(v) > 0 { |
| 139 | + uri = fmt.Sprintf("%s?%s", uri, v.Encode()) |
| 140 | + } |
| 141 | + |
| 142 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 143 | + if err != nil { |
| 144 | + return []PagesProject{}, ResultInfo{}, err |
| 145 | + } |
| 146 | + var r pagesProjectListResponse |
| 147 | + err = json.Unmarshal(res, &r) |
| 148 | + if err != nil { |
| 149 | + return []PagesProject{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError) |
| 150 | + } |
| 151 | + return r.Result, r.ResultInfo, nil |
| 152 | +} |
| 153 | + |
| 154 | +// PagesProject returns a single Pages project by name. |
| 155 | +// |
| 156 | +// API reference: https://api.cloudflare.com/#pages-project-get-project |
| 157 | +func (api *API) PagesProject(ctx context.Context, accountID, projectName string) (PagesProject, error) { |
| 158 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s", accountID, projectName) |
| 159 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 160 | + if err != nil { |
| 161 | + return PagesProject{}, err |
| 162 | + } |
| 163 | + var r pagesProjectResponse |
| 164 | + err = json.Unmarshal(res, &r) |
| 165 | + if err != nil { |
| 166 | + return PagesProject{}, errors.Wrap(err, errUnmarshalError) |
| 167 | + } |
| 168 | + return r.Result, nil |
| 169 | +} |
| 170 | + |
| 171 | +// CreatePagesProject creates a new Pages project in an account. |
| 172 | +// |
| 173 | +// API reference: https://api.cloudflare.com/#pages-project-create-project |
| 174 | +func (api *API) CreatePagesProject(ctx context.Context, accountID string, pagesProject PagesProject) (PagesProject, error) { |
| 175 | + uri := fmt.Sprintf("/accounts/%s/pages/projects", accountID) |
| 176 | + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, pagesProject) |
| 177 | + if err != nil { |
| 178 | + return PagesProject{}, err |
| 179 | + } |
| 180 | + var r pagesProjectResponse |
| 181 | + err = json.Unmarshal(res, &r) |
| 182 | + if err != nil { |
| 183 | + return PagesProject{}, errors.Wrap(err, errUnmarshalError) |
| 184 | + } |
| 185 | + return r.Result, nil |
| 186 | +} |
| 187 | + |
| 188 | +// UpdatePagesProject updates an existing Pages project. |
| 189 | +// |
| 190 | +// API reference: https://api.cloudflare.com/#pages-project-update-project |
| 191 | +func (api *API) UpdatePagesProject(ctx context.Context, accountID, projectName string, pagesProject PagesProject) (PagesProject, error) { |
| 192 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s", accountID, projectName) |
| 193 | + res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, pagesProject) |
| 194 | + if err != nil { |
| 195 | + return PagesProject{}, err |
| 196 | + } |
| 197 | + var r pagesProjectResponse |
| 198 | + err = json.Unmarshal(res, &r) |
| 199 | + if err != nil { |
| 200 | + return PagesProject{}, errors.Wrap(err, errUnmarshalError) |
| 201 | + } |
| 202 | + return r.Result, nil |
| 203 | +} |
| 204 | + |
| 205 | +// DeletePagesProject deletes a Pages project by name. |
| 206 | +// |
| 207 | +// API reference: https://api.cloudflare.com/#pages-project-delete-project |
| 208 | +func (api *API) DeletePagesProject(ctx context.Context, accountID, projectName string) error { |
| 209 | + uri := fmt.Sprintf("/accounts/%s/pages/projects/%s", accountID, projectName) |
| 210 | + res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) |
| 211 | + if err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + var r pagesProjectResponse |
| 215 | + err = json.Unmarshal(res, &r) |
| 216 | + if err != nil { |
| 217 | + return errors.Wrap(err, errUnmarshalError) |
| 218 | + } |
| 219 | + return nil |
| 220 | +} |
0 commit comments