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 armmonitor 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) 20 21// DiagnosticSettingsClient contains the methods for the DiagnosticSettings group. 22// Don't use this type directly, use NewDiagnosticSettingsClient() instead. 23type DiagnosticSettingsClient struct { 24 con *armcore.Connection 25} 26 27// NewDiagnosticSettingsClient creates a new instance of DiagnosticSettingsClient with the specified values. 28func NewDiagnosticSettingsClient(con *armcore.Connection) *DiagnosticSettingsClient { 29 return &DiagnosticSettingsClient{con: con} 30} 31 32// CreateOrUpdate - Creates or updates diagnostic settings for the specified resource. 33// If the operation fails it returns the *ErrorResponse error type. 34func (client *DiagnosticSettingsClient) CreateOrUpdate(ctx context.Context, resourceURI string, name string, parameters DiagnosticSettingsResource, options *DiagnosticSettingsCreateOrUpdateOptions) (DiagnosticSettingsResourceResponse, error) { 35 req, err := client.createOrUpdateCreateRequest(ctx, resourceURI, name, parameters, options) 36 if err != nil { 37 return DiagnosticSettingsResourceResponse{}, err 38 } 39 resp, err := client.con.Pipeline().Do(req) 40 if err != nil { 41 return DiagnosticSettingsResourceResponse{}, err 42 } 43 if !resp.HasStatusCode(http.StatusOK) { 44 return DiagnosticSettingsResourceResponse{}, client.createOrUpdateHandleError(resp) 45 } 46 return client.createOrUpdateHandleResponse(resp) 47} 48 49// createOrUpdateCreateRequest creates the CreateOrUpdate request. 50func (client *DiagnosticSettingsClient) createOrUpdateCreateRequest(ctx context.Context, resourceURI string, name string, parameters DiagnosticSettingsResource, options *DiagnosticSettingsCreateOrUpdateOptions) (*azcore.Request, error) { 51 urlPath := "/{resourceUri}/providers/Microsoft.Insights/diagnosticSettings/{name}" 52 if resourceURI == "" { 53 return nil, errors.New("parameter resourceURI cannot be empty") 54 } 55 urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI) 56 if name == "" { 57 return nil, errors.New("parameter name cannot be empty") 58 } 59 urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name)) 60 req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 61 if err != nil { 62 return nil, err 63 } 64 req.Telemetry(telemetryInfo) 65 reqQP := req.URL.Query() 66 reqQP.Set("api-version", "2017-05-01-preview") 67 req.URL.RawQuery = reqQP.Encode() 68 req.Header.Set("Accept", "application/json") 69 return req, req.MarshalAsJSON(parameters) 70} 71 72// createOrUpdateHandleResponse handles the CreateOrUpdate response. 73func (client *DiagnosticSettingsClient) createOrUpdateHandleResponse(resp *azcore.Response) (DiagnosticSettingsResourceResponse, error) { 74 var val *DiagnosticSettingsResource 75 if err := resp.UnmarshalAsJSON(&val); err != nil { 76 return DiagnosticSettingsResourceResponse{}, err 77 } 78 return DiagnosticSettingsResourceResponse{RawResponse: resp.Response, DiagnosticSettingsResource: val}, nil 79} 80 81// createOrUpdateHandleError handles the CreateOrUpdate error response. 82func (client *DiagnosticSettingsClient) createOrUpdateHandleError(resp *azcore.Response) error { 83 body, err := resp.Payload() 84 if err != nil { 85 return azcore.NewResponseError(err, resp.Response) 86 } 87 errType := ErrorResponse{raw: string(body)} 88 if err := resp.UnmarshalAsJSON(&errType); err != nil { 89 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 90 } 91 return azcore.NewResponseError(&errType, resp.Response) 92} 93 94// Delete - Deletes existing diagnostic settings for the specified resource. 95// If the operation fails it returns the *ErrorResponse error type. 96func (client *DiagnosticSettingsClient) Delete(ctx context.Context, resourceURI string, name string, options *DiagnosticSettingsDeleteOptions) (*http.Response, error) { 97 req, err := client.deleteCreateRequest(ctx, resourceURI, name, options) 98 if err != nil { 99 return nil, err 100 } 101 resp, err := client.con.Pipeline().Do(req) 102 if err != nil { 103 return nil, err 104 } 105 if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) { 106 return nil, client.deleteHandleError(resp) 107 } 108 return resp.Response, nil 109} 110 111// deleteCreateRequest creates the Delete request. 112func (client *DiagnosticSettingsClient) deleteCreateRequest(ctx context.Context, resourceURI string, name string, options *DiagnosticSettingsDeleteOptions) (*azcore.Request, error) { 113 urlPath := "/{resourceUri}/providers/Microsoft.Insights/diagnosticSettings/{name}" 114 if resourceURI == "" { 115 return nil, errors.New("parameter resourceURI cannot be empty") 116 } 117 urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI) 118 if name == "" { 119 return nil, errors.New("parameter name cannot be empty") 120 } 121 urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name)) 122 req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 123 if err != nil { 124 return nil, err 125 } 126 req.Telemetry(telemetryInfo) 127 reqQP := req.URL.Query() 128 reqQP.Set("api-version", "2017-05-01-preview") 129 req.URL.RawQuery = reqQP.Encode() 130 req.Header.Set("Accept", "application/json") 131 return req, nil 132} 133 134// deleteHandleError handles the Delete error response. 135func (client *DiagnosticSettingsClient) deleteHandleError(resp *azcore.Response) error { 136 body, err := resp.Payload() 137 if err != nil { 138 return azcore.NewResponseError(err, resp.Response) 139 } 140 errType := ErrorResponse{raw: string(body)} 141 if err := resp.UnmarshalAsJSON(&errType); err != nil { 142 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 143 } 144 return azcore.NewResponseError(&errType, resp.Response) 145} 146 147// Get - Gets the active diagnostic settings for the specified resource. 148// If the operation fails it returns the *ErrorResponse error type. 149func (client *DiagnosticSettingsClient) Get(ctx context.Context, resourceURI string, name string, options *DiagnosticSettingsGetOptions) (DiagnosticSettingsResourceResponse, error) { 150 req, err := client.getCreateRequest(ctx, resourceURI, name, options) 151 if err != nil { 152 return DiagnosticSettingsResourceResponse{}, err 153 } 154 resp, err := client.con.Pipeline().Do(req) 155 if err != nil { 156 return DiagnosticSettingsResourceResponse{}, err 157 } 158 if !resp.HasStatusCode(http.StatusOK) { 159 return DiagnosticSettingsResourceResponse{}, client.getHandleError(resp) 160 } 161 return client.getHandleResponse(resp) 162} 163 164// getCreateRequest creates the Get request. 165func (client *DiagnosticSettingsClient) getCreateRequest(ctx context.Context, resourceURI string, name string, options *DiagnosticSettingsGetOptions) (*azcore.Request, error) { 166 urlPath := "/{resourceUri}/providers/Microsoft.Insights/diagnosticSettings/{name}" 167 if resourceURI == "" { 168 return nil, errors.New("parameter resourceURI cannot be empty") 169 } 170 urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI) 171 if name == "" { 172 return nil, errors.New("parameter name cannot be empty") 173 } 174 urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name)) 175 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 176 if err != nil { 177 return nil, err 178 } 179 req.Telemetry(telemetryInfo) 180 reqQP := req.URL.Query() 181 reqQP.Set("api-version", "2017-05-01-preview") 182 req.URL.RawQuery = reqQP.Encode() 183 req.Header.Set("Accept", "application/json") 184 return req, nil 185} 186 187// getHandleResponse handles the Get response. 188func (client *DiagnosticSettingsClient) getHandleResponse(resp *azcore.Response) (DiagnosticSettingsResourceResponse, error) { 189 var val *DiagnosticSettingsResource 190 if err := resp.UnmarshalAsJSON(&val); err != nil { 191 return DiagnosticSettingsResourceResponse{}, err 192 } 193 return DiagnosticSettingsResourceResponse{RawResponse: resp.Response, DiagnosticSettingsResource: val}, nil 194} 195 196// getHandleError handles the Get error response. 197func (client *DiagnosticSettingsClient) getHandleError(resp *azcore.Response) error { 198 body, err := resp.Payload() 199 if err != nil { 200 return azcore.NewResponseError(err, resp.Response) 201 } 202 errType := ErrorResponse{raw: string(body)} 203 if err := resp.UnmarshalAsJSON(&errType); err != nil { 204 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 205 } 206 return azcore.NewResponseError(&errType, resp.Response) 207} 208 209// List - Gets the active diagnostic settings list for the specified resource. 210// If the operation fails it returns the *ErrorResponse error type. 211func (client *DiagnosticSettingsClient) List(ctx context.Context, resourceURI string, options *DiagnosticSettingsListOptions) (DiagnosticSettingsResourceCollectionResponse, error) { 212 req, err := client.listCreateRequest(ctx, resourceURI, options) 213 if err != nil { 214 return DiagnosticSettingsResourceCollectionResponse{}, err 215 } 216 resp, err := client.con.Pipeline().Do(req) 217 if err != nil { 218 return DiagnosticSettingsResourceCollectionResponse{}, err 219 } 220 if !resp.HasStatusCode(http.StatusOK) { 221 return DiagnosticSettingsResourceCollectionResponse{}, client.listHandleError(resp) 222 } 223 return client.listHandleResponse(resp) 224} 225 226// listCreateRequest creates the List request. 227func (client *DiagnosticSettingsClient) listCreateRequest(ctx context.Context, resourceURI string, options *DiagnosticSettingsListOptions) (*azcore.Request, error) { 228 urlPath := "/{resourceUri}/providers/Microsoft.Insights/diagnosticSettings" 229 if resourceURI == "" { 230 return nil, errors.New("parameter resourceURI cannot be empty") 231 } 232 urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI) 233 req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath)) 234 if err != nil { 235 return nil, err 236 } 237 req.Telemetry(telemetryInfo) 238 reqQP := req.URL.Query() 239 reqQP.Set("api-version", "2017-05-01-preview") 240 req.URL.RawQuery = reqQP.Encode() 241 req.Header.Set("Accept", "application/json") 242 return req, nil 243} 244 245// listHandleResponse handles the List response. 246func (client *DiagnosticSettingsClient) listHandleResponse(resp *azcore.Response) (DiagnosticSettingsResourceCollectionResponse, error) { 247 var val *DiagnosticSettingsResourceCollection 248 if err := resp.UnmarshalAsJSON(&val); err != nil { 249 return DiagnosticSettingsResourceCollectionResponse{}, err 250 } 251 return DiagnosticSettingsResourceCollectionResponse{RawResponse: resp.Response, DiagnosticSettingsResourceCollection: val}, nil 252} 253 254// listHandleError handles the List error response. 255func (client *DiagnosticSettingsClient) listHandleError(resp *azcore.Response) error { 256 body, err := resp.Payload() 257 if err != nil { 258 return azcore.NewResponseError(err, resp.Response) 259 } 260 errType := ErrorResponse{raw: string(body)} 261 if err := resp.UnmarshalAsJSON(&errType); err != nil { 262 return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response) 263 } 264 return azcore.NewResponseError(&errType, resp.Response) 265} 266