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// PrivateEndpointsClient contains the methods for the PrivateEndpoints group. 23// Don't use this type directly, use NewPrivateEndpointsClient() instead. 24type PrivateEndpointsClient struct { 25 con *armcore.Connection 26 subscriptionID string 27} 28 29// NewPrivateEndpointsClient creates a new instance of PrivateEndpointsClient with the specified values. 30func NewPrivateEndpointsClient(con *armcore.Connection, subscriptionID string) *PrivateEndpointsClient { 31 return &PrivateEndpointsClient{con: con, subscriptionID: subscriptionID} 32} 33 34// BeginCreateOrUpdate - Creates or updates an private endpoint in the specified resource group. 35// If the operation fails it returns the *Error error type. 36func (client *PrivateEndpointsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, privateEndpointName string, parameters PrivateEndpoint, options *PrivateEndpointsBeginCreateOrUpdateOptions) (PrivateEndpointPollerResponse, error) { 37 resp, err := client.createOrUpdate(ctx, resourceGroupName, privateEndpointName, parameters, options) 38 if err != nil { 39 return PrivateEndpointPollerResponse{}, err 40 } 41 result := PrivateEndpointPollerResponse{ 42 RawResponse: resp.Response, 43 } 44 pt, err := armcore.NewLROPoller("PrivateEndpointsClient.CreateOrUpdate", "azure-async-operation", resp, client.con.Pipeline(), client.createOrUpdateHandleError) 45 if err != nil { 46 return PrivateEndpointPollerResponse{}, err 47 } 48 poller := &privateEndpointPoller{ 49 pt: pt, 50 } 51 result.Poller = poller 52 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (PrivateEndpointResponse, error) { 53 return poller.pollUntilDone(ctx, frequency) 54 } 55 return result, nil 56} 57 58// ResumeCreateOrUpdate creates a new PrivateEndpointPoller from the specified resume token. 59// token - The value must come from a previous call to PrivateEndpointPoller.ResumeToken(). 60func (client *PrivateEndpointsClient) ResumeCreateOrUpdate(ctx context.Context, token string) (PrivateEndpointPollerResponse, error) { 61 pt, err := armcore.NewLROPollerFromResumeToken("PrivateEndpointsClient.CreateOrUpdate", token, client.con.Pipeline(), client.createOrUpdateHandleError) 62 if err != nil { 63 return PrivateEndpointPollerResponse{}, err 64 } 65 poller := &privateEndpointPoller{ 66 pt: pt, 67 } 68 resp, err := poller.Poll(ctx) 69 if err != nil { 70 return PrivateEndpointPollerResponse{}, err 71 } 72 result := PrivateEndpointPollerResponse{ 73 RawResponse: resp, 74 } 75 result.Poller = poller 76 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (PrivateEndpointResponse, error) { 77 return poller.pollUntilDone(ctx, frequency) 78 } 79 return result, nil 80} 81 82// CreateOrUpdate - Creates or updates an private endpoint in the specified resource group. 83// If the operation fails it returns the *Error error type. 84func (client *PrivateEndpointsClient) createOrUpdate(ctx context.Context, resourceGroupName string, privateEndpointName string, parameters PrivateEndpoint, options *PrivateEndpointsBeginCreateOrUpdateOptions) (*azcore.Response, error) { 85 req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, privateEndpointName, 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.StatusOK, http.StatusCreated) { 94 return nil, client.createOrUpdateHandleError(resp) 95 } 96 return resp, nil 97} 98 99// createOrUpdateCreateRequest creates the CreateOrUpdate request. 100func (client *PrivateEndpointsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, privateEndpointName string, parameters PrivateEndpoint, options *PrivateEndpointsBeginCreateOrUpdateOptions) (*azcore.Request, error) { 101 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/privateEndpoints/{privateEndpointName}" 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 privateEndpointName == "" { 107 return nil, errors.New("parameter privateEndpointName cannot be empty") 108 } 109 urlPath = strings.ReplaceAll(urlPath, "{privateEndpointName}", url.PathEscape(privateEndpointName)) 110 if client.subscriptionID == "" { 111 return nil, errors.New("parameter client.subscriptionID cannot be empty") 112 } 113 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 114 req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 115 if err != nil { 116 return nil, err 117 } 118 req.Telemetry(telemetryInfo) 119 reqQP := req.URL.Query() 120 reqQP.Set("api-version", "2021-02-01") 121 req.URL.RawQuery = reqQP.Encode() 122 req.Header.Set("Accept", "application/json") 123 return req, req.MarshalAsJSON(parameters) 124} 125 126// createOrUpdateHandleError handles the CreateOrUpdate error response. 127func (client *PrivateEndpointsClient) createOrUpdateHandleError(resp *azcore.Response) error { 128 body, err := resp.Payload() 129 if err != nil { 130 return azcore.NewResponseError(err, resp.Response) 131 } 132 errType := Error{raw: string(body)} 133 if err := resp.UnmarshalAsJSON(&errType); err != nil { 134 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 135 } 136 return azcore.NewResponseError(&errType, resp.Response) 137} 138 139// BeginDelete - Deletes the specified private endpoint. 140// If the operation fails it returns the *Error error type. 141func (client *PrivateEndpointsClient) BeginDelete(ctx context.Context, resourceGroupName string, privateEndpointName string, options *PrivateEndpointsBeginDeleteOptions) (HTTPPollerResponse, error) { 142 resp, err := client.deleteOperation(ctx, resourceGroupName, privateEndpointName, options) 143 if err != nil { 144 return HTTPPollerResponse{}, err 145 } 146 result := HTTPPollerResponse{ 147 RawResponse: resp.Response, 148 } 149 pt, err := armcore.NewLROPoller("PrivateEndpointsClient.Delete", "location", resp, client.con.Pipeline(), client.deleteHandleError) 150 if err != nil { 151 return HTTPPollerResponse{}, err 152 } 153 poller := &httpPoller{ 154 pt: pt, 155 } 156 result.Poller = poller 157 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) { 158 return poller.pollUntilDone(ctx, frequency) 159 } 160 return result, nil 161} 162 163// ResumeDelete creates a new HTTPPoller from the specified resume token. 164// token - The value must come from a previous call to HTTPPoller.ResumeToken(). 165func (client *PrivateEndpointsClient) ResumeDelete(ctx context.Context, token string) (HTTPPollerResponse, error) { 166 pt, err := armcore.NewLROPollerFromResumeToken("PrivateEndpointsClient.Delete", token, client.con.Pipeline(), client.deleteHandleError) 167 if err != nil { 168 return HTTPPollerResponse{}, err 169 } 170 poller := &httpPoller{ 171 pt: pt, 172 } 173 resp, err := poller.Poll(ctx) 174 if err != nil { 175 return HTTPPollerResponse{}, err 176 } 177 result := HTTPPollerResponse{ 178 RawResponse: resp, 179 } 180 result.Poller = poller 181 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) { 182 return poller.pollUntilDone(ctx, frequency) 183 } 184 return result, nil 185} 186 187// Delete - Deletes the specified private endpoint. 188// If the operation fails it returns the *Error error type. 189func (client *PrivateEndpointsClient) deleteOperation(ctx context.Context, resourceGroupName string, privateEndpointName string, options *PrivateEndpointsBeginDeleteOptions) (*azcore.Response, error) { 190 req, err := client.deleteCreateRequest(ctx, resourceGroupName, privateEndpointName, options) 191 if err != nil { 192 return nil, err 193 } 194 resp, err := client.con.Pipeline().Do(req) 195 if err != nil { 196 return nil, err 197 } 198 if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent) { 199 return nil, client.deleteHandleError(resp) 200 } 201 return resp, nil 202} 203 204// deleteCreateRequest creates the Delete request. 205func (client *PrivateEndpointsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, privateEndpointName string, options *PrivateEndpointsBeginDeleteOptions) (*azcore.Request, error) { 206 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/privateEndpoints/{privateEndpointName}" 207 if resourceGroupName == "" { 208 return nil, errors.New("parameter resourceGroupName cannot be empty") 209 } 210 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 211 if privateEndpointName == "" { 212 return nil, errors.New("parameter privateEndpointName cannot be empty") 213 } 214 urlPath = strings.ReplaceAll(urlPath, "{privateEndpointName}", url.PathEscape(privateEndpointName)) 215 if client.subscriptionID == "" { 216 return nil, errors.New("parameter client.subscriptionID cannot be empty") 217 } 218 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 219 req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 220 if err != nil { 221 return nil, err 222 } 223 req.Telemetry(telemetryInfo) 224 reqQP := req.URL.Query() 225 reqQP.Set("api-version", "2021-02-01") 226 req.URL.RawQuery = reqQP.Encode() 227 req.Header.Set("Accept", "application/json") 228 return req, nil 229} 230 231// deleteHandleError handles the Delete error response. 232func (client *PrivateEndpointsClient) deleteHandleError(resp *azcore.Response) error { 233 body, err := resp.Payload() 234 if err != nil { 235 return azcore.NewResponseError(err, resp.Response) 236 } 237 errType := Error{raw: string(body)} 238 if err := resp.UnmarshalAsJSON(&errType); err != nil { 239 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 240 } 241 return azcore.NewResponseError(&errType, resp.Response) 242} 243 244// Get - Gets the specified private endpoint by resource group. 245// If the operation fails it returns the *Error error type. 246func (client *PrivateEndpointsClient) Get(ctx context.Context, resourceGroupName string, privateEndpointName string, options *PrivateEndpointsGetOptions) (PrivateEndpointResponse, error) { 247 req, err := client.getCreateRequest(ctx, resourceGroupName, privateEndpointName, options) 248 if err != nil { 249 return PrivateEndpointResponse{}, err 250 } 251 resp, err := client.con.Pipeline().Do(req) 252 if err != nil { 253 return PrivateEndpointResponse{}, err 254 } 255 if !resp.HasStatusCode(http.StatusOK) { 256 return PrivateEndpointResponse{}, client.getHandleError(resp) 257 } 258 return client.getHandleResponse(resp) 259} 260 261// getCreateRequest creates the Get request. 262func (client *PrivateEndpointsClient) getCreateRequest(ctx context.Context, resourceGroupName string, privateEndpointName string, options *PrivateEndpointsGetOptions) (*azcore.Request, error) { 263 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/privateEndpoints/{privateEndpointName}" 264 if resourceGroupName == "" { 265 return nil, errors.New("parameter resourceGroupName cannot be empty") 266 } 267 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 268 if privateEndpointName == "" { 269 return nil, errors.New("parameter privateEndpointName cannot be empty") 270 } 271 urlPath = strings.ReplaceAll(urlPath, "{privateEndpointName}", url.PathEscape(privateEndpointName)) 272 if client.subscriptionID == "" { 273 return nil, errors.New("parameter client.subscriptionID cannot be empty") 274 } 275 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 276 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 277 if err != nil { 278 return nil, err 279 } 280 req.Telemetry(telemetryInfo) 281 reqQP := req.URL.Query() 282 reqQP.Set("api-version", "2021-02-01") 283 if options != nil && options.Expand != nil { 284 reqQP.Set("$expand", *options.Expand) 285 } 286 req.URL.RawQuery = reqQP.Encode() 287 req.Header.Set("Accept", "application/json") 288 return req, nil 289} 290 291// getHandleResponse handles the Get response. 292func (client *PrivateEndpointsClient) getHandleResponse(resp *azcore.Response) (PrivateEndpointResponse, error) { 293 var val *PrivateEndpoint 294 if err := resp.UnmarshalAsJSON(&val); err != nil { 295 return PrivateEndpointResponse{}, err 296 } 297 return PrivateEndpointResponse{RawResponse: resp.Response, PrivateEndpoint: val}, nil 298} 299 300// getHandleError handles the Get error response. 301func (client *PrivateEndpointsClient) getHandleError(resp *azcore.Response) error { 302 body, err := resp.Payload() 303 if err != nil { 304 return azcore.NewResponseError(err, resp.Response) 305 } 306 errType := Error{raw: string(body)} 307 if err := resp.UnmarshalAsJSON(&errType); err != nil { 308 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 309 } 310 return azcore.NewResponseError(&errType, resp.Response) 311} 312 313// List - Gets all private endpoints in a resource group. 314// If the operation fails it returns the *Error error type. 315func (client *PrivateEndpointsClient) List(resourceGroupName string, options *PrivateEndpointsListOptions) PrivateEndpointListResultPager { 316 return &privateEndpointListResultPager{ 317 pipeline: client.con.Pipeline(), 318 requester: func(ctx context.Context) (*azcore.Request, error) { 319 return client.listCreateRequest(ctx, resourceGroupName, options) 320 }, 321 responder: client.listHandleResponse, 322 errorer: client.listHandleError, 323 advancer: func(ctx context.Context, resp PrivateEndpointListResultResponse) (*azcore.Request, error) { 324 return azcore.NewRequest(ctx, http.MethodGet, *resp.PrivateEndpointListResult.NextLink) 325 }, 326 statusCodes: []int{http.StatusOK}, 327 } 328} 329 330// listCreateRequest creates the List request. 331func (client *PrivateEndpointsClient) listCreateRequest(ctx context.Context, resourceGroupName string, options *PrivateEndpointsListOptions) (*azcore.Request, error) { 332 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/privateEndpoints" 333 if resourceGroupName == "" { 334 return nil, errors.New("parameter resourceGroupName cannot be empty") 335 } 336 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 337 if client.subscriptionID == "" { 338 return nil, errors.New("parameter client.subscriptionID cannot be empty") 339 } 340 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 341 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 342 if err != nil { 343 return nil, err 344 } 345 req.Telemetry(telemetryInfo) 346 reqQP := req.URL.Query() 347 reqQP.Set("api-version", "2021-02-01") 348 req.URL.RawQuery = reqQP.Encode() 349 req.Header.Set("Accept", "application/json") 350 return req, nil 351} 352 353// listHandleResponse handles the List response. 354func (client *PrivateEndpointsClient) listHandleResponse(resp *azcore.Response) (PrivateEndpointListResultResponse, error) { 355 var val *PrivateEndpointListResult 356 if err := resp.UnmarshalAsJSON(&val); err != nil { 357 return PrivateEndpointListResultResponse{}, err 358 } 359 return PrivateEndpointListResultResponse{RawResponse: resp.Response, PrivateEndpointListResult: val}, nil 360} 361 362// listHandleError handles the List error response. 363func (client *PrivateEndpointsClient) listHandleError(resp *azcore.Response) error { 364 body, err := resp.Payload() 365 if err != nil { 366 return azcore.NewResponseError(err, resp.Response) 367 } 368 errType := Error{raw: string(body)} 369 if err := resp.UnmarshalAsJSON(&errType); err != nil { 370 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 371 } 372 return azcore.NewResponseError(&errType, resp.Response) 373} 374 375// ListBySubscription - Gets all private endpoints in a subscription. 376// If the operation fails it returns the *Error error type. 377func (client *PrivateEndpointsClient) ListBySubscription(options *PrivateEndpointsListBySubscriptionOptions) PrivateEndpointListResultPager { 378 return &privateEndpointListResultPager{ 379 pipeline: client.con.Pipeline(), 380 requester: func(ctx context.Context) (*azcore.Request, error) { 381 return client.listBySubscriptionCreateRequest(ctx, options) 382 }, 383 responder: client.listBySubscriptionHandleResponse, 384 errorer: client.listBySubscriptionHandleError, 385 advancer: func(ctx context.Context, resp PrivateEndpointListResultResponse) (*azcore.Request, error) { 386 return azcore.NewRequest(ctx, http.MethodGet, *resp.PrivateEndpointListResult.NextLink) 387 }, 388 statusCodes: []int{http.StatusOK}, 389 } 390} 391 392// listBySubscriptionCreateRequest creates the ListBySubscription request. 393func (client *PrivateEndpointsClient) listBySubscriptionCreateRequest(ctx context.Context, options *PrivateEndpointsListBySubscriptionOptions) (*azcore.Request, error) { 394 urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Network/privateEndpoints" 395 if client.subscriptionID == "" { 396 return nil, errors.New("parameter client.subscriptionID cannot be empty") 397 } 398 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 399 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 400 if err != nil { 401 return nil, err 402 } 403 req.Telemetry(telemetryInfo) 404 reqQP := req.URL.Query() 405 reqQP.Set("api-version", "2021-02-01") 406 req.URL.RawQuery = reqQP.Encode() 407 req.Header.Set("Accept", "application/json") 408 return req, nil 409} 410 411// listBySubscriptionHandleResponse handles the ListBySubscription response. 412func (client *PrivateEndpointsClient) listBySubscriptionHandleResponse(resp *azcore.Response) (PrivateEndpointListResultResponse, error) { 413 var val *PrivateEndpointListResult 414 if err := resp.UnmarshalAsJSON(&val); err != nil { 415 return PrivateEndpointListResultResponse{}, err 416 } 417 return PrivateEndpointListResultResponse{RawResponse: resp.Response, PrivateEndpointListResult: val}, nil 418} 419 420// listBySubscriptionHandleError handles the ListBySubscription error response. 421func (client *PrivateEndpointsClient) listBySubscriptionHandleError(resp *azcore.Response) error { 422 body, err := resp.Payload() 423 if err != nil { 424 return azcore.NewResponseError(err, resp.Response) 425 } 426 errType := Error{raw: string(body)} 427 if err := resp.UnmarshalAsJSON(&errType); err != nil { 428 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 429 } 430 return azcore.NewResponseError(&errType, resp.Response) 431} 432