github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/groups_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 "strconv" 21 "strings" 22 ) 23 24 // GroupsClient contains the methods for the NetworkGroups group. 25 // Don't use this type directly, use NewGroupsClient() instead. 26 type GroupsClient struct { 27 internal *arm.Client 28 subscriptionID string 29 } 30 31 // NewGroupsClient creates a new instance of GroupsClient with the specified values. 32 // - subscriptionID - The subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription 33 // ID forms part of the URI for every service call. 34 // - credential - used to authorize requests. Usually a credential from azidentity. 35 // - options - pass nil to accept the default values. 36 func NewGroupsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*GroupsClient, error) { 37 cl, err := arm.NewClient(moduleName, moduleVersion, credential, options) 38 if err != nil { 39 return nil, err 40 } 41 client := &GroupsClient{ 42 subscriptionID: subscriptionID, 43 internal: cl, 44 } 45 return client, nil 46 } 47 48 // CreateOrUpdate - Creates or updates a network group. 49 // If the operation fails it returns an *azcore.ResponseError type. 50 // 51 // Generated from API version 2024-05-01 52 // - resourceGroupName - The name of the resource group. 53 // - networkManagerName - The name of the network manager. 54 // - networkGroupName - The name of the network group. 55 // - parameters - Parameters supplied to the specify which network group need to create 56 // - options - GroupsClientCreateOrUpdateOptions contains the optional parameters for the GroupsClient.CreateOrUpdate method. 57 func (client *GroupsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, parameters Group, options *GroupsClientCreateOrUpdateOptions) (GroupsClientCreateOrUpdateResponse, error) { 58 var err error 59 const operationName = "GroupsClient.CreateOrUpdate" 60 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 61 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 62 defer func() { endSpan(err) }() 63 req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, networkManagerName, networkGroupName, parameters, options) 64 if err != nil { 65 return GroupsClientCreateOrUpdateResponse{}, err 66 } 67 httpResp, err := client.internal.Pipeline().Do(req) 68 if err != nil { 69 return GroupsClientCreateOrUpdateResponse{}, err 70 } 71 if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) { 72 err = runtime.NewResponseError(httpResp) 73 return GroupsClientCreateOrUpdateResponse{}, err 74 } 75 resp, err := client.createOrUpdateHandleResponse(httpResp) 76 return resp, err 77 } 78 79 // createOrUpdateCreateRequest creates the CreateOrUpdate request. 80 func (client *GroupsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, parameters Group, options *GroupsClientCreateOrUpdateOptions) (*policy.Request, error) { 81 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/networkGroups/{networkGroupName}" 82 if client.subscriptionID == "" { 83 return nil, errors.New("parameter client.subscriptionID cannot be empty") 84 } 85 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 86 if resourceGroupName == "" { 87 return nil, errors.New("parameter resourceGroupName cannot be empty") 88 } 89 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 90 if networkManagerName == "" { 91 return nil, errors.New("parameter networkManagerName cannot be empty") 92 } 93 urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName)) 94 if networkGroupName == "" { 95 return nil, errors.New("parameter networkGroupName cannot be empty") 96 } 97 urlPath = strings.ReplaceAll(urlPath, "{networkGroupName}", url.PathEscape(networkGroupName)) 98 req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 99 if err != nil { 100 return nil, err 101 } 102 reqQP := req.Raw().URL.Query() 103 reqQP.Set("api-version", "2024-05-01") 104 req.Raw().URL.RawQuery = reqQP.Encode() 105 req.Raw().Header["Accept"] = []string{"application/json"} 106 if options != nil && options.IfMatch != nil { 107 req.Raw().Header["If-Match"] = []string{*options.IfMatch} 108 } 109 if err := runtime.MarshalAsJSON(req, parameters); err != nil { 110 return nil, err 111 } 112 return req, nil 113 } 114 115 // createOrUpdateHandleResponse handles the CreateOrUpdate response. 116 func (client *GroupsClient) createOrUpdateHandleResponse(resp *http.Response) (GroupsClientCreateOrUpdateResponse, error) { 117 result := GroupsClientCreateOrUpdateResponse{} 118 if val := resp.Header.Get("ETag"); val != "" { 119 result.ETag = &val 120 } 121 if err := runtime.UnmarshalAsJSON(resp, &result.Group); err != nil { 122 return GroupsClientCreateOrUpdateResponse{}, err 123 } 124 return result, nil 125 } 126 127 // BeginDelete - Deletes a network group. 128 // If the operation fails it returns an *azcore.ResponseError type. 129 // 130 // Generated from API version 2024-05-01 131 // - resourceGroupName - The name of the resource group. 132 // - networkManagerName - The name of the network manager. 133 // - networkGroupName - The name of the network group. 134 // - options - GroupsClientBeginDeleteOptions contains the optional parameters for the GroupsClient.BeginDelete method. 135 func (client *GroupsClient) BeginDelete(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, options *GroupsClientBeginDeleteOptions) (*runtime.Poller[GroupsClientDeleteResponse], error) { 136 if options == nil || options.ResumeToken == "" { 137 resp, err := client.deleteOperation(ctx, resourceGroupName, networkManagerName, networkGroupName, options) 138 if err != nil { 139 return nil, err 140 } 141 poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[GroupsClientDeleteResponse]{ 142 FinalStateVia: runtime.FinalStateViaLocation, 143 Tracer: client.internal.Tracer(), 144 }) 145 return poller, err 146 } else { 147 return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[GroupsClientDeleteResponse]{ 148 Tracer: client.internal.Tracer(), 149 }) 150 } 151 } 152 153 // Delete - Deletes a network group. 154 // If the operation fails it returns an *azcore.ResponseError type. 155 // 156 // Generated from API version 2024-05-01 157 func (client *GroupsClient) deleteOperation(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, options *GroupsClientBeginDeleteOptions) (*http.Response, error) { 158 var err error 159 const operationName = "GroupsClient.BeginDelete" 160 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 161 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 162 defer func() { endSpan(err) }() 163 req, err := client.deleteCreateRequest(ctx, resourceGroupName, networkManagerName, networkGroupName, options) 164 if err != nil { 165 return nil, err 166 } 167 httpResp, err := client.internal.Pipeline().Do(req) 168 if err != nil { 169 return nil, err 170 } 171 if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted, http.StatusNoContent) { 172 err = runtime.NewResponseError(httpResp) 173 return nil, err 174 } 175 return httpResp, nil 176 } 177 178 // deleteCreateRequest creates the Delete request. 179 func (client *GroupsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, options *GroupsClientBeginDeleteOptions) (*policy.Request, error) { 180 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/networkGroups/{networkGroupName}" 181 if client.subscriptionID == "" { 182 return nil, errors.New("parameter client.subscriptionID cannot be empty") 183 } 184 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 185 if resourceGroupName == "" { 186 return nil, errors.New("parameter resourceGroupName cannot be empty") 187 } 188 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 189 if networkManagerName == "" { 190 return nil, errors.New("parameter networkManagerName cannot be empty") 191 } 192 urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName)) 193 if networkGroupName == "" { 194 return nil, errors.New("parameter networkGroupName cannot be empty") 195 } 196 urlPath = strings.ReplaceAll(urlPath, "{networkGroupName}", url.PathEscape(networkGroupName)) 197 req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 198 if err != nil { 199 return nil, err 200 } 201 reqQP := req.Raw().URL.Query() 202 reqQP.Set("api-version", "2024-05-01") 203 if options != nil && options.Force != nil { 204 reqQP.Set("force", strconv.FormatBool(*options.Force)) 205 } 206 req.Raw().URL.RawQuery = reqQP.Encode() 207 req.Raw().Header["Accept"] = []string{"application/json"} 208 return req, nil 209 } 210 211 // Get - Gets the specified network group. 212 // If the operation fails it returns an *azcore.ResponseError type. 213 // 214 // Generated from API version 2024-05-01 215 // - resourceGroupName - The name of the resource group. 216 // - networkManagerName - The name of the network manager. 217 // - networkGroupName - The name of the network group. 218 // - options - GroupsClientGetOptions contains the optional parameters for the GroupsClient.Get method. 219 func (client *GroupsClient) Get(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, options *GroupsClientGetOptions) (GroupsClientGetResponse, error) { 220 var err error 221 const operationName = "GroupsClient.Get" 222 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName) 223 ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil) 224 defer func() { endSpan(err) }() 225 req, err := client.getCreateRequest(ctx, resourceGroupName, networkManagerName, networkGroupName, options) 226 if err != nil { 227 return GroupsClientGetResponse{}, err 228 } 229 httpResp, err := client.internal.Pipeline().Do(req) 230 if err != nil { 231 return GroupsClientGetResponse{}, err 232 } 233 if !runtime.HasStatusCode(httpResp, http.StatusOK) { 234 err = runtime.NewResponseError(httpResp) 235 return GroupsClientGetResponse{}, err 236 } 237 resp, err := client.getHandleResponse(httpResp) 238 return resp, err 239 } 240 241 // getCreateRequest creates the Get request. 242 func (client *GroupsClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, networkGroupName string, options *GroupsClientGetOptions) (*policy.Request, error) { 243 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/networkGroups/{networkGroupName}" 244 if client.subscriptionID == "" { 245 return nil, errors.New("parameter client.subscriptionID cannot be empty") 246 } 247 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 248 if resourceGroupName == "" { 249 return nil, errors.New("parameter resourceGroupName cannot be empty") 250 } 251 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 252 if networkManagerName == "" { 253 return nil, errors.New("parameter networkManagerName cannot be empty") 254 } 255 urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName)) 256 if networkGroupName == "" { 257 return nil, errors.New("parameter networkGroupName cannot be empty") 258 } 259 urlPath = strings.ReplaceAll(urlPath, "{networkGroupName}", url.PathEscape(networkGroupName)) 260 req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 261 if err != nil { 262 return nil, err 263 } 264 reqQP := req.Raw().URL.Query() 265 reqQP.Set("api-version", "2024-05-01") 266 req.Raw().URL.RawQuery = reqQP.Encode() 267 req.Raw().Header["Accept"] = []string{"application/json"} 268 return req, nil 269 } 270 271 // getHandleResponse handles the Get response. 272 func (client *GroupsClient) getHandleResponse(resp *http.Response) (GroupsClientGetResponse, error) { 273 result := GroupsClientGetResponse{} 274 if err := runtime.UnmarshalAsJSON(resp, &result.Group); err != nil { 275 return GroupsClientGetResponse{}, err 276 } 277 return result, nil 278 } 279 280 // NewListPager - Lists the specified network group. 281 // 282 // Generated from API version 2024-05-01 283 // - resourceGroupName - The name of the resource group. 284 // - networkManagerName - The name of the network manager. 285 // - options - GroupsClientListOptions contains the optional parameters for the GroupsClient.NewListPager method. 286 func (client *GroupsClient) NewListPager(resourceGroupName string, networkManagerName string, options *GroupsClientListOptions) *runtime.Pager[GroupsClientListResponse] { 287 return runtime.NewPager(runtime.PagingHandler[GroupsClientListResponse]{ 288 More: func(page GroupsClientListResponse) bool { 289 return page.NextLink != nil && len(*page.NextLink) > 0 290 }, 291 Fetcher: func(ctx context.Context, page *GroupsClientListResponse) (GroupsClientListResponse, error) { 292 ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "GroupsClient.NewListPager") 293 nextLink := "" 294 if page != nil { 295 nextLink = *page.NextLink 296 } 297 resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) { 298 return client.listCreateRequest(ctx, resourceGroupName, networkManagerName, options) 299 }, nil) 300 if err != nil { 301 return GroupsClientListResponse{}, err 302 } 303 return client.listHandleResponse(resp) 304 }, 305 Tracer: client.internal.Tracer(), 306 }) 307 } 308 309 // listCreateRequest creates the List request. 310 func (client *GroupsClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, options *GroupsClientListOptions) (*policy.Request, error) { 311 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/networkGroups" 312 if client.subscriptionID == "" { 313 return nil, errors.New("parameter client.subscriptionID cannot be empty") 314 } 315 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 316 if resourceGroupName == "" { 317 return nil, errors.New("parameter resourceGroupName cannot be empty") 318 } 319 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 320 if networkManagerName == "" { 321 return nil, errors.New("parameter networkManagerName cannot be empty") 322 } 323 urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName)) 324 req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath)) 325 if err != nil { 326 return nil, err 327 } 328 reqQP := req.Raw().URL.Query() 329 if options != nil && options.SkipToken != nil { 330 reqQP.Set("$skipToken", *options.SkipToken) 331 } 332 if options != nil && options.Top != nil { 333 reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10)) 334 } 335 reqQP.Set("api-version", "2024-05-01") 336 req.Raw().URL.RawQuery = reqQP.Encode() 337 req.Raw().Header["Accept"] = []string{"application/json"} 338 return req, nil 339 } 340 341 // listHandleResponse handles the List response. 342 func (client *GroupsClient) listHandleResponse(resp *http.Response) (GroupsClientListResponse, error) { 343 result := GroupsClientListResponse{} 344 if err := runtime.UnmarshalAsJSON(resp, &result.GroupListResult); err != nil { 345 return GroupsClientListResponse{}, err 346 } 347 return result, nil 348 }