Skip to content

Commit

Permalink
Update org test to only test import functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin-Holmes committed Oct 14, 2022
1 parent 760a8cf commit e162072
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 78 deletions.
8 changes: 7 additions & 1 deletion internal/provider/resource_cloudflare_access_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type contextKey int

const orgAccessImportCtxKey contextKey = iota

func resourceCloudflareAccessOrganization() *schema.Resource {
return &schema.Resource{
Schema: resourceCloudflareAccessOrganizationSchema(),
Expand Down Expand Up @@ -58,7 +62,7 @@ func resourceCloudflareAccessOrganizationRead(ctx context.Context, d *schema.Res
d.Set("auth_domain", organization.AuthDomain)
d.Set("is_ui_read_only", organization.IsUIReadOnly)

loginDesign := convertLoginDesignStructToSchema(d, &organization.LoginDesign)
loginDesign := convertLoginDesignStructToSchema(ctx, d, &organization.LoginDesign)
if loginDesignErr := d.Set("login_design", loginDesign); loginDesignErr != nil {
return diag.FromErr(fmt.Errorf("error setting Access Organization Login Design configuration: %w", loginDesignErr))
}
Expand Down Expand Up @@ -97,6 +101,8 @@ func resourceCloudflareAccessOrganizationUpdate(ctx context.Context, d *schema.R
}

func resourceCloudflareAccessOrganizationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
ctx = context.WithValue(ctx, orgAccessImportCtxKey, true)

accountID := d.Id()

tflog.Info(ctx, fmt.Sprintf("Importing Cloudflare Access Organization for account %s", accountID))
Expand Down
99 changes: 25 additions & 74 deletions internal/provider/resource_cloudflare_access_organization_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package provider

import (
"context"
"errors"
"fmt"
"testing"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand All @@ -15,72 +12,45 @@ func TestAccCloudflareAccessOrganization(t *testing.T) {
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_access_organization.%s", rnd)

updatedName := fmt.Sprintf("%s-updated", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheck(t)
testAccessAccPreCheck(t)
},
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareAccessOrganizationDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudflareAccessOrganizationConfigBasic(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "auth_domain", fmt.Sprintf("%s.cloudflareaccess.com", rnd)),
resource.TestCheckResourceAttr(name, "is_ui_read_only", "true"),
resource.TestCheckResourceAttr(name, "login_design.#", "1"),
resource.TestCheckResourceAttr(name, "login_design.0.background_color", "#FFFFFF"),
resource.TestCheckResourceAttr(name, "login_design.0.text_color", "#000000"),
resource.TestCheckResourceAttr(name, "login_design.0.logo_path", "https://example.com/logo.png"),
resource.TestCheckResourceAttr(name, "login_design.0.header_text", "My header text"),
resource.TestCheckResourceAttr(name, "login_design.0.footer_text", "My footer text"),
),
},
{
Config: testAccCloudflareAccessOrganizationConfigBasicUpdated(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", updatedName),
resource.TestCheckResourceAttr(name, "auth_domain", fmt.Sprintf("%s.cloudflareaccess.com", rnd)),
resource.TestCheckResourceAttr(name, "is_ui_read_only", "false"),
resource.TestCheckResourceAttr(name, "login_design.#", "1"),
resource.TestCheckResourceAttr(name, "login_design.0.background_color", "#FFFFFF"),
resource.TestCheckResourceAttr(name, "login_design.0.text_color", "#000000"),
resource.TestCheckResourceAttr(name, "login_design.0.logo_path", "https://example.com/logo.png"),
resource.TestCheckResourceAttr(name, "login_design.0.header_text", "My header text"),
resource.TestCheckResourceAttr(name, "login_design.0.footer_text", "My footer text"),
),
Config: testAccCloudflareAccessOrganizationConfigBasic(rnd, accountID),
ResourceName: name,
ImportState: true,
ImportStateId: accountID,
ImportStateCheck: accessOrgImportStateCheck,
},
},
})
}

func testAccCheckCloudflareAccessOrganizationDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudflare.API)
func accessOrgImportStateCheck(instanceStates []*terraform.InstanceState) error {
state := instanceStates[0]
attrs := state.Attributes

for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudflare_access_organization" {
continue
}

var notFoundError *cloudflare.NotFoundError
if rs.Primary.Attributes["zone_id"] != "" {
_, _, err := client.ZoneLevelAccessOrganization(context.Background(), rs.Primary.Attributes["zone_id"])
if !errors.As(err, &notFoundError) {
return fmt.Errorf("AccessOrganization still exists")
}
}
stateChecks := []struct {
field string
stateValue string
expectedValue string
}{
{field: "ID", stateValue: state.ID, expectedValue: accountID},
{field: "account_id", stateValue: attrs["account_id"], expectedValue: accountID},
{field: "name", stateValue: attrs["name"], expectedValue: "my test org"},
{field: "auth_domain", stateValue: attrs["auth_domain"], expectedValue: "authdomain.cloudflareaccess.com"},
{field: "is_ui_read_only", stateValue: attrs["is_ui_read_only"], expectedValue: "false"},
{field: "login_design.#", stateValue: attrs["login_design.#"], expectedValue: "1"},
}

if rs.Primary.Attributes["account_id"] != "" {
_, _, err := client.AccessOrganization(context.Background(), rs.Primary.Attributes["account_id"])
if !errors.As(err, &notFoundError) {
return fmt.Errorf("AccessOrganization still exists")
}
for _, check := range stateChecks {
if check.stateValue != check.expectedValue {
return fmt.Errorf("%s has value %s and does not match expected value %s", check.field, check.stateValue, check.expectedValue)
}
}

Expand All @@ -91,27 +61,8 @@ func testAccCloudflareAccessOrganizationConfigBasic(rnd, accountID string) strin
return fmt.Sprintf(`
resource "cloudflare_access_organization" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
auth_domain = "%[1]s.cloudflareaccess.com"
is_ui_read_only = true
login_design {
background_color = "#FFFFFF"
text_color = "#000000"
logo_path = "https://example.com/logo.png"
header_text = "My header text"
footer_text = "My footer text"
}
}
`, rnd, accountID)
}

func testAccCloudflareAccessOrganizationConfigBasicUpdated(rnd, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_access_organization" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s-updated"
auth_domain = "%[1]s.cloudflareaccess.com"
name = "my test org"
auth_domain = "authdomain.cloudflareaccess.com"
is_ui_read_only = false
login_design {
Expand Down
14 changes: 11 additions & 3 deletions internal/provider/schema_cloudflare_access_organization.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package provider

import (
"context"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -75,7 +77,7 @@ func resourceCloudflareAccessOrganizationSchema() map[string]*schema.Schema {
func convertLoginDesignSchemaToStruct(d *schema.ResourceData) *cloudflare.AccessOrganizationLoginDesign {
LoginDesign := cloudflare.AccessOrganizationLoginDesign{}

if _, ok := d.GetOk("login_design"); ok {
if _, ok := d.GetOk("login_design.0"); ok {
LoginDesign.BackgroundColor = d.Get("login_design.0.background_color").(string)
LoginDesign.TextColor = d.Get("login_design.0.text_color").(string)
LoginDesign.LogoPath = d.Get("login_design.0.logo_path").(string)
Expand All @@ -86,8 +88,14 @@ func convertLoginDesignSchemaToStruct(d *schema.ResourceData) *cloudflare.Access
return &LoginDesign
}

func convertLoginDesignStructToSchema(d *schema.ResourceData, loginDesign *cloudflare.AccessOrganizationLoginDesign) []interface{} {
if _, ok := d.GetOk("login_design"); !ok {
func convertLoginDesignStructToSchema(ctx context.Context, d *schema.ResourceData, loginDesign *cloudflare.AccessOrganizationLoginDesign) []interface{} {
var onImport bool
var ok bool
if onImport, ok = ctx.Value(orgAccessImportCtxKey).(bool); !ok {
onImport = false
}

if _, ok := d.GetOk("login_design"); !ok && !onImport {
return []interface{}{}
}

Expand Down

0 comments on commit e162072

Please sign in to comment.