Skip to content

Commit 872e253

Browse files
tjcorrkfcampbell
andauthored
Add support for deleting an enterprise organization (#1669)
Co-authored-by: Keegan Campbell <[email protected]>
1 parent c968888 commit 872e253

3 files changed

+137
-3
lines changed

github/resource_github_enterprise_organization.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,19 @@ func resourceGithubEnterpriseOrganizationRead(data *schema.ResourceData, meta in
190190
}
191191

192192
func resourceGithubEnterpriseOrganizationDelete(data *schema.ResourceData, meta interface{}) error {
193-
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")
193+
owner := meta.(*Owner)
194+
v3 := owner.v3client
195+
196+
ctx := context.WithValue(context.Background(), ctxId, data.Id())
197+
198+
_, err := v3.Organizations.Delete(ctx, data.Get("name").(string))
199+
200+
// We expect the delete to return with a 202 Accepted error so ignore those
201+
if _, ok := err.(*github.AcceptedError); ok {
202+
return nil
203+
}
204+
205+
return err
194206
}
195207

196208
func resourceGithubEnterpriseOrganizationImport(data *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
)
11+
12+
func TestAccGithubEnterpriseOrganization(t *testing.T) {
13+
14+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
15+
orgName := fmt.Sprintf("tf-acc-test-%s", randomID)
16+
17+
desc := "Initial org description"
18+
updatedDesc := "Updated org description"
19+
20+
config := fmt.Sprintf(`
21+
data "github_enterprise" "enterprise" {
22+
slug = "%s"
23+
}
24+
25+
data "github_user" "current" {
26+
username = ""
27+
}
28+
29+
resource "github_enterprise_organization" "org" {
30+
enterprise_id = data.github_enterprise.enterprise.id
31+
name = "%s"
32+
description = "%s"
33+
billing_email = data.github_user.current.email
34+
admin_logins = [
35+
data.github_user.current.login
36+
]
37+
}
38+
`, testEnterprise, orgName, desc)
39+
40+
t.Run("creates and updates an enterprise organization without error", func(t *testing.T) {
41+
checks := map[string]resource.TestCheckFunc{
42+
"before": resource.ComposeTestCheckFunc(
43+
resource.TestCheckResourceAttrSet(
44+
"github_enterprise_organization.org", "enterprise_id",
45+
),
46+
resource.TestCheckResourceAttr(
47+
"github_enterprise_organization.org", "name",
48+
orgName,
49+
),
50+
resource.TestCheckResourceAttr(
51+
"github_enterprise_organization.org", "description",
52+
desc,
53+
),
54+
resource.TestCheckResourceAttrSet(
55+
"github_enterprise_organization.org", "billing_email",
56+
),
57+
resource.TestCheckResourceAttr(
58+
"github_enterprise_organization.org", "admin_logins.#",
59+
"1",
60+
),
61+
),
62+
"after": resource.ComposeTestCheckFunc(
63+
resource.TestCheckResourceAttr(
64+
"github_enterprise_organization.org", "description",
65+
updatedDesc,
66+
),
67+
),
68+
}
69+
70+
testCase := func(t *testing.T, mode string) {
71+
resource.Test(t, resource.TestCase{
72+
PreCheck: func() { skipUnlessMode(t, mode) },
73+
Providers: testAccProviders,
74+
Steps: []resource.TestStep{
75+
{
76+
Config: config,
77+
Check: checks["before"],
78+
},
79+
{
80+
Config: strings.Replace(config,
81+
desc,
82+
updatedDesc, 1),
83+
Check: checks["after"],
84+
},
85+
},
86+
})
87+
}
88+
89+
t.Run("with an enterprise account", func(t *testing.T) {
90+
if isEnterprise != "true" {
91+
t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false")
92+
}
93+
if testEnterprise == "" {
94+
t.Skip("Skipping because `ENTERPRISE_SLUG` is not set")
95+
}
96+
testCase(t, enterprise)
97+
})
98+
})
99+
100+
t.Run("deletes an enterprise organization without error", func(t *testing.T) {
101+
testCase := func(t *testing.T, mode string) {
102+
resource.Test(t, resource.TestCase{
103+
PreCheck: func() { skipUnlessMode(t, mode) },
104+
Providers: testAccProviders,
105+
Steps: []resource.TestStep{
106+
{
107+
Config: config,
108+
Destroy: true,
109+
},
110+
},
111+
})
112+
}
113+
114+
t.Run("with an enterprise account", func(t *testing.T) {
115+
if isEnterprise != "true" {
116+
t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false")
117+
}
118+
if testEnterprise == "" {
119+
t.Skip("Skipping because `ENTERPRISE_SLUG` is not set")
120+
}
121+
testCase(t, enterprise)
122+
})
123+
})
124+
}

website/docs/r/enterprise_organization.html.markdown

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ description: |-
99

1010
This resource allows you to create and manage a GitHub enterprise organization.
1111

12-
~> **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`.
13-
1412
## Example Usage
1513

1614
```

0 commit comments

Comments
 (0)