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 support for specifying location when creating KV stores #834

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/kvstore.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ $ terraform import fastly_kvstore.example xxxxxxxxxxxxxxxxxxxx
### Optional

- `force_destroy` (Boolean) Allow the KV Store to be deleted, even if it contains entries. Defaults to false.
- `location` (String) The regional location of the KV Store. Valid values are `US`, `EU`, `ASIA`, and `AUS`.

### Read-Only

Expand Down
15 changes: 15 additions & 0 deletions fastly/resource_fastly_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
gofastly "github.com/fastly/go-fastly/v9/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceFastlyKVStore() *schema.Resource {
Expand All @@ -27,6 +28,16 @@ func resourceFastlyKVStore() *schema.Resource {
Optional: true,
Description: "Allow the KV Store to be deleted, even if it contains entries. Defaults to false.",
},
// TODO: Move values to constants inside of go-fastly.
"location": {
Integralist marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Description: "The regional location of the KV Store. Valid values are `US`, `EU`, `ASIA`, and `AUS`.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(
[]string{"US", "EU", "ASIA", "AUS"},
false,
)),
},
"name": {
Type: schema.TypeString,
Required: true,
Expand All @@ -44,6 +55,10 @@ func resourceFastlyKVStoreCreate(_ context.Context, d *schema.ResourceData, meta
Name: d.Get("name").(string),
}

if v, ok := d.GetOk("location"); ok {
input.Location = v.(string)
}

log.Printf("[DEBUG] CREATE: KV Store input: %#v", input)

store, err := conn.CreateKVStore(input)
Expand Down
139 changes: 139 additions & 0 deletions fastly/resource_fastly_kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,54 @@ func TestAccFastlyKVStore_validate(t *testing.T) {
})
}

func TestAccFastlyKVStore_WithLocation_validate(t *testing.T) {
var (
kvStore gofastly.KVStore
service gofastly.ServiceDetail
)
kvStoreName := fmt.Sprintf("KV Store %s", acctest.RandString(10))
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
domainName := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))
linkName := fmt.Sprintf("resource_link_%s", acctest.RandString(10))
location := "EU"
Integralist marked this conversation as resolved.
Show resolved Hide resolved

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckServiceVCLDestroy,
Steps: []resource.TestStep{
{
Config: testAccKVStoreConfigWithLocation(kvStoreName, serviceName, domainName, linkName, location),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_compute.with_location", &service),
testAccCheckFastlyKVStoreRemoteState(&service, &kvStore, serviceName, kvStoreName, linkName),
),
},
{
ResourceName: "fastly_kvstore.with_location",
ImportState: true,
ImportStateVerify: true,
// Attributes not stored on the Fastly API are ignored.
// "location" should be validated if the API starts returning it in the response.
ImportStateVerifyIgnore: []string{"activate", "force_destroy", "package.0.filename", "imported", "location"},
},
// IMPORTANT: Add a key to the store so we can validate force delete.
{
Config: testAccKVStoreConfigWithLocation(kvStoreName, serviceName, domainName, linkName, location),
Check: testAccAddKVStoreItems(&kvStore), // triggers side-effect of adding a KV Store key/value
},
{
Config: testAccKVStoreConfigWithLocationDeleteStep1(kvStoreName, serviceName, domainName, location),
},
{
Config: testAccKVStoreConfigWithLocationDeleteStep2(serviceName, domainName),
},
},
})
}

func testAccCheckFastlyKVStoreRemoteState(service *gofastly.ServiceDetail, kvStore *gofastly.KVStore, serviceName, kvStoreName, linkName string) resource.TestCheckFunc {
return func(_ *terraform.State) error {
if gofastly.ToValue(service.Name) != serviceName {
Expand Down Expand Up @@ -192,6 +240,97 @@ data "fastly_package_hash" "example" {
`, serviceName, domainName)
}

func testAccKVStoreConfigWithLocation(kvStoreName, serviceName, domainName, linkName, location string) string {
return fmt.Sprintf(`
resource "fastly_kvstore" "with_location" {
name = "%s"
location = "%s"
force_destroy = true
}

resource "fastly_service_compute" "with_location" {
name = "%s"

domain {
name = "%s"
}

package {
filename = "test_fixtures/package/valid.tar.gz"
source_code_hash = data.fastly_package_hash.example.hash
}

resource_link {
name = "%s"
resource_id = fastly_kvstore.with_location.id
}

force_destroy = true
}

data "fastly_package_hash" "example" {
filename = "./test_fixtures/package/valid.tar.gz"
}
`, kvStoreName, location, serviceName, domainName, linkName)
}

// IMPORTANT: Deleting a KV Store requires first deleting its resource_link.
// This requires a two-step `terraform apply` as we can't guarantee deletion order.
// e.g. resource_link deletion within fastly_service_compute might not finish first.
func testAccKVStoreConfigWithLocationDeleteStep1(kvStoreName, serviceName, domainName, location string) string {
return fmt.Sprintf(`
resource "fastly_kvstore" "with_location" {
name = "%s"
location = "%s"
force_destroy = true
}

resource "fastly_service_compute" "with_location" {
name = "%s"

domain {
name = "%s"
}

package {
filename = "test_fixtures/package/valid.tar.gz"
source_code_hash = data.fastly_package_hash.example.hash
}

force_destroy = true
}

data "fastly_package_hash" "example" {
filename = "./test_fixtures/package/valid.tar.gz"
}
`, kvStoreName, location, serviceName, domainName)
}

// Step 1 deleted the resource_link first.
// Step 2 will now delete the fastly_kvstore.
func testAccKVStoreConfigWithLocationDeleteStep2(serviceName, domainName string) string {
return fmt.Sprintf(`
resource "fastly_service_compute" "with_location" {
name = "%s"

domain {
name = "%s"
}

package {
filename = "test_fixtures/package/valid.tar.gz"
source_code_hash = data.fastly_package_hash.example.hash
}

force_destroy = true
}

data "fastly_package_hash" "example" {
filename = "./test_fixtures/package/valid.tar.gz"
}
`, serviceName, domainName)
}

// testAccAddKVStoreItems doesn't technically check for anything despite
// returning a TestCheckFunc. Instead it is used for its side effect of adding
// a single KV Store entry so we can later validate we're able to force delete
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/bflad/tfproviderlint v0.29.0
github.com/fastly/go-fastly/v9 v9.2.2
github.com/fastly/go-fastly/v9 v9.3.1
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/fastly/go-fastly/v9 v9.2.2 h1:YNoinvf9vlPmsIbR6weT6XQ0l8nNPjS5DjC7ILwwaE8=
github.com/fastly/go-fastly/v9 v9.2.2/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fastly/go-fastly/v9 v9.3.1 h1:2UZUe8Kkz60aPrkDV82dpHkEqsT/J2qxOZxFhWEJJ2k=
github.com/fastly/go-fastly/v9 v9.3.1/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
Expand Down
28 changes: 17 additions & 11 deletions vendor/github.com/fastly/go-fastly/v9/fastly/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/fastly/go-fastly/v9/fastly/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion vendor/github.com/fastly/go-fastly/v9/fastly/kv_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading