From 30a8273399d8568099206397e6ada781a9956e4f Mon Sep 17 00:00:00 2001 From: Chris Trombley Date: Wed, 2 Aug 2023 10:43:48 -0700 Subject: [PATCH] fix: handle deleted org when provisioning admin org settings --- CHANGELOG.md | 1 + ...esource_tfe_admin_organization_settings.go | 10 +++++-- ...ce_tfe_admin_organization_settings_test.go | 29 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bece63d5b..4a5644266 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ BUG FIXES: * `r/tfe_workspace`: Fix panic when updating `trigger_patterns` attribute, by @liamstevens [969](https://github.com/hashicorp/terraform-provider-tfe/pull/969) +* `r/tfe_admin_organization_settings`: Allow reprovisioning when the parent organization has been deleted, by @ctrombley [981](https://github.com/hashicorp/terraform-provider-tfe/pull/981) FEATURES: * **New Resource**: `r/tfe_saml_settings` manages SAML Settings, by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970) diff --git a/tfe/resource_tfe_admin_organization_settings.go b/tfe/resource_tfe_admin_organization_settings.go index b749cc304..93bafe49e 100644 --- a/tfe/resource_tfe_admin_organization_settings.go +++ b/tfe/resource_tfe_admin_organization_settings.go @@ -66,6 +66,12 @@ func resourceTFEAdminOrganizationSettingsRead(d *schema.ResourceData, meta inter log.Printf("[DEBUG] Read configuration of admin organization: %s", name) org, err := config.Client.Admin.Organizations.Read(ctx, name) if err != nil { + if errors.Is(err, tfe.ErrResourceNotFound) { + log.Printf("[DEBUG] Organization %s no longer exists", d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("failed to read admin organization %s: %w", name, err) } @@ -86,7 +92,7 @@ func resourceTFEAdminOrganizationSettingsRead(d *schema.ResourceData, meta inter consumerList, err := config.Client.Admin.Organizations.ListModuleConsumers(ctx, d.Id(), options) if err != nil { if errors.Is(err, tfe.ErrResourceNotFound) { - log.Printf("[DEBUG] Organization %s does not longer exist", d.Id()) + log.Printf("[DEBUG] Organization %s no longer exists", d.Id()) d.SetId("") return nil } @@ -144,7 +150,7 @@ func resourceTFEAdminOrganizationSettingsUpdate(d *schema.ResourceData, meta int } } - if !globalModuleSharing && set != nil { + if !globalModuleSharing && set != nil && set.Len() > 0 { if err != nil { return fmt.Errorf("failed to fetch admin organizations for module consumer ids: %w", err) } diff --git a/tfe/resource_tfe_admin_organization_settings_test.go b/tfe/resource_tfe_admin_organization_settings_test.go index cde55a5c6..ad6fadf4a 100644 --- a/tfe/resource_tfe_admin_organization_settings_test.go +++ b/tfe/resource_tfe_admin_organization_settings_test.go @@ -4,6 +4,7 @@ package tfe import ( + "context" "fmt" "math/rand" "regexp" @@ -48,6 +49,27 @@ func TestAccTFEAdminOrganizationSettings_basic(t *testing.T) { Config: testConfigTFEAdminOrganizationSettings_conflict(rInt1, rInt2), ExpectError: regexp.MustCompile(`global_module_sharing cannot be true if module_sharing_consumer_organizations are set`), }, + { + PreConfig: deleteOrganization(fmt.Sprintf("tst-terraform-%d", rInt1)), + Config: testConfigTFEAdminOrganizationSettings_basic(rInt1, rInt2, rInt3), + Check: resource.ComposeAggregateTestCheckFunc( + // organization attribute */ + resource.TestCheckResourceAttr( + "tfe_admin_organization_settings.settings", "organization", fmt.Sprintf("tst-terraform-%d", rInt1)), + resource.TestCheckResourceAttr( + "tfe_admin_organization_settings.settings", "global_module_sharing", "false"), + resource.TestCheckResourceAttr( + "tfe_admin_organization_settings.settings", "access_beta_tools", "true"), + + // module_consumers attribute + resource.TestCheckResourceAttr( + "tfe_admin_organization_settings.settings", "module_sharing_consumer_organizations.#", "2"), + resource.TestCheckResourceAttrSet( + "tfe_admin_organization_settings.settings", "module_sharing_consumer_organizations.0"), + resource.TestCheckResourceAttrSet( + "tfe_admin_organization_settings.settings", "module_sharing_consumer_organizations.1"), + ), + }, }, }) } @@ -96,3 +118,10 @@ resource "tfe_admin_organization_settings" "settings" { module_sharing_consumer_organizations = [tfe_organization.foo.id] }`, rInt1, rInt2) } + +func deleteOrganization(name string) func() { + return func() { + client := testAccProvider.Meta().(ConfiguredClient).Client + client.Organizations.Delete(context.Background(), name) + } +}