/// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IAutomatedExportOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name.
 /// </param>
 /// <param name='serverName'>
 /// Required. The server name.
 /// </param>
 /// <param name='databaseName'>
 /// Required. The database name.
 /// </param>
 /// <param name='automatedExportSettingName'>
 /// Required. The automated export setting name.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse CreateOrUpdateSetting(this IAutomatedExportOperations operations, string resourceGroupName, string serverName, string databaseName, string automatedExportSettingName, AutomatedExportSettingCreateOrUpdateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IAutomatedExportOperations)s).CreateOrUpdateSettingAsync(resourceGroupName, serverName, databaseName, automatedExportSettingName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IAutomatedExportOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name.
 /// </param>
 /// <param name='serverName'>
 /// Required. The server name.
 /// </param>
 /// <param name='databaseName'>
 /// Required. The database name.
 /// </param>
 /// <param name='automatedExportSettingName'>
 /// Required. The automated export setting name.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task <AzureOperationResponse> CreateOrUpdateSettingAsync(this IAutomatedExportOperations operations, string resourceGroupName, string serverName, string databaseName, string automatedExportSettingName, AutomatedExportSettingCreateOrUpdateParameters parameters)
 {
     return(operations.CreateOrUpdateSettingAsync(resourceGroupName, serverName, databaseName, automatedExportSettingName, parameters, CancellationToken.None));
 }
Пример #3
0
        /// <param name='resourceGroupName'>
        /// Required. The resource group name.
        /// </param>
        /// <param name='serverName'>
        /// Required. The server name.
        /// </param>
        /// <param name='databaseName'>
        /// Required. The database name.
        /// </param>
        /// <param name='parameters'>
        /// Required. Parameters supplied to the operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// A standard service response including an HTTP status code and
        /// request ID.
        /// </returns>
        public async Task <AzureOperationResponse> CreateOrUpdateSettingAsync(string resourceGroupName, string serverName, string databaseName, AutomatedExportSettingCreateOrUpdateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }
            if (databaseName == null)
            {
                throw new ArgumentNullException("databaseName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            // 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);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateOrUpdateSettingAsync", 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/microsoft.sql/servers/";
            url = url + Uri.EscapeDataString(serverName);
            url = url + "/databases/";
            url = url + Uri.EscapeDataString(databaseName);
            url = url + "/providers/microsoft.insights/automatedexportsettings/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.Put;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Serialize Request
                string requestContent = null;
                JToken requestDoc     = null;

                JObject automatedExportSettingCreateOrUpdateParametersValue = new JObject();
                requestDoc = automatedExportSettingCreateOrUpdateParametersValue;

                if (parameters.Location != null)
                {
                    automatedExportSettingCreateOrUpdateParametersValue["location"] = parameters.Location;
                }

                if (parameters.Tags != null)
                {
                    if (parameters.Tags is ILazyCollection == false || ((ILazyCollection)parameters.Tags).IsInitialized)
                    {
                        JObject tagsDictionary = new JObject();
                        foreach (KeyValuePair <string, string> pair in parameters.Tags)
                        {
                            string tagsKey   = pair.Key;
                            string tagsValue = pair.Value;
                            tagsDictionary[tagsKey] = tagsValue;
                        }
                        automatedExportSettingCreateOrUpdateParametersValue["tags"] = tagsDictionary;
                    }
                }

                if (parameters.Properties != null)
                {
                    JObject propertiesValue = new JObject();
                    automatedExportSettingCreateOrUpdateParametersValue["properties"] = propertiesValue;

                    if (parameters.Properties.DatabaseCredential != null)
                    {
                        JObject databaseCredentialValue = new JObject();
                        propertiesValue["databaseCredential"] = databaseCredentialValue;

                        if (parameters.Properties.DatabaseCredential.DatabaseUserName != null)
                        {
                            databaseCredentialValue["databaseUserName"] = parameters.Properties.DatabaseCredential.DatabaseUserName;
                        }

                        if (parameters.Properties.DatabaseCredential.DatabasePassword != null)
                        {
                            databaseCredentialValue["databasePassword"] = parameters.Properties.DatabaseCredential.DatabasePassword;
                        }
                    }

                    if (parameters.Properties.BlobStorageSetting != null)
                    {
                        JObject blobStorageSettingValue = new JObject();
                        propertiesValue["blobStorageSetting"] = blobStorageSettingValue;

                        if (parameters.Properties.BlobStorageSetting.StorageAccount != null)
                        {
                            blobStorageSettingValue["storageAccount"] = parameters.Properties.BlobStorageSetting.StorageAccount;
                        }
                    }

                    if (parameters.Properties.BackupOption != null)
                    {
                        JObject backupOptionValue = new JObject();
                        propertiesValue["backupOption"] = backupOptionValue;

                        backupOptionValue["startTime"] = parameters.Properties.BackupOption.StartTime;

                        backupOptionValue["repeatInterval"] = XmlConvert.ToString(parameters.Properties.BackupOption.RepeatInterval);

                        backupOptionValue["retentionPeriod"] = XmlConvert.ToString(parameters.Properties.BackupOption.RetentionPeriod);

                        backupOptionValue["keepAtLeastOneBackup"] = parameters.Properties.BackupOption.KeepAtLeastOneBackup;

                        backupOptionValue["consistencyOption"] = parameters.Properties.BackupOption.ConsistencyOption.ToString();
                    }

                    propertiesValue["enabled"] = parameters.Properties.Enabled;
                }

                requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                // 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 && statusCode != HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    AzureOperationResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new AzureOperationResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                        }
                    }
                    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();
                }
            }
        }