Skip to content

Commit d075d01

Browse files
authored
Merge branch 'release-v6.9' into enterprise-scim
2 parents 34dd75e + c608f19 commit d075d01

16 files changed

+1626
-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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceGithubEnterpriseCostCenter() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceGithubEnterpriseCostCenterRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"enterprise_slug": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
Description: "The slug of the enterprise.",
21+
},
22+
"cost_center_id": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
Description: "The ID of the cost center.",
26+
},
27+
"name": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
Description: "The name of the cost center.",
31+
},
32+
"state": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
Description: "The state of the cost center.",
36+
},
37+
"azure_subscription": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
Description: "The Azure subscription associated with the cost center.",
41+
},
42+
"resources": {
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"type": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"name": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
},
56+
},
57+
},
58+
},
59+
}
60+
}
61+
62+
func dataSourceGithubEnterpriseCostCenterRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
63+
client := meta.(*Owner).v3client
64+
enterpriseSlug := d.Get("enterprise_slug").(string)
65+
costCenterID := d.Get("cost_center_id").(string)
66+
67+
ctx = context.WithValue(ctx, ctxId, fmt.Sprintf("%s/%s", enterpriseSlug, costCenterID))
68+
69+
cc, err := enterpriseCostCenterGet(ctx, client, enterpriseSlug, costCenterID)
70+
if err != nil {
71+
return diag.FromErr(err)
72+
}
73+
74+
d.SetId(costCenterID)
75+
_ = d.Set("name", cc.Name)
76+
77+
state := strings.ToLower(cc.State)
78+
if state == "" {
79+
state = "active"
80+
}
81+
_ = d.Set("state", state)
82+
_ = d.Set("azure_subscription", cc.AzureSubscription)
83+
84+
resources := make([]map[string]any, 0)
85+
for _, r := range cc.Resources {
86+
resources = append(resources, map[string]any{
87+
"type": r.Type,
88+
"name": r.Name,
89+
})
90+
}
91+
_ = d.Set("resources", resources)
92+
93+
return nil
94+
}
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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
10+
)
11+
12+
func dataSourceGithubEnterpriseCostCenters() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceGithubEnterpriseCostCentersRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"enterprise_slug": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
Description: "The slug of the enterprise.",
21+
},
22+
"state": {
23+
Type: schema.TypeString,
24+
Optional: true,
25+
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"active", "deleted"}, false), "state"),
26+
Description: "Filter cost centers by state.",
27+
},
28+
"cost_centers": {
29+
Type: schema.TypeSet,
30+
Computed: true,
31+
Elem: &schema.Resource{
32+
Schema: map[string]*schema.Schema{
33+
"id": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
"name": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"state": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
"azure_subscription": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"resources": {
50+
Type: schema.TypeList,
51+
Computed: true,
52+
Elem: &schema.Resource{
53+
Schema: map[string]*schema.Schema{
54+
"type": {Type: schema.TypeString, Computed: true},
55+
"name": {Type: schema.TypeString, Computed: true},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
},
62+
},
63+
}
64+
}
65+
66+
func dataSourceGithubEnterpriseCostCentersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
67+
client := meta.(*Owner).v3client
68+
enterpriseSlug := d.Get("enterprise_slug").(string)
69+
state := ""
70+
if v, ok := d.GetOk("state"); ok {
71+
state = v.(string)
72+
}
73+
74+
ctx = context.WithValue(ctx, ctxId, fmt.Sprintf("%s/cost-centers", enterpriseSlug))
75+
centers, err := enterpriseCostCentersList(ctx, client, enterpriseSlug, state)
76+
if err != nil {
77+
return diag.FromErr(err)
78+
}
79+
80+
items := make([]any, 0, len(centers))
81+
for _, cc := range centers {
82+
resources := make([]map[string]any, 0)
83+
for _, r := range cc.Resources {
84+
resources = append(resources, map[string]any{"type": r.Type, "name": r.Name})
85+
}
86+
items = append(items, map[string]any{
87+
"id": cc.ID,
88+
"name": cc.Name,
89+
"state": cc.State,
90+
"azure_subscription": cc.AzureSubscription,
91+
"resources": resources,
92+
})
93+
}
94+
95+
d.SetId(fmt.Sprintf("%s/%s", enterpriseSlug, state))
96+
_ = d.Set("cost_centers", items)
97+
return nil
98+
}

0 commit comments

Comments
 (0)