public static ScalingGroup CreateGroup(this IAutoScaleService service, ScalingGroupConfiguration configuration)
        {
            if (service == null)
                throw new ArgumentNullException("service");

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

                throw;
            }
        }
        /// <inheritdoc/>
        public Task <ScalingGroup> CreateGroupAsync(ScalingGroupConfiguration configuration, CancellationToken cancellationToken)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            UriTemplate template   = new UriTemplate("/groups");
            var         parameters = new Dictionary <string, string>();

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

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

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

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

                return(valueToken.ToObject <ScalingGroup>());
            };

            return(AuthenticateServiceAsync(cancellationToken)
                   .Then(prepareRequest)
                   .Then(requestResource)
                   .Select(resultSelector));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a new scaling group.
        /// </summary>
        /// <param name="service">The Auto Scale service instance.</param>
        /// <param name="configuration">A <see cref="ScalingGroupConfiguration"/> object describing the scaling group configuration.</param>
        /// <returns>A <see cref="ScalingGroup"/> object describing the newly created scaling group.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="service"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException">If <paramref name="configuration"/> is <see langword="null"/>.</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_createGroup_v1.0__tenantId__groups_autoscale-groups.html">Create group (Rackspace Auto Scale Developer Guide - API v1.0)</seealso>
        public static ScalingGroup CreateGroup(this IAutoScaleService service, ScalingGroupConfiguration configuration)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

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

                throw;
            }
        }