1// +build go1.13 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// Code generated by Microsoft (R) AutoRest Code Generator. 6// Changes may cause incorrect behavior and will be lost if the code is regenerated. 7 8package armnetwork 9 10import ( 11 "context" 12 "errors" 13 "fmt" 14 "github.com/Azure/azure-sdk-for-go/sdk/armcore" 15 "github.com/Azure/azure-sdk-for-go/sdk/azcore" 16 "net/http" 17 "net/url" 18 "strings" 19 "time" 20) 21 22// PacketCapturesClient contains the methods for the PacketCaptures group. 23// Don't use this type directly, use NewPacketCapturesClient() instead. 24type PacketCapturesClient struct { 25 con *armcore.Connection 26 subscriptionID string 27} 28 29// NewPacketCapturesClient creates a new instance of PacketCapturesClient with the specified values. 30func NewPacketCapturesClient(con *armcore.Connection, subscriptionID string) *PacketCapturesClient { 31 return &PacketCapturesClient{con: con, subscriptionID: subscriptionID} 32} 33 34// BeginCreate - Create and start a packet capture on the specified VM. 35// If the operation fails it returns the *ErrorResponse error type. 36func (client *PacketCapturesClient) BeginCreate(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, parameters PacketCapture, options *PacketCapturesBeginCreateOptions) (PacketCaptureResultPollerResponse, error) { 37 resp, err := client.create(ctx, resourceGroupName, networkWatcherName, packetCaptureName, parameters, options) 38 if err != nil { 39 return PacketCaptureResultPollerResponse{}, err 40 } 41 result := PacketCaptureResultPollerResponse{ 42 RawResponse: resp.Response, 43 } 44 pt, err := armcore.NewLROPoller("PacketCapturesClient.Create", "azure-async-operation", resp, client.con.Pipeline(), client.createHandleError) 45 if err != nil { 46 return PacketCaptureResultPollerResponse{}, err 47 } 48 poller := &packetCaptureResultPoller{ 49 pt: pt, 50 } 51 result.Poller = poller 52 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (PacketCaptureResultResponse, error) { 53 return poller.pollUntilDone(ctx, frequency) 54 } 55 return result, nil 56} 57 58// ResumeCreate creates a new PacketCaptureResultPoller from the specified resume token. 59// token - The value must come from a previous call to PacketCaptureResultPoller.ResumeToken(). 60func (client *PacketCapturesClient) ResumeCreate(ctx context.Context, token string) (PacketCaptureResultPollerResponse, error) { 61 pt, err := armcore.NewLROPollerFromResumeToken("PacketCapturesClient.Create", token, client.con.Pipeline(), client.createHandleError) 62 if err != nil { 63 return PacketCaptureResultPollerResponse{}, err 64 } 65 poller := &packetCaptureResultPoller{ 66 pt: pt, 67 } 68 resp, err := poller.Poll(ctx) 69 if err != nil { 70 return PacketCaptureResultPollerResponse{}, err 71 } 72 result := PacketCaptureResultPollerResponse{ 73 RawResponse: resp, 74 } 75 result.Poller = poller 76 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (PacketCaptureResultResponse, error) { 77 return poller.pollUntilDone(ctx, frequency) 78 } 79 return result, nil 80} 81 82// Create - Create and start a packet capture on the specified VM. 83// If the operation fails it returns the *ErrorResponse error type. 84func (client *PacketCapturesClient) create(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, parameters PacketCapture, options *PacketCapturesBeginCreateOptions) (*azcore.Response, error) { 85 req, err := client.createCreateRequest(ctx, resourceGroupName, networkWatcherName, packetCaptureName, parameters, options) 86 if err != nil { 87 return nil, err 88 } 89 resp, err := client.con.Pipeline().Do(req) 90 if err != nil { 91 return nil, err 92 } 93 if !resp.HasStatusCode(http.StatusCreated) { 94 return nil, client.createHandleError(resp) 95 } 96 return resp, nil 97} 98 99// createCreateRequest creates the Create request. 100func (client *PacketCapturesClient) createCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, parameters PacketCapture, options *PacketCapturesBeginCreateOptions) (*azcore.Request, error) { 101 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/packetCaptures/{packetCaptureName}" 102 if resourceGroupName == "" { 103 return nil, errors.New("parameter resourceGroupName cannot be empty") 104 } 105 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 106 if networkWatcherName == "" { 107 return nil, errors.New("parameter networkWatcherName cannot be empty") 108 } 109 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 110 if packetCaptureName == "" { 111 return nil, errors.New("parameter packetCaptureName cannot be empty") 112 } 113 urlPath = strings.ReplaceAll(urlPath, "{packetCaptureName}", url.PathEscape(packetCaptureName)) 114 if client.subscriptionID == "" { 115 return nil, errors.New("parameter client.subscriptionID cannot be empty") 116 } 117 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 118 req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 119 if err != nil { 120 return nil, err 121 } 122 req.Telemetry(telemetryInfo) 123 reqQP := req.URL.Query() 124 reqQP.Set("api-version", "2021-02-01") 125 req.URL.RawQuery = reqQP.Encode() 126 req.Header.Set("Accept", "application/json") 127 return req, req.MarshalAsJSON(parameters) 128} 129 130// createHandleError handles the Create error response. 131func (client *PacketCapturesClient) createHandleError(resp *azcore.Response) error { 132 body, err := resp.Payload() 133 if err != nil { 134 return azcore.NewResponseError(err, resp.Response) 135 } 136 errType := ErrorResponse{raw: string(body)} 137 if err := resp.UnmarshalAsJSON(&errType); err != nil { 138 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 139 } 140 return azcore.NewResponseError(&errType, resp.Response) 141} 142 143// BeginDelete - Deletes the specified packet capture session. 144// If the operation fails it returns the *ErrorResponse error type. 145func (client *PacketCapturesClient) BeginDelete(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginDeleteOptions) (HTTPPollerResponse, error) { 146 resp, err := client.deleteOperation(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 147 if err != nil { 148 return HTTPPollerResponse{}, err 149 } 150 result := HTTPPollerResponse{ 151 RawResponse: resp.Response, 152 } 153 pt, err := armcore.NewLROPoller("PacketCapturesClient.Delete", "location", resp, client.con.Pipeline(), client.deleteHandleError) 154 if err != nil { 155 return HTTPPollerResponse{}, err 156 } 157 poller := &httpPoller{ 158 pt: pt, 159 } 160 result.Poller = poller 161 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) { 162 return poller.pollUntilDone(ctx, frequency) 163 } 164 return result, nil 165} 166 167// ResumeDelete creates a new HTTPPoller from the specified resume token. 168// token - The value must come from a previous call to HTTPPoller.ResumeToken(). 169func (client *PacketCapturesClient) ResumeDelete(ctx context.Context, token string) (HTTPPollerResponse, error) { 170 pt, err := armcore.NewLROPollerFromResumeToken("PacketCapturesClient.Delete", token, client.con.Pipeline(), client.deleteHandleError) 171 if err != nil { 172 return HTTPPollerResponse{}, err 173 } 174 poller := &httpPoller{ 175 pt: pt, 176 } 177 resp, err := poller.Poll(ctx) 178 if err != nil { 179 return HTTPPollerResponse{}, err 180 } 181 result := HTTPPollerResponse{ 182 RawResponse: resp, 183 } 184 result.Poller = poller 185 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) { 186 return poller.pollUntilDone(ctx, frequency) 187 } 188 return result, nil 189} 190 191// Delete - Deletes the specified packet capture session. 192// If the operation fails it returns the *ErrorResponse error type. 193func (client *PacketCapturesClient) deleteOperation(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginDeleteOptions) (*azcore.Response, error) { 194 req, err := client.deleteCreateRequest(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 195 if err != nil { 196 return nil, err 197 } 198 resp, err := client.con.Pipeline().Do(req) 199 if err != nil { 200 return nil, err 201 } 202 if !resp.HasStatusCode(http.StatusAccepted, http.StatusNoContent) { 203 return nil, client.deleteHandleError(resp) 204 } 205 return resp, nil 206} 207 208// deleteCreateRequest creates the Delete request. 209func (client *PacketCapturesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginDeleteOptions) (*azcore.Request, error) { 210 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/packetCaptures/{packetCaptureName}" 211 if resourceGroupName == "" { 212 return nil, errors.New("parameter resourceGroupName cannot be empty") 213 } 214 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 215 if networkWatcherName == "" { 216 return nil, errors.New("parameter networkWatcherName cannot be empty") 217 } 218 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 219 if packetCaptureName == "" { 220 return nil, errors.New("parameter packetCaptureName cannot be empty") 221 } 222 urlPath = strings.ReplaceAll(urlPath, "{packetCaptureName}", url.PathEscape(packetCaptureName)) 223 if client.subscriptionID == "" { 224 return nil, errors.New("parameter client.subscriptionID cannot be empty") 225 } 226 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 227 req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 228 if err != nil { 229 return nil, err 230 } 231 req.Telemetry(telemetryInfo) 232 reqQP := req.URL.Query() 233 reqQP.Set("api-version", "2021-02-01") 234 req.URL.RawQuery = reqQP.Encode() 235 req.Header.Set("Accept", "application/json") 236 return req, nil 237} 238 239// deleteHandleError handles the Delete error response. 240func (client *PacketCapturesClient) deleteHandleError(resp *azcore.Response) error { 241 body, err := resp.Payload() 242 if err != nil { 243 return azcore.NewResponseError(err, resp.Response) 244 } 245 errType := ErrorResponse{raw: string(body)} 246 if err := resp.UnmarshalAsJSON(&errType); err != nil { 247 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 248 } 249 return azcore.NewResponseError(&errType, resp.Response) 250} 251 252// Get - Gets a packet capture session by name. 253// If the operation fails it returns the *ErrorResponse error type. 254func (client *PacketCapturesClient) Get(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesGetOptions) (PacketCaptureResultResponse, error) { 255 req, err := client.getCreateRequest(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 256 if err != nil { 257 return PacketCaptureResultResponse{}, err 258 } 259 resp, err := client.con.Pipeline().Do(req) 260 if err != nil { 261 return PacketCaptureResultResponse{}, err 262 } 263 if !resp.HasStatusCode(http.StatusOK) { 264 return PacketCaptureResultResponse{}, client.getHandleError(resp) 265 } 266 return client.getHandleResponse(resp) 267} 268 269// getCreateRequest creates the Get request. 270func (client *PacketCapturesClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesGetOptions) (*azcore.Request, error) { 271 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/packetCaptures/{packetCaptureName}" 272 if resourceGroupName == "" { 273 return nil, errors.New("parameter resourceGroupName cannot be empty") 274 } 275 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 276 if networkWatcherName == "" { 277 return nil, errors.New("parameter networkWatcherName cannot be empty") 278 } 279 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 280 if packetCaptureName == "" { 281 return nil, errors.New("parameter packetCaptureName cannot be empty") 282 } 283 urlPath = strings.ReplaceAll(urlPath, "{packetCaptureName}", url.PathEscape(packetCaptureName)) 284 if client.subscriptionID == "" { 285 return nil, errors.New("parameter client.subscriptionID cannot be empty") 286 } 287 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 288 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 289 if err != nil { 290 return nil, err 291 } 292 req.Telemetry(telemetryInfo) 293 reqQP := req.URL.Query() 294 reqQP.Set("api-version", "2021-02-01") 295 req.URL.RawQuery = reqQP.Encode() 296 req.Header.Set("Accept", "application/json") 297 return req, nil 298} 299 300// getHandleResponse handles the Get response. 301func (client *PacketCapturesClient) getHandleResponse(resp *azcore.Response) (PacketCaptureResultResponse, error) { 302 var val *PacketCaptureResult 303 if err := resp.UnmarshalAsJSON(&val); err != nil { 304 return PacketCaptureResultResponse{}, err 305 } 306 return PacketCaptureResultResponse{RawResponse: resp.Response, PacketCaptureResult: val}, nil 307} 308 309// getHandleError handles the Get error response. 310func (client *PacketCapturesClient) getHandleError(resp *azcore.Response) error { 311 body, err := resp.Payload() 312 if err != nil { 313 return azcore.NewResponseError(err, resp.Response) 314 } 315 errType := ErrorResponse{raw: string(body)} 316 if err := resp.UnmarshalAsJSON(&errType); err != nil { 317 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 318 } 319 return azcore.NewResponseError(&errType, resp.Response) 320} 321 322// BeginGetStatus - Query the status of a running packet capture session. 323// If the operation fails it returns the *ErrorResponse error type. 324func (client *PacketCapturesClient) BeginGetStatus(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginGetStatusOptions) (PacketCaptureQueryStatusResultPollerResponse, error) { 325 resp, err := client.getStatus(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 326 if err != nil { 327 return PacketCaptureQueryStatusResultPollerResponse{}, err 328 } 329 result := PacketCaptureQueryStatusResultPollerResponse{ 330 RawResponse: resp.Response, 331 } 332 pt, err := armcore.NewLROPoller("PacketCapturesClient.GetStatus", "location", resp, client.con.Pipeline(), client.getStatusHandleError) 333 if err != nil { 334 return PacketCaptureQueryStatusResultPollerResponse{}, err 335 } 336 poller := &packetCaptureQueryStatusResultPoller{ 337 pt: pt, 338 } 339 result.Poller = poller 340 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (PacketCaptureQueryStatusResultResponse, error) { 341 return poller.pollUntilDone(ctx, frequency) 342 } 343 return result, nil 344} 345 346// ResumeGetStatus creates a new PacketCaptureQueryStatusResultPoller from the specified resume token. 347// token - The value must come from a previous call to PacketCaptureQueryStatusResultPoller.ResumeToken(). 348func (client *PacketCapturesClient) ResumeGetStatus(ctx context.Context, token string) (PacketCaptureQueryStatusResultPollerResponse, error) { 349 pt, err := armcore.NewLROPollerFromResumeToken("PacketCapturesClient.GetStatus", token, client.con.Pipeline(), client.getStatusHandleError) 350 if err != nil { 351 return PacketCaptureQueryStatusResultPollerResponse{}, err 352 } 353 poller := &packetCaptureQueryStatusResultPoller{ 354 pt: pt, 355 } 356 resp, err := poller.Poll(ctx) 357 if err != nil { 358 return PacketCaptureQueryStatusResultPollerResponse{}, err 359 } 360 result := PacketCaptureQueryStatusResultPollerResponse{ 361 RawResponse: resp, 362 } 363 result.Poller = poller 364 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (PacketCaptureQueryStatusResultResponse, error) { 365 return poller.pollUntilDone(ctx, frequency) 366 } 367 return result, nil 368} 369 370// GetStatus - Query the status of a running packet capture session. 371// If the operation fails it returns the *ErrorResponse error type. 372func (client *PacketCapturesClient) getStatus(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginGetStatusOptions) (*azcore.Response, error) { 373 req, err := client.getStatusCreateRequest(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 374 if err != nil { 375 return nil, err 376 } 377 resp, err := client.con.Pipeline().Do(req) 378 if err != nil { 379 return nil, err 380 } 381 if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted) { 382 return nil, client.getStatusHandleError(resp) 383 } 384 return resp, nil 385} 386 387// getStatusCreateRequest creates the GetStatus request. 388func (client *PacketCapturesClient) getStatusCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginGetStatusOptions) (*azcore.Request, error) { 389 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/packetCaptures/{packetCaptureName}/queryStatus" 390 if resourceGroupName == "" { 391 return nil, errors.New("parameter resourceGroupName cannot be empty") 392 } 393 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 394 if networkWatcherName == "" { 395 return nil, errors.New("parameter networkWatcherName cannot be empty") 396 } 397 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 398 if packetCaptureName == "" { 399 return nil, errors.New("parameter packetCaptureName cannot be empty") 400 } 401 urlPath = strings.ReplaceAll(urlPath, "{packetCaptureName}", url.PathEscape(packetCaptureName)) 402 if client.subscriptionID == "" { 403 return nil, errors.New("parameter client.subscriptionID cannot be empty") 404 } 405 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 406 req, err := azcore.NewRequest(ctx, http.MethodPost, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 407 if err != nil { 408 return nil, err 409 } 410 req.Telemetry(telemetryInfo) 411 reqQP := req.URL.Query() 412 reqQP.Set("api-version", "2021-02-01") 413 req.URL.RawQuery = reqQP.Encode() 414 req.Header.Set("Accept", "application/json") 415 return req, nil 416} 417 418// getStatusHandleError handles the GetStatus error response. 419func (client *PacketCapturesClient) getStatusHandleError(resp *azcore.Response) error { 420 body, err := resp.Payload() 421 if err != nil { 422 return azcore.NewResponseError(err, resp.Response) 423 } 424 errType := ErrorResponse{raw: string(body)} 425 if err := resp.UnmarshalAsJSON(&errType); err != nil { 426 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 427 } 428 return azcore.NewResponseError(&errType, resp.Response) 429} 430 431// List - Lists all packet capture sessions within the specified resource group. 432// If the operation fails it returns the *ErrorResponse error type. 433func (client *PacketCapturesClient) List(ctx context.Context, resourceGroupName string, networkWatcherName string, options *PacketCapturesListOptions) (PacketCaptureListResultResponse, error) { 434 req, err := client.listCreateRequest(ctx, resourceGroupName, networkWatcherName, options) 435 if err != nil { 436 return PacketCaptureListResultResponse{}, err 437 } 438 resp, err := client.con.Pipeline().Do(req) 439 if err != nil { 440 return PacketCaptureListResultResponse{}, err 441 } 442 if !resp.HasStatusCode(http.StatusOK) { 443 return PacketCaptureListResultResponse{}, client.listHandleError(resp) 444 } 445 return client.listHandleResponse(resp) 446} 447 448// listCreateRequest creates the List request. 449func (client *PacketCapturesClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, options *PacketCapturesListOptions) (*azcore.Request, error) { 450 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/packetCaptures" 451 if resourceGroupName == "" { 452 return nil, errors.New("parameter resourceGroupName cannot be empty") 453 } 454 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 455 if networkWatcherName == "" { 456 return nil, errors.New("parameter networkWatcherName cannot be empty") 457 } 458 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 459 if client.subscriptionID == "" { 460 return nil, errors.New("parameter client.subscriptionID cannot be empty") 461 } 462 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 463 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 464 if err != nil { 465 return nil, err 466 } 467 req.Telemetry(telemetryInfo) 468 reqQP := req.URL.Query() 469 reqQP.Set("api-version", "2021-02-01") 470 req.URL.RawQuery = reqQP.Encode() 471 req.Header.Set("Accept", "application/json") 472 return req, nil 473} 474 475// listHandleResponse handles the List response. 476func (client *PacketCapturesClient) listHandleResponse(resp *azcore.Response) (PacketCaptureListResultResponse, error) { 477 var val *PacketCaptureListResult 478 if err := resp.UnmarshalAsJSON(&val); err != nil { 479 return PacketCaptureListResultResponse{}, err 480 } 481 return PacketCaptureListResultResponse{RawResponse: resp.Response, PacketCaptureListResult: val}, nil 482} 483 484// listHandleError handles the List error response. 485func (client *PacketCapturesClient) listHandleError(resp *azcore.Response) error { 486 body, err := resp.Payload() 487 if err != nil { 488 return azcore.NewResponseError(err, resp.Response) 489 } 490 errType := ErrorResponse{raw: string(body)} 491 if err := resp.UnmarshalAsJSON(&errType); err != nil { 492 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 493 } 494 return azcore.NewResponseError(&errType, resp.Response) 495} 496 497// BeginStop - Stops a specified packet capture session. 498// If the operation fails it returns the *ErrorResponse error type. 499func (client *PacketCapturesClient) BeginStop(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginStopOptions) (HTTPPollerResponse, error) { 500 resp, err := client.stop(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 501 if err != nil { 502 return HTTPPollerResponse{}, err 503 } 504 result := HTTPPollerResponse{ 505 RawResponse: resp.Response, 506 } 507 pt, err := armcore.NewLROPoller("PacketCapturesClient.Stop", "location", resp, client.con.Pipeline(), client.stopHandleError) 508 if err != nil { 509 return HTTPPollerResponse{}, err 510 } 511 poller := &httpPoller{ 512 pt: pt, 513 } 514 result.Poller = poller 515 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) { 516 return poller.pollUntilDone(ctx, frequency) 517 } 518 return result, nil 519} 520 521// ResumeStop creates a new HTTPPoller from the specified resume token. 522// token - The value must come from a previous call to HTTPPoller.ResumeToken(). 523func (client *PacketCapturesClient) ResumeStop(ctx context.Context, token string) (HTTPPollerResponse, error) { 524 pt, err := armcore.NewLROPollerFromResumeToken("PacketCapturesClient.Stop", token, client.con.Pipeline(), client.stopHandleError) 525 if err != nil { 526 return HTTPPollerResponse{}, err 527 } 528 poller := &httpPoller{ 529 pt: pt, 530 } 531 resp, err := poller.Poll(ctx) 532 if err != nil { 533 return HTTPPollerResponse{}, err 534 } 535 result := HTTPPollerResponse{ 536 RawResponse: resp, 537 } 538 result.Poller = poller 539 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) { 540 return poller.pollUntilDone(ctx, frequency) 541 } 542 return result, nil 543} 544 545// Stop - Stops a specified packet capture session. 546// If the operation fails it returns the *ErrorResponse error type. 547func (client *PacketCapturesClient) stop(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginStopOptions) (*azcore.Response, error) { 548 req, err := client.stopCreateRequest(ctx, resourceGroupName, networkWatcherName, packetCaptureName, options) 549 if err != nil { 550 return nil, err 551 } 552 resp, err := client.con.Pipeline().Do(req) 553 if err != nil { 554 return nil, err 555 } 556 if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted) { 557 return nil, client.stopHandleError(resp) 558 } 559 return resp, nil 560} 561 562// stopCreateRequest creates the Stop request. 563func (client *PacketCapturesClient) stopCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, packetCaptureName string, options *PacketCapturesBeginStopOptions) (*azcore.Request, error) { 564 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/packetCaptures/{packetCaptureName}/stop" 565 if resourceGroupName == "" { 566 return nil, errors.New("parameter resourceGroupName cannot be empty") 567 } 568 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 569 if networkWatcherName == "" { 570 return nil, errors.New("parameter networkWatcherName cannot be empty") 571 } 572 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 573 if packetCaptureName == "" { 574 return nil, errors.New("parameter packetCaptureName cannot be empty") 575 } 576 urlPath = strings.ReplaceAll(urlPath, "{packetCaptureName}", url.PathEscape(packetCaptureName)) 577 if client.subscriptionID == "" { 578 return nil, errors.New("parameter client.subscriptionID cannot be empty") 579 } 580 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 581 req, err := azcore.NewRequest(ctx, http.MethodPost, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 582 if err != nil { 583 return nil, err 584 } 585 req.Telemetry(telemetryInfo) 586 reqQP := req.URL.Query() 587 reqQP.Set("api-version", "2021-02-01") 588 req.URL.RawQuery = reqQP.Encode() 589 req.Header.Set("Accept", "application/json") 590 return req, nil 591} 592 593// stopHandleError handles the Stop error response. 594func (client *PacketCapturesClient) stopHandleError(resp *azcore.Response) error { 595 body, err := resp.Payload() 596 if err != nil { 597 return azcore.NewResponseError(err, resp.Response) 598 } 599 errType := ErrorResponse{raw: string(body)} 600 if err := resp.UnmarshalAsJSON(&errType); err != nil { 601 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 602 } 603 return azcore.NewResponseError(&errType, resp.Response) 604} 605