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

Pages updates #2083

Merged
merged 13 commits into from Jan 10, 2023
3 changes: 3 additions & 0 deletions .changelog/2083.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_pages_project: Updates Pages project resource to support API changes
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
```
40 changes: 37 additions & 3 deletions docs/resources/pages_project.md
Expand Up @@ -148,37 +148,71 @@ Optional:
<a id="nestedblock--deployment_configs"></a>
### Nested Schema for `deployment_configs`

Optional:
Required:

- `preview` (Block List, Max: 1) Configuration for preview deploys. (see [below for nested schema](#nestedblock--deployment_configs--preview))
- `production` (Block List, Max: 1) Configuration for production deploys. (see [below for nested schema](#nestedblock--deployment_configs--production))
- `preview` (Block List, Min: 1, Max: 1) Configuration for preview deploys. (see [below for nested schema](#nestedblock--deployment_configs--preview))
- `production` (Block List, Min: 1, Max: 1) Configuration for production deploys. (see [below for nested schema](#nestedblock--deployment_configs--production))

<a id="nestedblock--deployment_configs--preview"></a>
### Nested Schema for `deployment_configs.preview`

Optional:

- `always_use_latest_compatibility_date` (Boolean) Use latest compatibility date for Pages Functions. Defaults to `false`.
- `compatibility_date` (String) Compatibility date used for Pages Functions.
- `compatibility_flags` (List of String) Compatibility flags used for Pages Functions.
- `d1_databases` (Map of String) D1 Databases used for Pages Functions.
- `durable_object_namespaces` (Map of String) Durable Object namespaces used for Pages Functions.
- `environment_variables` (Map of String) Environment variables for Pages Functions.
- `fail_open` (Boolean) Fail open used for Pages Functions. Defaults to `false`.
- `kv_namespaces` (Map of String) KV namespaces used for Pages Functions.
- `r2_buckets` (Map of String) R2 Buckets used for Pages Functions.
- `service_binding` (Block Set) Services used for Pages Functions. (see [below for nested schema](#nestedblock--deployment_configs--preview--service_binding))
- `usage_model` (String) Usage model used for Pages Functions. Defaults to `bundled`.

<a id="nestedblock--deployment_configs--preview--service_binding"></a>
### Nested Schema for `deployment_configs.preview.service_binding`

Required:

- `name` (String) The global variable for the binding in your Worker code.
- `service` (String) The name of the Worker to bind to.

Optional:

- `environment` (String) The name of the Worker environment to bind to.



<a id="nestedblock--deployment_configs--production"></a>
### Nested Schema for `deployment_configs.production`

Optional:

- `always_use_latest_compatibility_date` (Boolean) Use latest compatibility date for Pages Functions. Defaults to `false`.
- `compatibility_date` (String) Compatibility date used for Pages Functions.
- `compatibility_flags` (List of String) Compatibility flags used for Pages Functions.
- `d1_databases` (Map of String) D1 Databases used for Pages Functions.
- `durable_object_namespaces` (Map of String) Durable Object namespaces used for Pages Functions.
- `environment_variables` (Map of String) Environment variables for Pages Functions.
- `fail_open` (Boolean) Fail open used for Pages Functions. Defaults to `false`.
- `kv_namespaces` (Map of String) KV namespaces used for Pages Functions.
- `r2_buckets` (Map of String) R2 Buckets used for Pages Functions.
- `service_binding` (Block Set) Services used for Pages Functions. (see [below for nested schema](#nestedblock--deployment_configs--production--service_binding))
- `usage_model` (String) Usage model used for Pages Functions. Defaults to `bundled`.

<a id="nestedblock--deployment_configs--production--service_binding"></a>
### Nested Schema for `deployment_configs.production.service_binding`

Required:

- `name` (String) The global variable for the binding in your Worker code.
- `service` (String) The name of the Worker to bind to.

Optional:

- `environment` (String) The name of the Worker environment to bind to.




Expand Down
79 changes: 55 additions & 24 deletions internal/provider/resource_cloudflare_pages_project.go
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -34,15 +33,19 @@ func resourceCloudflarePagesProject() *schema.Resource {
func buildDeploymentConfig(environment interface{}) cloudflare.PagesProjectDeploymentConfigEnvironment {
config := cloudflare.PagesProjectDeploymentConfigEnvironment{}
parsed := environment.(map[string]interface{})
deploymentVariables := cloudflare.EnvironmentVariableMap{}
for key, value := range parsed {
switch key {
case "environment_variables":
deploymentVariables := cloudflare.EnvironmentVariableMap{}
variables := value.(map[string]interface{})
for i, variable := range variables {
deploymentVariables[i] = &cloudflare.EnvironmentVariable{Value: variable.(string)}
envVar := cloudflare.EnvironmentVariable{
Value: variable.(string),
Type: cloudflare.PlainText,
}
deploymentVariables[i] = &envVar
}
config.EnvVars = deploymentVariables

break
case "kv_namespaces":
namespace := cloudflare.NamespaceBindingMap{}
Expand Down Expand Up @@ -84,21 +87,48 @@ func buildDeploymentConfig(environment interface{}) cloudflare.PagesProjectDeplo
config.CompatibilityFlags = append(config.CompatibilityFlags, item.(string))
}
break
case "fail_open":
config.FailOpen = value.(bool)
break
case "always_use_latest_compatibility_date":
config.AlwaysUseLatestCompatibilityDate = value.(bool)
break
case "usage_model":
config.UsageModel = cloudflare.UsageModel(value.(string))
break
case "service_binding":
serviceMap := cloudflare.ServiceBindingMap{}
for _, item := range value.(*schema.Set).List() {
data := item.(map[string]interface{})
serviceMap[data["name"].(string)] = &cloudflare.ServiceBinding{
Service: data["service"].(string),
Environment: data["environment"].(string),
}
}
config.ServiceBindings = serviceMap
break
}
}

config.EnvVars = deploymentVariables
return config
}

func parseDeployementConfig(deployment cloudflare.PagesProjectDeploymentConfigEnvironment) (returnValue []map[string]interface{}) {
func parseDeploymentConfig(deployment cloudflare.PagesProjectDeploymentConfigEnvironment) (returnValue []map[string]interface{}) {
config := make(map[string]interface{})

config["compatibility_date"] = deployment.CompatibilityDate
config["compatibility_flags"] = deployment.CompatibilityFlags

config["fail_open"] = deployment.FailOpen
config["always_use_latest_compatibility_date"] = deployment.AlwaysUseLatestCompatibilityDate
config["usage_model"] = deployment.UsageModel

deploymentVars := map[string]string{}
//secretsBinding := &schema.Set{F: schema.HashResource(secretTextBindingResource)}
for key, value := range deployment.EnvVars {
deploymentVars[key] = value.Value
if value.Type == cloudflare.PlainText {
deploymentVars[key] = value.Value
}
}
config["environment_variables"] = deploymentVars

Expand Down Expand Up @@ -126,6 +156,16 @@ func parseDeployementConfig(deployment cloudflare.PagesProjectDeploymentConfigEn
}
config["d1_databases"] = deploymentVars

serviceBindings := &schema.Set{F: schema.HashResource(serviceBindingResource)}
for key, value := range deployment.ServiceBindings {
serviceBindings.Add(map[string]interface{}{
"name": key,
"service": value.Service,
"environment": value.Environment,
})
}
config["service_binding"] = serviceBindings

returnValue = append(returnValue, config)
return
}
Expand Down Expand Up @@ -228,7 +268,7 @@ func resourceCloudflarePagesProjectRead(ctx context.Context, d *schema.ResourceD
d.Set("created_on", project.CreatedOn.Format(time.RFC3339))

if project.Source != nil {
source := []map[string]interface{}{}
var source []map[string]interface{}
source = append(source, map[string]interface{}{
"type": project.Source.Type,
"config": []map[string]interface{}{
Expand All @@ -250,7 +290,7 @@ func resourceCloudflarePagesProjectRead(ctx context.Context, d *schema.ResourceD
}
emptyProjectBuildConfig := cloudflare.PagesProjectBuildConfig{}
if project.BuildConfig != emptyProjectBuildConfig {
buildConfig := []map[string]interface{}{}
var buildConfig []map[string]interface{}
buildConfig = append(buildConfig, map[string]interface{}{
"build_command": project.BuildConfig.BuildCommand,
"destination_dir": project.BuildConfig.DestinationDir,
Expand All @@ -262,21 +302,12 @@ func resourceCloudflarePagesProjectRead(ctx context.Context, d *schema.ResourceD
d.Set("build_config", buildConfig)
}

emptyDeploymentConfig := cloudflare.PagesProjectDeploymentConfigs{}
if !reflect.DeepEqual(project.DeploymentConfigs, emptyDeploymentConfig) {
deploymentConfigs := []map[string]interface{}{}
deploymentConfig := make(map[string]interface{})
emptyDeploymentEnviroment := cloudflare.PagesProjectDeploymentConfigEnvironment{}
if !reflect.DeepEqual(project.DeploymentConfigs.Preview, emptyDeploymentEnviroment) {
deploymentConfig["preview"] = parseDeployementConfig(project.DeploymentConfigs.Preview)
}

if !reflect.DeepEqual(project.DeploymentConfigs.Production, emptyDeploymentEnviroment) {
deploymentConfig["production"] = parseDeployementConfig(project.DeploymentConfigs.Production)
}
deploymentConfigs = append(deploymentConfigs, deploymentConfig)
d.Set("deployment_configs", deploymentConfigs)
}
var deploymentConfigs []map[string]interface{}
deploymentConfig := make(map[string]interface{})
deploymentConfig["preview"] = parseDeploymentConfig(project.DeploymentConfigs.Preview)
deploymentConfig["production"] = parseDeploymentConfig(project.DeploymentConfigs.Production)
deploymentConfigs = append(deploymentConfigs, deploymentConfig)
d.Set("deployment_configs", deploymentConfigs)

return nil
}
Expand Down