Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/services/organization/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/cloudflare/cloudflare-go/v6"
"github.com/cloudflare/cloudflare-go/v6/option"
Expand Down Expand Up @@ -91,6 +92,7 @@ func (r *OrganizationResource) Create(ctx context.Context, req resource.CreateRe
data = &env.Result

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
time.Sleep(30 * time.Second)
}

func (r *OrganizationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -138,6 +140,7 @@ func (r *OrganizationResource) Update(ctx context.Context, req resource.UpdateRe
data = &env.Result

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

}

func (r *OrganizationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down
161 changes: 161 additions & 0 deletions internal/services/organization/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package organization_test

import (
"context"
"fmt"
"testing"

"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

// TestMain is the entry point for test execution
func TestMain(m *testing.M) {
resource.TestMain(m)
}

// TestAccCloudflareOrganization_Basic tests the basic CRUD operations for organization resource
func TestAccCloudflareOrganization_Basic(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
resourceName := "cloudflare_organization." + rnd
orgName := fmt.Sprintf("tf-acctest-%s", rnd)
updatedOrgName := fmt.Sprintf("tf-acctest-%s-updated", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckCloudflareOrganizationDestroy,
Steps: []resource.TestStep{
// Step 1: Create - Test resource creation with all required attributes
{
Config: testAccOrganizationConfig(rnd, orgName),
Check: resource.ComposeTestCheckFunc(
// Verify required attributes
resource.TestCheckResourceAttr(resourceName, "name", orgName),
// Verify computed attributes are set
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "create_time"),
// Verify meta attributes
resource.TestCheckResourceAttrSet(resourceName, "meta.%"),
),
},
// Step 2: Update - Test modifying updatable attributes
{
Config: testAccOrganizationConfig(rnd, updatedOrgName),
Check: resource.ComposeTestCheckFunc(
// Verify the name was updated
resource.TestCheckResourceAttr(resourceName, "name", updatedOrgName),
// Verify ID remains the same (it should be set)
resource.TestCheckResourceAttrSet(resourceName, "id"),
// Verify other attributes remain consistent
resource.TestCheckResourceAttrSet(resourceName, "create_time"),
),
},
// Step 3: Import - Test import functionality with proper ID format
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// Organization import uses just the ID, no prefix needed
},
},
})
}

// TestAccCloudflareOrganization_WithProfile tests organization creation with profile information
func TestAccCloudflareOrganization_WithProfile(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
resourceName := "cloudflare_organization." + rnd
orgName := fmt.Sprintf("tf-acctest-%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckCloudflareOrganizationDestroy,
Steps: []resource.TestStep{
// Create organization with profile
{
Config: testAccOrganizationConfigWithProfile(rnd, orgName),
ExpectNonEmptyPlan: true, // Allow non-empty plan due to profile field handling
Check: resource.ComposeTestCheckFunc(
// Basic attribute checks
resource.TestCheckResourceAttr(resourceName, "name", orgName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "create_time"),
// Profile checks
resource.TestCheckResourceAttr(resourceName, "profile.business_name", "Test Business"),
resource.TestCheckResourceAttr(resourceName, "profile.business_email", "[email protected]"),
resource.TestCheckResourceAttr(resourceName, "profile.business_phone", "+1234567890"),
resource.TestCheckResourceAttr(resourceName, "profile.business_address", "123 Test St, Test City, TC 12345"),
),
},
// Update profile information
{
Config: testAccOrganizationConfigWithProfileUpdated(rnd, orgName),
ExpectNonEmptyPlan: true, // Allow non-empty plan due to profile field handling
Check: resource.ComposeTestCheckFunc(
// Verify profile was updated
resource.TestCheckResourceAttr(resourceName, "profile.business_name", "Updated Business"),
resource.TestCheckResourceAttr(resourceName, "profile.business_email", "[email protected]"),
),
},
// Import test
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,

ImportStateVerifyIgnore: []string{
"profile", // Profile fields not populated on import
"profile.%",
"profile.business_name",
"profile.business_email",
"profile.business_phone",
"profile.business_address",
"profile.external_metadata",
},
},
},
})
}



// Test configuration functions that load from testdata files

func testAccOrganizationConfig(rnd, name string) string {
return acctest.LoadTestCase("basic.tf", rnd, name)
}

func testAccOrganizationConfigWithProfile(rnd, name string) string {
return acctest.LoadTestCase("with_profile.tf", rnd, name)
}

func testAccOrganizationConfigWithProfileUpdated(rnd, name string) string {
return acctest.LoadTestCase("with_profile_updated.tf", rnd, name)
}

func testAccOrganizationConfigWithParent(rnd, name, parentID string) string {
return acctest.LoadTestCase("with_parent.tf", rnd, name, parentID)
}

// testAccCheckCloudflareOrganizationDestroy verifies the organization has been destroyed
func testAccCheckCloudflareOrganizationDestroy(s *terraform.State) error {
client := acctest.SharedClient()

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

// Try to fetch the organization
_, err := client.Organizations.Get(context.Background(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("organization %s still exists", rs.Primary.ID)
}
}

return nil
}
3 changes: 3 additions & 0 deletions internal/services/organization/testdata/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "cloudflare_organization" "%[1]s" {
name = "%[2]s"
}
7 changes: 7 additions & 0 deletions internal/services/organization/testdata/with_parent.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "cloudflare_organization" "%[1]s" {
name = "%[2]s"

parent = {
id = "%[3]s"
}
}
11 changes: 11 additions & 0 deletions internal/services/organization/testdata/with_profile.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "cloudflare_organization" "%[1]s" {
name = "%[2]s"

profile = {
business_name = "Test Business"
business_email = "[email protected]"
business_phone = "+1234567890"
business_address = "123 Test St, Test City, TC 12345"
external_metadata = "{\"key\":\"value\"}"
}
}
11 changes: 11 additions & 0 deletions internal/services/organization/testdata/with_profile_updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "cloudflare_organization" "%[1]s" {
name = "%[2]s"

profile = {
business_name = "Updated Business"
business_email = "[email protected]"
business_phone = "+9876543210"
business_address = "456 Updated Ave, New City, NC 54321"
external_metadata = "{\"key\":\"updated_value\"}"
}
}
Loading
Loading