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