1package apimanagement
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/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// PolicyClient is the apiManagement Client
19type PolicyClient struct {
20	BaseClient
21}
22
23// NewPolicyClient creates an instance of the PolicyClient client.
24func NewPolicyClient(subscriptionID string) PolicyClient {
25	return NewPolicyClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewPolicyClientWithBaseURI creates an instance of the PolicyClient client using a custom endpoint.  Use this when
29// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewPolicyClientWithBaseURI(baseURI string, subscriptionID string) PolicyClient {
31	return PolicyClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate creates or updates the global policy configuration of the Api Management service.
35// Parameters:
36// resourceGroupName - the name of the resource group.
37// serviceName - the name of the API Management service.
38// parameters - the policy contents to apply.
39// ifMatch - eTag of the Entity. Not required when creating an entity, but required when updating an entity.
40func (client PolicyClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, parameters PolicyContract, ifMatch string) (result PolicyContract, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/PolicyClient.CreateOrUpdate")
43		defer func() {
44			sc := -1
45			if result.Response.Response != nil {
46				sc = result.Response.Response.StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: serviceName,
53			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
54				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
55				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
56		{TargetValue: parameters,
57			Constraints: []validation.Constraint{{Target: "parameters.PolicyContractProperties", Name: validation.Null, Rule: false,
58				Chain: []validation.Constraint{{Target: "parameters.PolicyContractProperties.Value", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
59		return result, validation.NewError("apimanagement.PolicyClient", "CreateOrUpdate", err.Error())
60	}
61
62	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, parameters, ifMatch)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "CreateOrUpdate", nil, "Failure preparing request")
65		return
66	}
67
68	resp, err := client.CreateOrUpdateSender(req)
69	if err != nil {
70		result.Response = autorest.Response{Response: resp}
71		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "CreateOrUpdate", resp, "Failure sending request")
72		return
73	}
74
75	result, err = client.CreateOrUpdateResponder(resp)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "CreateOrUpdate", resp, "Failure responding to request")
78		return
79	}
80
81	return
82}
83
84// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
85func (client PolicyClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, parameters PolicyContract, ifMatch string) (*http.Request, error) {
86	pathParameters := map[string]interface{}{
87		"policyId":          autorest.Encode("path", "policy"),
88		"resourceGroupName": autorest.Encode("path", resourceGroupName),
89		"serviceName":       autorest.Encode("path", serviceName),
90		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
91	}
92
93	const APIVersion = "2021-01-01-preview"
94	queryParameters := map[string]interface{}{
95		"api-version": APIVersion,
96	}
97
98	preparer := autorest.CreatePreparer(
99		autorest.AsContentType("application/json; charset=utf-8"),
100		autorest.AsPut(),
101		autorest.WithBaseURL(client.BaseURI),
102		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies/{policyId}", pathParameters),
103		autorest.WithJSON(parameters),
104		autorest.WithQueryParameters(queryParameters))
105	if len(ifMatch) > 0 {
106		preparer = autorest.DecoratePreparer(preparer,
107			autorest.WithHeader("If-Match", autorest.String(ifMatch)))
108	}
109	return preparer.Prepare((&http.Request{}).WithContext(ctx))
110}
111
112// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
113// http.Response Body if it receives an error.
114func (client PolicyClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
115	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
116}
117
118// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
119// closes the http.Response Body.
120func (client PolicyClient) CreateOrUpdateResponder(resp *http.Response) (result PolicyContract, err error) {
121	err = autorest.Respond(
122		resp,
123		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
124		autorest.ByUnmarshallingJSON(&result),
125		autorest.ByClosing())
126	result.Response = autorest.Response{Response: resp}
127	return
128}
129
130// Delete deletes the global policy configuration of the Api Management Service.
131// Parameters:
132// resourceGroupName - the name of the resource group.
133// serviceName - the name of the API Management service.
134// ifMatch - eTag of the Entity. ETag should match the current entity state from the header response of the GET
135// request or it should be * for unconditional update.
136func (client PolicyClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, ifMatch string) (result autorest.Response, err error) {
137	if tracing.IsEnabled() {
138		ctx = tracing.StartSpan(ctx, fqdn+"/PolicyClient.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: serviceName,
149			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
150				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
151				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
152		return result, validation.NewError("apimanagement.PolicyClient", "Delete", err.Error())
153	}
154
155	req, err := client.DeletePreparer(ctx, resourceGroupName, serviceName, ifMatch)
156	if err != nil {
157		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "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.PolicyClient", "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.PolicyClient", "Delete", resp, "Failure responding to request")
171		return
172	}
173
174	return
175}
176
177// DeletePreparer prepares the Delete request.
178func (client PolicyClient) DeletePreparer(ctx context.Context, resourceGroupName string, serviceName string, ifMatch string) (*http.Request, error) {
179	pathParameters := map[string]interface{}{
180		"policyId":          autorest.Encode("path", "policy"),
181		"resourceGroupName": autorest.Encode("path", resourceGroupName),
182		"serviceName":       autorest.Encode("path", serviceName),
183		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
184	}
185
186	const APIVersion = "2021-01-01-preview"
187	queryParameters := map[string]interface{}{
188		"api-version": APIVersion,
189	}
190
191	preparer := autorest.CreatePreparer(
192		autorest.AsDelete(),
193		autorest.WithBaseURL(client.BaseURI),
194		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies/{policyId}", pathParameters),
195		autorest.WithQueryParameters(queryParameters),
196		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
197	return preparer.Prepare((&http.Request{}).WithContext(ctx))
198}
199
200// DeleteSender sends the Delete request. The method will close the
201// http.Response Body if it receives an error.
202func (client PolicyClient) DeleteSender(req *http.Request) (*http.Response, error) {
203	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
204}
205
206// DeleteResponder handles the response to the Delete request. The method always
207// closes the http.Response Body.
208func (client PolicyClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
209	err = autorest.Respond(
210		resp,
211		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
212		autorest.ByClosing())
213	result.Response = resp
214	return
215}
216
217// Get get the Global policy definition of the Api Management service.
218// Parameters:
219// resourceGroupName - the name of the resource group.
220// serviceName - the name of the API Management service.
221// formatParameter - policy Export Format.
222func (client PolicyClient) Get(ctx context.Context, resourceGroupName string, serviceName string, formatParameter PolicyExportFormat) (result PolicyContract, err error) {
223	if tracing.IsEnabled() {
224		ctx = tracing.StartSpan(ctx, fqdn+"/PolicyClient.Get")
225		defer func() {
226			sc := -1
227			if result.Response.Response != nil {
228				sc = result.Response.Response.StatusCode
229			}
230			tracing.EndSpan(ctx, sc, err)
231		}()
232	}
233	if err := validation.Validate([]validation.Validation{
234		{TargetValue: serviceName,
235			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
236				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
237				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
238		return result, validation.NewError("apimanagement.PolicyClient", "Get", err.Error())
239	}
240
241	req, err := client.GetPreparer(ctx, resourceGroupName, serviceName, formatParameter)
242	if err != nil {
243		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "Get", nil, "Failure preparing request")
244		return
245	}
246
247	resp, err := client.GetSender(req)
248	if err != nil {
249		result.Response = autorest.Response{Response: resp}
250		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "Get", resp, "Failure sending request")
251		return
252	}
253
254	result, err = client.GetResponder(resp)
255	if err != nil {
256		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "Get", resp, "Failure responding to request")
257		return
258	}
259
260	return
261}
262
263// GetPreparer prepares the Get request.
264func (client PolicyClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string, formatParameter PolicyExportFormat) (*http.Request, error) {
265	pathParameters := map[string]interface{}{
266		"policyId":          autorest.Encode("path", "policy"),
267		"resourceGroupName": autorest.Encode("path", resourceGroupName),
268		"serviceName":       autorest.Encode("path", serviceName),
269		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
270	}
271
272	const APIVersion = "2021-01-01-preview"
273	queryParameters := map[string]interface{}{
274		"api-version": APIVersion,
275	}
276	if len(string(formatParameter)) > 0 {
277		queryParameters["format"] = autorest.Encode("query", formatParameter)
278	} else {
279		queryParameters["format"] = autorest.Encode("query", "xml")
280	}
281
282	preparer := autorest.CreatePreparer(
283		autorest.AsGet(),
284		autorest.WithBaseURL(client.BaseURI),
285		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies/{policyId}", pathParameters),
286		autorest.WithQueryParameters(queryParameters))
287	return preparer.Prepare((&http.Request{}).WithContext(ctx))
288}
289
290// GetSender sends the Get request. The method will close the
291// http.Response Body if it receives an error.
292func (client PolicyClient) GetSender(req *http.Request) (*http.Response, error) {
293	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
294}
295
296// GetResponder handles the response to the Get request. The method always
297// closes the http.Response Body.
298func (client PolicyClient) GetResponder(resp *http.Response) (result PolicyContract, err error) {
299	err = autorest.Respond(
300		resp,
301		azure.WithErrorUnlessStatusCode(http.StatusOK),
302		autorest.ByUnmarshallingJSON(&result),
303		autorest.ByClosing())
304	result.Response = autorest.Response{Response: resp}
305	return
306}
307
308// GetEntityTag gets the entity state (Etag) version of the Global policy definition in the Api Management service.
309// Parameters:
310// resourceGroupName - the name of the resource group.
311// serviceName - the name of the API Management service.
312func (client PolicyClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string) (result autorest.Response, err error) {
313	if tracing.IsEnabled() {
314		ctx = tracing.StartSpan(ctx, fqdn+"/PolicyClient.GetEntityTag")
315		defer func() {
316			sc := -1
317			if result.Response != nil {
318				sc = result.Response.StatusCode
319			}
320			tracing.EndSpan(ctx, sc, err)
321		}()
322	}
323	if err := validation.Validate([]validation.Validation{
324		{TargetValue: serviceName,
325			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
326				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
327				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
328		return result, validation.NewError("apimanagement.PolicyClient", "GetEntityTag", err.Error())
329	}
330
331	req, err := client.GetEntityTagPreparer(ctx, resourceGroupName, serviceName)
332	if err != nil {
333		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "GetEntityTag", nil, "Failure preparing request")
334		return
335	}
336
337	resp, err := client.GetEntityTagSender(req)
338	if err != nil {
339		result.Response = resp
340		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "GetEntityTag", resp, "Failure sending request")
341		return
342	}
343
344	result, err = client.GetEntityTagResponder(resp)
345	if err != nil {
346		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "GetEntityTag", resp, "Failure responding to request")
347		return
348	}
349
350	return
351}
352
353// GetEntityTagPreparer prepares the GetEntityTag request.
354func (client PolicyClient) GetEntityTagPreparer(ctx context.Context, resourceGroupName string, serviceName string) (*http.Request, error) {
355	pathParameters := map[string]interface{}{
356		"policyId":          autorest.Encode("path", "policy"),
357		"resourceGroupName": autorest.Encode("path", resourceGroupName),
358		"serviceName":       autorest.Encode("path", serviceName),
359		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
360	}
361
362	const APIVersion = "2021-01-01-preview"
363	queryParameters := map[string]interface{}{
364		"api-version": APIVersion,
365	}
366
367	preparer := autorest.CreatePreparer(
368		autorest.AsHead(),
369		autorest.WithBaseURL(client.BaseURI),
370		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies/{policyId}", pathParameters),
371		autorest.WithQueryParameters(queryParameters))
372	return preparer.Prepare((&http.Request{}).WithContext(ctx))
373}
374
375// GetEntityTagSender sends the GetEntityTag request. The method will close the
376// http.Response Body if it receives an error.
377func (client PolicyClient) GetEntityTagSender(req *http.Request) (*http.Response, error) {
378	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
379}
380
381// GetEntityTagResponder handles the response to the GetEntityTag request. The method always
382// closes the http.Response Body.
383func (client PolicyClient) GetEntityTagResponder(resp *http.Response) (result autorest.Response, err error) {
384	err = autorest.Respond(
385		resp,
386		azure.WithErrorUnlessStatusCode(http.StatusOK),
387		autorest.ByClosing())
388	result.Response = resp
389	return
390}
391
392// ListByService lists all the Global Policy definitions of the Api Management service.
393// Parameters:
394// resourceGroupName - the name of the resource group.
395// serviceName - the name of the API Management service.
396func (client PolicyClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string) (result PolicyCollection, err error) {
397	if tracing.IsEnabled() {
398		ctx = tracing.StartSpan(ctx, fqdn+"/PolicyClient.ListByService")
399		defer func() {
400			sc := -1
401			if result.Response.Response != nil {
402				sc = result.Response.Response.StatusCode
403			}
404			tracing.EndSpan(ctx, sc, err)
405		}()
406	}
407	if err := validation.Validate([]validation.Validation{
408		{TargetValue: serviceName,
409			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
410				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
411				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
412		return result, validation.NewError("apimanagement.PolicyClient", "ListByService", err.Error())
413	}
414
415	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName)
416	if err != nil {
417		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "ListByService", nil, "Failure preparing request")
418		return
419	}
420
421	resp, err := client.ListByServiceSender(req)
422	if err != nil {
423		result.Response = autorest.Response{Response: resp}
424		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "ListByService", resp, "Failure sending request")
425		return
426	}
427
428	result, err = client.ListByServiceResponder(resp)
429	if err != nil {
430		err = autorest.NewErrorWithError(err, "apimanagement.PolicyClient", "ListByService", resp, "Failure responding to request")
431		return
432	}
433
434	return
435}
436
437// ListByServicePreparer prepares the ListByService request.
438func (client PolicyClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string) (*http.Request, error) {
439	pathParameters := map[string]interface{}{
440		"resourceGroupName": autorest.Encode("path", resourceGroupName),
441		"serviceName":       autorest.Encode("path", serviceName),
442		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
443	}
444
445	const APIVersion = "2021-01-01-preview"
446	queryParameters := map[string]interface{}{
447		"api-version": APIVersion,
448	}
449
450	preparer := autorest.CreatePreparer(
451		autorest.AsGet(),
452		autorest.WithBaseURL(client.BaseURI),
453		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/policies", pathParameters),
454		autorest.WithQueryParameters(queryParameters))
455	return preparer.Prepare((&http.Request{}).WithContext(ctx))
456}
457
458// ListByServiceSender sends the ListByService request. The method will close the
459// http.Response Body if it receives an error.
460func (client PolicyClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
461	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
462}
463
464// ListByServiceResponder handles the response to the ListByService request. The method always
465// closes the http.Response Body.
466func (client PolicyClient) ListByServiceResponder(resp *http.Response) (result PolicyCollection, err error) {
467	err = autorest.Respond(
468		resp,
469		azure.WithErrorUnlessStatusCode(http.StatusOK),
470		autorest.ByUnmarshallingJSON(&result),
471		autorest.ByClosing())
472	result.Response = autorest.Response{Response: resp}
473	return
474}
475