Skip to content

Commit 8adfbd2

Browse files
authored
[feat] Add support for provisioning github orgs to enterprises.
2 parents 207d526 + 8d0cfaf commit 8adfbd2

File tree

7 files changed

+536
-0
lines changed

7 files changed

+536
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
"github.com/shurcooL/githubv4"
8+
)
9+
10+
func dataSourceGithubEnterprise() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubEnterpriseRead,
13+
Schema: map[string]*schema.Schema{
14+
"slug": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
},
18+
"name": {
19+
Type: schema.TypeString,
20+
Computed: true,
21+
},
22+
"description": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
},
26+
"created_at": {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
"url": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
},
35+
}
36+
}
37+
38+
func dataSourceGithubEnterpriseRead(data *schema.ResourceData, meta interface{}) error {
39+
var query struct {
40+
Enterprise struct {
41+
ID githubv4.String
42+
Name githubv4.String
43+
Description githubv4.String
44+
CreatedAt githubv4.String
45+
Url githubv4.String
46+
} `graphql:"enterprise(slug: $slug)"`
47+
}
48+
49+
slug := data.Get("slug").(string)
50+
client := meta.(*Owner).v4client
51+
variables := map[string]interface{}{
52+
"slug": githubv4.String(slug),
53+
}
54+
err := client.Query(context.Background(), &query, variables)
55+
if err != nil {
56+
return err
57+
}
58+
if query.Enterprise.ID == "" {
59+
return fmt.Errorf("could not find enterprise %v", slug)
60+
}
61+
data.SetId(string(query.Enterprise.ID))
62+
data.Set("name", query.Enterprise.Name)
63+
data.Set("description", query.Enterprise.Description)
64+
data.Set("created_at", query.Enterprise.CreatedAt)
65+
data.Set("url", query.Enterprise.Url)
66+
67+
return nil
68+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
6+
"testing"
7+
)
8+
9+
func TestAccGithubEnterpriseDataSource(t *testing.T) {
10+
if isEnterprise != "true" {
11+
t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false")
12+
}
13+
14+
if testEnterprise == "" {
15+
t.Skip("Skipping because `ENTERPRISE_SLUG` is not set")
16+
}
17+
18+
config := fmt.Sprintf(`
19+
data "github_enterprise" "test" {
20+
slug = "%s"
21+
}
22+
`,
23+
testEnterprise,
24+
)
25+
26+
check := resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttr("data.github_enterprise.test", "slug", testEnterprise),
28+
resource.TestCheckResourceAttrSet("data.github_enterprise.test", "name"),
29+
resource.TestCheckResourceAttrSet("data.github_enterprise.test", "created_at"),
30+
resource.TestCheckResourceAttrSet("data.github_enterprise.test", "url"),
31+
)
32+
33+
resource.Test(
34+
t,
35+
resource.TestCase{
36+
PreCheck: func() { skipUnlessMode(t, enterprise) },
37+
Providers: testAccProviders,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: config,
41+
Check: check,
42+
},
43+
},
44+
},
45+
)
46+
}

github/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func Provider() terraform.ResourceProvider {
106106
"github_dependabot_organization_secret": resourceGithubDependabotOrganizationSecret(),
107107
"github_dependabot_organization_secret_repositories": resourceGithubDependabotOrganizationSecretRepositories(),
108108
"github_dependabot_secret": resourceGithubDependabotSecret(),
109+
"github_enterprise_organization": resourceGithubEnterpriseOrganization(),
109110
"github_emu_group_mapping": resourceGithubEMUGroupMapping(),
110111
"github_issue": resourceGithubIssue(),
111112
"github_issue_label": resourceGithubIssueLabel(),
@@ -152,6 +153,7 @@ func Provider() terraform.ResourceProvider {
152153
"github_dependabot_organization_secrets": dataSourceGithubDependabotOrganizationSecrets(),
153154
"github_dependabot_public_key": dataSourceGithubDependabotPublicKey(),
154155
"github_dependabot_secrets": dataSourceGithubDependabotSecrets(),
156+
"github_enterprise": dataSourceGithubEnterprise(),
155157
"github_external_groups": dataSourceGithubExternalGroups(),
156158
"github_ip_ranges": dataSourceGithubIpRanges(),
157159
"github_membership": dataSourceGithubMembership(),

github/provider_utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
var testCollaborator string = os.Getenv("GITHUB_TEST_COLLABORATOR")
1111
var isEnterprise string = os.Getenv("ENTERPRISE_ACCOUNT")
12+
var testEnterprise string = os.Getenv("ENTERPRISE_SLUG")
1213
var testOrganization string = testOrganizationFunc()
1314
var testOwner string = os.Getenv("GITHUB_OWNER")
1415
var testToken string = os.Getenv("GITHUB_TOKEN")
@@ -49,6 +50,13 @@ func skipUnlessMode(t *testing.T, providerMode string) {
4950
} else {
5051
t.Log("GITHUB_TOKEN environment variable should be empty")
5152
}
53+
case enterprise:
54+
if os.Getenv("GITHUB_TOKEN") == "" {
55+
t.Log("GITHUB_TOKEN environment variable should be set")
56+
} else {
57+
return
58+
}
59+
5260
case individual:
5361
if os.Getenv("GITHUB_TOKEN") != "" && os.Getenv("GITHUB_OWNER") != "" {
5462
return
@@ -125,3 +133,4 @@ func testOwnerFunc() string {
125133
const anonymous = "anonymous"
126134
const individual = "individual"
127135
const organization = "organization"
136+
const enterprise = "enterprise"

0 commit comments

Comments
 (0)