Esempio n. 1
0
        /// <summary>
        /// Create the node configuration identified by node configuration
        /// name.  (see http://aka.ms/azureautomationsdk/dscnodeconfigurations
        /// for more information)
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group
        /// </param>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='parameters'>
        /// Required. The create or update parameters for configuration.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get Dsc node configuration operation.
        /// </returns>
        public async Task <DscNodeConfigurationGetResponse> CreateOrUpdateAsync(string resourceGroupName, string automationAccount, DscNodeConfigurationCreateOrUpdateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Configuration == null)
            {
                throw new ArgumentNullException("parameters.Configuration");
            }
            if (parameters.Name == null)
            {
                throw new ArgumentNullException("parameters.Name");
            }
            if (parameters.Source == null)
            {
                throw new ArgumentNullException("parameters.Source");
            }
            if (parameters.Source.ContentHash != null)
            {
                if (parameters.Source.ContentHash.Algorithm == null)
                {
                    throw new ArgumentNullException("parameters.Source.ContentHash.Algorithm");
                }
                if (parameters.Source.ContentHash.Value == null)
                {
                    throw new ArgumentNullException("parameters.Source.ContentHash.Value");
                }
            }

            // 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("automationAccount", automationAccount);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateOrUpdateAsync", 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/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/nodeConfigurations/";
            url = url + Uri.EscapeDataString(parameters.Name);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-01-01-preview");
            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("x-ms-version", "2014-06-01");

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

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

                JObject dscNodeConfigurationCreateOrUpdateParametersValue = new JObject();
                requestDoc = dscNodeConfigurationCreateOrUpdateParametersValue;

                JObject sourceValue = new JObject();
                dscNodeConfigurationCreateOrUpdateParametersValue["source"] = sourceValue;

                if (parameters.Source.ContentHash != null)
                {
                    JObject hashValue = new JObject();
                    sourceValue["hash"] = hashValue;

                    hashValue["algorithm"] = parameters.Source.ContentHash.Algorithm;

                    hashValue["value"] = parameters.Source.ContentHash.Value;
                }

                if (parameters.Source.ContentType != null)
                {
                    sourceValue["type"] = parameters.Source.ContentType;
                }

                if (parameters.Source.Value != null)
                {
                    sourceValue["value"] = parameters.Source.Value;
                }

                if (parameters.Source.Version != null)
                {
                    sourceValue["version"] = parameters.Source.Version;
                }

                dscNodeConfigurationCreateOrUpdateParametersValue["name"] = parameters.Name;

                JObject configurationValue = new JObject();
                dscNodeConfigurationCreateOrUpdateParametersValue["configuration"] = configurationValue;

                if (parameters.Configuration.Name != null)
                {
                    configurationValue["name"] = parameters.Configuration.Name;
                }

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

                // 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
                    DscNodeConfigurationGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            DscNodeConfiguration nodeConfigurationInstance = new DscNodeConfiguration();
                            result.NodeConfiguration = nodeConfigurationInstance;

                            JToken configurationNameValue = responseDoc["configurationName"];
                            if (configurationNameValue != null && configurationNameValue.Type != JTokenType.Null)
                            {
                                string configurationNameInstance = ((string)configurationNameValue);
                                nodeConfigurationInstance.Name = configurationNameInstance;
                            }

                            JToken lastModifiedTimeValue = responseDoc["lastModifiedTime"];
                            if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null)
                            {
                                DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue);
                                nodeConfigurationInstance.LastModifiedTime = lastModifiedTimeInstance;
                            }

                            JToken creationTimeValue = responseDoc["creationTime"];
                            if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null)
                            {
                                DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue);
                                nodeConfigurationInstance.CreationTime = creationTimeInstance;
                            }

                            JToken configurationValue2 = responseDoc["configuration"];
                            if (configurationValue2 != null && configurationValue2.Type != JTokenType.Null)
                            {
                                DscConfigurationAssociationProperty configurationInstance = new DscConfigurationAssociationProperty();
                                nodeConfigurationInstance.Configuration = configurationInstance;

                                JToken nameValue = configurationValue2["name"];
                                if (nameValue != null && nameValue.Type != JTokenType.Null)
                                {
                                    string nameInstance = ((string)nameValue);
                                    configurationInstance.Name = nameInstance;
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }
 /// <summary>
 /// Create the node configuration identified by node configuration name.
 /// <see href="http://aka.ms/azureautomationsdk/dscnodeconfigurations" />
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Name of an Azure Resource group.
 /// </param>
 /// <param name='automationAccountName'>
 /// The name of the automation account.
 /// </param>
 /// <param name='nodeConfigurationName'>
 /// The Dsc node configuration name.
 /// </param>
 /// <param name='parameters'>
 /// The create or update parameters for configuration.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <DscNodeConfiguration> BeginCreateOrUpdateAsync(this IDscNodeConfigurationOperations operations, string resourceGroupName, string automationAccountName, string nodeConfigurationName, DscNodeConfigurationCreateOrUpdateParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroupName, automationAccountName, nodeConfigurationName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 /// <summary>
 /// Create the node configuration identified by node configuration
 /// name.  (see http://aka.ms/azureautomationsdk/dscnodeconfigurations
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IDscNodeConfigurationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group
 /// </param>
 /// <param name='automationAccount'>
 /// Required. The automation account name.
 /// </param>
 /// <param name='parameters'>
 /// Required. The create or update parameters for configuration.
 /// </param>
 /// <returns>
 /// The response model for the get Dsc node configuration operation.
 /// </returns>
 public static Task <DscNodeConfigurationGetResponse> CreateOrUpdateAsync(this IDscNodeConfigurationOperations operations, string resourceGroupName, string automationAccount, DscNodeConfigurationCreateOrUpdateParameters parameters)
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, automationAccount, parameters, CancellationToken.None));
 }
 /// <summary>
 /// Create the node configuration identified by node configuration name.
 /// <see href="http://aka.ms/azureautomationsdk/dscnodeconfigurations" />
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Name of an Azure Resource group.
 /// </param>
 /// <param name='automationAccountName'>
 /// The name of the automation account.
 /// </param>
 /// <param name='nodeConfigurationName'>
 /// The Dsc node configuration name.
 /// </param>
 /// <param name='parameters'>
 /// The create or update parameters for configuration.
 /// </param>
 public static DscNodeConfiguration BeginCreateOrUpdate(this IDscNodeConfigurationOperations operations, string resourceGroupName, string automationAccountName, string nodeConfigurationName, DscNodeConfigurationCreateOrUpdateParameters parameters)
 {
     return(operations.BeginCreateOrUpdateAsync(resourceGroupName, automationAccountName, nodeConfigurationName, parameters).GetAwaiter().GetResult());
 }
 /// <summary>
 /// Create the node configuration identified by node configuration
 /// name.  (see http://aka.ms/azureautomationsdk/dscnodeconfigurations
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IDscNodeConfigurationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group
 /// </param>
 /// <param name='automationAccount'>
 /// Required. The automation account name.
 /// </param>
 /// <param name='parameters'>
 /// Required. The create or update parameters for configuration.
 /// </param>
 /// <returns>
 /// The response model for the get Dsc node configuration operation.
 /// </returns>
 public static DscNodeConfigurationGetResponse CreateOrUpdate(this IDscNodeConfigurationOperations operations, string resourceGroupName, string automationAccount, DscNodeConfigurationCreateOrUpdateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IDscNodeConfigurationOperations)s).CreateOrUpdateAsync(resourceGroupName, automationAccount, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }