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// DdosCustomPoliciesClient contains the methods for the DdosCustomPolicies group. 23// Don't use this type directly, use NewDdosCustomPoliciesClient() instead. 24type DdosCustomPoliciesClient struct { 25 con *armcore.Connection 26 subscriptionID string 27} 28 29// NewDdosCustomPoliciesClient creates a new instance of DdosCustomPoliciesClient with the specified values. 30func NewDdosCustomPoliciesClient(con *armcore.Connection, subscriptionID string) *DdosCustomPoliciesClient { 31 return &DdosCustomPoliciesClient{con: con, subscriptionID: subscriptionID} 32} 33 34// BeginCreateOrUpdate - Creates or updates a DDoS custom policy. 35// If the operation fails it returns the *CloudError error type. 36func (client *DdosCustomPoliciesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters DdosCustomPolicy, options *DdosCustomPoliciesBeginCreateOrUpdateOptions) (DdosCustomPolicyPollerResponse, error) { 37 resp, err := client.createOrUpdate(ctx, resourceGroupName, ddosCustomPolicyName, parameters, options) 38 if err != nil { 39 return DdosCustomPolicyPollerResponse{}, err 40 } 41 result := DdosCustomPolicyPollerResponse{ 42 RawResponse: resp.Response, 43 } 44 pt, err := armcore.NewLROPoller("DdosCustomPoliciesClient.CreateOrUpdate", "azure-async-operation", resp, client.con.Pipeline(), client.createOrUpdateHandleError) 45 if err != nil { 46 return DdosCustomPolicyPollerResponse{}, err 47 } 48 poller := &ddosCustomPolicyPoller{ 49 pt: pt, 50 } 51 result.Poller = poller 52 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (DdosCustomPolicyResponse, error) { 53 return poller.pollUntilDone(ctx, frequency) 54 } 55 return result, nil 56} 57 58// ResumeCreateOrUpdate creates a new DdosCustomPolicyPoller from the specified resume token. 59// token - The value must come from a previous call to DdosCustomPolicyPoller.ResumeToken(). 60func (client *DdosCustomPoliciesClient) ResumeCreateOrUpdate(ctx context.Context, token string) (DdosCustomPolicyPollerResponse, error) { 61 pt, err := armcore.NewLROPollerFromResumeToken("DdosCustomPoliciesClient.CreateOrUpdate", token, client.con.Pipeline(), client.createOrUpdateHandleError) 62 if err != nil { 63 return DdosCustomPolicyPollerResponse{}, err 64 } 65 poller := &ddosCustomPolicyPoller{ 66 pt: pt, 67 } 68 resp, err := poller.Poll(ctx) 69 if err != nil { 70 return DdosCustomPolicyPollerResponse{}, err 71 } 72 result := DdosCustomPolicyPollerResponse{ 73 RawResponse: resp, 74 } 75 result.Poller = poller 76 result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (DdosCustomPolicyResponse, error) { 77 return poller.pollUntilDone(ctx, frequency) 78 } 79 return result, nil 80} 81 82// CreateOrUpdate - Creates or updates a DDoS custom policy. 83// If the operation fails it returns the *CloudError error type. 84func (client *DdosCustomPoliciesClient) createOrUpdate(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters DdosCustomPolicy, options *DdosCustomPoliciesBeginCreateOrUpdateOptions) (*azcore.Response, error) { 85 req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, 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 *DdosCustomPoliciesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters DdosCustomPolicy, options *DdosCustomPoliciesBeginCreateOrUpdateOptions) (*azcore.Request, error) { 101 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}" 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 ddosCustomPolicyName == "" { 107 return nil, errors.New("parameter ddosCustomPolicyName cannot be empty") 108 } 109 urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName)) 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 *DdosCustomPoliciesClient) createOrUpdateHandleError(resp *azcore.Response) error { 128 body, err := resp.Payload() 129 if err != nil { 130 return azcore.NewResponseError(err, resp.Response) 131 } 132 errType := CloudError{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 DDoS custom policy. 140// If the operation fails it returns the *CloudError error type. 141func (client *DdosCustomPoliciesClient) BeginDelete(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesBeginDeleteOptions) (HTTPPollerResponse, error) { 142 resp, err := client.deleteOperation(ctx, resourceGroupName, ddosCustomPolicyName, options) 143 if err != nil { 144 return HTTPPollerResponse{}, err 145 } 146 result := HTTPPollerResponse{ 147 RawResponse: resp.Response, 148 } 149 pt, err := armcore.NewLROPoller("DdosCustomPoliciesClient.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 *DdosCustomPoliciesClient) ResumeDelete(ctx context.Context, token string) (HTTPPollerResponse, error) { 166 pt, err := armcore.NewLROPollerFromResumeToken("DdosCustomPoliciesClient.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 DDoS custom policy. 188// If the operation fails it returns the *CloudError error type. 189func (client *DdosCustomPoliciesClient) deleteOperation(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesBeginDeleteOptions) (*azcore.Response, error) { 190 req, err := client.deleteCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, 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 *DdosCustomPoliciesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesBeginDeleteOptions) (*azcore.Request, error) { 206 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}" 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 ddosCustomPolicyName == "" { 212 return nil, errors.New("parameter ddosCustomPolicyName cannot be empty") 213 } 214 urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName)) 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 *DdosCustomPoliciesClient) deleteHandleError(resp *azcore.Response) error { 233 body, err := resp.Payload() 234 if err != nil { 235 return azcore.NewResponseError(err, resp.Response) 236 } 237 errType := CloudError{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 information about the specified DDoS custom policy. 245// If the operation fails it returns the *CloudError error type. 246func (client *DdosCustomPoliciesClient) Get(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesGetOptions) (DdosCustomPolicyResponse, error) { 247 req, err := client.getCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, options) 248 if err != nil { 249 return DdosCustomPolicyResponse{}, err 250 } 251 resp, err := client.con.Pipeline().Do(req) 252 if err != nil { 253 return DdosCustomPolicyResponse{}, err 254 } 255 if !resp.HasStatusCode(http.StatusOK) { 256 return DdosCustomPolicyResponse{}, client.getHandleError(resp) 257 } 258 return client.getHandleResponse(resp) 259} 260 261// getCreateRequest creates the Get request. 262func (client *DdosCustomPoliciesClient) getCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesGetOptions) (*azcore.Request, error) { 263 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}" 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 ddosCustomPolicyName == "" { 269 return nil, errors.New("parameter ddosCustomPolicyName cannot be empty") 270 } 271 urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName)) 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 req.URL.RawQuery = reqQP.Encode() 284 req.Header.Set("Accept", "application/json") 285 return req, nil 286} 287 288// getHandleResponse handles the Get response. 289func (client *DdosCustomPoliciesClient) getHandleResponse(resp *azcore.Response) (DdosCustomPolicyResponse, error) { 290 var val *DdosCustomPolicy 291 if err := resp.UnmarshalAsJSON(&val); err != nil { 292 return DdosCustomPolicyResponse{}, err 293 } 294 return DdosCustomPolicyResponse{RawResponse: resp.Response, DdosCustomPolicy: val}, nil 295} 296 297// getHandleError handles the Get error response. 298func (client *DdosCustomPoliciesClient) getHandleError(resp *azcore.Response) error { 299 body, err := resp.Payload() 300 if err != nil { 301 return azcore.NewResponseError(err, resp.Response) 302 } 303 errType := CloudError{raw: string(body)} 304 if err := resp.UnmarshalAsJSON(&errType); err != nil { 305 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 306 } 307 return azcore.NewResponseError(&errType, resp.Response) 308} 309 310// UpdateTags - Update a DDoS custom policy tags. 311// If the operation fails it returns the *CloudError error type. 312func (client *DdosCustomPoliciesClient) UpdateTags(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters TagsObject, options *DdosCustomPoliciesUpdateTagsOptions) (DdosCustomPolicyResponse, error) { 313 req, err := client.updateTagsCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, parameters, options) 314 if err != nil { 315 return DdosCustomPolicyResponse{}, err 316 } 317 resp, err := client.con.Pipeline().Do(req) 318 if err != nil { 319 return DdosCustomPolicyResponse{}, err 320 } 321 if !resp.HasStatusCode(http.StatusOK) { 322 return DdosCustomPolicyResponse{}, client.updateTagsHandleError(resp) 323 } 324 return client.updateTagsHandleResponse(resp) 325} 326 327// updateTagsCreateRequest creates the UpdateTags request. 328func (client *DdosCustomPoliciesClient) updateTagsCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters TagsObject, options *DdosCustomPoliciesUpdateTagsOptions) (*azcore.Request, error) { 329 urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}" 330 if resourceGroupName == "" { 331 return nil, errors.New("parameter resourceGroupName cannot be empty") 332 } 333 urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName)) 334 if ddosCustomPolicyName == "" { 335 return nil, errors.New("parameter ddosCustomPolicyName cannot be empty") 336 } 337 urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName)) 338 if client.subscriptionID == "" { 339 return nil, errors.New("parameter client.subscriptionID cannot be empty") 340 } 341 urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID)) 342 req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 343 if err != nil { 344 return nil, err 345 } 346 req.Telemetry(telemetryInfo) 347 reqQP := req.URL.Query() 348 reqQP.Set("api-version", "2021-02-01") 349 req.URL.RawQuery = reqQP.Encode() 350 req.Header.Set("Accept", "application/json") 351 return req, req.MarshalAsJSON(parameters) 352} 353 354// updateTagsHandleResponse handles the UpdateTags response. 355func (client *DdosCustomPoliciesClient) updateTagsHandleResponse(resp *azcore.Response) (DdosCustomPolicyResponse, error) { 356 var val *DdosCustomPolicy 357 if err := resp.UnmarshalAsJSON(&val); err != nil { 358 return DdosCustomPolicyResponse{}, err 359 } 360 return DdosCustomPolicyResponse{RawResponse: resp.Response, DdosCustomPolicy: val}, nil 361} 362 363// updateTagsHandleError handles the UpdateTags error response. 364func (client *DdosCustomPoliciesClient) updateTagsHandleError(resp *azcore.Response) error { 365 body, err := resp.Payload() 366 if err != nil { 367 return azcore.NewResponseError(err, resp.Response) 368 } 369 errType := CloudError{raw: string(body)} 370 if err := resp.UnmarshalAsJSON(&errType); err != nil { 371 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 372 } 373 return azcore.NewResponseError(&errType, resp.Response) 374} 375