Skip to content

Commit 682fb2c

Browse files
authored
Merge pull request #982 from ctrombley/fix/admin-org-settings-deleted-org
fix: handle deleted org when provisioning admin org settings
2 parents e7ada2c + 30a8273 commit 682fb2c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
BUG FIXES:
44
* `r/tfe_workspace`: Fix panic when updating `trigger_patterns` attribute, by @liamstevens [969](https://github.com/hashicorp/terraform-provider-tfe/pull/969)
5+
* `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)
56

67
FEATURES:
78
* **New Resource**: `r/tfe_saml_settings` manages SAML Settings, by @karvounis-form3 [970](https://github.com/hashicorp/terraform-provider-tfe/pull/970)

tfe/resource_tfe_admin_organization_settings.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func resourceTFEAdminOrganizationSettingsRead(d *schema.ResourceData, meta inter
6666
log.Printf("[DEBUG] Read configuration of admin organization: %s", name)
6767
org, err := config.Client.Admin.Organizations.Read(ctx, name)
6868
if err != nil {
69+
if errors.Is(err, tfe.ErrResourceNotFound) {
70+
log.Printf("[DEBUG] Organization %s no longer exists", d.Id())
71+
d.SetId("")
72+
return nil
73+
}
74+
6975
return fmt.Errorf("failed to read admin organization %s: %w", name, err)
7076
}
7177

@@ -86,7 +92,7 @@ func resourceTFEAdminOrganizationSettingsRead(d *schema.ResourceData, meta inter
8692
consumerList, err := config.Client.Admin.Organizations.ListModuleConsumers(ctx, d.Id(), options)
8793
if err != nil {
8894
if errors.Is(err, tfe.ErrResourceNotFound) {
89-
log.Printf("[DEBUG] Organization %s does not longer exist", d.Id())
95+
log.Printf("[DEBUG] Organization %s no longer exists", d.Id())
9096
d.SetId("")
9197
return nil
9298
}
@@ -144,7 +150,7 @@ func resourceTFEAdminOrganizationSettingsUpdate(d *schema.ResourceData, meta int
144150
}
145151
}
146152

147-
if !globalModuleSharing && set != nil {
153+
if !globalModuleSharing && set != nil && set.Len() > 0 {
148154
if err != nil {
149155
return fmt.Errorf("failed to fetch admin organizations for module consumer ids: %w", err)
150156
}

tfe/resource_tfe_admin_organization_settings_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package tfe
55

66
import (
7+
"context"
78
"fmt"
89
"math/rand"
910
"regexp"
@@ -48,6 +49,27 @@ func TestAccTFEAdminOrganizationSettings_basic(t *testing.T) {
4849
Config: testConfigTFEAdminOrganizationSettings_conflict(rInt1, rInt2),
4950
ExpectError: regexp.MustCompile(`global_module_sharing cannot be true if module_sharing_consumer_organizations are set`),
5051
},
52+
{
53+
PreConfig: deleteOrganization(fmt.Sprintf("tst-terraform-%d", rInt1)),
54+
Config: testConfigTFEAdminOrganizationSettings_basic(rInt1, rInt2, rInt3),
55+
Check: resource.ComposeAggregateTestCheckFunc(
56+
// organization attribute */
57+
resource.TestCheckResourceAttr(
58+
"tfe_admin_organization_settings.settings", "organization", fmt.Sprintf("tst-terraform-%d", rInt1)),
59+
resource.TestCheckResourceAttr(
60+
"tfe_admin_organization_settings.settings", "global_module_sharing", "false"),
61+
resource.TestCheckResourceAttr(
62+
"tfe_admin_organization_settings.settings", "access_beta_tools", "true"),
63+
64+
// module_consumers attribute
65+
resource.TestCheckResourceAttr(
66+
"tfe_admin_organization_settings.settings", "module_sharing_consumer_organizations.#", "2"),
67+
resource.TestCheckResourceAttrSet(
68+
"tfe_admin_organization_settings.settings", "module_sharing_consumer_organizations.0"),
69+
resource.TestCheckResourceAttrSet(
70+
"tfe_admin_organization_settings.settings", "module_sharing_consumer_organizations.1"),
71+
),
72+
},
5173
},
5274
})
5375
}
@@ -96,3 +118,10 @@ resource "tfe_admin_organization_settings" "settings" {
96118
module_sharing_consumer_organizations = [tfe_organization.foo.id]
97119
}`, rInt1, rInt2)
98120
}
121+
122+
func deleteOrganization(name string) func() {
123+
return func() {
124+
client := testAccProvider.Meta().(ConfiguredClient).Client
125+
client.Organizations.Delete(context.Background(), name)
126+
}
127+
}

0 commit comments

Comments
 (0)