Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle deleted org when provisioning admin org settings #984

Merged
merged 1 commit into from
Aug 3, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions tfe/resource_tfe_admin_organization_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 29 additions & 0 deletions tfe/resource_tfe_admin_organization_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package tfe

import (
"context"
"fmt"
"math/rand"
"regexp"
Expand Down Expand Up @@ -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"),
),
},
},
})
}
Expand Down Expand Up @@ -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)
}
}
Loading