This repository was archived by the owner on Apr 15, 2026. It is now read-only.
forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_enterprise_cost_centers.go
More file actions
100 lines (91 loc) · 2.76 KB
/
data_source_github_enterprise_cost_centers.go
File metadata and controls
100 lines (91 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package github
import (
"context"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceGithubEnterpriseCostCenters() *schema.Resource {
return &schema.Resource{
Description: "Retrieves a list of GitHub enterprise cost centers.",
ReadContext: dataSourceGithubEnterpriseCostCentersRead,
Schema: map[string]*schema.Schema{
"enterprise_slug": {
Type: schema.TypeString,
Required: true,
Description: "The slug of the enterprise.",
},
"state": {
Type: schema.TypeString,
Optional: true,
Default: "all",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"all", "active", "deleted"}, false)),
Description: "Filter cost centers by state. Valid values are 'all', 'active', and 'deleted'.",
},
"cost_centers": {
Type: schema.TypeSet,
Computed: true,
Description: "The list of cost centers.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The cost center ID.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the cost center.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "The state of the cost center.",
},
"azure_subscription": {
Type: schema.TypeString,
Computed: true,
Description: "The Azure subscription associated with the cost center.",
},
},
},
},
},
}
}
func dataSourceGithubEnterpriseCostCentersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
enterpriseSlug := d.Get("enterprise_slug").(string)
stateFilter := d.Get("state").(string)
var opts github.ListCostCenterOptions
if stateFilter != "all" {
opts.State = &stateFilter
}
result, _, err := client.Enterprise.ListCostCenters(ctx, enterpriseSlug, &opts)
if err != nil {
return diag.FromErr(err)
}
items := make([]any, 0, len(result.CostCenters))
for _, cc := range result.CostCenters {
if cc == nil {
continue
}
items = append(items, map[string]any{
"id": cc.ID,
"name": cc.Name,
"state": cc.GetState(),
"azure_subscription": cc.GetAzureSubscription(),
})
}
id, err := buildID(enterpriseSlug, stateFilter)
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
if err := d.Set("cost_centers", items); err != nil {
return diag.FromErr(err)
}
return nil
}