-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling_service.go
157 lines (135 loc) · 4.03 KB
/
billing_service.go
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
package go_gcp_service
import (
"context"
"fmt"
"cloud.google.com/go/billing/budgets/apiv1/budgetspb"
"google.golang.org/api/cloudbilling/v1"
"google.golang.org/api/iterator"
"google.golang.org/genproto/googleapis/type/money"
)
type billingServiceLogger interface {
Errorf(template string, args ...interface{})
Panic(args ...interface{})
}
// BillingService -> handles the gcp billing related functions
type BillingService struct {
ProjectName string
BillingAccountID string
BudgetDisplayName string
BudgetAmount int64
Logger billingServiceLogger
GcpBilling BillingClient
BudgetClient BudgetClient
}
// NewGCPBillingService for the GCPBilling struct
func NewGCPBillingService(
configService BillingService,
) BillingService {
return configService
}
// GetBillingInfo Get Billing info for certain date
func (s BillingService) GetBillingInfo() (*cloudbilling.ProjectBillingInfo, error) {
projectName := fmt.Sprintf("projects/%s", s.ProjectName)
billingInfo, err := s.GcpBilling.Projects.GetBillingInfo(projectName).Do()
return billingInfo, err
}
// GetExistingBudgetList Get Billing info for certain date
func (s BillingService) GetExistingBudgetList(
ctx context.Context,
) (*budgetspb.Budget, error) {
var budgetList []*budgetspb.Budget
var err error
parentId := fmt.Sprintf("billingAccounts/%s", s.BillingAccountID)
req := budgetspb.ListBudgetsRequest{
Parent: parentId,
}
budgetsIter := s.BudgetClient.ListBudgets(ctx, &req)
for {
budget, err := budgetsIter.Next()
if err == iterator.Done {
break
}
if err != nil {
s.Logger.Errorf("failed to retrieve budget: %v", err)
}
budgetList = append(budgetList, budget)
}
if len(budgetList) > 0 {
return budgetList[0], err
}
return nil, err
}
func (s BillingService) GetBudgetCreateUpdateRequest() *budgetspb.Budget {
projectName := fmt.Sprintf("projects/%s", s.ProjectName)
budget := &budgetspb.Budget{
DisplayName: "Project Budget",
Name: s.BudgetDisplayName,
BudgetFilter: &budgetspb.Filter{
CreditTypesTreatment: budgetspb.Filter_INCLUDE_ALL_CREDITS,
Projects: []string{projectName},
},
Amount: &budgetspb.BudgetAmount{
BudgetAmount: &budgetspb.BudgetAmount_SpecifiedAmount{
SpecifiedAmount: &money.Money{
Units: s.BudgetAmount,
Nanos: 0,
CurrencyCode: "JPY",
},
},
},
ThresholdRules: []*budgetspb.ThresholdRule{
{
ThresholdPercent: 0.25,
SpendBasis: budgetspb.ThresholdRule_CURRENT_SPEND,
},
{
ThresholdPercent: 0.50,
SpendBasis: budgetspb.ThresholdRule_CURRENT_SPEND,
},
{
ThresholdPercent: 0.75,
SpendBasis: budgetspb.ThresholdRule_CURRENT_SPEND,
},
{
ThresholdPercent: 1,
SpendBasis: budgetspb.ThresholdRule_CURRENT_SPEND,
},
},
}
return budget
}
func (s BillingService) CreateBudget(ctx context.Context) (*budgetspb.Budget, error) {
parentId := fmt.Sprintf("billingAccounts/%s", s.BillingAccountID)
budget := s.GetBudgetCreateUpdateRequest()
createRequest := budgetspb.CreateBudgetRequest{
Parent: parentId,
Budget: budget,
}
billingInfo, err := s.BudgetClient.CreateBudget(ctx, &createRequest)
if err != nil {
s.Logger.Errorf("failed to create budget: %v\n", err)
}
return billingInfo, err
}
func (s BillingService) CreateOrUpdateBudget(ctx context.Context) (*budgetspb.Budget, error) {
budget, _ := s.GetExistingBudgetList(ctx)
if budget == nil {
return s.CreateBudget(ctx)
} else {
return s.EditBudget(ctx, budget)
}
}
func (s BillingService) EditBudget(ctx context.Context, budget *budgetspb.Budget) (*budgetspb.Budget, error) {
// Modify the budget configuration here
envBudget := s.GetBudgetCreateUpdateRequest()
budget.Amount.BudgetAmount = envBudget.Amount.BudgetAmount
budget.ThresholdRules = envBudget.ThresholdRules
editRequest := budgetspb.UpdateBudgetRequest{
Budget: budget,
}
billingInfo, err := s.BudgetClient.UpdateBudget(ctx, &editRequest)
if err != nil {
s.Logger.Errorf("failed to retrieve budget: %v\n", err)
}
return billingInfo, err
}