Skip to content

Commit

Permalink
Add support for deleting an enterprise organization (#1669)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
tjcorr and kfcampbell committed May 1, 2023
1 parent c968888 commit 872e253
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 3 deletions.
14 changes: 13 additions & 1 deletion github/resource_github_enterprise_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
124 changes: 124 additions & 0 deletions github/resource_github_enterprise_organization_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
})
}
2 changes: 0 additions & 2 deletions website/docs/r/enterprise_organization.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down

0 comments on commit 872e253

Please sign in to comment.