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_center.go
More file actions
113 lines (102 loc) · 3.12 KB
/
data_source_github_enterprise_cost_center.go
File metadata and controls
113 lines (102 loc) · 3.12 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
101
102
103
104
105
106
107
108
109
110
111
112
113
package github
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubEnterpriseCostCenter() *schema.Resource {
return &schema.Resource{
Description: "Retrieves information about a specific GitHub enterprise cost center.",
ReadContext: dataSourceGithubEnterpriseCostCenterRead,
Schema: map[string]*schema.Schema{
"enterprise_slug": {
Type: schema.TypeString,
Required: true,
Description: "The slug of the enterprise.",
},
"cost_center_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the cost center.",
},
"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.",
},
"users": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The usernames assigned to this cost center.",
},
"organizations": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The organization logins assigned to this cost center.",
},
"repositories": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The repositories (full name) assigned to this cost center.",
},
},
}
}
func dataSourceGithubEnterpriseCostCenterRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
enterpriseSlug := d.Get("enterprise_slug").(string)
costCenterID := d.Get("cost_center_id").(string)
cc, _, err := client.Enterprise.GetCostCenter(ctx, enterpriseSlug, costCenterID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(costCenterID)
if err := d.Set("name", cc.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("state", cc.GetState()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("azure_subscription", cc.GetAzureSubscription()); err != nil {
return diag.FromErr(err)
}
users := make([]string, 0)
organizations := make([]string, 0)
repositories := make([]string, 0)
for _, resource := range cc.Resources {
if resource == nil {
continue
}
switch resource.Type {
case CostCenterResourceTypeUser:
users = append(users, resource.Name)
case CostCenterResourceTypeOrg:
organizations = append(organizations, resource.Name)
case CostCenterResourceTypeRepo:
repositories = append(repositories, resource.Name)
}
}
if err := d.Set("users", users); err != nil {
return diag.FromErr(err)
}
if err := d.Set("organizations", organizations); err != nil {
return diag.FromErr(err)
}
if err := d.Set("repositories", repositories); err != nil {
return diag.FromErr(err)
}
return nil
}