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 1
Expand file tree
/
Copy pathdata_source_github_enterprise_billing_premium_request_usage.go
More file actions
213 lines (204 loc) · 6.47 KB
/
data_source_github_enterprise_billing_premium_request_usage.go
File metadata and controls
213 lines (204 loc) · 6.47 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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 dataSourceGithubEnterpriseBillingPremiumRequestUsage() *schema.Resource {
return &schema.Resource{
Description: "Gets a billing premium request usage report for a GitHub enterprise.",
ReadContext: dataSourceGithubEnterpriseBillingPremiumRequestUsageRead,
Schema: map[string]*schema.Schema{
"enterprise_slug": {
Type: schema.TypeString,
Required: true,
Description: "The slug of the enterprise.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty),
},
"year": {
Type: schema.TypeInt,
Optional: true,
Description: "If specified, only return results for a single year.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(2000)),
},
"month": {
Type: schema.TypeInt,
Optional: true,
Description: "If specified, only return results for a single month. Value between 1 and 12.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(1, 12)),
},
"day": {
Type: schema.TypeInt,
Optional: true,
Description: "If specified, only return results for a single day. Value between 1 and 31.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(1, 31)),
},
"organization": {
Type: schema.TypeString,
Optional: true,
Description: "The organization name to query usage for.",
},
"user": {
Type: schema.TypeString,
Optional: true,
Description: "The user name to query usage for.",
},
"model": {
Type: schema.TypeString,
Optional: true,
Description: "The model name to query usage for.",
},
"product": {
Type: schema.TypeString,
Optional: true,
Description: "The product name to query usage for.",
},
"cost_center_id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID corresponding to a cost center. Use `none` to target usage not associated to any cost center.",
},
"time_period": {
Type: schema.TypeList,
Computed: true,
Description: "The time period of the report.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"year": {
Type: schema.TypeInt,
Computed: true,
Description: "The year of the time period.",
},
"month": {
Type: schema.TypeInt,
Computed: true,
Description: "The month of the time period.",
},
"day": {
Type: schema.TypeInt,
Computed: true,
Description: "The day of the time period.",
},
},
},
},
"enterprise": {
Type: schema.TypeString,
Computed: true,
Description: "The enterprise name from the report.",
},
"usage_items": {
Type: schema.TypeList,
Computed: true,
Description: "The list of premium request usage items.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"product": {
Type: schema.TypeString,
Computed: true,
Description: "The product name.",
},
"sku": {
Type: schema.TypeString,
Computed: true,
Description: "The SKU name.",
},
"model": {
Type: schema.TypeString,
Computed: true,
Description: "The model name.",
},
"unit_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of unit for the usage.",
},
"price_per_unit": {
Type: schema.TypeFloat,
Computed: true,
Description: "The price per unit of usage.",
},
"gross_quantity": {
Type: schema.TypeFloat,
Computed: true,
Description: "The gross quantity of usage.",
},
"gross_amount": {
Type: schema.TypeFloat,
Computed: true,
Description: "The gross amount of usage.",
},
"discount_quantity": {
Type: schema.TypeFloat,
Computed: true,
Description: "The discount quantity applied.",
},
"discount_amount": {
Type: schema.TypeFloat,
Computed: true,
Description: "The discount amount applied.",
},
"net_quantity": {
Type: schema.TypeFloat,
Computed: true,
Description: "The net quantity after discounts.",
},
"net_amount": {
Type: schema.TypeFloat,
Computed: true,
Description: "The net amount after discounts.",
},
},
},
},
},
}
}
func dataSourceGithubEnterpriseBillingPremiumRequestUsageRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
enterpriseSlug := d.Get("enterprise_slug").(string)
opts := &EnterprisePremiumRequestUsageOptions{}
if yearVal, ok := d.GetOk("year"); ok {
opts.Year = github.Ptr(yearVal.(int))
}
if monthVal, ok := d.GetOk("month"); ok {
opts.Month = github.Ptr(monthVal.(int))
}
if dayVal, ok := d.GetOk("day"); ok {
opts.Day = github.Ptr(dayVal.(int))
}
if orgVal, ok := d.GetOk("organization"); ok {
opts.Organization = github.Ptr(orgVal.(string))
}
if userVal, ok := d.GetOk("user"); ok {
opts.User = github.Ptr(userVal.(string))
}
if modelVal, ok := d.GetOk("model"); ok {
opts.Model = github.Ptr(modelVal.(string))
}
if productVal, ok := d.GetOk("product"); ok {
opts.Product = github.Ptr(productVal.(string))
}
if costCenterID, ok := d.GetOk("cost_center_id"); ok {
opts.CostCenterID = github.Ptr(costCenterID.(string))
}
report, err := getEnterprisePremiumRequestUsage(ctx, client, enterpriseSlug, opts)
if err != nil {
return diag.Errorf("error getting enterprise billing premium request usage for %q: %s", enterpriseSlug, err)
}
d.SetId(buildTwoPartID(enterpriseSlug, "billing-premium-request-usage"))
if err := d.Set("time_period", flattenTimePeriod(report.TimePeriod)); err != nil {
return diag.FromErr(err)
}
if report.Enterprise != nil {
if err := d.Set("enterprise", *report.Enterprise); err != nil {
return diag.FromErr(err)
}
}
if err := d.Set("usage_items", flattenPremiumRequestUsageItems(report.UsageItems)); err != nil {
return diag.FromErr(err)
}
return nil
}