/// <inheritdoc/>
        public Task <Webhook> CreateWebhookAsync(ScalingGroupId groupId, PolicyId policyId, NewWebhookConfiguration configuration, CancellationToken cancellationToken)
        {
            if (groupId == null)
            {
                throw new ArgumentNullException("groupId");
            }
            if (policyId == null)
            {
                throw new ArgumentNullException("policyId");
            }
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            UriTemplate template   = new UriTemplate("/groups/{groupId}/policies/{policyId}/webhooks");
            var         parameters = new Dictionary <string, string> {
                { "groupId", groupId.Value }, { "policyId", policyId.Value }
            };

            Func <Task <Tuple <IdentityToken, Uri> >, Task <HttpWebRequest> > prepareRequest =
                PrepareRequestAsyncFunc(HttpMethod.POST, template, parameters, new[] { configuration });

            Func <Task <HttpWebRequest>, Task <JObject> > requestResource =
                GetResponseAsyncFunc <JObject>(cancellationToken);

            Func <Task <JObject>, Webhook> resultSelector =
                task =>
            {
                JObject result = task.Result;
                if (result == null)
                {
                    return(null);
                }

                JToken valueToken = result["webhooks"];
                if (valueToken == null)
                {
                    return(null);
                }

                Webhook[] webhooks = valueToken.ToObject <Webhook[]>();
                return(webhooks[0]);
            };

            return(AuthenticateServiceAsync(cancellationToken)
                   .Then(prepareRequest)
                   .Then(requestResource)
                   .Select(resultSelector));
        }
Example #2
0
        /// <summary>
        /// Create a webhook capable of anonymously executing a scaling policy.
        /// </summary>
        /// <param name="service">The Auto Scale service instance.</param>
        /// <param name="groupId">The ID of the scaling group. This is obtained from <see cref="ScalingGroup.Id">ScalingGroup.Id</see>.</param>
        /// <param name="policyId">The ID of the scaling policy. This is obtained from <see cref="Policy.Id">Policy.Id</see>.</param>
        /// <param name="configuration">A <see cref="NewWebhookConfiguration"/> object describing the webhook configuration.</param>
        /// <returns>A <see cref="Webhook"/> object describing the newly created webhook.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="service"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="groupId"/> is <see langword="null"/>.
        /// <para>-or-</para>
        /// <para>If <paramref name="policyId"/> is <see langword="null"/>.</para>
        /// <para>-or-</para>
        /// <para>If <paramref name="configuration"/> is <see langword="null"/>.</para>
        /// </exception>
        /// <exception cref="WebException">If the REST request does not return successfully.</exception>
        /// <seealso href="http://docs.rackspace.com/cas/api/v1.0/autoscale-devguide/content/POST_createWebhook_v1.0__tenantId__groups__groupId__policies__policyId__webhooks_autoscale-webhooks.html">Create a webhook (Rackspace Auto Scale Developer Guide - API v1.0)</seealso>
        public static Webhook CreateWebhook(this IAutoScaleService service, ScalingGroupId groupId, PolicyId policyId, NewWebhookConfiguration configuration)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

            try
            {
                return(service.CreateWebhookAsync(groupId, policyId, configuration, CancellationToken.None).Result);
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection <Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                {
                    throw innerExceptions[0];
                }

                throw;
            }
        }