/// <summary> /// Gets the database security alert policy for the given database in the given database server in the given resource group /// </summary> public Management.Sql.LegacySdk.Models.DatabaseSecurityAlertPolicy GetDatabaseSecurityAlertPolicy(string resourceGroupName, string serverName, string databaseName) { ISecurityAlertPolicyOperations operations = GetLegacySqlClient().SecurityAlertPolicy; DatabaseSecurityAlertPolicyGetResponse response = operations.GetDatabaseSecurityAlertPolicy(resourceGroupName, serverName, databaseName); return(response.SecurityAlertPolicy); }
/// <summary> /// Gets the database security alert policy for the given database in the given database server in the given resource group /// </summary> public DatabaseSecurityAlertPolicy GetDatabaseSecurityAlertPolicy(string resourceGroupName, string serverName, string databaseName, string clientRequestId) { ISecurityAlertPolicyOperations operations = GetCurrentSqlClient(clientRequestId).SecurityAlertPolicy; DatabaseSecurityAlertPolicyGetResponse response = operations.GetDatabaseSecurityAlertPolicy(resourceGroupName, serverName, databaseName); return(response.SecurityAlertPolicy); }
/// <summary> /// The non-boilerplated test code of the APIs for managing the lifecycle of a given database's security alert policy. It is meant to be called with a name of an already existing database (and therefore already existing /// server and resource group). This test does not create these resources and does not remove them. /// </summary> private void TestDatabaseSecurityAlertAPIs(SqlManagementClient sqlClient, string resourceGroupName, Server server, Database database) { DatabaseSecurityAlertPolicyGetResponse getDefaultDatabaseSecurityAlertPolicyResponse = sqlClient.SecurityAlertPolicy.GetDatabaseSecurityAlertPolicy(resourceGroupName, server.Name, database.Name); DatabaseSecurityAlertPolicyProperties properties = getDefaultDatabaseSecurityAlertPolicyResponse.SecurityAlertPolicy.Properties; // Verify that the initial Get request contains the default policy. TestUtilities.ValidateOperationResponse(getDefaultDatabaseSecurityAlertPolicyResponse, HttpStatusCode.OK); VerifyDatabaseSecurityAlertPolicyInformation(GetDefaultDatabaseSecurityAlertProperties(), properties); // Modify the policy properties, send and receive, see it its still ok ChangeDataBaseSecurityAlertPolicy(properties); DatabaseSecurityAlertPolicyCreateOrUpdateParameters updateParams = new DatabaseSecurityAlertPolicyCreateOrUpdateParameters { Properties = properties }; var updateResponse = sqlClient.SecurityAlertPolicy.CreateOrUpdateDatabaseSecurityAlertPolicy(resourceGroupName, server.Name, database.Name, updateParams); // Verify that the initial Get request contains the default policy. TestUtilities.ValidateOperationResponse(updateResponse, HttpStatusCode.OK); DatabaseSecurityAlertPolicyGetResponse getUpdatedPolicyResponse = sqlClient.SecurityAlertPolicy.GetDatabaseSecurityAlertPolicy(resourceGroupName, server.Name, database.Name); DatabaseSecurityAlertPolicyProperties updatedProperties = getUpdatedPolicyResponse.SecurityAlertPolicy.Properties; // Verify that the Get request contains the updated policy. TestUtilities.ValidateOperationResponse(getUpdatedPolicyResponse, HttpStatusCode.OK); VerifyDatabaseSecurityAlertPolicyInformation(properties, updatedProperties); }
/// <summary> /// Returns an Azure SQL Database security alert policy. /// </summary> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the server /// belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server on which the /// database is hosted. /// </param> /// <param name='databaseName'> /// Required. The name of the Azure SQL Database for which the security /// alert policy applies. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Represents the response to a get database security alert policy /// request. /// </returns> public async Task <DatabaseSecurityAlertPolicyGetResponse> GetDatabaseSecurityAlertPolicyAsync(string resourceGroupName, string serverName, string databaseName, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (serverName == null) { throw new ArgumentNullException("serverName"); } if (databaseName == null) { throw new ArgumentNullException("databaseName"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("resourceGroupName", resourceGroupName); tracingParameters.Add("serverName", serverName); tracingParameters.Add("databaseName", databaseName); TracingAdapter.Enter(invocationId, this, "GetDatabaseSecurityAlertPolicyAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/resourceGroups/"; url = url + Uri.EscapeDataString(resourceGroupName); url = url + "/providers/"; url = url + "Microsoft.Sql"; url = url + "/servers/"; url = url + Uri.EscapeDataString(serverName); url = url + "/databases/"; url = url + Uri.EscapeDataString(databaseName); url = url + "/securityAlertPolicies/Default"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2014-04-01"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result DatabaseSecurityAlertPolicyGetResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new DatabaseSecurityAlertPolicyGetResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { DatabaseSecurityAlertPolicy securityAlertPolicyInstance = new DatabaseSecurityAlertPolicy(); result.SecurityAlertPolicy = securityAlertPolicyInstance; JToken propertiesValue = responseDoc["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { DatabaseSecurityAlertPolicyProperties propertiesInstance = new DatabaseSecurityAlertPolicyProperties(); securityAlertPolicyInstance.Properties = propertiesInstance; JToken stateValue = propertiesValue["state"]; if (stateValue != null && stateValue.Type != JTokenType.Null) { string stateInstance = ((string)stateValue); propertiesInstance.State = stateInstance; } JToken disabledAlertsValue = propertiesValue["disabledAlerts"]; if (disabledAlertsValue != null && disabledAlertsValue.Type != JTokenType.Null) { string disabledAlertsInstance = ((string)disabledAlertsValue); propertiesInstance.DisabledAlerts = disabledAlertsInstance; } JToken emailAddressesValue = propertiesValue["emailAddresses"]; if (emailAddressesValue != null && emailAddressesValue.Type != JTokenType.Null) { string emailAddressesInstance = ((string)emailAddressesValue); propertiesInstance.EmailAddresses = emailAddressesInstance; } JToken emailAccountAdminsValue = propertiesValue["emailAccountAdmins"]; if (emailAccountAdminsValue != null && emailAccountAdminsValue.Type != JTokenType.Null) { string emailAccountAdminsInstance = ((string)emailAccountAdminsValue); propertiesInstance.EmailAccountAdmins = emailAccountAdminsInstance; } } JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); securityAlertPolicyInstance.Id = idInstance; } JToken nameValue = responseDoc["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); securityAlertPolicyInstance.Name = nameInstance; } JToken typeValue = responseDoc["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); securityAlertPolicyInstance.Type = typeInstance; } JToken locationValue = responseDoc["location"]; if (locationValue != null && locationValue.Type != JTokenType.Null) { string locationInstance = ((string)locationValue); securityAlertPolicyInstance.Location = locationInstance; } JToken tagsSequenceElement = ((JToken)responseDoc["tags"]); if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null) { foreach (JProperty property in tagsSequenceElement) { string tagsKey = ((string)property.Name); string tagsValue = ((string)property.Value); securityAlertPolicyInstance.Tags.Add(tagsKey, tagsValue); } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }