1package apimanagement
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/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// BackendClient is the client for the Backend methods of the Apimanagement service.
30type BackendClient struct {
31	BaseClient
32}
33
34// NewBackendClient creates an instance of the BackendClient client.
35func NewBackendClient() BackendClient {
36	return BackendClient{New()}
37}
38
39// CreateOrUpdate creates or Updates a backend.
40// Parameters:
41// apimBaseURL - the management endpoint of the API Management service, for example
42// https://myapimservice.management.azure-api.net.
43// backendid - identifier of the Backend entity. Must be unique in the current API Management service instance.
44// parameters - create parameters.
45func (client BackendClient) CreateOrUpdate(ctx context.Context, apimBaseURL string, backendid string, parameters BackendContract) (result BackendContract, err error) {
46	if tracing.IsEnabled() {
47		ctx = tracing.StartSpan(ctx, fqdn+"/BackendClient.CreateOrUpdate")
48		defer func() {
49			sc := -1
50			if result.Response.Response != nil {
51				sc = result.Response.Response.StatusCode
52			}
53			tracing.EndSpan(ctx, sc, err)
54		}()
55	}
56	if err := validation.Validate([]validation.Validation{
57		{TargetValue: backendid,
58			Constraints: []validation.Constraint{{Target: "backendid", Name: validation.MaxLength, Rule: 255, Chain: nil},
59				{Target: "backendid", Name: validation.MinLength, Rule: 1, Chain: nil},
60				{Target: "backendid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
61		return result, validation.NewError("apimanagement.BackendClient", "CreateOrUpdate", err.Error())
62	}
63
64	req, err := client.CreateOrUpdatePreparer(ctx, apimBaseURL, backendid, parameters)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "CreateOrUpdate", nil, "Failure preparing request")
67		return
68	}
69
70	resp, err := client.CreateOrUpdateSender(req)
71	if err != nil {
72		result.Response = autorest.Response{Response: resp}
73		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "CreateOrUpdate", resp, "Failure sending request")
74		return
75	}
76
77	result, err = client.CreateOrUpdateResponder(resp)
78	if err != nil {
79		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "CreateOrUpdate", resp, "Failure responding to request")
80		return
81	}
82
83	return
84}
85
86// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
87func (client BackendClient) CreateOrUpdatePreparer(ctx context.Context, apimBaseURL string, backendid string, parameters BackendContract) (*http.Request, error) {
88	urlParameters := map[string]interface{}{
89		"apimBaseUrl": apimBaseURL,
90	}
91
92	pathParameters := map[string]interface{}{
93		"backendid": autorest.Encode("path", backendid),
94	}
95
96	const APIVersion = "2017-03-01"
97	queryParameters := map[string]interface{}{
98		"api-version": APIVersion,
99	}
100
101	preparer := autorest.CreatePreparer(
102		autorest.AsContentType("application/json; charset=utf-8"),
103		autorest.AsPut(),
104		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
105		autorest.WithPathParameters("/backends/{backendid}", pathParameters),
106		autorest.WithJSON(parameters),
107		autorest.WithQueryParameters(queryParameters))
108	return preparer.Prepare((&http.Request{}).WithContext(ctx))
109}
110
111// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
112// http.Response Body if it receives an error.
113func (client BackendClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
114	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
115}
116
117// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
118// closes the http.Response Body.
119func (client BackendClient) CreateOrUpdateResponder(resp *http.Response) (result BackendContract, err error) {
120	err = autorest.Respond(
121		resp,
122		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
123		autorest.ByUnmarshallingJSON(&result),
124		autorest.ByClosing())
125	result.Response = autorest.Response{Response: resp}
126	return
127}
128
129// Delete deletes the specified backend.
130// Parameters:
131// apimBaseURL - the management endpoint of the API Management service, for example
132// https://myapimservice.management.azure-api.net.
133// backendid - identifier of the Backend entity. Must be unique in the current API Management service instance.
134// ifMatch - the entity state (Etag) version of the backend to delete. A value of "*" can be used for If-Match
135// to unconditionally apply the operation.
136func (client BackendClient) Delete(ctx context.Context, apimBaseURL string, backendid string, ifMatch string) (result autorest.Response, err error) {
137	if tracing.IsEnabled() {
138		ctx = tracing.StartSpan(ctx, fqdn+"/BackendClient.Delete")
139		defer func() {
140			sc := -1
141			if result.Response != nil {
142				sc = result.Response.StatusCode
143			}
144			tracing.EndSpan(ctx, sc, err)
145		}()
146	}
147	if err := validation.Validate([]validation.Validation{
148		{TargetValue: backendid,
149			Constraints: []validation.Constraint{{Target: "backendid", Name: validation.MaxLength, Rule: 255, Chain: nil},
150				{Target: "backendid", Name: validation.MinLength, Rule: 1, Chain: nil},
151				{Target: "backendid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
152		return result, validation.NewError("apimanagement.BackendClient", "Delete", err.Error())
153	}
154
155	req, err := client.DeletePreparer(ctx, apimBaseURL, backendid, ifMatch)
156	if err != nil {
157		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Delete", nil, "Failure preparing request")
158		return
159	}
160
161	resp, err := client.DeleteSender(req)
162	if err != nil {
163		result.Response = resp
164		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Delete", resp, "Failure sending request")
165		return
166	}
167
168	result, err = client.DeleteResponder(resp)
169	if err != nil {
170		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Delete", resp, "Failure responding to request")
171		return
172	}
173
174	return
175}
176
177// DeletePreparer prepares the Delete request.
178func (client BackendClient) DeletePreparer(ctx context.Context, apimBaseURL string, backendid string, ifMatch string) (*http.Request, error) {
179	urlParameters := map[string]interface{}{
180		"apimBaseUrl": apimBaseURL,
181	}
182
183	pathParameters := map[string]interface{}{
184		"backendid": autorest.Encode("path", backendid),
185	}
186
187	const APIVersion = "2017-03-01"
188	queryParameters := map[string]interface{}{
189		"api-version": APIVersion,
190	}
191
192	preparer := autorest.CreatePreparer(
193		autorest.AsDelete(),
194		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
195		autorest.WithPathParameters("/backends/{backendid}", pathParameters),
196		autorest.WithQueryParameters(queryParameters),
197		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
198	return preparer.Prepare((&http.Request{}).WithContext(ctx))
199}
200
201// DeleteSender sends the Delete request. The method will close the
202// http.Response Body if it receives an error.
203func (client BackendClient) DeleteSender(req *http.Request) (*http.Response, error) {
204	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
205}
206
207// DeleteResponder handles the response to the Delete request. The method always
208// closes the http.Response Body.
209func (client BackendClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
210	err = autorest.Respond(
211		resp,
212		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
213		autorest.ByClosing())
214	result.Response = resp
215	return
216}
217
218// Get gets the details of the backend specified by its identifier.
219// Parameters:
220// apimBaseURL - the management endpoint of the API Management service, for example
221// https://myapimservice.management.azure-api.net.
222// backendid - identifier of the Backend entity. Must be unique in the current API Management service instance.
223func (client BackendClient) Get(ctx context.Context, apimBaseURL string, backendid string) (result BackendContract, err error) {
224	if tracing.IsEnabled() {
225		ctx = tracing.StartSpan(ctx, fqdn+"/BackendClient.Get")
226		defer func() {
227			sc := -1
228			if result.Response.Response != nil {
229				sc = result.Response.Response.StatusCode
230			}
231			tracing.EndSpan(ctx, sc, err)
232		}()
233	}
234	if err := validation.Validate([]validation.Validation{
235		{TargetValue: backendid,
236			Constraints: []validation.Constraint{{Target: "backendid", Name: validation.MaxLength, Rule: 255, Chain: nil},
237				{Target: "backendid", Name: validation.MinLength, Rule: 1, Chain: nil},
238				{Target: "backendid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
239		return result, validation.NewError("apimanagement.BackendClient", "Get", err.Error())
240	}
241
242	req, err := client.GetPreparer(ctx, apimBaseURL, backendid)
243	if err != nil {
244		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Get", nil, "Failure preparing request")
245		return
246	}
247
248	resp, err := client.GetSender(req)
249	if err != nil {
250		result.Response = autorest.Response{Response: resp}
251		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Get", resp, "Failure sending request")
252		return
253	}
254
255	result, err = client.GetResponder(resp)
256	if err != nil {
257		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Get", resp, "Failure responding to request")
258		return
259	}
260
261	return
262}
263
264// GetPreparer prepares the Get request.
265func (client BackendClient) GetPreparer(ctx context.Context, apimBaseURL string, backendid string) (*http.Request, error) {
266	urlParameters := map[string]interface{}{
267		"apimBaseUrl": apimBaseURL,
268	}
269
270	pathParameters := map[string]interface{}{
271		"backendid": autorest.Encode("path", backendid),
272	}
273
274	const APIVersion = "2017-03-01"
275	queryParameters := map[string]interface{}{
276		"api-version": APIVersion,
277	}
278
279	preparer := autorest.CreatePreparer(
280		autorest.AsGet(),
281		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
282		autorest.WithPathParameters("/backends/{backendid}", pathParameters),
283		autorest.WithQueryParameters(queryParameters))
284	return preparer.Prepare((&http.Request{}).WithContext(ctx))
285}
286
287// GetSender sends the Get request. The method will close the
288// http.Response Body if it receives an error.
289func (client BackendClient) GetSender(req *http.Request) (*http.Response, error) {
290	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
291}
292
293// GetResponder handles the response to the Get request. The method always
294// closes the http.Response Body.
295func (client BackendClient) GetResponder(resp *http.Response) (result BackendContract, err error) {
296	err = autorest.Respond(
297		resp,
298		azure.WithErrorUnlessStatusCode(http.StatusOK),
299		autorest.ByUnmarshallingJSON(&result),
300		autorest.ByClosing())
301	result.Response = autorest.Response{Response: resp}
302	return
303}
304
305// List lists a collection of backends in the specified service instance.
306// Parameters:
307// apimBaseURL - the management endpoint of the API Management service, for example
308// https://myapimservice.management.azure-api.net.
309// filter - | Field | Supported operators    | Supported functions                         |
310// |-------|------------------------|---------------------------------------------|
311// | id    | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
312// | host  | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
313// top - number of records to return.
314// skip - number of records to skip.
315func (client BackendClient) List(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result BackendCollectionPage, err error) {
316	if tracing.IsEnabled() {
317		ctx = tracing.StartSpan(ctx, fqdn+"/BackendClient.List")
318		defer func() {
319			sc := -1
320			if result.bc.Response.Response != nil {
321				sc = result.bc.Response.Response.StatusCode
322			}
323			tracing.EndSpan(ctx, sc, err)
324		}()
325	}
326	if err := validation.Validate([]validation.Validation{
327		{TargetValue: top,
328			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
329				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
330		{TargetValue: skip,
331			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
332				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
333		return result, validation.NewError("apimanagement.BackendClient", "List", err.Error())
334	}
335
336	result.fn = client.listNextResults
337	req, err := client.ListPreparer(ctx, apimBaseURL, filter, top, skip)
338	if err != nil {
339		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "List", nil, "Failure preparing request")
340		return
341	}
342
343	resp, err := client.ListSender(req)
344	if err != nil {
345		result.bc.Response = autorest.Response{Response: resp}
346		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "List", resp, "Failure sending request")
347		return
348	}
349
350	result.bc, err = client.ListResponder(resp)
351	if err != nil {
352		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "List", resp, "Failure responding to request")
353		return
354	}
355	if result.bc.hasNextLink() && result.bc.IsEmpty() {
356		err = result.NextWithContext(ctx)
357		return
358	}
359
360	return
361}
362
363// ListPreparer prepares the List request.
364func (client BackendClient) ListPreparer(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (*http.Request, error) {
365	urlParameters := map[string]interface{}{
366		"apimBaseUrl": apimBaseURL,
367	}
368
369	const APIVersion = "2017-03-01"
370	queryParameters := map[string]interface{}{
371		"api-version": APIVersion,
372	}
373	if len(filter) > 0 {
374		queryParameters["$filter"] = autorest.Encode("query", filter)
375	}
376	if top != nil {
377		queryParameters["$top"] = autorest.Encode("query", *top)
378	}
379	if skip != nil {
380		queryParameters["$skip"] = autorest.Encode("query", *skip)
381	}
382
383	preparer := autorest.CreatePreparer(
384		autorest.AsGet(),
385		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
386		autorest.WithPath("/backends"),
387		autorest.WithQueryParameters(queryParameters))
388	return preparer.Prepare((&http.Request{}).WithContext(ctx))
389}
390
391// ListSender sends the List request. The method will close the
392// http.Response Body if it receives an error.
393func (client BackendClient) ListSender(req *http.Request) (*http.Response, error) {
394	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
395}
396
397// ListResponder handles the response to the List request. The method always
398// closes the http.Response Body.
399func (client BackendClient) ListResponder(resp *http.Response) (result BackendCollection, err error) {
400	err = autorest.Respond(
401		resp,
402		azure.WithErrorUnlessStatusCode(http.StatusOK),
403		autorest.ByUnmarshallingJSON(&result),
404		autorest.ByClosing())
405	result.Response = autorest.Response{Response: resp}
406	return
407}
408
409// listNextResults retrieves the next set of results, if any.
410func (client BackendClient) listNextResults(ctx context.Context, lastResults BackendCollection) (result BackendCollection, err error) {
411	req, err := lastResults.backendCollectionPreparer(ctx)
412	if err != nil {
413		return result, autorest.NewErrorWithError(err, "apimanagement.BackendClient", "listNextResults", nil, "Failure preparing next results request")
414	}
415	if req == nil {
416		return
417	}
418	resp, err := client.ListSender(req)
419	if err != nil {
420		result.Response = autorest.Response{Response: resp}
421		return result, autorest.NewErrorWithError(err, "apimanagement.BackendClient", "listNextResults", resp, "Failure sending next results request")
422	}
423	result, err = client.ListResponder(resp)
424	if err != nil {
425		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "listNextResults", resp, "Failure responding to next results request")
426	}
427	return
428}
429
430// ListComplete enumerates all values, automatically crossing page boundaries as required.
431func (client BackendClient) ListComplete(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result BackendCollectionIterator, err error) {
432	if tracing.IsEnabled() {
433		ctx = tracing.StartSpan(ctx, fqdn+"/BackendClient.List")
434		defer func() {
435			sc := -1
436			if result.Response().Response.Response != nil {
437				sc = result.page.Response().Response.Response.StatusCode
438			}
439			tracing.EndSpan(ctx, sc, err)
440		}()
441	}
442	result.page, err = client.List(ctx, apimBaseURL, filter, top, skip)
443	return
444}
445
446// Update updates an existing backend.
447// Parameters:
448// apimBaseURL - the management endpoint of the API Management service, for example
449// https://myapimservice.management.azure-api.net.
450// backendid - identifier of the Backend entity. Must be unique in the current API Management service instance.
451// parameters - update parameters.
452// ifMatch - the entity state (Etag) version of the backend to update. A value of "*" can be used for If-Match
453// to unconditionally apply the operation.
454func (client BackendClient) Update(ctx context.Context, apimBaseURL string, backendid string, parameters BackendUpdateParameters, ifMatch string) (result autorest.Response, err error) {
455	if tracing.IsEnabled() {
456		ctx = tracing.StartSpan(ctx, fqdn+"/BackendClient.Update")
457		defer func() {
458			sc := -1
459			if result.Response != nil {
460				sc = result.Response.StatusCode
461			}
462			tracing.EndSpan(ctx, sc, err)
463		}()
464	}
465	if err := validation.Validate([]validation.Validation{
466		{TargetValue: backendid,
467			Constraints: []validation.Constraint{{Target: "backendid", Name: validation.MaxLength, Rule: 255, Chain: nil},
468				{Target: "backendid", Name: validation.MinLength, Rule: 1, Chain: nil},
469				{Target: "backendid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
470		return result, validation.NewError("apimanagement.BackendClient", "Update", err.Error())
471	}
472
473	req, err := client.UpdatePreparer(ctx, apimBaseURL, backendid, parameters, ifMatch)
474	if err != nil {
475		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Update", nil, "Failure preparing request")
476		return
477	}
478
479	resp, err := client.UpdateSender(req)
480	if err != nil {
481		result.Response = resp
482		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Update", resp, "Failure sending request")
483		return
484	}
485
486	result, err = client.UpdateResponder(resp)
487	if err != nil {
488		err = autorest.NewErrorWithError(err, "apimanagement.BackendClient", "Update", resp, "Failure responding to request")
489		return
490	}
491
492	return
493}
494
495// UpdatePreparer prepares the Update request.
496func (client BackendClient) UpdatePreparer(ctx context.Context, apimBaseURL string, backendid string, parameters BackendUpdateParameters, ifMatch string) (*http.Request, error) {
497	urlParameters := map[string]interface{}{
498		"apimBaseUrl": apimBaseURL,
499	}
500
501	pathParameters := map[string]interface{}{
502		"backendid": autorest.Encode("path", backendid),
503	}
504
505	const APIVersion = "2017-03-01"
506	queryParameters := map[string]interface{}{
507		"api-version": APIVersion,
508	}
509
510	preparer := autorest.CreatePreparer(
511		autorest.AsContentType("application/json; charset=utf-8"),
512		autorest.AsPatch(),
513		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
514		autorest.WithPathParameters("/backends/{backendid}", pathParameters),
515		autorest.WithJSON(parameters),
516		autorest.WithQueryParameters(queryParameters),
517		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
518	return preparer.Prepare((&http.Request{}).WithContext(ctx))
519}
520
521// UpdateSender sends the Update request. The method will close the
522// http.Response Body if it receives an error.
523func (client BackendClient) UpdateSender(req *http.Request) (*http.Response, error) {
524	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
525}
526
527// UpdateResponder handles the response to the Update request. The method always
528// closes the http.Response Body.
529func (client BackendClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
530	err = autorest.Respond(
531		resp,
532		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
533		autorest.ByClosing())
534	result.Response = resp
535	return
536}
537