Skip to content

Commit 4e6cb30

Browse files
authored
Merge branch 'alpha' into enterprise-teams
2 parents 17ba121 + ac7503c commit 4e6cb30

16 files changed

+1530
-0
lines changed

examples/cost_centers/main.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
terraform {
2+
required_providers {
3+
github = {
4+
source = "integrations/github"
5+
version = "~> 6.0"
6+
}
7+
}
8+
}
9+
10+
provider "github" {
11+
token = var.github_token
12+
}
13+
14+
variable "github_token" {
15+
description = "GitHub classic personal access token (PAT) for an enterprise admin"
16+
type = string
17+
sensitive = true
18+
}
19+
20+
variable "enterprise_slug" {
21+
description = "The GitHub Enterprise slug"
22+
type = string
23+
}
24+
25+
variable "cost_center_name" {
26+
description = "Name for the cost center"
27+
type = string
28+
}
29+
30+
variable "users" {
31+
description = "Usernames to assign to the cost center"
32+
type = list(string)
33+
default = []
34+
}
35+
36+
variable "organizations" {
37+
description = "Organization logins to assign to the cost center"
38+
type = list(string)
39+
default = []
40+
}
41+
42+
variable "repositories" {
43+
description = "Repositories (full name, e.g. org/repo) to assign to the cost center"
44+
type = list(string)
45+
default = []
46+
}
47+
48+
resource "github_enterprise_cost_center" "example" {
49+
enterprise_slug = var.enterprise_slug
50+
name = var.cost_center_name
51+
}
52+
53+
# Authoritative assignments: Terraform will add/remove to match these lists.
54+
resource "github_enterprise_cost_center_resources" "example" {
55+
enterprise_slug = var.enterprise_slug
56+
cost_center_id = github_enterprise_cost_center.example.id
57+
58+
users = var.users
59+
organizations = var.organizations
60+
repositories = var.repositories
61+
}
62+
63+
data "github_enterprise_cost_center" "by_id" {
64+
enterprise_slug = var.enterprise_slug
65+
cost_center_id = github_enterprise_cost_center.example.id
66+
67+
depends_on = [github_enterprise_cost_center_resources.example]
68+
}
69+
70+
data "github_enterprise_cost_centers" "active" {
71+
enterprise_slug = var.enterprise_slug
72+
state = "active"
73+
74+
depends_on = [github_enterprise_cost_center.example]
75+
}
76+
77+
output "cost_center" {
78+
description = "Created cost center"
79+
value = {
80+
id = github_enterprise_cost_center.example.id
81+
name = github_enterprise_cost_center.example.name
82+
state = github_enterprise_cost_center.example.state
83+
azure_subscription = github_enterprise_cost_center.example.azure_subscription
84+
}
85+
}
86+
87+
output "cost_center_resources" {
88+
description = "Effective assignments (read from API)"
89+
value = {
90+
users = sort(tolist(github_enterprise_cost_center_resources.example.users))
91+
organizations = sort(tolist(github_enterprise_cost_center_resources.example.organizations))
92+
repositories = sort(tolist(github_enterprise_cost_center_resources.example.repositories))
93+
}
94+
}
95+
96+
output "cost_center_from_data_source" {
97+
description = "Cost center fetched by data source"
98+
value = {
99+
id = data.github_enterprise_cost_center.by_id.cost_center_id
100+
name = data.github_enterprise_cost_center.by_id.name
101+
state = data.github_enterprise_cost_center.by_id.state
102+
}
103+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGithubEnterpriseCostCenter() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubEnterpriseCostCenterRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"enterprise_slug": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
Description: "The slug of the enterprise.",
20+
},
21+
"cost_center_id": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
Description: "The ID of the cost center.",
25+
},
26+
"name": {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
Description: "The name of the cost center.",
30+
},
31+
"state": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "The state of the cost center.",
35+
},
36+
"azure_subscription": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
Description: "The Azure subscription associated with the cost center.",
40+
},
41+
"resources": {
42+
Type: schema.TypeList,
43+
Computed: true,
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"type": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"name": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
},
55+
},
56+
},
57+
},
58+
}
59+
}
60+
61+
func dataSourceGithubEnterpriseCostCenterRead(d *schema.ResourceData, meta any) error {
62+
client := meta.(*Owner).v3client
63+
enterpriseSlug := d.Get("enterprise_slug").(string)
64+
costCenterID := d.Get("cost_center_id").(string)
65+
66+
ctx := context.WithValue(context.Background(), ctxId, fmt.Sprintf("%s/%s", enterpriseSlug, costCenterID))
67+
68+
cc, err := enterpriseCostCenterGet(ctx, client, enterpriseSlug, costCenterID)
69+
if err != nil {
70+
return err
71+
}
72+
73+
d.SetId(costCenterID)
74+
_ = d.Set("name", cc.Name)
75+
76+
state := strings.ToLower(cc.State)
77+
if state == "" {
78+
state = "active"
79+
}
80+
_ = d.Set("state", state)
81+
_ = d.Set("azure_subscription", cc.AzureSubscription)
82+
83+
resources := make([]map[string]any, 0)
84+
for _, r := range cc.Resources {
85+
resources = append(resources, map[string]any{
86+
"type": r.Type,
87+
"name": r.Name,
88+
})
89+
}
90+
_ = d.Set("resources", resources)
91+
92+
return nil
93+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccGithubEnterpriseCostCenterDataSource(t *testing.T) {
12+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
13+
14+
if isEnterprise != "true" {
15+
t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false")
16+
}
17+
if testEnterprise == "" {
18+
t.Skip("Skipping because `ENTERPRISE_SLUG` is not set")
19+
}
20+
21+
config := fmt.Sprintf(`
22+
data "github_enterprise" "enterprise" {
23+
slug = "%s"
24+
}
25+
26+
resource "github_enterprise_cost_center" "test" {
27+
enterprise_slug = data.github_enterprise.enterprise.slug
28+
name = "tf-acc-test-%s"
29+
}
30+
31+
data "github_enterprise_cost_center" "test" {
32+
enterprise_slug = data.github_enterprise.enterprise.slug
33+
cost_center_id = github_enterprise_cost_center.test.id
34+
}
35+
`, testEnterprise, randomID)
36+
37+
check := resource.ComposeTestCheckFunc(
38+
resource.TestCheckResourceAttrPair("data.github_enterprise_cost_center.test", "cost_center_id", "github_enterprise_cost_center.test", "id"),
39+
resource.TestCheckResourceAttrPair("data.github_enterprise_cost_center.test", "name", "github_enterprise_cost_center.test", "name"),
40+
resource.TestCheckResourceAttr("data.github_enterprise_cost_center.test", "state", "active"),
41+
)
42+
43+
resource.Test(t, resource.TestCase{
44+
PreCheck: func() { skipUnlessMode(t, enterprise) },
45+
Providers: testAccProviders,
46+
Steps: []resource.TestStep{{Config: config, Check: check}},
47+
})
48+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
9+
)
10+
11+
func dataSourceGithubEnterpriseCostCenters() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGithubEnterpriseCostCentersRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"enterprise_slug": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
Description: "The slug of the enterprise.",
20+
},
21+
"state": {
22+
Type: schema.TypeString,
23+
Optional: true,
24+
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"active", "deleted"}, false), "state"),
25+
Description: "Filter cost centers by state.",
26+
},
27+
"cost_centers": {
28+
Type: schema.TypeSet,
29+
Computed: true,
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
"id": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"name": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
"state": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
},
44+
"azure_subscription": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"resources": {
49+
Type: schema.TypeList,
50+
Computed: true,
51+
Elem: &schema.Resource{
52+
Schema: map[string]*schema.Schema{
53+
"type": {Type: schema.TypeString, Computed: true},
54+
"name": {Type: schema.TypeString, Computed: true},
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
},
62+
}
63+
}
64+
65+
func dataSourceGithubEnterpriseCostCentersRead(d *schema.ResourceData, meta any) error {
66+
client := meta.(*Owner).v3client
67+
enterpriseSlug := d.Get("enterprise_slug").(string)
68+
state := ""
69+
if v, ok := d.GetOk("state"); ok {
70+
state = v.(string)
71+
}
72+
73+
ctx := context.WithValue(context.Background(), ctxId, fmt.Sprintf("%s/cost-centers", enterpriseSlug))
74+
centers, err := enterpriseCostCentersList(ctx, client, enterpriseSlug, state)
75+
if err != nil {
76+
return err
77+
}
78+
79+
items := make([]any, 0, len(centers))
80+
for _, cc := range centers {
81+
resources := make([]map[string]any, 0)
82+
for _, r := range cc.Resources {
83+
resources = append(resources, map[string]any{"type": r.Type, "name": r.Name})
84+
}
85+
items = append(items, map[string]any{
86+
"id": cc.ID,
87+
"name": cc.Name,
88+
"state": cc.State,
89+
"azure_subscription": cc.AzureSubscription,
90+
"resources": resources,
91+
})
92+
}
93+
94+
d.SetId(fmt.Sprintf("%s/%s", enterpriseSlug, state))
95+
_ = d.Set("cost_centers", items)
96+
return nil
97+
}

0 commit comments

Comments
 (0)