1package dtl
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/tracing"
25	"net/http"
26)
27
28// CostClient is the azure DevTest Labs REST API version 2015-05-21-preview.
29type CostClient struct {
30	BaseClient
31}
32
33// NewCostClient creates an instance of the CostClient client.
34func NewCostClient(subscriptionID string) CostClient {
35	return NewCostClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewCostClientWithBaseURI creates an instance of the CostClient client using a custom endpoint.  Use this when
39// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
40func NewCostClientWithBaseURI(baseURI string, subscriptionID string) CostClient {
41	return CostClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// GetResource get cost.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// labName - the name of the lab.
48// name - the name of the cost.
49func (client CostClient) GetResource(ctx context.Context, resourceGroupName string, labName string, name string) (result Cost, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/CostClient.GetResource")
52		defer func() {
53			sc := -1
54			if result.Response.Response != nil {
55				sc = result.Response.Response.StatusCode
56			}
57			tracing.EndSpan(ctx, sc, err)
58		}()
59	}
60	req, err := client.GetResourcePreparer(ctx, resourceGroupName, labName, name)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "dtl.CostClient", "GetResource", nil, "Failure preparing request")
63		return
64	}
65
66	resp, err := client.GetResourceSender(req)
67	if err != nil {
68		result.Response = autorest.Response{Response: resp}
69		err = autorest.NewErrorWithError(err, "dtl.CostClient", "GetResource", resp, "Failure sending request")
70		return
71	}
72
73	result, err = client.GetResourceResponder(resp)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "dtl.CostClient", "GetResource", resp, "Failure responding to request")
76	}
77
78	return
79}
80
81// GetResourcePreparer prepares the GetResource request.
82func (client CostClient) GetResourcePreparer(ctx context.Context, resourceGroupName string, labName string, name string) (*http.Request, error) {
83	pathParameters := map[string]interface{}{
84		"labName":           autorest.Encode("path", labName),
85		"name":              autorest.Encode("path", name),
86		"resourceGroupName": autorest.Encode("path", resourceGroupName),
87		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
88	}
89
90	const APIVersion = "2015-05-21-preview"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsGet(),
97		autorest.WithBaseURL(client.BaseURI),
98		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/costs/{name}", pathParameters),
99		autorest.WithQueryParameters(queryParameters))
100	return preparer.Prepare((&http.Request{}).WithContext(ctx))
101}
102
103// GetResourceSender sends the GetResource request. The method will close the
104// http.Response Body if it receives an error.
105func (client CostClient) GetResourceSender(req *http.Request) (*http.Response, error) {
106	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
107}
108
109// GetResourceResponder handles the response to the GetResource request. The method always
110// closes the http.Response Body.
111func (client CostClient) GetResourceResponder(resp *http.Response) (result Cost, err error) {
112	err = autorest.Respond(
113		resp,
114		client.ByInspecting(),
115		azure.WithErrorUnlessStatusCode(http.StatusOK),
116		autorest.ByUnmarshallingJSON(&result),
117		autorest.ByClosing())
118	result.Response = autorest.Response{Response: resp}
119	return
120}
121
122// List list costs.
123// Parameters:
124// resourceGroupName - the name of the resource group.
125// labName - the name of the lab.
126// filter - the filter to apply on the operation.
127func (client CostClient) List(ctx context.Context, resourceGroupName string, labName string, filter string, top *int32, orderBy string) (result ResponseWithContinuationCostPage, err error) {
128	if tracing.IsEnabled() {
129		ctx = tracing.StartSpan(ctx, fqdn+"/CostClient.List")
130		defer func() {
131			sc := -1
132			if result.rwcc.Response.Response != nil {
133				sc = result.rwcc.Response.Response.StatusCode
134			}
135			tracing.EndSpan(ctx, sc, err)
136		}()
137	}
138	result.fn = client.listNextResults
139	req, err := client.ListPreparer(ctx, resourceGroupName, labName, filter, top, orderBy)
140	if err != nil {
141		err = autorest.NewErrorWithError(err, "dtl.CostClient", "List", nil, "Failure preparing request")
142		return
143	}
144
145	resp, err := client.ListSender(req)
146	if err != nil {
147		result.rwcc.Response = autorest.Response{Response: resp}
148		err = autorest.NewErrorWithError(err, "dtl.CostClient", "List", resp, "Failure sending request")
149		return
150	}
151
152	result.rwcc, err = client.ListResponder(resp)
153	if err != nil {
154		err = autorest.NewErrorWithError(err, "dtl.CostClient", "List", resp, "Failure responding to request")
155	}
156
157	return
158}
159
160// ListPreparer prepares the List request.
161func (client CostClient) ListPreparer(ctx context.Context, resourceGroupName string, labName string, filter string, top *int32, orderBy string) (*http.Request, error) {
162	pathParameters := map[string]interface{}{
163		"labName":           autorest.Encode("path", labName),
164		"resourceGroupName": autorest.Encode("path", resourceGroupName),
165		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
166	}
167
168	const APIVersion = "2015-05-21-preview"
169	queryParameters := map[string]interface{}{
170		"api-version": APIVersion,
171	}
172	if len(filter) > 0 {
173		queryParameters["$filter"] = autorest.Encode("query", filter)
174	}
175	if top != nil {
176		queryParameters["$top"] = autorest.Encode("query", *top)
177	}
178	if len(orderBy) > 0 {
179		queryParameters["$orderBy"] = autorest.Encode("query", orderBy)
180	}
181
182	preparer := autorest.CreatePreparer(
183		autorest.AsGet(),
184		autorest.WithBaseURL(client.BaseURI),
185		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/costs", pathParameters),
186		autorest.WithQueryParameters(queryParameters))
187	return preparer.Prepare((&http.Request{}).WithContext(ctx))
188}
189
190// ListSender sends the List request. The method will close the
191// http.Response Body if it receives an error.
192func (client CostClient) ListSender(req *http.Request) (*http.Response, error) {
193	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
194}
195
196// ListResponder handles the response to the List request. The method always
197// closes the http.Response Body.
198func (client CostClient) ListResponder(resp *http.Response) (result ResponseWithContinuationCost, err error) {
199	err = autorest.Respond(
200		resp,
201		client.ByInspecting(),
202		azure.WithErrorUnlessStatusCode(http.StatusOK),
203		autorest.ByUnmarshallingJSON(&result),
204		autorest.ByClosing())
205	result.Response = autorest.Response{Response: resp}
206	return
207}
208
209// listNextResults retrieves the next set of results, if any.
210func (client CostClient) listNextResults(ctx context.Context, lastResults ResponseWithContinuationCost) (result ResponseWithContinuationCost, err error) {
211	req, err := lastResults.responseWithContinuationCostPreparer(ctx)
212	if err != nil {
213		return result, autorest.NewErrorWithError(err, "dtl.CostClient", "listNextResults", nil, "Failure preparing next results request")
214	}
215	if req == nil {
216		return
217	}
218	resp, err := client.ListSender(req)
219	if err != nil {
220		result.Response = autorest.Response{Response: resp}
221		return result, autorest.NewErrorWithError(err, "dtl.CostClient", "listNextResults", resp, "Failure sending next results request")
222	}
223	result, err = client.ListResponder(resp)
224	if err != nil {
225		err = autorest.NewErrorWithError(err, "dtl.CostClient", "listNextResults", resp, "Failure responding to next results request")
226	}
227	return
228}
229
230// ListComplete enumerates all values, automatically crossing page boundaries as required.
231func (client CostClient) ListComplete(ctx context.Context, resourceGroupName string, labName string, filter string, top *int32, orderBy string) (result ResponseWithContinuationCostIterator, err error) {
232	if tracing.IsEnabled() {
233		ctx = tracing.StartSpan(ctx, fqdn+"/CostClient.List")
234		defer func() {
235			sc := -1
236			if result.Response().Response.Response != nil {
237				sc = result.page.Response().Response.Response.StatusCode
238			}
239			tracing.EndSpan(ctx, sc, err)
240		}()
241	}
242	result.page, err = client.List(ctx, resourceGroupName, labName, filter, top, orderBy)
243	return
244}
245
246// RefreshData refresh Lab's Cost Data. This operation can take a while to complete.
247// Parameters:
248// resourceGroupName - the name of the resource group.
249// labName - the name of the lab.
250// name - the name of the cost.
251func (client CostClient) RefreshData(ctx context.Context, resourceGroupName string, labName string, name string) (result CostRefreshDataFuture, err error) {
252	if tracing.IsEnabled() {
253		ctx = tracing.StartSpan(ctx, fqdn+"/CostClient.RefreshData")
254		defer func() {
255			sc := -1
256			if result.Response() != nil {
257				sc = result.Response().StatusCode
258			}
259			tracing.EndSpan(ctx, sc, err)
260		}()
261	}
262	req, err := client.RefreshDataPreparer(ctx, resourceGroupName, labName, name)
263	if err != nil {
264		err = autorest.NewErrorWithError(err, "dtl.CostClient", "RefreshData", nil, "Failure preparing request")
265		return
266	}
267
268	result, err = client.RefreshDataSender(req)
269	if err != nil {
270		err = autorest.NewErrorWithError(err, "dtl.CostClient", "RefreshData", result.Response(), "Failure sending request")
271		return
272	}
273
274	return
275}
276
277// RefreshDataPreparer prepares the RefreshData request.
278func (client CostClient) RefreshDataPreparer(ctx context.Context, resourceGroupName string, labName string, name string) (*http.Request, error) {
279	pathParameters := map[string]interface{}{
280		"labName":           autorest.Encode("path", labName),
281		"name":              autorest.Encode("path", name),
282		"resourceGroupName": autorest.Encode("path", resourceGroupName),
283		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
284	}
285
286	const APIVersion = "2015-05-21-preview"
287	queryParameters := map[string]interface{}{
288		"api-version": APIVersion,
289	}
290
291	preparer := autorest.CreatePreparer(
292		autorest.AsPost(),
293		autorest.WithBaseURL(client.BaseURI),
294		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/costs/{name}/refreshData", pathParameters),
295		autorest.WithQueryParameters(queryParameters))
296	return preparer.Prepare((&http.Request{}).WithContext(ctx))
297}
298
299// RefreshDataSender sends the RefreshData request. The method will close the
300// http.Response Body if it receives an error.
301func (client CostClient) RefreshDataSender(req *http.Request) (future CostRefreshDataFuture, err error) {
302	var resp *http.Response
303	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
304	if err != nil {
305		return
306	}
307	future.Future, err = azure.NewFutureFromResponse(resp)
308	return
309}
310
311// RefreshDataResponder handles the response to the RefreshData request. The method always
312// closes the http.Response Body.
313func (client CostClient) RefreshDataResponder(resp *http.Response) (result autorest.Response, err error) {
314	err = autorest.Respond(
315		resp,
316		client.ByInspecting(),
317		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
318		autorest.ByClosing())
319	result.Response = resp
320	return
321}
322