1package billing
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// AccountsClient is the billing client provides access to billing resources for Azure subscriptions.
18type AccountsClient struct {
19	BaseClient
20}
21
22// NewAccountsClient creates an instance of the AccountsClient client.
23func NewAccountsClient(subscriptionID string) AccountsClient {
24	return NewAccountsClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewAccountsClientWithBaseURI creates an instance of the AccountsClient client using a custom endpoint.  Use this
28// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewAccountsClientWithBaseURI(baseURI string, subscriptionID string) AccountsClient {
30	return AccountsClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// Get get the billing account by id.
34// Parameters:
35// billingAccountName - billing Account Id.
36// expand - may be used to expand the invoiceSections and billingProfiles.
37func (client AccountsClient) Get(ctx context.Context, billingAccountName string, expand string) (result Account, err error) {
38	if tracing.IsEnabled() {
39		ctx = tracing.StartSpan(ctx, fqdn+"/AccountsClient.Get")
40		defer func() {
41			sc := -1
42			if result.Response.Response != nil {
43				sc = result.Response.Response.StatusCode
44			}
45			tracing.EndSpan(ctx, sc, err)
46		}()
47	}
48	req, err := client.GetPreparer(ctx, billingAccountName, expand)
49	if err != nil {
50		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "Get", nil, "Failure preparing request")
51		return
52	}
53
54	resp, err := client.GetSender(req)
55	if err != nil {
56		result.Response = autorest.Response{Response: resp}
57		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "Get", resp, "Failure sending request")
58		return
59	}
60
61	result, err = client.GetResponder(resp)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "Get", resp, "Failure responding to request")
64		return
65	}
66
67	return
68}
69
70// GetPreparer prepares the Get request.
71func (client AccountsClient) GetPreparer(ctx context.Context, billingAccountName string, expand string) (*http.Request, error) {
72	pathParameters := map[string]interface{}{
73		"billingAccountName": autorest.Encode("path", billingAccountName),
74	}
75
76	const APIVersion = "2018-11-01-preview"
77	queryParameters := map[string]interface{}{
78		"api-version": APIVersion,
79	}
80	if len(expand) > 0 {
81		queryParameters["$expand"] = autorest.Encode("query", expand)
82	}
83
84	preparer := autorest.CreatePreparer(
85		autorest.AsGet(),
86		autorest.WithBaseURL(client.BaseURI),
87		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountName}", pathParameters),
88		autorest.WithQueryParameters(queryParameters))
89	return preparer.Prepare((&http.Request{}).WithContext(ctx))
90}
91
92// GetSender sends the Get request. The method will close the
93// http.Response Body if it receives an error.
94func (client AccountsClient) GetSender(req *http.Request) (*http.Response, error) {
95	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
96}
97
98// GetResponder handles the response to the Get request. The method always
99// closes the http.Response Body.
100func (client AccountsClient) GetResponder(resp *http.Response) (result Account, err error) {
101	err = autorest.Respond(
102		resp,
103		azure.WithErrorUnlessStatusCode(http.StatusOK),
104		autorest.ByUnmarshallingJSON(&result),
105		autorest.ByClosing())
106	result.Response = autorest.Response{Response: resp}
107	return
108}
109
110// List lists all billing accounts for which a user has access.
111// Parameters:
112// expand - may be used to expand the invoiceSections and billingProfiles.
113func (client AccountsClient) List(ctx context.Context, expand string) (result AccountListResultPage, err error) {
114	if tracing.IsEnabled() {
115		ctx = tracing.StartSpan(ctx, fqdn+"/AccountsClient.List")
116		defer func() {
117			sc := -1
118			if result.alr.Response.Response != nil {
119				sc = result.alr.Response.Response.StatusCode
120			}
121			tracing.EndSpan(ctx, sc, err)
122		}()
123	}
124	result.fn = client.listNextResults
125	req, err := client.ListPreparer(ctx, expand)
126	if err != nil {
127		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "List", nil, "Failure preparing request")
128		return
129	}
130
131	resp, err := client.ListSender(req)
132	if err != nil {
133		result.alr.Response = autorest.Response{Response: resp}
134		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "List", resp, "Failure sending request")
135		return
136	}
137
138	result.alr, err = client.ListResponder(resp)
139	if err != nil {
140		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "List", resp, "Failure responding to request")
141		return
142	}
143	if result.alr.hasNextLink() && result.alr.IsEmpty() {
144		err = result.NextWithContext(ctx)
145		return
146	}
147
148	return
149}
150
151// ListPreparer prepares the List request.
152func (client AccountsClient) ListPreparer(ctx context.Context, expand string) (*http.Request, error) {
153	const APIVersion = "2018-11-01-preview"
154	queryParameters := map[string]interface{}{
155		"api-version": APIVersion,
156	}
157	if len(expand) > 0 {
158		queryParameters["$expand"] = autorest.Encode("query", expand)
159	}
160
161	preparer := autorest.CreatePreparer(
162		autorest.AsGet(),
163		autorest.WithBaseURL(client.BaseURI),
164		autorest.WithPath("/providers/Microsoft.Billing/billingAccounts"),
165		autorest.WithQueryParameters(queryParameters))
166	return preparer.Prepare((&http.Request{}).WithContext(ctx))
167}
168
169// ListSender sends the List request. The method will close the
170// http.Response Body if it receives an error.
171func (client AccountsClient) ListSender(req *http.Request) (*http.Response, error) {
172	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
173}
174
175// ListResponder handles the response to the List request. The method always
176// closes the http.Response Body.
177func (client AccountsClient) ListResponder(resp *http.Response) (result AccountListResult, err error) {
178	err = autorest.Respond(
179		resp,
180		azure.WithErrorUnlessStatusCode(http.StatusOK),
181		autorest.ByUnmarshallingJSON(&result),
182		autorest.ByClosing())
183	result.Response = autorest.Response{Response: resp}
184	return
185}
186
187// listNextResults retrieves the next set of results, if any.
188func (client AccountsClient) listNextResults(ctx context.Context, lastResults AccountListResult) (result AccountListResult, err error) {
189	req, err := lastResults.accountListResultPreparer(ctx)
190	if err != nil {
191		return result, autorest.NewErrorWithError(err, "billing.AccountsClient", "listNextResults", nil, "Failure preparing next results request")
192	}
193	if req == nil {
194		return
195	}
196	resp, err := client.ListSender(req)
197	if err != nil {
198		result.Response = autorest.Response{Response: resp}
199		return result, autorest.NewErrorWithError(err, "billing.AccountsClient", "listNextResults", resp, "Failure sending next results request")
200	}
201	result, err = client.ListResponder(resp)
202	if err != nil {
203		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "listNextResults", resp, "Failure responding to next results request")
204	}
205	return
206}
207
208// ListComplete enumerates all values, automatically crossing page boundaries as required.
209func (client AccountsClient) ListComplete(ctx context.Context, expand string) (result AccountListResultIterator, err error) {
210	if tracing.IsEnabled() {
211		ctx = tracing.StartSpan(ctx, fqdn+"/AccountsClient.List")
212		defer func() {
213			sc := -1
214			if result.Response().Response.Response != nil {
215				sc = result.page.Response().Response.Response.StatusCode
216			}
217			tracing.EndSpan(ctx, sc, err)
218		}()
219	}
220	result.page, err = client.List(ctx, expand)
221	return
222}
223
224// Update the operation to update a billing account.
225// Parameters:
226// billingAccountName - billing Account Id.
227// parameters - parameters supplied to the update billing account operation.
228func (client AccountsClient) Update(ctx context.Context, billingAccountName string, parameters AccountUpdateProperties) (result AccountsUpdateFuture, err error) {
229	if tracing.IsEnabled() {
230		ctx = tracing.StartSpan(ctx, fqdn+"/AccountsClient.Update")
231		defer func() {
232			sc := -1
233			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
234				sc = result.FutureAPI.Response().StatusCode
235			}
236			tracing.EndSpan(ctx, sc, err)
237		}()
238	}
239	req, err := client.UpdatePreparer(ctx, billingAccountName, parameters)
240	if err != nil {
241		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "Update", nil, "Failure preparing request")
242		return
243	}
244
245	result, err = client.UpdateSender(req)
246	if err != nil {
247		err = autorest.NewErrorWithError(err, "billing.AccountsClient", "Update", nil, "Failure sending request")
248		return
249	}
250
251	return
252}
253
254// UpdatePreparer prepares the Update request.
255func (client AccountsClient) UpdatePreparer(ctx context.Context, billingAccountName string, parameters AccountUpdateProperties) (*http.Request, error) {
256	pathParameters := map[string]interface{}{
257		"billingAccountName": autorest.Encode("path", billingAccountName),
258	}
259
260	const APIVersion = "2018-11-01-preview"
261	queryParameters := map[string]interface{}{
262		"api-version": APIVersion,
263	}
264
265	preparer := autorest.CreatePreparer(
266		autorest.AsContentType("application/json; charset=utf-8"),
267		autorest.AsPatch(),
268		autorest.WithBaseURL(client.BaseURI),
269		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountName}", pathParameters),
270		autorest.WithJSON(parameters),
271		autorest.WithQueryParameters(queryParameters))
272	return preparer.Prepare((&http.Request{}).WithContext(ctx))
273}
274
275// UpdateSender sends the Update request. The method will close the
276// http.Response Body if it receives an error.
277func (client AccountsClient) UpdateSender(req *http.Request) (future AccountsUpdateFuture, err error) {
278	var resp *http.Response
279	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
280	if err != nil {
281		return
282	}
283	var azf azure.Future
284	azf, err = azure.NewFutureFromResponse(resp)
285	future.FutureAPI = &azf
286	future.Result = future.result
287	return
288}
289
290// UpdateResponder handles the response to the Update request. The method always
291// closes the http.Response Body.
292func (client AccountsClient) UpdateResponder(resp *http.Response) (result Account, err error) {
293	err = autorest.Respond(
294		resp,
295		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
296		autorest.ByUnmarshallingJSON(&result),
297		autorest.ByClosing())
298	result.Response = autorest.Response{Response: resp}
299	return
300}
301