Skip to content

Commit

Permalink
Add access resources to use account_id over zone_id
Browse files Browse the repository at this point in the history
Also adds deprecation warning to zone_id
  • Loading branch information
Justin Holmes committed Aug 24, 2020
1 parent 8c87a15 commit 6a74cad
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 71 deletions.
51 changes: 34 additions & 17 deletions cloudflare/resource_cloudflare_access_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ func resourceCloudflareAccessApplication() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"zone_id": {
"account_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"zone_id": {
Deprecated: "This field will be removed in version 3 and replaced with the account_id field.",
Type: schema.TypeString,
Optional: true,
},
"aud": {
Type: schema.TypeString,
Expand Down Expand Up @@ -111,7 +116,10 @@ func resourceCloudflareAccessApplication() *schema.Resource {

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

newAccessApplication := cloudflare.AccessApplication{
Expand All @@ -132,9 +140,9 @@ func resourceCloudflareAccessApplicationCreate(d *schema.ResourceData, meta inte

log.Printf("[DEBUG] Creating Cloudflare Access Application from struct: %+v", newAccessApplication)

accessApplication, err := client.CreateAccessApplication(zoneID, newAccessApplication)
accessApplication, err := client.CreateAccessApplication(accountID, newAccessApplication)
if err != nil {
return fmt.Errorf("error creating Access Application for zone %q: %s", zoneID, err)
return fmt.Errorf("error creating Access Application for account %q: %s", accountID, err)
}

d.SetId(accessApplication.ID)
Expand All @@ -144,9 +152,12 @@ func resourceCloudflareAccessApplicationCreate(d *schema.ResourceData, meta inte

func resourceCloudflareAccessApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}

accessApplication, err := client.AccessApplication(zoneID, d.Id())
accessApplication, err := client.AccessApplication(accountID, d.Id())
if err != nil {
if strings.Contains(err.Error(), "HTTP status 404") {
log.Printf("[INFO] Access Application %s no longer exists", d.Id())
Expand All @@ -172,7 +183,10 @@ 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)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}
allowedIDPList := expandInterfaceToStringList(d.Get("allowed_idps"))

updatedAccessApplication := cloudflare.AccessApplication{
Expand All @@ -194,9 +208,9 @@ func resourceCloudflareAccessApplicationUpdate(d *schema.ResourceData, meta inte

log.Printf("[DEBUG] Updating Cloudflare Access Application from struct: %+v", updatedAccessApplication)

accessApplication, err := client.UpdateAccessApplication(zoneID, updatedAccessApplication)
accessApplication, err := client.UpdateAccessApplication(accountID, updatedAccessApplication)
if err != nil {
return fmt.Errorf("error updating Access Application for zone %q: %s", zoneID, err)
return fmt.Errorf("error updating Access Application for account %q: %s", accountID, err)
}

if accessApplication.ID == "" {
Expand All @@ -208,14 +222,17 @@ func resourceCloudflareAccessApplicationUpdate(d *schema.ResourceData, meta inte

func resourceCloudflareAccessApplicationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}
appID := d.Id()

log.Printf("[DEBUG] Deleting Cloudflare Access Application using ID: %s", appID)

err := client.DeleteAccessApplication(zoneID, appID)
err = client.DeleteAccessApplication(accountID, appID)
if err != nil {
return fmt.Errorf("error deleting Access Application for zone %q: %s", zoneID, err)
return fmt.Errorf("error deleting Access Application for account %q: %s", accountID, err)
}

resourceCloudflareAccessApplicationRead(d, meta)
Expand All @@ -227,14 +244,14 @@ func resourceCloudflareAccessApplicationImport(d *schema.ResourceData, meta inte
attributes := strings.SplitN(d.Id(), "/", 2)

if len(attributes) != 2 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"zoneID/accessApplicationID\"", d.Id())
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/accessApplicationID\"", d.Id())
}

zoneID, accessApplicationID := attributes[0], attributes[1]
accountID, accessApplicationID := attributes[0], attributes[1]

log.Printf("[DEBUG] Importing Cloudflare Access Application: id %s for zone %s", accessApplicationID, zoneID)
log.Printf("[DEBUG] Importing Cloudflare Access Application: id %s for account %s", accessApplicationID, accountID)

d.Set("zone_id", zoneID)
d.Set("account_id", accountID)
d.SetId(accessApplicationID)

resourceCloudflareAccessApplicationRead(d, meta)
Expand Down
63 changes: 63 additions & 0 deletions cloudflare/resource_cloudflare_access_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,66 @@ func testAccCheckCloudflareAccessApplicationDestroy(s *terraform.State) error {

return nil
}

func TestAccCloudflareAccessApplicationWithZoneID(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
defer func(apiToken string) {
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken)
}(os.Getenv("CLOUDFLARE_API_TOKEN"))
os.Setenv("CLOUDFLARE_API_TOKEN", "")
}

rnd := generateRandomResourceName()
name := "cloudflare_access_application" + rnd
zone := os.Getenv("CLOUDFLARE_DOMAIN")
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
updatedName := fmt.Sprintf("%s-updated", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccessApplicationWithZoneID(rnd, zone, zoneID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "account_id", accountID),
),
},
{
Config: testAccessApplicationWithZoneIDUpdated(rnd, zone, zoneID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", updatedName),
resource.TestCheckResourceAttr(name, "account_id", accountID),
),
},
},
})
}

