Skip to content

Commit

Permalink
[feat] Add support for provisioning github orgs to enterprises.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Jan 11, 2023
2 parents b0e28a4 + 4ee09d4 commit f60e556
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 0 deletions.
68 changes: 68 additions & 0 deletions github/data_source_github_enterprise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package github

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/shurcooL/githubv4"
)

func dataSourceGithubEnterprise() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubEnterpriseRead,
Schema: map[string]*schema.Schema{
"slug": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGithubEnterpriseRead(data *schema.ResourceData, meta interface{}) error {
var query struct {
Enterprise struct {
ID githubv4.String
Name githubv4.String
Description githubv4.String
CreatedAt githubv4.String
Url githubv4.String
} `graphql:"enterprise(slug: $slug)"`
}

slug := data.Get("slug").(string)
client := meta.(*Owner).v4client
variables := map[string]interface{}{
"slug": githubv4.String(slug),
}
err := client.Query(context.Background(), &query, variables)
if err != nil {
return err
}
if query.Enterprise.ID == "" {
return fmt.Errorf("could not find enterprise %v", slug)
}
data.SetId(string(query.Enterprise.ID))
data.Set("name", query.Enterprise.Name)
data.Set("description", query.Enterprise.Description)
data.Set("created_at", query.Enterprise.CreatedAt)
data.Set("url", query.Enterprise.Url)

return nil
}
46 changes: 46 additions & 0 deletions github/data_source_github_enterprise_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package github

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"testing"
)

func TestAccGithubEnterpriseDataSource(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")
}

config := fmt.Sprintf(`
data "github_enterprise" "test" {
slug = "%s"
}
`,
testEnterprise,
)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.github_enterprise.test", "slug", testEnterprise),
resource.TestCheckResourceAttrSet("data.github_enterprise.test", "name"),
resource.TestCheckResourceAttrSet("data.github_enterprise.test", "created_at"),
resource.TestCheckResourceAttrSet("data.github_enterprise.test", "url"),
)

resource.Test(
t,
resource.TestCase{
PreCheck: func() { skipUnlessMode(t, enterprise) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
},
)
}
2 changes: 2 additions & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func Provider() terraform.ResourceProvider {
"github_dependabot_organization_secret": resourceGithubDependabotOrganizationSecret(),
"github_dependabot_organization_secret_repositories": resourceGithubDependabotOrganizationSecretRepositories(),
"github_dependabot_secret": resourceGithubDependabotSecret(),
"github_enterprise_organization": resourceGithubEnterpriseOrganization(),
"github_emu_group_mapping": resourceGithubEMUGroupMapping(),
"github_issue": resourceGithubIssue(),
"github_issue_label": resourceGithubIssueLabel(),
Expand Down Expand Up @@ -152,6 +153,7 @@ func Provider() terraform.ResourceProvider {
"github_dependabot_organization_secrets": dataSourceGithubDependabotOrganizationSecrets(),
"github_dependabot_public_key": dataSourceGithubDependabotPublicKey(),
"github_dependabot_secrets": dataSourceGithubDependabotSecrets(),
"github_enterprise": dataSourceGithubEnterprise(),
"github_external_groups": dataSourceGithubExternalGroups(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_membership": dataSourceGithubMembership(),
Expand Down
9 changes: 9 additions & 0 deletions github/provider_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var testCollaborator string = os.Getenv("GITHUB_TEST_COLLABORATOR")
var isEnterprise string = os.Getenv("ENTERPRISE_ACCOUNT")
var testEnterprise string = os.Getenv("ENTERPRISE_SLUG")
var testOrganization string = testOrganizationFunc()
var testOwner string = os.Getenv("GITHUB_OWNER")
var testToken string = os.Getenv("GITHUB_TOKEN")
Expand Down Expand Up @@ -49,6 +50,13 @@ func skipUnlessMode(t *testing.T, providerMode string) {
} else {
t.Log("GITHUB_TOKEN environment variable should be empty")
}
case enterprise:
if os.Getenv("GITHUB_TOKEN") == "" {
t.Log("GITHUB_TOKEN environment variable should be set")
} else {
return
}

case individual:
if os.Getenv("GITHUB_TOKEN") != "" && os.Getenv("GITHUB_OWNER") != "" {
return
Expand Down Expand Up @@ -125,3 +133,4 @@ func testOwnerFunc() string {
const anonymous = "anonymous"
const individual = "individual"
const organization = "organization"
const enterprise = "enterprise"
Loading

0 comments on commit f60e556

Please sign in to comment.