Skip to content

Commit

Permalink
Merge pull request cloudflare#734 from xens/720-add-support-for-allow…
Browse files Browse the repository at this point in the history
…ed_idps

access: add support for allowed_idps
  • Loading branch information
jacobbednarz authored Jul 15, 2020
2 parents acf56bc + 32bdd74 commit 127e8c6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cloudflare/resource_cloudflare_access_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,21 @@ func resourceCloudflareAccessApplication() *schema.Resource {
Optional: true,
Default: false,
},
"allowed_idps": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceCloudflareAccessApplicationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
allowedIDPList := expandInterfaceToStringList(d.Get("allowed_idps"))

newAccessApplication := cloudflare.AccessApplication{
Name: d.Get("name").(string),
Expand All @@ -113,6 +121,10 @@ func resourceCloudflareAccessApplicationCreate(d *schema.ResourceData, meta inte
AutoRedirectToIdentity: d.Get("auto_redirect_to_identity").(bool),
}

if len(allowedIDPList) > 0 {
newAccessApplication.AllowedIdps = allowedIDPList
}

if _, ok := d.GetOk("cors_headers"); ok {
CORSConfig, _ := convertCORSSchemaToStruct(d)
newAccessApplication.CorsHeaders = CORSConfig
Expand Down Expand Up @@ -148,6 +160,7 @@ func resourceCloudflareAccessApplicationRead(d *schema.ResourceData, meta interf
d.Set("session_duration", accessApplication.SessionDuration)
d.Set("domain", accessApplication.Domain)
d.Set("auto_redirect_to_identity", accessApplication.AutoRedirectToIdentity)
d.Set("allowed_idps", accessApplication.AllowedIdps)

corsConfig := convertCORSStructToSchema(d, accessApplication.CorsHeaders)
if corsConfigErr := d.Set("cors_headers", corsConfig); corsConfigErr != nil {
Expand All @@ -160,6 +173,7 @@ func resourceCloudflareAccessApplicationRead(d *schema.ResourceData, meta interf
func resourceCloudflareAccessApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
allowedIDPList := expandInterfaceToStringList(d.Get("allowed_idps"))

updatedAccessApplication := cloudflare.AccessApplication{
ID: d.Id(),
Expand All @@ -169,6 +183,10 @@ func resourceCloudflareAccessApplicationUpdate(d *schema.ResourceData, meta inte
AutoRedirectToIdentity: d.Get("auto_redirect_to_identity").(bool),
}

if len(allowedIDPList) > 0 {
updatedAccessApplication.AllowedIdps = allowedIDPList
}

if _, ok := d.GetOk("cors_headers"); ok {
CORSConfig, _ := convertCORSSchemaToStruct(d)
updatedAccessApplication.CorsHeaders = CORSConfig
Expand Down
45 changes: 45 additions & 0 deletions cloudflare/resource_cloudflare_access_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ func TestAccCloudflareAccessApplicationWithAutoRedirectToIdentity(t *testing.T)
})
}

func TestAccCloudflareAccessApplicationWithADefinedIdps(t *testing.T) {
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
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: testAccCloudflareAccessApplicationConfigWithADefinedIdp(rnd, zoneID, domain, accountID),
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, "auto_redirect_to_identity", "true"),
resource.TestCheckResourceAttr(name, "allowed_idps.#", "1"),
),
},
},
})
}

func testAccCloudflareAccessApplicationConfigBasic(rnd, zoneID, domain string) string {
return fmt.Sprintf(`
resource "cloudflare_access_application" "%[1]s" {
Expand Down Expand Up @@ -137,6 +164,24 @@ resource "cloudflare_access_application" "%[1]s" {
`, rnd, zoneID, domain)
}

func testAccCloudflareAccessApplicationConfigWithADefinedIdp(rnd, zoneID, domain string, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_access_identity_provider" "%[1]s" {
account_id = "%[4]s"
name = "%[1]s"
type = "onetimepin"
}
resource "cloudflare_access_application" "%[1]s" {
zone_id = "%[2]s"
name = "%[1]s"
domain = "%[1]s.%[3]s"
session_duration = "24h"
auto_redirect_to_identity = true
allowed_idps = [cloudflare_access_identity_provider.%[1]s.id]
}
`, rnd, zoneID, domain, accountID)
}

func testAccCheckCloudflareAccessApplicationDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudflare.API)

Expand Down
5 changes: 4 additions & 1 deletion website/docs/r/access_application.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ resource "cloudflare_access_application" "staging_app" {
name = "staging application"
domain = "staging.example.com"
session_duration = "24h"
auto_redirect_to_identity = false
auto_redirect_to_identity = false
}
# With CORS configuration
Expand Down Expand Up @@ -50,6 +50,8 @@ The following arguments are supported:
re-authorise. Must be one of `30m`, `6h`, `12h`, `24h`, `168h`, `730h`.
* `cors_headers` - (Optional) CORS configuration for the Access Application. See
below for reference structure.
* `allowed_idps` - (Optional) The identity providers selected for the application.


**cors_headers** allows the following:

Expand Down Expand Up @@ -80,6 +82,7 @@ The following additional attributes are exported:
* `domain` - Domain of the application
* `session_duration` - Length of session for the application before prompting for a sign in
* `auto_redirect_to_identity` - If the IdP selection page is skipped or not
* `allowed_idps` - The identity providers selected for the application

## Import

Expand Down

0 comments on commit 127e8c6

Please sign in to comment.