func testAccessApplicationWithZoneID(resourceID, zone, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_access_application" "%[1]s" {
name = "%[1]s"
zone_id = "%[3]s"
domain = "%[1]s.%[2]s"
}
`, resourceID, zone, zoneID)
}

func testAccessApplicationWithZoneIDUpdated(resourceID, zone, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_access_application" "%[1]s" {
name = "%[1]s"
zone_id = "%[3]s"
domain = "%[1]s.%[2]s"
}
`, resourceID, zone, zoneID)
}
42 changes: 29 additions & 13 deletions cloudflare/resource_cloudflare_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ func resourceCloudflareAccessPolicy() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"account_id": {
Type: schema.TypeString,
Optional: true,
},
"zone_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -63,10 +67,13 @@ func resourceCloudflareAccessPolicy() *schema.Resource {

func resourceCloudflareAccessPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}
appID := d.Get("application_id").(string)

accessPolicy, err := client.AccessPolicy(zoneID, appID, d.Id())
accessPolicy, err := client.AccessPolicy(accountID, appID, d.Id())
if err != nil {
if strings.Contains(err.Error(), "HTTP status 404") {
log.Printf("[INFO] Access Policy %s no longer exists", d.Id())
Expand All @@ -89,7 +96,10 @@ func resourceCloudflareAccessPolicyRead(d *schema.ResourceData, meta interface{}
func resourceCloudflareAccessPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
appID := d.Get("application_id").(string)
zoneID := d.Get("zone_id").(string)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}
newAccessPolicy := cloudflare.AccessPolicy{
Name: d.Get("name").(string),
Precedence: d.Get("precedence").(int),
Expand All @@ -100,7 +110,7 @@ func resourceCloudflareAccessPolicyCreate(d *schema.ResourceData, meta interface

log.Printf("[DEBUG] Creating Cloudflare Access Policy from struct: %+v", newAccessPolicy)

accessPolicy, err := client.CreateAccessPolicy(zoneID, appID, newAccessPolicy)
accessPolicy, err := client.CreateAccessPolicy(accountID, appID, newAccessPolicy)
if err != nil {
return fmt.Errorf("error creating Access Policy for ID %q: %s", accessPolicy.ID, err)
}
Expand All @@ -112,7 +122,10 @@ func resourceCloudflareAccessPolicyCreate(d *schema.ResourceData, meta interface

func resourceCloudflareAccessPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}
appID := d.Get("application_id").(string)
updatedAccessPolicy := cloudflare.AccessPolicy{
Name: d.Get("name").(string),
Expand All @@ -125,7 +138,7 @@ func resourceCloudflareAccessPolicyUpdate(d *schema.ResourceData, meta interface

log.Printf("[DEBUG] Updating Cloudflare Access Policy from struct: %+v", updatedAccessPolicy)

accessPolicy, err := client.UpdateAccessPolicy(zoneID, appID, updatedAccessPolicy)
accessPolicy, err := client.UpdateAccessPolicy(accountID, appID, updatedAccessPolicy)
if err != nil {
return fmt.Errorf("error updating Access Policy for ID %q: %s", d.Id(), err)
}
Expand All @@ -139,12 +152,15 @@ func resourceCloudflareAccessPolicyUpdate(d *schema.ResourceData, meta interface

func resourceCloudflareAccessPolicyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
accountID, err := getAccountIDFromZoneID(d, client)
if err != nil {
return err
}
appID := d.Get("application_id").(string)

log.Printf("[DEBUG] Deleting Cloudflare Access Policy using ID: %s", d.Id())

err := client.DeleteAccessPolicy(zoneID, appID, d.Id())
err = client.DeleteAccessPolicy(accountID, appID, d.Id())
if err != nil {
return fmt.Errorf("error deleting Access Policy for ID %q: %s", d.Id(), err)
}
Expand All @@ -158,14 +174,14 @@ func resourceCloudflareAccessPolicyImport(d *schema.ResourceData, meta interface
attributes := strings.SplitN(d.Id(), "/", 3)

if len(attributes) != 3 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"zoneID/accessApplicationID/accessPolicyID\"", d.Id())
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/accessApplicationID/accessPolicyID\"", d.Id())
}

zoneID, accessAppID, accessPolicyID := attributes[0], attributes[1], attributes[2]
accountID, accessAppID, accessPolicyID := attributes[0], attributes[1], attributes[2]

log.Printf("[DEBUG] Importing Cloudflare Access Policy: zoneID %q, appID %q, accessPolicyID %q", zoneID, accessAppID, accessPolicyID)
log.Printf("[DEBUG] Importing Cloudflare Access Policy: accountID %q, appID %q, accessPolicyID %q", accountID, accessAppID, accessPolicyID)

d.Set("zone_id", zoneID)
d.Set("account_id", accountID)
d.Set("application_id", accessAppID)
d.SetId(accessPolicyID)

Expand Down
Loading

0 comments on commit 6a74cad

Please sign in to comment.