diff --git a/github/resource_github_enterprise_organization.go b/github/resource_github_enterprise_organization.go index 2dce2f34e2..d053f6793d 100644 --- a/github/resource_github_enterprise_organization.go +++ b/github/resource_github_enterprise_organization.go @@ -190,7 +190,19 @@ func resourceGithubEnterpriseOrganizationRead(data *schema.ResourceData, meta in } func resourceGithubEnterpriseOrganizationDelete(data *schema.ResourceData, meta interface{}) error { - return errors.New("deleting organizations is not supported programmatically by github, and hence is not supported by the provider. You will need to remove the org in the github ui, then use `terraform state rm` to remove the org from the state file") + owner := meta.(*Owner) + v3 := owner.v3client + + ctx := context.WithValue(context.Background(), ctxId, data.Id()) + + _, err := v3.Organizations.Delete(ctx, data.Get("name").(string)) + + // We expect the delete to return with a 202 Accepted error so ignore those + if _, ok := err.(*github.AcceptedError); ok { + return nil + } + + return err } func resourceGithubEnterpriseOrganizationImport(data *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { diff --git a/github/resource_github_enterprise_organization_test.go b/github/resource_github_enterprise_organization_test.go new file mode 100644 index 0000000000..09ed40cca7 --- /dev/null +++ b/github/resource_github_enterprise_organization_test.go @@ -0,0 +1,124 @@ +package github + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccGithubEnterpriseOrganization(t *testing.T) { + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + orgName := fmt.Sprintf("tf-acc-test-%s", randomID) + + desc := "Initial org description" + updatedDesc := "Updated org description" + + config := fmt.Sprintf(` + data "github_enterprise" "enterprise" { + slug = "%s" + } + + data "github_user" "current" { + username = "" + } + + resource "github_enterprise_organization" "org" { + enterprise_id = data.github_enterprise.enterprise.id + name = "%s" + description = "%s" + billing_email = data.github_user.current.email + admin_logins = [ + data.github_user.current.login + ] + } + `, testEnterprise, orgName, desc) + + t.Run("creates and updates an enterprise organization without error", func(t *testing.T) { + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet( + "github_enterprise_organization.org", "enterprise_id", + ), + resource.TestCheckResourceAttr( + "github_enterprise_organization.org", "name", + orgName, + ), + resource.TestCheckResourceAttr( + "github_enterprise_organization.org", "description", + desc, + ), + resource.TestCheckResourceAttrSet( + "github_enterprise_organization.org", "billing_email", + ), + resource.TestCheckResourceAttr( + "github_enterprise_organization.org", "admin_logins.#", + "1", + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_enterprise_organization.org", "description", + updatedDesc, + ), + ), + } + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: strings.Replace(config, + desc, + updatedDesc, 1), + Check: checks["after"], + }, + }, + }) + } + + t.Run("with an enterprise account", func(t *testing.T) { + if isEnterprise != "true" { + t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") + } + if testEnterprise == "" { + t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") + } + testCase(t, enterprise) + }) + }) + + t.Run("deletes an enterprise organization without error", func(t *testing.T) { + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, + }, + }, + }) + } + + t.Run("with an enterprise account", func(t *testing.T) { + if isEnterprise != "true" { + t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") + } + if testEnterprise == "" { + t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") + } + testCase(t, enterprise) + }) + }) +} diff --git a/website/docs/r/enterprise_organization.html.markdown b/website/docs/r/enterprise_organization.html.markdown index 3beea263b0..1f9173d3ea 100644 --- a/website/docs/r/enterprise_organization.html.markdown +++ b/website/docs/r/enterprise_organization.html.markdown @@ -9,8 +9,6 @@ description: |- This resource allows you to create and manage a GitHub enterprise organization. -~> **Note** This resource cannot delete an organization. Organizations must be deleted through the GitHub UI and remove them from the state using `terraform state rm`. - ## Example Usage ```