1package consumption
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5//
6// Code generated by Microsoft (R) AutoRest Code Generator.
7// Changes may cause incorrect behavior and will be lost if the code is regenerated.
8
9import (
10	"context"
11	"github.com/Azure/go-autorest/autorest"
12	"github.com/Azure/go-autorest/autorest/azure"
13	"github.com/Azure/go-autorest/tracing"
14	"net/http"
15)
16
17// BalancesClient is the consumption management client provides access to consumption resources for Azure Enterprise
18// Subscriptions.
19type BalancesClient struct {
20	BaseClient
21}
22
23// NewBalancesClient creates an instance of the BalancesClient client.
24func NewBalancesClient(subscriptionID string) BalancesClient {
25	return NewBalancesClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewBalancesClientWithBaseURI creates an instance of the BalancesClient client using a custom endpoint.  Use this
29// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewBalancesClientWithBaseURI(baseURI string, subscriptionID string) BalancesClient {
31	return BalancesClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// GetByBillingAccount gets the balances for a scope by billingAccountId. Balances are available via this API only for
35// May 1, 2014 or later.
36// Parameters:
37// billingAccountID - billingAccount ID
38func (client BalancesClient) GetByBillingAccount(ctx context.Context, billingAccountID string) (result Balance, err error) {
39	if tracing.IsEnabled() {
40		ctx = tracing.StartSpan(ctx, fqdn+"/BalancesClient.GetByBillingAccount")
41		defer func() {
42			sc := -1
43			if result.Response.Response != nil {
44				sc = result.Response.Response.StatusCode
45			}
46			tracing.EndSpan(ctx, sc, err)
47		}()
48	}
49	req, err := client.GetByBillingAccountPreparer(ctx, billingAccountID)
50	if err != nil {
51		err = autorest.NewErrorWithError(err, "consumption.BalancesClient", "GetByBillingAccount", nil, "Failure preparing request")
52		return
53	}
54
55	resp, err := client.GetByBillingAccountSender(req)
56	if err != nil {
57		result.Response = autorest.Response{Response: resp}
58		err = autorest.NewErrorWithError(err, "consumption.BalancesClient", "GetByBillingAccount", resp, "Failure sending request")
59		return
60	}
61
62	result, err = client.GetByBillingAccountResponder(resp)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "consumption.BalancesClient", "GetByBillingAccount", resp, "Failure responding to request")
65		return
66	}
67
68	return
69}
70
71// GetByBillingAccountPreparer prepares the GetByBillingAccount request.
72func (client BalancesClient) GetByBillingAccountPreparer(ctx context.Context, billingAccountID string) (*http.Request, error) {
73	pathParameters := map[string]interface{}{
74		"billingAccountId": autorest.Encode("path", billingAccountID),
75	}
76
77	const APIVersion = "2019-10-01"
78	queryParameters := map[string]interface{}{
79		"api-version": APIVersion,
80	}
81
82	preparer := autorest.CreatePreparer(
83		autorest.AsGet(),
84		autorest.WithBaseURL(client.BaseURI),
85		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/providers/Microsoft.Consumption/balances", pathParameters),
86		autorest.WithQueryParameters(queryParameters))
87	return preparer.Prepare((&http.Request{}).WithContext(ctx))
88}
89
90// GetByBillingAccountSender sends the GetByBillingAccount request. The method will close the
91// http.Response Body if it receives an error.
92func (client BalancesClient) GetByBillingAccountSender(req *http.Request) (*http.Response, error) {
93	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
94}
95
96// GetByBillingAccountResponder handles the response to the GetByBillingAccount request. The method always
97// closes the http.Response Body.
98func (client BalancesClient) GetByBillingAccountResponder(resp *http.Response) (result Balance, err error) {
99	err = autorest.Respond(
100		resp,
101		azure.WithErrorUnlessStatusCode(http.StatusOK),
102		autorest.ByUnmarshallingJSON(&result),
103		autorest.ByClosing())
104	result.Response = autorest.Response{Response: resp}
105	return
106}
107
108// GetForBillingPeriodByBillingAccount gets the balances for a scope by billing period and billingAccountId. Balances
109// are available via this API only for May 1, 2014 or later.
110// Parameters:
111// billingAccountID - billingAccount ID
112// billingPeriodName - billing Period Name.
113func (client BalancesClient) GetForBillingPeriodByBillingAccount(ctx context.Context, billingAccountID string, billingPeriodName string) (result Balance, err error) {
114	if tracing.IsEnabled() {
115		ctx = tracing.StartSpan(ctx, fqdn+"/BalancesClient.GetForBillingPeriodByBillingAccount")
116		defer func() {
117			sc := -1
118			if result.Response.Response != nil {
119				sc = result.Response.Response.StatusCode
120			}
121			tracing.EndSpan(ctx, sc, err)
122		}()
123	}
124	req, err := client.GetForBillingPeriodByBillingAccountPreparer(ctx, billingAccountID, billingPeriodName)
125	if err != nil {
126		err = autorest.NewErrorWithError(err, "consumption.BalancesClient", "GetForBillingPeriodByBillingAccount", nil, "Failure preparing request")
127		return
128	}
129
130	resp, err := client.GetForBillingPeriodByBillingAccountSender(req)
131	if err != nil {
132		result.Response = autorest.Response{Response: resp}
133		err = autorest.NewErrorWithError(err, "consumption.BalancesClient", "GetForBillingPeriodByBillingAccount", resp, "Failure sending request")
134		return
135	}
136
137	result, err = client.GetForBillingPeriodByBillingAccountResponder(resp)
138	if err != nil {
139		err = autorest.NewErrorWithError(err, "consumption.BalancesClient", "GetForBillingPeriodByBillingAccount", resp, "Failure responding to request")
140		return
141	}
142
143	return
144}
145
146// GetForBillingPeriodByBillingAccountPreparer prepares the GetForBillingPeriodByBillingAccount request.
147func (client BalancesClient) GetForBillingPeriodByBillingAccountPreparer(ctx context.Context, billingAccountID string, billingPeriodName string) (*http.Request, error) {
148	pathParameters := map[string]interface{}{
149		"billingAccountId":  autorest.Encode("path", billingAccountID),
150		"billingPeriodName": autorest.Encode("path", billingPeriodName),
151	}
152
153	const APIVersion = "2019-10-01"
154	queryParameters := map[string]interface{}{
155		"api-version": APIVersion,
156	}
157
158	preparer := autorest.CreatePreparer(
159		autorest.AsGet(),
160		autorest.WithBaseURL(client.BaseURI),
161		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingPeriods/{billingPeriodName}/providers/Microsoft.Consumption/balances", pathParameters),
162		autorest.WithQueryParameters(queryParameters))
163	return preparer.Prepare((&http.Request{}).WithContext(ctx))
164}
165
166// GetForBillingPeriodByBillingAccountSender sends the GetForBillingPeriodByBillingAccount request. The method will close the
167// http.Response Body if it receives an error.
168func (client BalancesClient) GetForBillingPeriodByBillingAccountSender(req *http.Request) (*http.Response, error) {
169	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
170}
171
172// GetForBillingPeriodByBillingAccountResponder handles the response to the GetForBillingPeriodByBillingAccount request. The method always
173// closes the http.Response Body.
174func (client BalancesClient) GetForBillingPeriodByBillingAccountResponder(resp *http.Response) (result Balance, err error) {
175	err = autorest.Respond(
176		resp,
177		azure.WithErrorUnlessStatusCode(http.StatusOK),
178		autorest.ByUnmarshallingJSON(&result),
179		autorest.ByClosing())
180	result.Response = autorest.Response{Response: resp}
181	return
182}
183