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

feat(domain-v1): support v1 functionality #917

Merged
merged 12 commits into from
Jan 31, 2025
13 changes: 7 additions & 6 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
- name: Restore cache
uses: actions/cache@v3
with:
Expand All @@ -29,7 +29,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.20.x]
go-version: [1.22.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
- name: Generate Docs
run: |
make generate-docs
Expand All @@ -78,7 +78,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
- name: Restore cache
uses: actions/cache@v3
with:
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
- name: Restore cache
uses: actions/cache@v3
with:
Expand Down Expand Up @@ -187,7 +187,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
- name: Restore cache
uses: actions/cache@v3
with:
Expand All @@ -198,6 +198,7 @@ jobs:
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: v2.6.1
install-only: true
- name: Validate Goreleaser
run: make goreleaser GORELEASER_ARGS="--skip=validate --clean --snapshot" # snapshot is needed as local git has no tags
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
- name: Import GPG key
id: import_gpg
uses: fastly/ghaction-import-gpg@v2.1.0
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: v1.21.2 # Last version to support Go 1.20.x
version: v2.6.1
args: release --clean
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
Expand Down
5 changes: 3 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2
before:
hooks:
- go mod tidy
Expand All @@ -24,7 +25,7 @@ builds:
goarch: '386'
binary: '{{ .ProjectName }}_v{{ .Version }}'
archives:
- format: zip
- formats: [zip]
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
Expand All @@ -44,4 +45,4 @@ signs:
release:
draft: true
changelog:
skip: true
disable: true
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ errcheck:
@sh -c "'$(CURDIR)/scripts/errcheck.sh'"

goreleaser-bin:
@# This is the last version of goreleaser that supports Go 1.20.14 (the version used to build the provider)
$(GO_BIN) install github.com/goreleaser/goreleaser@v1.21.2
$(GO_BIN) install github.com/goreleaser/goreleaser/v2@latest

nilaway:
@nilaway ./...

# You can pass flags to goreleaser via GORELEASER_ARGS
# --skip-validate will skip the checks
# --skip=validate will skip the checks
# --clean will save you deleting the dist dir
# --single-target will be quicker and only build for your os & architecture
# e.g.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Requirements

- [Terraform](https://www.terraform.io/downloads.html) 0.12.x or higher
- [Go](https://golang.org/doc/install) 1.20 (to build the provider plugin)
- [Go](https://golang.org/doc/install) 1.22 (to build the provider plugin)

> NOTE: the last version of the Fastly provider to support Terraform 0.11.x and below was [v0.26.0](https://github.com/fastly/terraform-provider-fastly/releases/tag/v0.26.0)

Expand All @@ -30,7 +30,7 @@ $ make build

## Developing the Provider

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.20+ is *required*).
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.22+ is *required*).

To compile the provider, run `make build`. This will build the provider and put the provider binary in a local `bin` directory.

Expand Down
29 changes: 29 additions & 0 deletions docs/resources/domain_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "fastly_domain_v1 Resource - terraform-provider-fastly"
subcategory: ""
description: |-

---

# fastly_domain_v1 (Resource)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `fqdn` (String) The fully-qualified domain name for your domain (e.g. `www.example.com`, no trailing dot). Can be created, but not updated.

### Optional

- `service_id` (String) The service_id associated with your domain or null if there is no association.

### Read-Only

- `domain_id` (String) The Domain Identifier (UUID).
- `id` (String) The ID of this resource.
8 changes: 4 additions & 4 deletions fastly/block_fastly_service_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func (h *PackageServiceAttributeHandler) Register(s *schema.Resource) error {
ExactlyOneOf: []string{"package.0.content", "package.0.filename"},
},
"source_code_hash": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Used to trigger updates. Must be set to a SHA512 hash of all files (in sorted order) within the package. The usual way to set this is using the fastly_package_hash data source.",
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Used to trigger updates. Must be set to a SHA512 hash of all files (in sorted order) within the package. The usual way to set this is using the fastly_package_hash data source.",
},
},
},
Expand Down
1 change: 1 addition & 0 deletions fastly/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Provider() *schema.Provider {
"fastly_configstore": resourceFastlyConfigStore(),
"fastly_configstore_entries": resourceFastlyConfigStoreEntries(),
"fastly_custom_dashboard": resourceFastlyCustomDashboard(),
"fastly_domain_v1": resourceFastlyDomainV1(),
"fastly_integration": resourceFastlyIntegration(),
"fastly_kvstore": resourceFastlyKVStore(),
"fastly_secretstore": resourceFastlySecretStore(),
Expand Down
122 changes: 122 additions & 0 deletions fastly/resource_fastly_domain_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package fastly

import (
"context"
"log"

gofastly "github.com/fastly/go-fastly/v9/fastly"
v1 "github.com/fastly/go-fastly/v9/fastly/domains/v1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceFastlyDomainV1() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFastlyDomainV1Create,
ReadContext: resourceFastlyDomainV1Read,
UpdateContext: resourceFastlyDomainV1Update,
DeleteContext: resourceFastlyDomainV1Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"domain_id": {
Type: schema.TypeString,
Computed: true,
Description: "The Domain Identifier (UUID).",
},
"fqdn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The fully-qualified domain name for your domain (e.g. `www.example.com`, no trailing dot). Can be created, but not updated.",
},
"service_id": {
Type: schema.TypeString,
Optional: true,
Description: "The service_id associated with your domain or null if there is no association.",
},
},
}
}

func resourceFastlyDomainV1Create(_ context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*APIClient).conn

var input v1.CreateInput
if v, ok := d.GetOk("fqdn"); ok {
input.FQDN = gofastly.ToPointer(v.(string))
}
if v, ok := d.GetOk("service_id"); ok {
input.ServiceID = gofastly.ToPointer(v.(string))
}

data, err := v1.Create(conn, &input)
if err != nil {
return diag.FromErr(err)
}

d.SetId(data.DomainID)

if err := d.Set("domain_id", data.DomainID); err != nil {
return diag.FromErr(err)
}
return nil
}

func resourceFastlyDomainV1Read(_ context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
log.Printf("[DEBUG] Refreshing Domain V1 Configuration for (%s)", d.Id())
conn := meta.(*APIClient).conn

input := &v1.GetInput{
DomainID: gofastly.ToPointer(d.Id()),
}

data, err := v1.Get(conn, input)
if err != nil {
return diag.FromErr(err)
}

if err := d.Set("domain_id", data.DomainID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("fqdn", data.FQDN); err != nil {
return diag.FromErr(err)
}
if err := d.Set("service_id", data.ServiceID); err != nil {
return diag.FromErr(err)
}

return nil
}

func resourceFastlyDomainV1Update(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*APIClient).conn

input := &v1.UpdateInput{
DomainID: gofastly.ToPointer(d.Id()),
}
if v, ok := d.GetOk("service_id"); ok {
input.ServiceID = gofastly.ToPointer(v.(string))
}

_, err := v1.Update(conn, input)
if err != nil {
return diag.FromErr(err)
}

return resourceFastlyDomainV1Read(ctx, d, meta)
}

func resourceFastlyDomainV1Delete(_ context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*APIClient).conn
input := &v1.DeleteInput{
DomainID: gofastly.ToPointer(d.Id()),
}
err := v1.Delete(conn, input)
if err != nil {
return diag.FromErr(err)
}
return nil
}
87 changes: 87 additions & 0 deletions fastly/resource_fastly_domain_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fastly

import (
"fmt"
"testing"

gofastly "github.com/fastly/go-fastly/v9/fastly"
v1 "github.com/fastly/go-fastly/v9/fastly/domains/v1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccFastlyDomainV1_Basic(t *testing.T) {
domainName := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckDomainV1Destroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "fastly_domain_v1" "example" {
fqdn = "%s"
}
`, domainName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("fastly_domain_v1.example", "fqdn", domainName),
resource.TestCheckNoResourceAttr("fastly_domain_v1.example", "service_id"),
),
},
{
Config: fmt.Sprintf(`
resource "fastly_service_vcl" "example" {
name = "%s"
domain {
name = "%s"
}
force_destroy = true
}
resource "fastly_domain_v1" "example" {
fqdn = "%s"
service_id = resource.fastly_service_vcl.example.id
}
`, domainName, domainName, domainName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("fastly_domain_v1.example", "fqdn", domainName),
resource.TestCheckResourceAttrSet("fastly_domain_v1.example", "service_id"),
),
},
{
ResourceName: "fastly_domain_v1.example",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckDomainV1Destroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "fastly_domain_v1" {
continue
}
a := rs.Primary.Attributes
fqdn := a["fqdn"]
conn := testAccProvider.Meta().(*APIClient).conn
input := &v1.ListInput{
FQDN: gofastly.ToPointer(fqdn),
}
cl, err := v1.List(conn, input)
if err != nil {
return fmt.Errorf("failed to list domains for fastly_domain_v1 resource: %w", err)
}
if cl != nil && len(cl.Data) > 0 {
for _, d := range cl.Data {
if d.FQDN == fqdn {
return fmt.Errorf("tried deleting domain (%s), but was still found", fqdn)
}
}
}
return nil
}
return nil
}
Loading
Loading