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// DepartmentsClient is the billing client provides access to billing resources for Azure subscriptions.
18type DepartmentsClient struct {
19	BaseClient
20}
21
22// NewDepartmentsClient creates an instance of the DepartmentsClient client.
23func NewDepartmentsClient(subscriptionID string) DepartmentsClient {
24	return NewDepartmentsClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewDepartmentsClientWithBaseURI creates an instance of the DepartmentsClient client using a custom endpoint.  Use
28// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewDepartmentsClientWithBaseURI(baseURI string, subscriptionID string) DepartmentsClient {
30	return DepartmentsClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// Get get the department by id.
34// Parameters:
35// billingAccountName - billing Account Id.
36// departmentName - department Id.
37// expand - may be used to expand the enrollmentAccounts.
38// filter - the filter supports 'eq', 'lt', 'gt', 'le', 'ge', and 'and'. It does not currently support 'ne',
39// 'or', or 'not'. Tag filter is a key value pair string where key and value is separated by a colon (:).
40func (client DepartmentsClient) Get(ctx context.Context, billingAccountName string, departmentName string, expand string, filter string) (result Department, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/DepartmentsClient.Get")
43		defer func() {
44			sc := -1
45			if result.Response.Response != nil {
46				sc = result.Response.Response.StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	req, err := client.GetPreparer(ctx, billingAccountName, departmentName, expand, filter)
52	if err != nil {
53		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "Get", nil, "Failure preparing request")
54		return
55	}
56
57	resp, err := client.GetSender(req)
58	if err != nil {
59		result.Response = autorest.Response{Response: resp}
60		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "Get", resp, "Failure sending request")
61		return
62	}
63
64	result, err = client.GetResponder(resp)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "Get", resp, "Failure responding to request")
67		return
68	}
69
70	return
71}
72
73// GetPreparer prepares the Get request.
74func (client DepartmentsClient) GetPreparer(ctx context.Context, billingAccountName string, departmentName string, expand string, filter string) (*http.Request, error) {
75	pathParameters := map[string]interface{}{
76		"billingAccountName": autorest.Encode("path", billingAccountName),
77		"departmentName":     autorest.Encode("path", departmentName),
78	}
79
80	const APIVersion = "2018-11-01-preview"
81	queryParameters := map[string]interface{}{
82		"api-version": APIVersion,
83	}
84	if len(expand) > 0 {
85		queryParameters["$expand"] = autorest.Encode("query", expand)
86	}
87	if len(filter) > 0 {
88		queryParameters["$filter"] = autorest.Encode("query", filter)
89	}
90
91	preparer := autorest.CreatePreparer(
92		autorest.AsGet(),
93		autorest.WithBaseURL(client.BaseURI),
94		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/departments/{departmentName}", pathParameters),
95		autorest.WithQueryParameters(queryParameters))
96	return preparer.Prepare((&http.Request{}).WithContext(ctx))
97}
98
99// GetSender sends the Get request. The method will close the
100// http.Response Body if it receives an error.
101func (client DepartmentsClient) GetSender(req *http.Request) (*http.Response, error) {
102	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
103}
104
105// GetResponder handles the response to the Get request. The method always
106// closes the http.Response Body.
107func (client DepartmentsClient) GetResponder(resp *http.Response) (result Department, err error) {
108	err = autorest.Respond(
109		resp,
110		azure.WithErrorUnlessStatusCode(http.StatusOK),
111		autorest.ByUnmarshallingJSON(&result),
112		autorest.ByClosing())
113	result.Response = autorest.Response{Response: resp}
114	return
115}
116
117// ListByBillingAccountName lists all departments for which a user has access.
118// Parameters:
119// billingAccountName - billing Account Id.
120// expand - may be used to expand the enrollmentAccounts.
121// filter - the filter supports 'eq', 'lt', 'gt', 'le', 'ge', and 'and'. It does not currently support 'ne',
122// 'or', or 'not'. Tag filter is a key value pair string where key and value is separated by a colon (:).
123func (client DepartmentsClient) ListByBillingAccountName(ctx context.Context, billingAccountName string, expand string, filter string) (result DepartmentListResultPage, err error) {
124	if tracing.IsEnabled() {
125		ctx = tracing.StartSpan(ctx, fqdn+"/DepartmentsClient.ListByBillingAccountName")
126		defer func() {
127			sc := -1
128			if result.dlr.Response.Response != nil {
129				sc = result.dlr.Response.Response.StatusCode
130			}
131			tracing.EndSpan(ctx, sc, err)
132		}()
133	}
134	result.fn = client.listByBillingAccountNameNextResults
135	req, err := client.ListByBillingAccountNamePreparer(ctx, billingAccountName, expand, filter)
136	if err != nil {
137		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "ListByBillingAccountName", nil, "Failure preparing request")
138		return
139	}
140
141	resp, err := client.ListByBillingAccountNameSender(req)
142	if err != nil {
143		result.dlr.Response = autorest.Response{Response: resp}
144		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "ListByBillingAccountName", resp, "Failure sending request")
145		return
146	}
147
148	result.dlr, err = client.ListByBillingAccountNameResponder(resp)
149	if err != nil {
150		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "ListByBillingAccountName", resp, "Failure responding to request")
151		return
152	}
153	if result.dlr.hasNextLink() && result.dlr.IsEmpty() {
154		err = result.NextWithContext(ctx)
155		return
156	}
157
158	return
159}
160
161// ListByBillingAccountNamePreparer prepares the ListByBillingAccountName request.
162func (client DepartmentsClient) ListByBillingAccountNamePreparer(ctx context.Context, billingAccountName string, expand string, filter string) (*http.Request, error) {
163	pathParameters := map[string]interface{}{
164		"billingAccountName": autorest.Encode("path", billingAccountName),
165	}
166
167	const APIVersion = "2018-11-01-preview"
168	queryParameters := map[string]interface{}{
169		"api-version": APIVersion,
170	}
171	if len(expand) > 0 {
172		queryParameters["$expand"] = autorest.Encode("query", expand)
173	}
174	if len(filter) > 0 {
175		queryParameters["$filter"] = autorest.Encode("query", filter)
176	}
177
178	preparer := autorest.CreatePreparer(
179		autorest.AsGet(),
180		autorest.WithBaseURL(client.BaseURI),
181		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/departments", pathParameters),
182		autorest.WithQueryParameters(queryParameters))
183	return preparer.Prepare((&http.Request{}).WithContext(ctx))
184}
185
186// ListByBillingAccountNameSender sends the ListByBillingAccountName request. The method will close the
187// http.Response Body if it receives an error.
188func (client DepartmentsClient) ListByBillingAccountNameSender(req *http.Request) (*http.Response, error) {
189	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
190}
191
192// ListByBillingAccountNameResponder handles the response to the ListByBillingAccountName request. The method always
193// closes the http.Response Body.
194func (client DepartmentsClient) ListByBillingAccountNameResponder(resp *http.Response) (result DepartmentListResult, err error) {
195	err = autorest.Respond(
196		resp,
197		azure.WithErrorUnlessStatusCode(http.StatusOK),
198		autorest.ByUnmarshallingJSON(&result),
199		autorest.ByClosing())
200	result.Response = autorest.Response{Response: resp}
201	return
202}
203
204// listByBillingAccountNameNextResults retrieves the next set of results, if any.
205func (client DepartmentsClient) listByBillingAccountNameNextResults(ctx context.Context, lastResults DepartmentListResult) (result DepartmentListResult, err error) {
206	req, err := lastResults.departmentListResultPreparer(ctx)
207	if err != nil {
208		return result, autorest.NewErrorWithError(err, "billing.DepartmentsClient", "listByBillingAccountNameNextResults", nil, "Failure preparing next results request")
209	}
210	if req == nil {
211		return
212	}
213	resp, err := client.ListByBillingAccountNameSender(req)
214	if err != nil {
215		result.Response = autorest.Response{Response: resp}
216		return result, autorest.NewErrorWithError(err, "billing.DepartmentsClient", "listByBillingAccountNameNextResults", resp, "Failure sending next results request")
217	}
218	result, err = client.ListByBillingAccountNameResponder(resp)
219	if err != nil {
220		err = autorest.NewErrorWithError(err, "billing.DepartmentsClient", "listByBillingAccountNameNextResults", resp, "Failure responding to next results request")
221	}
222	return
223}
224
225// ListByBillingAccountNameComplete enumerates all values, automatically crossing page boundaries as required.
226func (client DepartmentsClient) ListByBillingAccountNameComplete(ctx context.Context, billingAccountName string, expand string, filter string) (result DepartmentListResultIterator, err error) {
227	if tracing.IsEnabled() {
228		ctx = tracing.StartSpan(ctx, fqdn+"/DepartmentsClient.ListByBillingAccountName")
229		defer func() {
230			sc := -1
231			if result.Response().Response.Response != nil {
232				sc = result.page.Response().Response.Response.StatusCode
233			}
234			tracing.EndSpan(ctx, sc, err)
235		}()
236	}
237	result.page, err = client.ListByBillingAccountName(ctx, billingAccountName, expand, filter)
238	return
239}
240