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// CustomersClient is the billing client provides access to billing resources for Azure subscriptions.
18type CustomersClient struct {
19	BaseClient
20}
21
22// NewCustomersClient creates an instance of the CustomersClient client.
23func NewCustomersClient(subscriptionID string) CustomersClient {
24	return NewCustomersClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewCustomersClientWithBaseURI creates an instance of the CustomersClient 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 NewCustomersClientWithBaseURI(baseURI string, subscriptionID string) CustomersClient {
30	return CustomersClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// Get get the customer by id.
34// Parameters:
35// billingAccountName - billing Account Id.
36// customerName - customer Id.
37// expand - may be used to expand enabledAzureSkus, resellers.
38func (client CustomersClient) Get(ctx context.Context, billingAccountName string, customerName string, expand string) (result Customer, err error) {
39	if tracing.IsEnabled() {
40		ctx = tracing.StartSpan(ctx, fqdn+"/CustomersClient.Get")
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.GetPreparer(ctx, billingAccountName, customerName, expand)
50	if err != nil {
51		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "Get", nil, "Failure preparing request")
52		return
53	}
54
55	resp, err := client.GetSender(req)
56	if err != nil {
57		result.Response = autorest.Response{Response: resp}
58		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "Get", resp, "Failure sending request")
59		return
60	}
61
62	result, err = client.GetResponder(resp)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "Get", resp, "Failure responding to request")
65		return
66	}
67
68	return
69}
70
71// GetPreparer prepares the Get request.
72func (client CustomersClient) GetPreparer(ctx context.Context, billingAccountName string, customerName string, expand string) (*http.Request, error) {
73	pathParameters := map[string]interface{}{
74		"billingAccountName": autorest.Encode("path", billingAccountName),
75		"customerName":       autorest.Encode("path", customerName),
76	}
77
78	const APIVersion = "2018-11-01-preview"
79	queryParameters := map[string]interface{}{
80		"api-version": APIVersion,
81	}
82	if len(expand) > 0 {
83		queryParameters["$expand"] = autorest.Encode("query", expand)
84	}
85
86	preparer := autorest.CreatePreparer(
87		autorest.AsGet(),
88		autorest.WithBaseURL(client.BaseURI),
89		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/customers/{customerName}", pathParameters),
90		autorest.WithQueryParameters(queryParameters))
91	return preparer.Prepare((&http.Request{}).WithContext(ctx))
92}
93
94// GetSender sends the Get request. The method will close the
95// http.Response Body if it receives an error.
96func (client CustomersClient) GetSender(req *http.Request) (*http.Response, error) {
97	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
98}
99
100// GetResponder handles the response to the Get request. The method always
101// closes the http.Response Body.
102func (client CustomersClient) GetResponder(resp *http.Response) (result Customer, err error) {
103	err = autorest.Respond(
104		resp,
105		azure.WithErrorUnlessStatusCode(http.StatusOK),
106		autorest.ByUnmarshallingJSON(&result),
107		autorest.ByClosing())
108	result.Response = autorest.Response{Response: resp}
109	return
110}
111
112// ListByBillingAccountName lists all customers which the current user can work with on-behalf of a partner.
113// Parameters:
114// billingAccountName - billing Account Id.
115// filter - may be used to filter using hasPermission('{permissionId}') to only return customers for which the
116// caller has the specified permission.
117// skiptoken - skiptoken is only used if a previous operation returned a partial result. If a previous response
118// contains a nextLink element, the value of the nextLink element will include a skiptoken parameter that
119// specifies a starting point to use for subsequent calls.
120func (client CustomersClient) ListByBillingAccountName(ctx context.Context, billingAccountName string, filter string, skiptoken string) (result CustomerListResultPage, err error) {
121	if tracing.IsEnabled() {
122		ctx = tracing.StartSpan(ctx, fqdn+"/CustomersClient.ListByBillingAccountName")
123		defer func() {
124			sc := -1
125			if result.clr.Response.Response != nil {
126				sc = result.clr.Response.Response.StatusCode
127			}
128			tracing.EndSpan(ctx, sc, err)
129		}()
130	}
131	result.fn = client.listByBillingAccountNameNextResults
132	req, err := client.ListByBillingAccountNamePreparer(ctx, billingAccountName, filter, skiptoken)
133	if err != nil {
134		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "ListByBillingAccountName", nil, "Failure preparing request")
135		return
136	}
137
138	resp, err := client.ListByBillingAccountNameSender(req)
139	if err != nil {
140		result.clr.Response = autorest.Response{Response: resp}
141		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "ListByBillingAccountName", resp, "Failure sending request")
142		return
143	}
144
145	result.clr, err = client.ListByBillingAccountNameResponder(resp)
146	if err != nil {
147		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "ListByBillingAccountName", resp, "Failure responding to request")
148		return
149	}
150	if result.clr.hasNextLink() && result.clr.IsEmpty() {
151		err = result.NextWithContext(ctx)
152		return
153	}
154
155	return
156}
157
158// ListByBillingAccountNamePreparer prepares the ListByBillingAccountName request.
159func (client CustomersClient) ListByBillingAccountNamePreparer(ctx context.Context, billingAccountName string, filter string, skiptoken string) (*http.Request, error) {
160	pathParameters := map[string]interface{}{
161		"billingAccountName": autorest.Encode("path", billingAccountName),
162	}
163
164	const APIVersion = "2018-11-01-preview"
165	queryParameters := map[string]interface{}{
166		"api-version": APIVersion,
167	}
168	if len(filter) > 0 {
169		queryParameters["$filter"] = autorest.Encode("query", filter)
170	}
171	if len(skiptoken) > 0 {
172		queryParameters["$skiptoken"] = autorest.Encode("query", skiptoken)
173	}
174
175	preparer := autorest.CreatePreparer(
176		autorest.AsGet(),
177		autorest.WithBaseURL(client.BaseURI),
178		autorest.WithPathParameters("/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/customers", pathParameters),
179		autorest.WithQueryParameters(queryParameters))
180	return preparer.Prepare((&http.Request{}).WithContext(ctx))
181}
182
183// ListByBillingAccountNameSender sends the ListByBillingAccountName request. The method will close the
184// http.Response Body if it receives an error.
185func (client CustomersClient) ListByBillingAccountNameSender(req *http.Request) (*http.Response, error) {
186	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
187}
188
189// ListByBillingAccountNameResponder handles the response to the ListByBillingAccountName request. The method always
190// closes the http.Response Body.
191func (client CustomersClient) ListByBillingAccountNameResponder(resp *http.Response) (result CustomerListResult, err error) {
192	err = autorest.Respond(
193		resp,
194		azure.WithErrorUnlessStatusCode(http.StatusOK),
195		autorest.ByUnmarshallingJSON(&result),
196		autorest.ByClosing())
197	result.Response = autorest.Response{Response: resp}
198	return
199}
200
201// listByBillingAccountNameNextResults retrieves the next set of results, if any.
202func (client CustomersClient) listByBillingAccountNameNextResults(ctx context.Context, lastResults CustomerListResult) (result CustomerListResult, err error) {
203	req, err := lastResults.customerListResultPreparer(ctx)
204	if err != nil {
205		return result, autorest.NewErrorWithError(err, "billing.CustomersClient", "listByBillingAccountNameNextResults", nil, "Failure preparing next results request")
206	}
207	if req == nil {
208		return
209	}
210	resp, err := client.ListByBillingAccountNameSender(req)
211	if err != nil {
212		result.Response = autorest.Response{Response: resp}
213		return result, autorest.NewErrorWithError(err, "billing.CustomersClient", "listByBillingAccountNameNextResults", resp, "Failure sending next results request")
214	}
215	result, err = client.ListByBillingAccountNameResponder(resp)
216	if err != nil {
217		err = autorest.NewErrorWithError(err, "billing.CustomersClient", "listByBillingAccountNameNextResults", resp, "Failure responding to next results request")
218	}
219	return
220}
221
222// ListByBillingAccountNameComplete enumerates all values, automatically crossing page boundaries as required.
223func (client CustomersClient) ListByBillingAccountNameComplete(ctx context.Context, billingAccountName string, filter string, skiptoken string) (result CustomerListResultIterator, err error) {
224	if tracing.IsEnabled() {
225		ctx = tracing.StartSpan(ctx, fqdn+"/CustomersClient.ListByBillingAccountName")
226		defer func() {
227			sc := -1
228			if result.Response().Response.Response != nil {
229				sc = result.page.Response().Response.Response.StatusCode
230			}
231			tracing.EndSpan(ctx, sc, err)
232		}()
233	}
234	result.page, err = client.ListByBillingAccountName(ctx, billingAccountName, filter, skiptoken)
235	return
236}
237