示例#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();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Retrieve the Dsc node configurations by node configuration.  (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='nodeConfigurationName'>
        /// Required. The Dsc node configuration name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get Dsc node configuration operation.
        /// </returns>
        public async Task <DscNodeConfigurationGetResponse> GetAsync(string resourceGroupName, string automationAccount, string nodeConfigurationName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (nodeConfigurationName == null)
            {
                throw new ArgumentNullException("nodeConfigurationName");
            }

            // 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("nodeConfigurationName", nodeConfigurationName);
                TracingAdapter.Enter(invocationId, this, "GetAsync", 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(nodeConfigurationName);
            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.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-06-01");

                // 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
                    DscNodeConfigurationGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        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 configurationValue = responseDoc["configuration"];
                            if (configurationValue != null && configurationValue.Type != JTokenType.Null)
                            {
                                DscConfigurationAssociationProperty configurationInstance = new DscConfigurationAssociationProperty();
                                nodeConfigurationInstance.Configuration = configurationInstance;

                                JToken nameValue = configurationValue["name"];
                                if (nameValue != null && nameValue.Type != JTokenType.Null)
                                {
                                    string nameInstance = ((string)nameValue);
                                    configurationInstance.Name = nameInstance;
                                }
                            }

                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                nodeConfigurationInstance.Id = idInstance;
                            }

                            JToken nameValue2 = responseDoc["name"];
                            if (nameValue2 != null && nameValue2.Type != JTokenType.Null)
                            {
                                string nameInstance2 = ((string)nameValue2);
                                nodeConfigurationInstance.Name = nameInstance2;
                            }

                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                nodeConfigurationInstance.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);
                                    nodeConfigurationInstance.Tags.Add(tagsKey, tagsValue);
                                }
                            }

                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                nodeConfigurationInstance.Type = typeInstance;
                            }

                            JToken etagValue = responseDoc["etag"];
                            if (etagValue != null && etagValue.Type != JTokenType.Null)
                            {
                                string etagInstance = ((string)etagValue);
                                nodeConfigurationInstance.Etag = etagInstance;
                            }
                        }
                    }
                    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();
                }
            }
        }