github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/flowlogs_client.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // Licensed under the MIT License. See License.txt in the project root for license information. 6 // Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT. 7 // Changes may cause incorrect behavior and will be lost if the code is regenerated. 8 9 package armnetwork 10 11 import ( 12 "context" 13 "errors" 14 "github.com/Azure/azure-sdk-for-go/sdk/azcore" 15 "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" 16 "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" 17 "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" 18 "net/http" 19 "net/url" 20 "strings" 21 ) 22 23 // FlowLogsClient contains the methods for the FlowLogs group. 24 // Don't use this type directly, use NewFlowLogsClient() instead. 25 type FlowLogsClient struct { 26 internal *arm.Client 27 subscriptionID string 28 } 29 30 // NewFlowLogsClient creates a new instance of FlowLogsClient with the specified values. 31 // - subscriptionID - The subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription 32 // ID forms part of the URI for every service call. 33 // - credential - used to authorize requests. Usually a credential from azidentity. 34 // - options - pass nil to accept the default values. 35 func NewFlowLogsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*FlowLogsClient, error) { 36 cl, err := arm.NewClient(moduleName, moduleVersion, credential, options) 37 if err != nil { 38 return nil, err 39 } 40 client := &FlowLogsClient{ 41 subscriptionID: subscriptionID, 42 internal: cl, 43 } 44 return client, nil 45 } 46 47 // BeginCreateOrUpdate - Create or update a flow log for the specified network security group. 48 // If the operation fails it returns an *azcore.ResponseError type. 49 // 50 // Generated from API version 2024-05-01 51 // - resourceGroupName - The name of the resource group. 52 // - networkWatcherName - The name of the network watcher. 53 // - flowLogName - The name of the flow log. 54 // - parameters - Parameters that define the create or update flow log resource. 55 // - options - FlowLogsClientBeginCreateOrUpdateOptions contains the optional parameters for the FlowLogsClient.BeginCreateOrUpdate 56 // method. 57 func (client *FlowLogsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, parameters FlowLog, options *FlowLogsClientBeginCreateOrUpdateOptions) (*runtime.Poller[FlowLogsClientCreateOrUpdateResponse], error) { 58 if options == nil || options.ResumeToken == "" { 59 resp, err := client.createOrUpdate(ctx, resourceGroupName, networkWatcherName, flowLogName, parameters, options) 60 if err != nil { 61 return nil, err 62 } 63 poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[FlowLogsClientCreateOrUpdateResponse]{ 64 FinalStateVia: runtime.FinalStateViaAzureAsyncOp, 65 Tracer: client.internal.Tracer(), 66 }) 67 return poller, err 68 } else { 69 return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[FlowLogsClientCreateOrUpdateResponse]{ 70 Tracer: client.internal.Tracer(), 71 }) 72 } 73 } 74 75 // CreateOrUpdate - Create or update a flow log for the specified network security group. 76 // If the operation fails it returns an *azcore.ResponseError type. 77 // 78 // Generated from API version 2024-05-01 79 func (client *FlowLogsClient) createOrUpdate(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, parameters FlowLog, options *FlowLogsClientBeginCreateOrUpdateOptions) (*http.Response, error) { 80 var err error 81 const operationName = "FlowLogsClient.BeginCreateOrUpdate" 82 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 83 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 84 defer func() { endSpan(err) }() 85 req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, networkWatcherName, flowLogName, parameters, options) 86 if err != nil { 87 return nil, err 88 } 89 httpResp, err := client.internal.Pipeline().Do(req) 90 if err != nil { 91 return nil, err 92 } 93 if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) { 94 err = runtime.NewResponseError(httpResp) 95 return nil, err 96 } 97 return httpResp, nil 98 } 99 100 // createOrUpdateCreateRequest creates the CreateOrUpdate request. 101 func (client *FlowLogsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, parameters FlowLog, options *FlowLogsClientBeginCreateOrUpdateOptions) (*policy.Request, error) { 102 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}" 103 if resourceGroupName == "" { 104 return nil, errors.New("parameter resourceGroupName cannot be empty") 105 } 106 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 107 if networkWatcherName == "" { 108 return nil, errors.New("parameter networkWatcherName cannot be empty") 109 } 110 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 111 if flowLogName == "" { 112 return nil, errors.New("parameter flowLogName cannot be empty") 113 } 114 urlPath = strings.ReplaceAll(urlPath, "{flowLogName}", url.PathEscape(flowLogName)) 115 if client.subscriptionID == "" { 116 return nil, errors.New("parameter client.subscriptionID cannot be empty") 117 } 118 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 119 req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 120 if err != nil { 121 return nil, err 122 } 123 reqQP := req.Raw().URL.Query() 124 reqQP.Set("api-version", "2024-05-01") 125 req.Raw().URL.RawQuery = reqQP.Encode() 126 req.Raw().Header["Accept"] = []string{"application/json"} 127 if err := runtime.MarshalAsJSON(req, parameters); err != nil { 128 return nil, err 129 } 130 return req, nil 131 } 132 133 // BeginDelete - Deletes the specified flow log resource. 134 // If the operation fails it returns an *azcore.ResponseError type. 135 // 136 // Generated from API version 2024-05-01 137 // - resourceGroupName - The name of the resource group. 138 // - networkWatcherName - The name of the network watcher. 139 // - flowLogName - The name of the flow log resource. 140 // - options - FlowLogsClientBeginDeleteOptions contains the optional parameters for the FlowLogsClient.BeginDelete method. 141 func (client *FlowLogsClient) BeginDelete(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, options *FlowLogsClientBeginDeleteOptions) (*runtime.Poller[FlowLogsClientDeleteResponse], error) { 142 if options == nil || options.ResumeToken == "" { 143 resp, err := client.deleteOperation(ctx, resourceGroupName, networkWatcherName, flowLogName, options) 144 if err != nil { 145 return nil, err 146 } 147 poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[FlowLogsClientDeleteResponse]{ 148 FinalStateVia: runtime.FinalStateViaLocation, 149 Tracer: client.internal.Tracer(), 150 }) 151 return poller, err 152 } else { 153 return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[FlowLogsClientDeleteResponse]{ 154 Tracer: client.internal.Tracer(), 155 }) 156 } 157 } 158 159 // Delete - Deletes the specified flow log resource. 160 // If the operation fails it returns an *azcore.ResponseError type. 161 // 162 // Generated from API version 2024-05-01 163 func (client *FlowLogsClient) deleteOperation(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, options *FlowLogsClientBeginDeleteOptions) (*http.Response, error) { 164 var err error 165 const operationName = "FlowLogsClient.BeginDelete" 166 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 167 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 168 defer func() { endSpan(err) }() 169 req, err := client.deleteCreateRequest(ctx, resourceGroupName, networkWatcherName, flowLogName, options) 170 if err != nil { 171 return nil, err 172 } 173 httpResp, err := client.internal.Pipeline().Do(req) 174 if err != nil { 175 return nil, err 176 } 177 if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) { 178 err = runtime.NewResponseError(httpResp) 179 return nil, err 180 } 181 return httpResp, nil 182 } 183 184 // deleteCreateRequest creates the Delete request. 185 func (client *FlowLogsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, options *FlowLogsClientBeginDeleteOptions) (*policy.Request, error) { 186 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}" 187 if resourceGroupName == "" { 188 return nil, errors.New("parameter resourceGroupName cannot be empty") 189 } 190 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 191 if networkWatcherName == "" { 192 return nil, errors.New("parameter networkWatcherName cannot be empty") 193 } 194 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 195 if flowLogName == "" { 196 return nil, errors.New("parameter flowLogName cannot be empty") 197 } 198 urlPath = strings.ReplaceAll(urlPath, "{flowLogName}", url.PathEscape(flowLogName)) 199 if client.subscriptionID == "" { 200 return nil, errors.New("parameter client.subscriptionID cannot be empty") 201 } 202 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 203 req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 204 if err != nil { 205 return nil, err 206 } 207 reqQP := req.Raw().URL.Query() 208 reqQP.Set("api-version", "2024-05-01") 209 req.Raw().URL.RawQuery = reqQP.Encode() 210 req.Raw().Header["Accept"] = []string{"application/json"} 211 return req, nil 212 } 213 214 // Get - Gets a flow log resource by name. 215 // If the operation fails it returns an *azcore.ResponseError type. 216 // 217 // Generated from API version 2024-05-01 218 // - resourceGroupName - The name of the resource group. 219 // - networkWatcherName - The name of the network watcher. 220 // - flowLogName - The name of the flow log resource. 221 // - options - FlowLogsClientGetOptions contains the optional parameters for the FlowLogsClient.Get method. 222 func (client *FlowLogsClient) Get(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, options *FlowLogsClientGetOptions) (FlowLogsClientGetResponse, error) { 223 var err error 224 const operationName = "FlowLogsClient.Get" 225 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 226 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 227 defer func() { endSpan(err) }() 228 req, err := client.getCreateRequest(ctx, resourceGroupName, networkWatcherName, flowLogName, options) 229 if err != nil { 230 return FlowLogsClientGetResponse{}, err 231 } 232 httpResp, err := client.internal.Pipeline().Do(req) 233 if err != nil { 234 return FlowLogsClientGetResponse{}, err 235 } 236 if !runtime.HasStatusCode(httpResp, http.StatusOK) { 237 err = runtime.NewResponseError(httpResp) 238 return FlowLogsClientGetResponse{}, err 239 } 240 resp, err := client.getHandleResponse(httpResp) 241 return resp, err 242 } 243 244 // getCreateRequest creates the Get request. 245 func (client *FlowLogsClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, options *FlowLogsClientGetOptions) (*policy.Request, error) { 246 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}" 247 if resourceGroupName == "" { 248 return nil, errors.New("parameter resourceGroupName cannot be empty") 249 } 250 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 251 if networkWatcherName == "" { 252 return nil, errors.New("parameter networkWatcherName cannot be empty") 253 } 254 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 255 if flowLogName == "" { 256 return nil, errors.New("parameter flowLogName cannot be empty") 257 } 258 urlPath = strings.ReplaceAll(urlPath, "{flowLogName}", url.PathEscape(flowLogName)) 259 if client.subscriptionID == "" { 260 return nil, errors.New("parameter client.subscriptionID cannot be empty") 261 } 262 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 263 req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 264 if err != nil { 265 return nil, err 266 } 267 reqQP := req.Raw().URL.Query() 268 reqQP.Set("api-version", "2024-05-01") 269 req.Raw().URL.RawQuery = reqQP.Encode() 270 req.Raw().Header["Accept"] = []string{"application/json"} 271 return req, nil 272 } 273 274 // getHandleResponse handles the Get response. 275 func (client *FlowLogsClient) getHandleResponse(resp *http.Response) (FlowLogsClientGetResponse, error) { 276 result := FlowLogsClientGetResponse{} 277 if err := runtime.UnmarshalAsJSON(resp, &result.FlowLog); err != nil { 278 return FlowLogsClientGetResponse{}, err 279 } 280 return result, nil 281 } 282 283 // NewListPager - Lists all flow log resources for the specified Network Watcher. 284 // 285 // Generated from API version 2024-05-01 286 // - resourceGroupName - The name of the resource group containing Network Watcher. 287 // - networkWatcherName - The name of the Network Watcher resource. 288 // - options - FlowLogsClientListOptions contains the optional parameters for the FlowLogsClient.NewListPager method. 289 func (client *FlowLogsClient) NewListPager(resourceGroupName string, networkWatcherName string, options *FlowLogsClientListOptions) *runtime.Pager[FlowLogsClientListResponse] { 290 return runtime.NewPager(runtime.PagingHandler[FlowLogsClientListResponse]{ 291 More: func(page FlowLogsClientListResponse) bool { 292 return page.NextLink != nil && len(*page.NextLink) > 0 293 }, 294 Fetcher: func(ctx context.Context, page *FlowLogsClientListResponse) (FlowLogsClientListResponse, error) { 295 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "FlowLogsClient.NewListPager") 296 nextLink := "" 297 if page != nil { 298 nextLink = *page.NextLink 299 } 300 resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) { 301 return client.listCreateRequest(ctx, resourceGroupName, networkWatcherName, options) 302 }, nil) 303 if err != nil { 304 return FlowLogsClientListResponse{}, err 305 } 306 return client.listHandleResponse(resp) 307 }, 308 Tracer: client.internal.Tracer(), 309 }) 310 } 311 312 // listCreateRequest creates the List request. 313 func (client *FlowLogsClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, options *FlowLogsClientListOptions) (*policy.Request, error) { 314 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs" 315 if resourceGroupName == "" { 316 return nil, errors.New("parameter resourceGroupName cannot be empty") 317 } 318 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 319 if networkWatcherName == "" { 320 return nil, errors.New("parameter networkWatcherName cannot be empty") 321 } 322 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 323 if client.subscriptionID == "" { 324 return nil, errors.New("parameter client.subscriptionID cannot be empty") 325 } 326 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 327 req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 328 if err != nil { 329 return nil, err 330 } 331 reqQP := req.Raw().URL.Query() 332 reqQP.Set("api-version", "2024-05-01") 333 req.Raw().URL.RawQuery = reqQP.Encode() 334 req.Raw().Header["Accept"] = []string{"application/json"} 335 return req, nil 336 } 337 338 // listHandleResponse handles the List response. 339 func (client *FlowLogsClient) listHandleResponse(resp *http.Response) (FlowLogsClientListResponse, error) { 340 result := FlowLogsClientListResponse{} 341 if err := runtime.UnmarshalAsJSON(resp, &result.FlowLogListResult); err != nil { 342 return FlowLogsClientListResponse{}, err 343 } 344 return result, nil 345 } 346 347 // UpdateTags - Update tags of the specified flow log. 348 // If the operation fails it returns an *azcore.ResponseError type. 349 // 350 // Generated from API version 2024-05-01 351 // - resourceGroupName - The name of the resource group. 352 // - networkWatcherName - The name of the network watcher. 353 // - flowLogName - The name of the flow log. 354 // - parameters - Parameters supplied to update flow log tags. 355 // - options - FlowLogsClientUpdateTagsOptions contains the optional parameters for the FlowLogsClient.UpdateTags method. 356 func (client *FlowLogsClient) UpdateTags(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, parameters TagsObject, options *FlowLogsClientUpdateTagsOptions) (FlowLogsClientUpdateTagsResponse, error) { 357 var err error 358 const operationName = "FlowLogsClient.UpdateTags" 359 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 360 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 361 defer func() { endSpan(err) }() 362 req, err := client.updateTagsCreateRequest(ctx, resourceGroupName, networkWatcherName, flowLogName, parameters, options) 363 if err != nil { 364 return FlowLogsClientUpdateTagsResponse{}, err 365 } 366 httpResp, err := client.internal.Pipeline().Do(req) 367 if err != nil { 368 return FlowLogsClientUpdateTagsResponse{}, err 369 } 370 if !runtime.HasStatusCode(httpResp, http.StatusOK) { 371 err = runtime.NewResponseError(httpResp) 372 return FlowLogsClientUpdateTagsResponse{}, err 373 } 374 resp, err := client.updateTagsHandleResponse(httpResp) 375 return resp, err 376 } 377 378 // updateTagsCreateRequest creates the UpdateTags request. 379 func (client *FlowLogsClient) updateTagsCreateRequest(ctx context.Context, resourceGroupName string, networkWatcherName string, flowLogName string, parameters TagsObject, options *FlowLogsClientUpdateTagsOptions) (*policy.Request, error) { 380 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkWatchers/{networkWatcherName}/flowLogs/{flowLogName}" 381 if resourceGroupName == "" { 382 return nil, errors.New("parameter resourceGroupName cannot be empty") 383 } 384 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 385 if networkWatcherName == "" { 386 return nil, errors.New("parameter networkWatcherName cannot be empty") 387 } 388 urlPath = strings.ReplaceAll(urlPath, "{networkWatcherName}", url.PathEscape(networkWatcherName)) 389 if flowLogName == "" { 390 return nil, errors.New("parameter flowLogName cannot be empty") 391 } 392 urlPath = strings.ReplaceAll(urlPath, "{flowLogName}", url.PathEscape(flowLogName)) 393 if client.subscriptionID == "" { 394 return nil, errors.New("parameter client.subscriptionID cannot be empty") 395 } 396 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 397 req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 398 if err != nil { 399 return nil, err 400 } 401 reqQP := req.Raw().URL.Query() 402 reqQP.Set("api-version", "2024-05-01") 403 req.Raw().URL.RawQuery = reqQP.Encode() 404 req.Raw().Header["Accept"] = []string{"application/json"} 405 if err := runtime.MarshalAsJSON(req, parameters); err != nil { 406 return nil, err 407 } 408 return req, nil 409 } 410 411 // updateTagsHandleResponse handles the UpdateTags response. 412 func (client *FlowLogsClient) updateTagsHandleResponse(resp *http.Response) (FlowLogsClientUpdateTagsResponse, error) { 413 result := FlowLogsClientUpdateTagsResponse{} 414 if err := runtime.UnmarshalAsJSON(resp, &result.FlowLog); err != nil { 415 return FlowLogsClientUpdateTagsResponse{}, err 416 } 417 return result, nil 418 }