Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Functions GA #1136

Merged
merged 2 commits into from Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/1136.txt
@@ -0,0 +1,7 @@
```release-note:enhancement
pages: Updates bindings and other Functions related propreties. Service bindings, secrets, fail open/close and usage model are all now supported.
```

```release-note:breaking-change
pages: Changed the type of EnvVars in PagesProjectDeploymentConfigEnvironment & PagesProjectDeployment in order to properly support secrets.
```
65 changes: 3 additions & 62 deletions pages_deployment_test.go
Expand Up @@ -91,54 +91,6 @@ const (
"aliases": null
}`

testPagesDeploymentStageLogsResponse = `
{
"name": "build",
"started_on": "2021-01-01T00:00:00Z",
"ended_on": "2021-01-01T00:00:00Z",
"status": "success",
"start": 0,
"end": 5,
"total": 6,
"data": [
{
"id": 0,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Installing dependencies"
},
{
"id": 1,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Verify run directory"
},
{
"id": 2,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Executing user command: bash test.sh"
},
{
"id": 3,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Finished"
},
{
"id": 4,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Building functions..."
},
{
"id": 5,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Validating asset output directory"
},
{
"id": 6,
"timestamp": "2021-01-01T00:00:00Z",
"message": "Parsed 2 valid header rules."
}
]
}`

testPagesDeploymentLogsResponse = `
{
"total": 6,
Expand Down Expand Up @@ -190,9 +142,9 @@ var (
ModifiedOn: &pagesDeploymentDummyTime,
Aliases: nil,
LatestStage: *expectedPagesDeploymentLatestStage,
EnvVars: map[string]map[string]string{
"NODE_VERSION": {
"value": "16",
EnvVars: EnvironmentVariableMap{
"NODE_VERSION": &EnvironmentVariable{
Value: "16",
},
},
DeploymentTrigger: PagesProjectDeploymentTrigger{
Expand Down Expand Up @@ -255,17 +207,6 @@ var (
Status: "success",
}

expectedPagesDeploymentStageLogs = &PagesDeploymentStageLogs{
Name: "build",
StartedOn: &pagesDeploymentDummyTime,
EndedOn: &pagesDeploymentDummyTime,
Status: "success",
Start: 0,
End: 5,
Total: 6,
Data: expectedPagesDeploymentStageLogEntries,
}

expectedPagesDeploymentStageLogEntries = []PagesDeploymentStageLogEntry{
{
ID: 0,
Expand Down
61 changes: 48 additions & 13 deletions pages_project.go
Expand Up @@ -67,18 +67,17 @@ type PagesProjectDeploymentConfigs struct {

// PagesProjectDeploymentConfigEnvironment represents the configuration for preview or production deploys.
type PagesProjectDeploymentConfigEnvironment struct {
EnvVars map[string]PagesProjectDeploymentVar `json:"env_vars"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
KvNamespaces NamespaceBindingMap `json:"kv_namespaces,omitempty"`
DoNamespaces NamespaceBindingMap `json:"durable_object_namespaces,omitempty"`
D1Databases D1BindingMap `json:"d1_databases,omitempty"`
R2Bindings R2BindingMap `json:"r2_buckets,omitempty"`
}

// PagesProjectDeploymentVar represents a deployment environment variable.
type PagesProjectDeploymentVar struct {
Value string `json:"value"`
EnvVars EnvironmentVariableMap `json:"env_vars,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a typed language, this is a bit of a code smell. instead of including the type (map) in the name, could we call it EnvVars that expects a []map[string]EnvironmentVariable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use these types internally, so I'd personally like to keep this consistent. I can change if you really want though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, let's keep them for consistency but the only caveat is once we start looking at codegen'ing the library, this will be changed again to be the concrete type of map[string]*EnvironmentVariable so we'll be inconsistent there once that happens.

KvNamespaces NamespaceBindingMap `json:"kv_namespaces,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar with these types. as we are breaking changes already, can we fix these up?

DoNamespaces NamespaceBindingMap `json:"durable_object_namespaces,omitempty"`
D1Databases D1BindingMap `json:"d1_databases,omitempty"`
R2Bindings R2BindingMap `json:"r2_buckets,omitempty"`
ServiceBindings ServiceBindingMap `json:"services,omitempty"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
FailOpen bool `json:"fail_open"`
AlwaysUseLatestCompatibilityDate bool `json:"always_use_latest_compatibility_date"`
UsageModel UsageModel `json:"usage_model,omitempty"`
}

// PagesProjectDeployment represents a deployment to a Pages project.
Expand All @@ -93,13 +92,20 @@ type PagesProjectDeployment struct {
ModifiedOn *time.Time `json:"modified_on"`
Aliases []string `json:"aliases,omitempty"`
LatestStage PagesProjectDeploymentStage `json:"latest_stage"`
EnvVars map[string]map[string]string `json:"env_vars"`
EnvVars EnvironmentVariableMap `json:"env_vars"`
KvNamespaces NamespaceBindingMap `json:"kv_namespaces,omitempty"`
DoNamespaces NamespaceBindingMap `json:"durable_object_namespaces,omitempty"`
D1Databases D1BindingMap `json:"d1_databases,omitempty"`
R2Bindings R2BindingMap `json:"r2_buckets,omitempty"`
ServiceBindings ServiceBindingMap `json:"services,omitempty"`
DeploymentTrigger PagesProjectDeploymentTrigger `json:"deployment_trigger"`
Stages []PagesProjectDeploymentStage `json:"stages"`
BuildConfig PagesProjectBuildConfig `json:"build_config"`
Source PagesProjectSource `json:"source"`
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
UsageModel UsageModel `json:"usage_model,omitempty"`
IsSkipped bool `json:"is_skipped"`
ProductionBranch string `json:"production_branch,omitempty"`
}

Expand Down Expand Up @@ -135,6 +141,21 @@ type pagesProjectListResponse struct {
ResultInfo `json:"result_info"`
}

type EnvironmentVariableMap map[string]*EnvironmentVariable

// PagesProjectDeploymentVar represents a deployment environment variable.
type EnvironmentVariable struct {
Value string `json:"value"`
Type EnvVarType `json:"type"`
}

type EnvVarType string

const (
PlainText EnvVarType = "plain_text"
SecretText EnvVarType = "secret_text"
)

type NamespaceBindingMap map[string]*NamespaceBindingValue

type NamespaceBindingValue struct {
Expand All @@ -153,6 +174,20 @@ type D1Binding struct {
ID string `json:"id"`
}

type ServiceBindingMap map[string]*ServiceBinding

type ServiceBinding struct {
Service string `json:"service"`
Environment string `json:"environment"`
}

type UsageModel string

const (
Bundled UsageModel = "bundled"
Unbound UsageModel = "unbound"
)

// ListPagesProjects returns all Pages projects for an account.
//
// API reference: https://api.cloudflare.com/#pages-project-get-projects
Expand Down
90 changes: 72 additions & 18 deletions pages_project_test.go
Expand Up @@ -58,7 +58,11 @@ const (
},
"ENV": {
"value": "preview"
}
},
"API_KEY": {
"value": "",
"type": "secret_text"
}
},
"compatibility_date": "2022-08-15",
"compatibility_flags": ["preview_flag"]
Expand All @@ -70,7 +74,11 @@ const (
},
"ENV": {
"value": "production"
}
},
"API_KEY": {
"value": "",
"type": "secret_text"
}
},
"d1_databases": {
"D1_BINDING": {
Expand All @@ -92,8 +100,17 @@ const (
"name": "some-bucket"
}
},
"services": {
"SERVICE_BINDING": {
"service": "some-worker",
"environment": "production"
}
},
"compatibility_date": "2022-08-15",
"compatibility_flags": ["production_flag"]
"compatibility_flags": ["production_flag"],
"fail_open": false,
"always_use_latest_compatibility_date": false,
"usage_model": "bundled"
}
},
"latest_deployment": {
Expand Down Expand Up @@ -121,10 +138,17 @@ const (
},
"ENV": {
"value": "STAGING"
}
},
"API_KEY": {
"value": "",
"type": "secret_text"
}
},
"compatibility_date": "2022-08-15",
"compatibility_flags": ["deployment_flag"],
"fail_open": false,
"always_use_latest_compatibility_date": false,
"usage_model": "bundled",
"deployment_trigger": {
"type": "ad_hoc",
"metadata": {
Expand Down Expand Up @@ -201,10 +225,18 @@ const (
},
"ENV": {
"value": "STAGING"
}
},
"API_KEY": {
"value": "",
"type": "secret_text"
}
},
"compatibility_date": "2022-08-15",
"compatibility_flags": ["deployment_flag"],
"fail_open": false,
"always_use_latest_compatibility_date": false,
"build_image_major_version": 1,
"usage_model": "bundled",
"deployment_trigger": {
"type": "ad_hoc",
"metadata": {
Expand Down Expand Up @@ -295,16 +327,21 @@ var (
"https://branchname.pages-test.pages.dev",
},
LatestStage: *expectedPagesProjectLatestDeploymentStage,
EnvVars: map[string]map[string]string{
"BUILD_VERSION": {
"value": "1.2",
EnvVars: EnvironmentVariableMap{
"BUILD_VERSION": &EnvironmentVariable{
Value: "1.2",
},
"ENV": &EnvironmentVariable{
Value: "STAGING",
},
"ENV": {
"value": "STAGING",
"API_KEY": &EnvironmentVariable{
Value: "",
Type: SecretText,
},
},
CompatibilityFlags: []string{"deployment_flag"},
CompatibilityDate: "2022-08-15",
UsageModel: Bundled,
DeploymentTrigger: *expectedPagesProjectDeploymentTrigger,
Stages: expectedStages,
BuildConfig: *expectedPagesProjectBuildConfig,
Expand Down Expand Up @@ -363,29 +400,35 @@ var (
}

expectedPagesProjectDeploymentConfigPreview = &PagesProjectDeploymentConfigEnvironment{
EnvVars: map[string]PagesProjectDeploymentVar{
"BUILD_VERSION": {
EnvVars: EnvironmentVariableMap{
"BUILD_VERSION": &EnvironmentVariable{
Value: "1.2",
},
"ENV": {
"ENV": &EnvironmentVariable{
Value: "preview",
},
"API_KEY": &EnvironmentVariable{
Value: "",
Type: SecretText,
},
},
CompatibilityDate: "2022-08-15",
CompatibilityFlags: []string{"preview_flag"},
}

expectedPagesProjectDeploymentConfigProduction = &PagesProjectDeploymentConfigEnvironment{
EnvVars: map[string]PagesProjectDeploymentVar{
"BUILD_VERSION": {
EnvVars: EnvironmentVariableMap{
"BUILD_VERSION": &EnvironmentVariable{
Value: "1.2",
},
"ENV": {
"ENV": &EnvironmentVariable{
Value: "production",
},
"API_KEY": &EnvironmentVariable{
Value: "",
Type: SecretText,
},
},
CompatibilityDate: "2022-08-15",
CompatibilityFlags: []string{"production_flag"},
KvNamespaces: NamespaceBindingMap{
"KV_BINDING": &NamespaceBindingValue{Value: "5eb63bbbe01eeed093cb22bb8f5acdc3"},
},
Expand All @@ -398,6 +441,17 @@ var (
R2Bindings: R2BindingMap{
"R2_BINDING": &R2BindingValue{Name: "some-bucket"},
},
ServiceBindings: ServiceBindingMap{
"SERVICE_BINDING": &ServiceBinding{
Service: "some-worker",
Environment: "production",
},
},
CompatibilityDate: "2022-08-15",
CompatibilityFlags: []string{"production_flag"},
FailOpen: false,
AlwaysUseLatestCompatibilityDate: false,
UsageModel: Bundled,
}

expectedPagesProjectSource = &PagesProjectSource{
Expand Down