1package network
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// RoutesClient is the network Client
18type RoutesClient struct {
19	BaseClient
20}
21
22// NewRoutesClient creates an instance of the RoutesClient client.
23func NewRoutesClient(subscriptionID string) RoutesClient {
24	return NewRoutesClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewRoutesClientWithBaseURI creates an instance of the RoutesClient client using a custom endpoint.  Use this when
28// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewRoutesClientWithBaseURI(baseURI string, subscriptionID string) RoutesClient {
30	return RoutesClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// CreateOrUpdate creates or updates a route in the specified route table.
34// Parameters:
35// resourceGroupName - the name of the resource group.
36// routeTableName - the name of the route table.
37// routeName - the name of the route.
38// routeParameters - parameters supplied to the create or update route operation.
39func (client RoutesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, routeTableName string, routeName string, routeParameters Route) (result RoutesCreateOrUpdateFuture, err error) {
40	if tracing.IsEnabled() {
41		ctx = tracing.StartSpan(ctx, fqdn+"/RoutesClient.CreateOrUpdate")
42		defer func() {
43			sc := -1
44			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
45				sc = result.FutureAPI.Response().StatusCode
46			}
47			tracing.EndSpan(ctx, sc, err)
48		}()
49	}
50	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, routeTableName, routeName, routeParameters)
51	if err != nil {
52		err = autorest.NewErrorWithError(err, "network.RoutesClient", "CreateOrUpdate", nil, "Failure preparing request")
53		return
54	}
55
56	result, err = client.CreateOrUpdateSender(req)
57	if err != nil {
58		err = autorest.NewErrorWithError(err, "network.RoutesClient", "CreateOrUpdate", nil, "Failure sending request")
59		return
60	}
61
62	return
63}
64
65// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
66func (client RoutesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, routeTableName string, routeName string, routeParameters Route) (*http.Request, error) {
67	pathParameters := map[string]interface{}{
68		"resourceGroupName": autorest.Encode("path", resourceGroupName),
69		"routeName":         autorest.Encode("path", routeName),
70		"routeTableName":    autorest.Encode("path", routeTableName),
71		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
72	}
73
74	const APIVersion = "2020-06-01"
75	queryParameters := map[string]interface{}{
76		"api-version": APIVersion,
77	}
78
79	routeParameters.Etag = nil
80	preparer := autorest.CreatePreparer(
81		autorest.AsContentType("application/json; charset=utf-8"),
82		autorest.AsPut(),
83		autorest.WithBaseURL(client.BaseURI),
84		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/routeTables/{routeTableName}/routes/{routeName}", pathParameters),
85		autorest.WithJSON(routeParameters),
86		autorest.WithQueryParameters(queryParameters))
87	return preparer.Prepare((&http.Request{}).WithContext(ctx))
88}
89
90// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
91// http.Response Body if it receives an error.
92func (client RoutesClient) CreateOrUpdateSender(req *http.Request) (future RoutesCreateOrUpdateFuture, err error) {
93	var resp *http.Response
94	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
95	if err != nil {
96		return
97	}
98	var azf azure.Future
99	azf, err = azure.NewFutureFromResponse(resp)
100	future.FutureAPI = &azf
101	future.Result = future.result
102	return
103}
104
105// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
106// closes the http.Response Body.
107func (client RoutesClient) CreateOrUpdateResponder(resp *http.Response) (result Route, err error) {
108	err = autorest.Respond(
109		resp,
110		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
111		autorest.ByUnmarshallingJSON(&result),
112		autorest.ByClosing())
113	result.Response = autorest.Response{Response: resp}
114	return
115}
116
117// Delete deletes the specified route from a route table.
118// Parameters:
119// resourceGroupName - the name of the resource group.
120// routeTableName - the name of the route table.
121// routeName - the name of the route.
122func (client RoutesClient) Delete(ctx context.Context, resourceGroupName string, routeTableName string, routeName string) (result RoutesDeleteFuture, err error) {
123	if tracing.IsEnabled() {
124		ctx = tracing.StartSpan(ctx, fqdn+"/RoutesClient.Delete")
125		defer func() {
126			sc := -1
127			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
128				sc = result.FutureAPI.Response().StatusCode
129			}
130			tracing.EndSpan(ctx, sc, err)
131		}()
132	}
133	req, err := client.DeletePreparer(ctx, resourceGroupName, routeTableName, routeName)
134	if err != nil {
135		err = autorest.NewErrorWithError(err, "network.RoutesClient", "Delete", nil, "Failure preparing request")
136		return
137	}
138
139	result, err = client.DeleteSender(req)
140	if err != nil {
141		err = autorest.NewErrorWithError(err, "network.RoutesClient", "Delete", nil, "Failure sending request")
142		return
143	}
144
145	return
146}
147
148// DeletePreparer prepares the Delete request.
149func (client RoutesClient) DeletePreparer(ctx context.Context, resourceGroupName string, routeTableName string, routeName string) (*http.Request, error) {
150	pathParameters := map[string]interface{}{
151		"resourceGroupName": autorest.Encode("path", resourceGroupName),
152		"routeName":         autorest.Encode("path", routeName),
153		"routeTableName":    autorest.Encode("path", routeTableName),
154		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
155	}
156
157	const APIVersion = "2020-06-01"
158	queryParameters := map[string]interface{}{
159		"api-version": APIVersion,
160	}
161
162	preparer := autorest.CreatePreparer(
163		autorest.AsDelete(),
164		autorest.WithBaseURL(client.BaseURI),
165		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/routeTables/{routeTableName}/routes/{routeName}", pathParameters),
166		autorest.WithQueryParameters(queryParameters))
167	return preparer.Prepare((&http.Request{}).WithContext(ctx))
168}
169
170// DeleteSender sends the Delete request. The method will close the
171// http.Response Body if it receives an error.
172func (client RoutesClient) DeleteSender(req *http.Request) (future RoutesDeleteFuture, err error) {
173	var resp *http.Response
174	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
175	if err != nil {
176		return
177	}
178	var azf azure.Future
179	azf, err = azure.NewFutureFromResponse(resp)
180	future.FutureAPI = &azf
181	future.Result = future.result
182	return
183}
184
185// DeleteResponder handles the response to the Delete request. The method always
186// closes the http.Response Body.
187func (client RoutesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
188	err = autorest.Respond(
189		resp,
190		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
191		autorest.ByClosing())
192	result.Response = resp
193	return
194}
195
196// Get gets the specified route from a route table.
197// Parameters:
198// resourceGroupName - the name of the resource group.
199// routeTableName - the name of the route table.
200// routeName - the name of the route.
201func (client RoutesClient) Get(ctx context.Context, resourceGroupName string, routeTableName string, routeName string) (result Route, err error) {
202	if tracing.IsEnabled() {
203		ctx = tracing.StartSpan(ctx, fqdn+"/RoutesClient.Get")
204		defer func() {
205			sc := -1
206			if result.Response.Response != nil {
207				sc = result.Response.Response.StatusCode
208			}
209			tracing.EndSpan(ctx, sc, err)
210		}()
211	}
212	req, err := client.GetPreparer(ctx, resourceGroupName, routeTableName, routeName)
213	if err != nil {
214		err = autorest.NewErrorWithError(err, "network.RoutesClient", "Get", nil, "Failure preparing request")
215		return
216	}
217
218	resp, err := client.GetSender(req)
219	if err != nil {
220		result.Response = autorest.Response{Response: resp}
221		err = autorest.NewErrorWithError(err, "network.RoutesClient", "Get", resp, "Failure sending request")
222		return
223	}
224
225	result, err = client.GetResponder(resp)
226	if err != nil {
227		err = autorest.NewErrorWithError(err, "network.RoutesClient", "Get", resp, "Failure responding to request")
228		return
229	}
230
231	return
232}
233
234// GetPreparer prepares the Get request.
235func (client RoutesClient) GetPreparer(ctx context.Context, resourceGroupName string, routeTableName string, routeName string) (*http.Request, error) {
236	pathParameters := map[string]interface{}{
237		"resourceGroupName": autorest.Encode("path", resourceGroupName),
238		"routeName":         autorest.Encode("path", routeName),
239		"routeTableName":    autorest.Encode("path", routeTableName),
240		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
241	}
242
243	const APIVersion = "2020-06-01"
244	queryParameters := map[string]interface{}{
245		"api-version": APIVersion,
246	}
247
248	preparer := autorest.CreatePreparer(
249		autorest.AsGet(),
250		autorest.WithBaseURL(client.BaseURI),
251		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/routeTables/{routeTableName}/routes/{routeName}", pathParameters),
252		autorest.WithQueryParameters(queryParameters))
253	return preparer.Prepare((&http.Request{}).WithContext(ctx))
254}
255
256// GetSender sends the Get request. The method will close the
257// http.Response Body if it receives an error.
258func (client RoutesClient) GetSender(req *http.Request) (*http.Response, error) {
259	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
260}
261
262// GetResponder handles the response to the Get request. The method always
263// closes the http.Response Body.
264func (client RoutesClient) GetResponder(resp *http.Response) (result Route, err error) {
265	err = autorest.Respond(
266		resp,
267		azure.WithErrorUnlessStatusCode(http.StatusOK),
268		autorest.ByUnmarshallingJSON(&result),
269		autorest.ByClosing())
270	result.Response = autorest.Response{Response: resp}
271	return
272}
273
274// List gets all routes in a route table.
275// Parameters:
276// resourceGroupName - the name of the resource group.
277// routeTableName - the name of the route table.
278func (client RoutesClient) List(ctx context.Context, resourceGroupName string, routeTableName string) (result RouteListResultPage, err error) {
279	if tracing.IsEnabled() {
280		ctx = tracing.StartSpan(ctx, fqdn+"/RoutesClient.List")
281		defer func() {
282			sc := -1
283			if result.rlr.Response.Response != nil {
284				sc = result.rlr.Response.Response.StatusCode
285			}
286			tracing.EndSpan(ctx, sc, err)
287		}()
288	}
289	result.fn = client.listNextResults
290	req, err := client.ListPreparer(ctx, resourceGroupName, routeTableName)
291	if err != nil {
292		err = autorest.NewErrorWithError(err, "network.RoutesClient", "List", nil, "Failure preparing request")
293		return
294	}
295
296	resp, err := client.ListSender(req)
297	if err != nil {
298		result.rlr.Response = autorest.Response{Response: resp}
299		err = autorest.NewErrorWithError(err, "network.RoutesClient", "List", resp, "Failure sending request")
300		return
301	}
302
303	result.rlr, err = client.ListResponder(resp)
304	if err != nil {
305		err = autorest.NewErrorWithError(err, "network.RoutesClient", "List", resp, "Failure responding to request")
306		return
307	}
308	if result.rlr.hasNextLink() && result.rlr.IsEmpty() {
309		err = result.NextWithContext(ctx)
310		return
311	}
312
313	return
314}
315
316// ListPreparer prepares the List request.
317func (client RoutesClient) ListPreparer(ctx context.Context, resourceGroupName string, routeTableName string) (*http.Request, error) {
318	pathParameters := map[string]interface{}{
319		"resourceGroupName": autorest.Encode("path", resourceGroupName),
320		"routeTableName":    autorest.Encode("path", routeTableName),
321		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
322	}
323
324	const APIVersion = "2020-06-01"
325	queryParameters := map[string]interface{}{
326		"api-version": APIVersion,
327	}
328
329	preparer := autorest.CreatePreparer(
330		autorest.AsGet(),
331		autorest.WithBaseURL(client.BaseURI),
332		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/routeTables/{routeTableName}/routes", pathParameters),
333		autorest.WithQueryParameters(queryParameters))
334	return preparer.Prepare((&http.Request{}).WithContext(ctx))
335}
336
337// ListSender sends the List request. The method will close the
338// http.Response Body if it receives an error.
339func (client RoutesClient) ListSender(req *http.Request) (*http.Response, error) {
340	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
341}
342
343// ListResponder handles the response to the List request. The method always
344// closes the http.Response Body.
345func (client RoutesClient) ListResponder(resp *http.Response) (result RouteListResult, err error) {
346	err = autorest.Respond(
347		resp,
348		azure.WithErrorUnlessStatusCode(http.StatusOK),
349		autorest.ByUnmarshallingJSON(&result),
350		autorest.ByClosing())
351	result.Response = autorest.Response{Response: resp}
352	return
353}
354
355// listNextResults retrieves the next set of results, if any.
356func (client RoutesClient) listNextResults(ctx context.Context, lastResults RouteListResult) (result RouteListResult, err error) {
357	req, err := lastResults.routeListResultPreparer(ctx)
358	if err != nil {
359		return result, autorest.NewErrorWithError(err, "network.RoutesClient", "listNextResults", nil, "Failure preparing next results request")
360	}
361	if req == nil {
362		return
363	}
364	resp, err := client.ListSender(req)
365	if err != nil {
366		result.Response = autorest.Response{Response: resp}
367		return result, autorest.NewErrorWithError(err, "network.RoutesClient", "listNextResults", resp, "Failure sending next results request")
368	}
369	result, err = client.ListResponder(resp)
370	if err != nil {
371		err = autorest.NewErrorWithError(err, "network.RoutesClient", "listNextResults", resp, "Failure responding to next results request")
372	}
373	return
374}
375
376// ListComplete enumerates all values, automatically crossing page boundaries as required.
377func (client RoutesClient) ListComplete(ctx context.Context, resourceGroupName string, routeTableName string) (result RouteListResultIterator, err error) {
378	if tracing.IsEnabled() {
379		ctx = tracing.StartSpan(ctx, fqdn+"/RoutesClient.List")
380		defer func() {
381			sc := -1
382			if result.Response().Response.Response != nil {
383				sc = result.page.Response().Response.Response.StatusCode
384			}
385			tracing.EndSpan(ctx, sc, err)
386		}()
387	}
388	result.page, err = client.List(ctx, resourceGroupName, routeTableName)
389	return
390}
391