Esempio n. 1
0
 /// <summary>
 /// Create the watcher action identified by watcher name.  (see
 /// http://aka.ms/azureautomationsdk/watcheractionoperations for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IWatcherActionOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='automationAccount'>
 /// Required. The automation account name.
 /// </param>
 /// <param name='watcherName'>
 /// Required. The watcher's name.
 /// </param>
 /// <param name='parameters'>
 /// Required. The create or update parameters for watcher.
 /// </param>
 /// <returns>
 /// The response model for the create or update watcher action
 /// operation.
 /// </returns>
 public static WatcherActionCreateOrUpdateResponse CreateOrUpdate(this IWatcherActionOperations operations, string resourceGroupName, string automationAccount, string watcherName, WatcherActionCreateOrUpdateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IWatcherActionOperations)s).CreateOrUpdateAsync(resourceGroupName, automationAccount, watcherName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
Esempio n. 2
0
 /// <summary>
 /// Create the watcher action identified by watcher name.  (see
 /// http://aka.ms/azureautomationsdk/watcheractionoperations for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IWatcherActionOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='automationAccount'>
 /// Required. The automation account name.
 /// </param>
 /// <param name='watcherName'>
 /// Required. The watcher's name.
 /// </param>
 /// <param name='parameters'>
 /// Required. The create or update parameters for watcher.
 /// </param>
 /// <returns>
 /// The response model for the create or update watcher action
 /// operation.
 /// </returns>
 public static Task <WatcherActionCreateOrUpdateResponse> CreateOrUpdateAsync(this IWatcherActionOperations operations, string resourceGroupName, string automationAccount, string watcherName, WatcherActionCreateOrUpdateParameters parameters)
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, automationAccount, watcherName, parameters, CancellationToken.None));
 }
Esempio n. 3
0
        /// <summary>
        /// Create the watcher action identified by watcher name.  (see
        /// http://aka.ms/azureautomationsdk/watcheractionoperations 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='watcherName'>
        /// Required. The watcher's name.
        /// </param>
        /// <param name='parameters'>
        /// Required. The create or update parameters for watcher.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the create or update watcher action
        /// operation.
        /// </returns>
        public async Task <WatcherActionCreateOrUpdateResponse> CreateOrUpdateAsync(string resourceGroupName, string automationAccount, string watcherName, WatcherActionCreateOrUpdateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (watcherName == null)
            {
                throw new ArgumentNullException("watcherName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Name == null)
            {
                throw new ArgumentNullException("parameters.Name");
            }
            if (parameters.Properties == null)
            {
                throw new ArgumentNullException("parameters.Properties");
            }

            // 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("watcherName", watcherName);
                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 + "/watchers/";
            url = url + Uri.EscapeDataString(watcherName);
            url = url + "/watcherActions/";
            url = url + Uri.EscapeDataString(parameters.Name);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2017-05-15-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("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);

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

                JObject watcherActionCreateOrUpdateParametersValue = new JObject();
                requestDoc = watcherActionCreateOrUpdateParametersValue;

                watcherActionCreateOrUpdateParametersValue["name"] = parameters.Name;

                JObject propertiesValue = new JObject();
                watcherActionCreateOrUpdateParametersValue["properties"] = propertiesValue;

                if (parameters.Properties.WatcherName != null)
                {
                    propertiesValue["watcherName"] = parameters.Properties.WatcherName;
                }

                if (parameters.Properties.Type != null)
                {
                    propertiesValue["Type"] = parameters.Properties.Type;
                }

                if (parameters.Properties.ActionProperties != null)
                {
                    if (parameters.Properties.ActionProperties is ILazyCollection == false || ((ILazyCollection)parameters.Properties.ActionProperties).IsInitialized)
                    {
                        JObject actionPropertiesDictionary = new JObject();
                        foreach (KeyValuePair <string, string> pair in parameters.Properties.ActionProperties)
                        {
                            string actionPropertiesKey   = pair.Key;
                            string actionPropertiesValue = pair.Value;
                            actionPropertiesDictionary[actionPropertiesKey] = actionPropertiesValue;
                        }
                        propertiesValue["ActionProperties"] = actionPropertiesDictionary;
                    }
                }

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

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            WatcherAction watcherActionInstance = new WatcherAction();
                            result.WatcherAction = watcherActionInstance;

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

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

                            JToken propertiesValue2 = responseDoc["properties"];
                            if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null)
                            {
                                WatcherActionProperties propertiesInstance = new WatcherActionProperties();
                                watcherActionInstance.Properties = propertiesInstance;

                                JToken watcherNameValue = propertiesValue2["watcherName"];
                                if (watcherNameValue != null && watcherNameValue.Type != JTokenType.Null)
                                {
                                    string watcherNameInstance = ((string)watcherNameValue);
                                    propertiesInstance.WatcherName = watcherNameInstance;
                                }

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

                                JToken actionPropertiesSequenceElement = ((JToken)propertiesValue2["ActionProperties"]);
                                if (actionPropertiesSequenceElement != null && actionPropertiesSequenceElement.Type != JTokenType.Null)
                                {
                                    foreach (JProperty property in actionPropertiesSequenceElement)
                                    {
                                        string actionPropertiesKey2   = ((string)property.Name);
                                        string actionPropertiesValue2 = ((string)property.Value);
                                        propertiesInstance.ActionProperties.Add(actionPropertiesKey2, actionPropertiesValue2);
                                    }
                                }

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

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

                                JToken descriptionValue = propertiesValue2["description"];
                                if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                {
                                    string descriptionInstance = ((string)descriptionValue);
                                    propertiesInstance.Description = descriptionInstance;
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }