forked from cloudflare/terraform-provider-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cloudflare#725 from jacobbednarz/access-cors-support
resource/access_application: Add CORS support
- Loading branch information
Showing
3 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
cloudflare/resource_cloudflare_access_application_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
var ( | ||
zoneID = os.Getenv("CLOUDFLARE_ZONE_ID") | ||
domain = os.Getenv("CLOUDFLARE_DOMAIN") | ||
) | ||
|
||
func TestAccCloudflareAccessApplicationBasic(t *testing.T) { | ||
rnd := generateRandomResourceName() | ||
name := fmt.Sprintf("cloudflare_access_application.%s", rnd) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudflareAccessApplicationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCloudflareAccessApplicationConfigBasic(rnd, zoneID, domain), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(name, "zone_id", zoneID), | ||
resource.TestCheckResourceAttr(name, "name", rnd), | ||
resource.TestCheckResourceAttr(name, "domain", fmt.Sprintf("%s.%s", rnd, domain)), | ||
resource.TestCheckResourceAttr(name, "session_duration", "24h"), | ||
resource.TestCheckResourceAttr(name, "cors_headers.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccCloudflareAccessApplicationWithCORS(t *testing.T) { | ||
rnd := generateRandomResourceName() | ||
name := fmt.Sprintf("cloudflare_access_application.%s", rnd) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudflareAccessApplicationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCloudflareAccessApplicationConfigWithCORS(rnd, zoneID, domain), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(name, "zone_id", zoneID), | ||
resource.TestCheckResourceAttr(name, "name", rnd), | ||
resource.TestCheckResourceAttr(name, "domain", fmt.Sprintf("%s.%s", rnd, domain)), | ||
resource.TestCheckResourceAttr(name, "session_duration", "24h"), | ||
resource.TestCheckResourceAttr(name, "cors_headers.#", "1"), | ||
resource.TestCheckResourceAttr(name, "cors_headers.0.allowed_methods.#", "3"), | ||
resource.TestCheckResourceAttr(name, "cors_headers.0.allowed_origins.#", "1"), | ||
resource.TestCheckResourceAttr(name, "cors_headers.0.max_age", "10"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCloudflareAccessApplicationConfigBasic(rnd, zoneID, domain string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_access_application" "%[1]s" { | ||
zone_id = "%[2]s" | ||
name = "%[1]s" | ||
domain = "%[1]s.%[3]s" | ||
session_duration = "24h" | ||
} | ||
`, rnd, zoneID, domain) | ||
} | ||
|
||
func testAccCloudflareAccessApplicationConfigWithCORS(rnd, zoneID, domain string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_access_application" "%[1]s" { | ||
zone_id = "%[2]s" | ||
name = "%[1]s" | ||
domain = "%[1]s.%[3]s" | ||
session_duration = "24h" | ||
cors_headers { | ||
allowed_methods = ["GET", "POST", "OPTIONS"] | ||
allowed_origins = ["https://example.com"] | ||
allow_credentials = true | ||
max_age = 10 | ||
} | ||
} | ||
`, rnd, zoneID, domain) | ||
} | ||
|
||
func testAccCheckCloudflareAccessApplicationDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*cloudflare.API) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "cloudflare_access_application" { | ||
continue | ||
} | ||
|
||
_, err := client.AccessApplication(rs.Primary.Attributes["zone_id"], rs.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("AccessApplication still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters