1package frontdoor
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// PoliciesClient is the frontDoor Client
19type PoliciesClient struct {
20	BaseClient
21}
22
23// NewPoliciesClient creates an instance of the PoliciesClient client.
24func NewPoliciesClient(subscriptionID string) PoliciesClient {
25	return NewPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewPoliciesClientWithBaseURI creates an instance of the PoliciesClient client using a custom endpoint.  Use this
29// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewPoliciesClientWithBaseURI(baseURI string, subscriptionID string) PoliciesClient {
31	return PoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate create or update policy with specified rule set name within a resource group.
35// Parameters:
36// resourceGroupName - name of the Resource group within the Azure subscription.
37// policyName - the name of the Web Application Firewall Policy.
38// parameters - policy to be created.
39func (client PoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, policyName string, parameters WebApplicationFirewallPolicy) (result PoliciesCreateOrUpdateFuture, err error) {
40	if tracing.IsEnabled() {
41		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.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	if err := validation.Validate([]validation.Validation{
51		{TargetValue: resourceGroupName,
52			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
53				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
54				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}},
55		{TargetValue: policyName,
56			Constraints: []validation.Constraint{{Target: "policyName", Name: validation.MaxLength, Rule: 128, Chain: nil}}},
57		{TargetValue: parameters,
58			Constraints: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties", Name: validation.Null, Rule: false,
59				Chain: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties.PolicySettings", Name: validation.Null, Rule: false,
60					Chain: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties.PolicySettings.CustomBlockResponseBody", Name: validation.Null, Rule: false,
61						Chain: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties.PolicySettings.CustomBlockResponseBody", Name: validation.Pattern, Rule: `^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$`, Chain: nil}}},
62					}},
63				}}}}}); err != nil {
64		return result, validation.NewError("frontdoor.PoliciesClient", "CreateOrUpdate", err.Error())
65	}
66
67	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, policyName, parameters)
68	if err != nil {
69		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "CreateOrUpdate", nil, "Failure preparing request")
70		return
71	}
72
73	result, err = client.CreateOrUpdateSender(req)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "CreateOrUpdate", nil, "Failure sending request")
76		return
77	}
78
79	return
80}
81
82// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
83func (client PoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, policyName string, parameters WebApplicationFirewallPolicy) (*http.Request, error) {
84	pathParameters := map[string]interface{}{
85		"policyName":        autorest.Encode("path", policyName),
86		"resourceGroupName": autorest.Encode("path", resourceGroupName),
87		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
88	}
89
90	const APIVersion = "2019-10-01"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsContentType("application/json; charset=utf-8"),
97		autorest.AsPut(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies/{policyName}", pathParameters),
100		autorest.WithJSON(parameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
106// http.Response Body if it receives an error.
107func (client PoliciesClient) CreateOrUpdateSender(req *http.Request) (future PoliciesCreateOrUpdateFuture, err error) {
108	var resp *http.Response
109	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
110	if err != nil {
111		return
112	}
113	var azf azure.Future
114	azf, err = azure.NewFutureFromResponse(resp)
115	future.FutureAPI = &azf
116	future.Result = future.result
117	return
118}
119
120// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
121// closes the http.Response Body.
122func (client PoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result WebApplicationFirewallPolicy, err error) {
123	err = autorest.Respond(
124		resp,
125		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
126		autorest.ByUnmarshallingJSON(&result),
127		autorest.ByClosing())
128	result.Response = autorest.Response{Response: resp}
129	return
130}
131
132// Delete deletes Policy
133// Parameters:
134// resourceGroupName - name of the Resource group within the Azure subscription.
135// policyName - the name of the Web Application Firewall Policy.
136func (client PoliciesClient) Delete(ctx context.Context, resourceGroupName string, policyName string) (result PoliciesDeleteFuture, err error) {
137	if tracing.IsEnabled() {
138		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Delete")
139		defer func() {
140			sc := -1
141			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
142				sc = result.FutureAPI.Response().StatusCode
143			}
144			tracing.EndSpan(ctx, sc, err)
145		}()
146	}
147	if err := validation.Validate([]validation.Validation{
148		{TargetValue: resourceGroupName,
149			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
150				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
151				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}},
152		{TargetValue: policyName,
153			Constraints: []validation.Constraint{{Target: "policyName", Name: validation.MaxLength, Rule: 128, Chain: nil}}}}); err != nil {
154		return result, validation.NewError("frontdoor.PoliciesClient", "Delete", err.Error())
155	}
156
157	req, err := client.DeletePreparer(ctx, resourceGroupName, policyName)
158	if err != nil {
159		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Delete", nil, "Failure preparing request")
160		return
161	}
162
163	result, err = client.DeleteSender(req)
164	if err != nil {
165		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Delete", nil, "Failure sending request")
166		return
167	}
168
169	return
170}
171
172// DeletePreparer prepares the Delete request.
173func (client PoliciesClient) DeletePreparer(ctx context.Context, resourceGroupName string, policyName string) (*http.Request, error) {
174	pathParameters := map[string]interface{}{
175		"policyName":        autorest.Encode("path", policyName),
176		"resourceGroupName": autorest.Encode("path", resourceGroupName),
177		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
178	}
179
180	const APIVersion = "2019-10-01"
181	queryParameters := map[string]interface{}{
182		"api-version": APIVersion,
183	}
184
185	preparer := autorest.CreatePreparer(
186		autorest.AsDelete(),
187		autorest.WithBaseURL(client.BaseURI),
188		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies/{policyName}", pathParameters),
189		autorest.WithQueryParameters(queryParameters))
190	return preparer.Prepare((&http.Request{}).WithContext(ctx))
191}
192
193// DeleteSender sends the Delete request. The method will close the
194// http.Response Body if it receives an error.
195func (client PoliciesClient) DeleteSender(req *http.Request) (future PoliciesDeleteFuture, err error) {
196	var resp *http.Response
197	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
198	if err != nil {
199		return
200	}
201	var azf azure.Future
202	azf, err = azure.NewFutureFromResponse(resp)
203	future.FutureAPI = &azf
204	future.Result = future.result
205	return
206}
207
208// DeleteResponder handles the response to the Delete request. The method always
209// closes the http.Response Body.
210func (client PoliciesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
211	err = autorest.Respond(
212		resp,
213		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
214		autorest.ByClosing())
215	result.Response = resp
216	return
217}
218
219// Get retrieve protection policy with specified name within a resource group.
220// Parameters:
221// resourceGroupName - name of the Resource group within the Azure subscription.
222// policyName - the name of the Web Application Firewall Policy.
223func (client PoliciesClient) Get(ctx context.Context, resourceGroupName string, policyName string) (result WebApplicationFirewallPolicy, err error) {
224	if tracing.IsEnabled() {
225		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.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: resourceGroupName,
236			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
237				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
238				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}},
239		{TargetValue: policyName,
240			Constraints: []validation.Constraint{{Target: "policyName", Name: validation.MaxLength, Rule: 128, Chain: nil}}}}); err != nil {
241		return result, validation.NewError("frontdoor.PoliciesClient", "Get", err.Error())
242	}
243
244	req, err := client.GetPreparer(ctx, resourceGroupName, policyName)
245	if err != nil {
246		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Get", nil, "Failure preparing request")
247		return
248	}
249
250	resp, err := client.GetSender(req)
251	if err != nil {
252		result.Response = autorest.Response{Response: resp}
253		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Get", resp, "Failure sending request")
254		return
255	}
256
257	result, err = client.GetResponder(resp)
258	if err != nil {
259		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Get", resp, "Failure responding to request")
260		return
261	}
262
263	return
264}
265
266// GetPreparer prepares the Get request.
267func (client PoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, policyName string) (*http.Request, error) {
268	pathParameters := map[string]interface{}{
269		"policyName":        autorest.Encode("path", policyName),
270		"resourceGroupName": autorest.Encode("path", resourceGroupName),
271		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
272	}
273
274	const APIVersion = "2019-10-01"
275	queryParameters := map[string]interface{}{
276		"api-version": APIVersion,
277	}
278
279	preparer := autorest.CreatePreparer(
280		autorest.AsGet(),
281		autorest.WithBaseURL(client.BaseURI),
282		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies/{policyName}", 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 PoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
290	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
291}
292
293// GetResponder handles the response to the Get request. The method always
294// closes the http.Response Body.
295func (client PoliciesClient) GetResponder(resp *http.Response) (result WebApplicationFirewallPolicy, 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 all of the protection policies within a resource group.
306// Parameters:
307// resourceGroupName - name of the Resource group within the Azure subscription.
308func (client PoliciesClient) List(ctx context.Context, resourceGroupName string) (result WebApplicationFirewallPolicyListPage, err error) {
309	if tracing.IsEnabled() {
310		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List")
311		defer func() {
312			sc := -1
313			if result.wafpl.Response.Response != nil {
314				sc = result.wafpl.Response.Response.StatusCode
315			}
316			tracing.EndSpan(ctx, sc, err)
317		}()
318	}
319	if err := validation.Validate([]validation.Validation{
320		{TargetValue: resourceGroupName,
321			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
322				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
323				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}}}); err != nil {
324		return result, validation.NewError("frontdoor.PoliciesClient", "List", err.Error())
325	}
326
327	result.fn = client.listNextResults
328	req, err := client.ListPreparer(ctx, resourceGroupName)
329	if err != nil {
330		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "List", nil, "Failure preparing request")
331		return
332	}
333
334	resp, err := client.ListSender(req)
335	if err != nil {
336		result.wafpl.Response = autorest.Response{Response: resp}
337		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "List", resp, "Failure sending request")
338		return
339	}
340
341	result.wafpl, err = client.ListResponder(resp)
342	if err != nil {
343		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "List", resp, "Failure responding to request")
344		return
345	}
346	if result.wafpl.hasNextLink() && result.wafpl.IsEmpty() {
347		err = result.NextWithContext(ctx)
348		return
349	}
350
351	return
352}
353
354// ListPreparer prepares the List request.
355func (client PoliciesClient) ListPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
356	pathParameters := map[string]interface{}{
357		"resourceGroupName": autorest.Encode("path", resourceGroupName),
358		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
359	}
360
361	const APIVersion = "2019-10-01"
362	queryParameters := map[string]interface{}{
363		"api-version": APIVersion,
364	}
365
366	preparer := autorest.CreatePreparer(
367		autorest.AsGet(),
368		autorest.WithBaseURL(client.BaseURI),
369		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies", pathParameters),
370		autorest.WithQueryParameters(queryParameters))
371	return preparer.Prepare((&http.Request{}).WithContext(ctx))
372}
373
374// ListSender sends the List request. The method will close the
375// http.Response Body if it receives an error.
376func (client PoliciesClient) ListSender(req *http.Request) (*http.Response, error) {
377	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
378}
379
380// ListResponder handles the response to the List request. The method always
381// closes the http.Response Body.
382func (client PoliciesClient) ListResponder(resp *http.Response) (result WebApplicationFirewallPolicyList, err error) {
383	err = autorest.Respond(
384		resp,
385		azure.WithErrorUnlessStatusCode(http.StatusOK),
386		autorest.ByUnmarshallingJSON(&result),
387		autorest.ByClosing())
388	result.Response = autorest.Response{Response: resp}
389	return
390}
391
392// listNextResults retrieves the next set of results, if any.
393func (client PoliciesClient) listNextResults(ctx context.Context, lastResults WebApplicationFirewallPolicyList) (result WebApplicationFirewallPolicyList, err error) {
394	req, err := lastResults.webApplicationFirewallPolicyListPreparer(ctx)
395	if err != nil {
396		return result, autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "listNextResults", nil, "Failure preparing next results request")
397	}
398	if req == nil {
399		return
400	}
401	resp, err := client.ListSender(req)
402	if err != nil {
403		result.Response = autorest.Response{Response: resp}
404		return result, autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "listNextResults", resp, "Failure sending next results request")
405	}
406	result, err = client.ListResponder(resp)
407	if err != nil {
408		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "listNextResults", resp, "Failure responding to next results request")
409	}
410	return
411}
412
413// ListComplete enumerates all values, automatically crossing page boundaries as required.
414func (client PoliciesClient) ListComplete(ctx context.Context, resourceGroupName string) (result WebApplicationFirewallPolicyListIterator, err error) {
415	if tracing.IsEnabled() {
416		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List")
417		defer func() {
418			sc := -1
419			if result.Response().Response.Response != nil {
420				sc = result.page.Response().Response.Response.StatusCode
421			}
422			tracing.EndSpan(ctx, sc, err)
423		}()
424	}
425	result.page, err = client.List(ctx, resourceGroupName)
426	return
427}
428