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