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

Add Placement to Pages & Workers #1279

Merged
merged 2 commits into from
May 17, 2023

Conversation

WalshyDev
Copy link
Member

@WalshyDev WalshyDev commented May 9, 2023

Description

We're adding Smart Placement to Pages & Workers. This adds support for setting Smart Placement.

Has your change been tested?

Pages

Code:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/cloudflare/cloudflare-go"
)

func main() {
	accountId := os.Getenv("ACCOUNT_ID")
	apiToken := os.Getenv("API_TOKEN")
	projectName := "terraform-test"

	if accountId == "" || apiToken == "" {
		log.Fatal("account ID or API token not set!")
	}

	// api, err := cloudflare.NewWithAPIToken(apiToken, cloudflare.Debug(true))
	api, err := cloudflare.NewWithAPIToken(apiToken)
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()

	test, err := api.PagesProject(ctx, accountId, projectName)
	if err == nil && test.ID != "" {
		fmt.Println("Cleaning up from old run...")
		err = api.DeletePagesProject(ctx, accountId, projectName)
		if err != nil {
			log.Fatal(err)
		}
	}

	fmt.Println("Creating project...")
	proj, err := api.CreatePagesProject(ctx, accountId, cloudflare.PagesProject{
		Name:             projectName,
		ProductionBranch: "main",
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Project created! Id:", proj.ID)
	fmt.Println("Placement:", proj.DeploymentConfigs.Production.Placement)

	// Set placement to smart
	fmt.Println("Enabling smart placement...")
	proj, err = api.UpdatePagesProject(ctx, accountId, projectName, cloudflare.PagesProject{
		DeploymentConfigs: cloudflare.PagesProjectDeploymentConfigs{
			Production: cloudflare.PagesProjectDeploymentConfigEnvironment{
				Placement: &cloudflare.Placement{
					Mode: cloudflare.PlacementModeSmart,
				},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Validate
	fmt.Println("Placement:", proj.DeploymentConfigs.Production.Placement)
	if proj.DeploymentConfigs.Production.Placement.Mode != "smart" {
		log.Fatal("PLACEMENT MODE WASN'T SMART!")
	}
	fmt.Println("Validated smart placement!")

	fmt.Println("Doing unrelated update...")
	// Do an unrelated update and validate again
	proj, err = api.UpdatePagesProject(ctx, accountId, projectName, cloudflare.PagesProject{
		DeploymentConfigs: cloudflare.PagesProjectDeploymentConfigs{
			Production: cloudflare.PagesProjectDeploymentConfigEnvironment{
				KvNamespaces: cloudflare.NamespaceBindingMap{
					"KV": &cloudflare.NamespaceBindingValue{
						Value: "23659b4cab594caf9df915ff59b8b30a",
					},
				},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Validate
	fmt.Println("Placement:", proj.DeploymentConfigs.Production.Placement)
	if proj.DeploymentConfigs.Production.Placement.Mode != "smart" {
		log.Fatal("PLACEMENT MODE WASN'T SMART!")
	}
	fmt.Println("Validated smart placement again :)")

	fmt.Println("Disabling smart placement...")
	// Disable placement
	proj, err = api.UpdatePagesProject(ctx, accountId, projectName, cloudflare.PagesProject{
		DeploymentConfigs: cloudflare.PagesProjectDeploymentConfigs{
			Production: cloudflare.PagesProjectDeploymentConfigEnvironment{
				Placement: &cloudflare.Placement{
					Mode: cloudflare.PlacementModeOff,
				},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Placement:", proj.DeploymentConfigs.Production.Placement)
	if proj.DeploymentConfigs.Production.Placement != nil && proj.DeploymentConfigs.Production.Placement.Mode != cloudflare.PlacementModeOff {
		log.Fatal("PLACEMENT MODE WAS SMART!")
	}
	fmt.Println("Validated placement is disabled!")

	// Cleanup
	fmt.Println("Cleaning up...")
	err = api.DeletePagesProject(ctx, accountId, projectName)
	if err != nil {
		log.Fatal(err)
	}
}

Result:

Creating project...
Project created! Id: 307bafc6-822d-4884-97e4-97aa92422381
Placement: <nil>
Enabling smart placement...
Placement: &{smart}
Validated smart placement!
Doing unrelated update...
Placement: &{smart}
Validated smart placement again :)
Disabling smart placement...
Placement: &{}
Validated placement is disabled!
Cleaning up...

Workers

Code:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/cloudflare/cloudflare-go"
)

func main() {
	accountId := os.Getenv("ACCOUNT_ID")
	apiToken := os.Getenv("API_TOKEN")
	scriptName := "terraform-test"
	script := `export default {
		fetch(req) {
			return new Response('Hello, World!');
		}
	}`

	if accountId == "" || apiToken == "" {
		log.Fatal("account ID or API token not set!")
	}

	// api, err := cloudflare.NewWithAPIToken(apiToken, cloudflare.Debug(true))
	api, err := cloudflare.NewWithAPIToken(apiToken)
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()

	test, err := api.GetWorker(ctx, cloudflare.AccountIdentifier(accountId), scriptName)
	if err == nil && test.ID != "" {
		fmt.Println("Cleaning up from old run...")
		err = api.DeleteWorker(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.DeleteWorkerParams{
			ScriptName: scriptName,
		})
		if err != nil {
			log.Fatal(err)
		}
	}

	fmt.Println("Creating worker...")
	worker, err := api.UploadWorker(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.CreateWorkerParams{
		ScriptName: scriptName,
		Script:     script,
		Module:     true,
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Worker created! Deployment ID:", worker.DeploymentId)
	fmt.Println("Placement:", worker.PlacementMode)

	// Set placement to smart
	fmt.Println("Enabling smart placement...")
	worker, err = api.UploadWorker(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.CreateWorkerParams{
		ScriptName: scriptName,
		Script:     script,
		Module:     true,
		Placement: &cloudflare.Placement{
			Mode: cloudflare.PlacementModeSmart,
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Validate
	fmt.Println("Placement:", *worker.PlacementMode)
	if *worker.PlacementMode != cloudflare.PlacementModeSmart {
		log.Fatal("PLACEMENT MODE WASN'T SMART!")
	}
	fmt.Println("Validated smart placement!")

	fmt.Println("Disabling smart placement...")
	// Disable placement
	worker, err = api.UploadWorker(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.CreateWorkerParams{
		ScriptName: scriptName,
		Script:     script,
		Module:     true,
		Placement: &cloudflare.Placement{
			Mode: cloudflare.PlacementModeOff,
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Placement:", worker.PlacementMode)
	if worker.PlacementMode != nil && *worker.PlacementMode != cloudflare.PlacementModeOff {
		log.Fatal("PLACEMENT MODE WAS SMART!")
	}
	fmt.Println("Validated placement is disabled!")

	// Cleanup
	fmt.Println("Cleaning up...")
	err = api.DeleteWorker(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.DeleteWorkerParams{ScriptName: scriptName})
	if err != nil {
		log.Fatal(err)
	}
}

Result:

Creating worker...
Worker created! Deployment ID: 0x140002b80d0
Placement: <nil>
Enabling smart placement...
Placement: smart
Validated smart placement!
Disabling smart placement...
Placement: <nil>
Validated placement is disabled!
Cleaning up...

Types of changes

What sort of change does your code introduce/modify?

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.

@github-actions
Copy link
Contributor

github-actions bot commented May 9, 2023

changelog detected ✅

@codecov-commenter
Copy link

Codecov Report

Merging #1279 (536b53c) into master (3508050) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1279   +/-   ##
=======================================
  Coverage   49.07%   49.07%           
=======================================
  Files         133      133           
  Lines       13032    13034    +2     
=======================================
+ Hits         6395     6397    +2     
  Misses       5143     5143           
  Partials     1494     1494           
Impacted Files Coverage Δ
pages_project.go 50.81% <ø> (ø)
rulesets.go 34.64% <ø> (ø)
workers.go 59.01% <100.00%> (+0.45%) ⬆️

@WalshyDev WalshyDev marked this pull request as ready for review May 9, 2023 02:16
@WalshyDev
Copy link
Member Author

Docs are in prod now

@jacobbednarz jacobbednarz merged commit c6d6f07 into cloudflare:master May 17, 2023
@github-actions github-actions bot added this to the v0.68.0 milestone May 17, 2023
github-actions bot pushed a commit that referenced this pull request May 17, 2023
@github-actions
Copy link
Contributor

This functionality has been released in v0.68.0.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@WalshyDev WalshyDev deleted the walshy/PAGES-2606 branch May 23, 2023 23:36
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants