Skip to content

Commit

Permalink
CDN: Add Support for "needs-cloudflare-cert" (#1089)
Browse files Browse the repository at this point in the history
* CDN: Add Support for "needs-cloudflare-cert"

* add docs

* terraformfmt

* update

* remove doc

* needs -> need

* needs -> need
  • Loading branch information
danaelhe authored Dec 5, 2023
1 parent 7b5f984 commit 7dde232
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
31 changes: 21 additions & 10 deletions digitalocean/cdn/resource_cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

var (
needsCloudflareCert = "needs-cloudflare-cert"
)

func ResourceDigitalOceanCDN() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDigitalOceanCDNCreate,
Expand Down Expand Up @@ -142,12 +146,15 @@ func resourceDigitalOceanCDNCreate(ctx context.Context, d *schema.ResourceData,
if name, nameOk := d.GetOk("certificate_name"); nameOk {
certName := name.(string)
if certName != "" {
cert, err := certificate.FindCertificateByName(client, certName)
if err != nil {
return diag.FromErr(err)
if certName == needsCloudflareCert {
cdnRequest.CertificateID = needsCloudflareCert
} else {
cert, err := certificate.FindCertificateByName(client, certName)
if err != nil {
return diag.FromErr(err)
}
cdnRequest.CertificateID = cert.ID
}

cdnRequest.CertificateID = cert.ID
}
}

Expand Down Expand Up @@ -277,12 +284,16 @@ func resourceDigitalOceanCDNUpdate(ctx context.Context, d *schema.ResourceData,

certName := d.Get("certificate_name").(string)
if certName != "" {
cert, err := certificate.FindCertificateByName(client, certName)
if err != nil {
return diag.FromErr(err)
}
if certName == needsCloudflareCert {
cdnUpdateRequest.CertificateID = needsCloudflareCert
} else {
cert, err := certificate.FindCertificateByName(client, certName)
if err != nil {
return diag.FromErr(err)
}

cdnUpdateRequest.CertificateID = cert.ID
cdnUpdateRequest.CertificateID = cert.ID
}
}

_, _, err := client.CDNs.UpdateCustomDomain(context.Background(), d.Id(), cdnUpdateRequest)
Expand Down
39 changes: 39 additions & 0 deletions digitalocean/cdn/resource_cdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ func TestAccDigitalOceanCDN_Create(t *testing.T) {
})
}

func TestAccDigitalOceanCDN_CreateWithNeedCloudflareCert(t *testing.T) {

bucketName := generateBucketName()
cdnCreateConfig := fmt.Sprintf(testAccCheckDigitalOceanCDNConfig_CreateWithNeedCloudflareCert, bucketName)

expectedOrigin := bucketName + originSuffix
expectedTTL := "3600"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanCDNDestroy,
Steps: []resource.TestStep{
{
Config: cdnCreateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanCDNExists("digitalocean_cdn.foobar"),
resource.TestCheckResourceAttr(
"digitalocean_cdn.foobar", "origin", expectedOrigin),
resource.TestCheckResourceAttr("digitalocean_cdn.foobar", "ttl", expectedTTL),
resource.TestCheckResourceAttr("digitalocean_cdn.foobar", "certificate_name", "needs-cloudflare-cert"),
),
},
},
})
}

func TestAccDigitalOceanCDN_Create_with_TTL(t *testing.T) {

bucketName := generateBucketName()
Expand Down Expand Up @@ -207,6 +234,18 @@ resource "digitalocean_cdn" "foobar" {
origin = digitalocean_spaces_bucket.bucket.bucket_domain_name
}`

const testAccCheckDigitalOceanCDNConfig_CreateWithNeedCloudflareCert = `
resource "digitalocean_spaces_bucket" "bucket" {
name = "%s"
region = "ams3"
acl = "public-read"
}
resource "digitalocean_cdn" "foobar" {
origin = digitalocean_spaces_bucket.bucket.bucket_domain_name
certificate_name = "needs-cloudflare-cert"
}`

const testAccCheckDigitalOceanCDNConfig_Create_with_TTL = `
resource "digitalocean_spaces_bucket" "bucket" {
name = "%s"
Expand Down

0 comments on commit 7dde232

Please sign in to comment.