/// <summary>
        /// Helper method to get the AWS Credentials from environment variables
        /// </summary>
        /// <param name="environment">The cake environment.</param>
        /// <returns>A new <see cref="LoadBalancingSettings"/> instance to be used in calls to the <see cref="ILoadBalancingManager"/>.</returns>
        public static LoadBalancingSettings CreateLoadBalancingSettings(this ICakeEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            LoadBalancingSettings settings = new LoadBalancingSettings();

            //AWS Fallback
            AWSCredentials creds = FallbackCredentialsFactory.GetCredentials();

            if (creds != null)
            {
                settings.Credentials = creds;
            }

            //Environment Variables
            string region = environment.GetEnvironmentVariable("AWS_REGION");

            if (!String.IsNullOrEmpty(region))
            {
                settings.Region = RegionEndpoint.GetBySystemName(region);
            }

            return(settings);
        }
        /// <summary>
        /// Helper method to get the AWS Credentials from environment variables
        /// </summary>
        /// <param name="environment">The cake environment.</param>
        /// <returns>A new <see cref="LoadBalancingSettings"/> instance to be used in calls to the <see cref="ILoadBalancingManager"/>.</returns>
        public static LoadBalancingSettings CreateLoadBalancingSettings(this ICakeEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            LoadBalancingSettings settings = new LoadBalancingSettings();
            
            //AWS Fallback
            AWSCredentials creds = FallbackCredentialsFactory.GetCredentials();
            if (creds != null)
            {
                settings.Credentials = creds;
            }

            //Environment Variables
            string region = environment.GetEnvironmentVariable("AWS_REGION");
            if (!String.IsNullOrEmpty(region))
            {
                settings.Region = RegionEndpoint.GetBySystemName(region);
            }

            return settings;
        }
            private AmazonElasticLoadBalancingClient CreateClient(LoadBalancingSettings settings)
            {
                if (settings == null)
                {
                    throw new ArgumentNullException("settings");
                }
                
                if (settings.Region == null)
                {
                    throw new ArgumentNullException("settings.Region");
                }

                if (settings.Credentials == null)
                {
                    if (String.IsNullOrEmpty(settings.AccessKey))
                    {
                        throw new ArgumentNullException("settings.AccessKey");
                    }
                    if (String.IsNullOrEmpty(settings.SecretKey))
                    {
                        throw new ArgumentNullException("settings.SecretKey");
                    }

                    return new AmazonElasticLoadBalancingClient(settings.AccessKey, settings.SecretKey, settings.Region);
                }
                else
                {
                    return new AmazonElasticLoadBalancingClient(settings.Credentials, settings.Region);
                }
            }
예제 #4
0
        /// <summary>
        /// Specifies the AWS Secret Key to use as credentials.
        /// </summary>
        /// <param name="settings">The LoadBalancing settings.</param>
        /// <param name="key">The AWS Secret Key</param>
        /// <returns>The same <see cref="LoadBalancingSettings"/> instance so that multiple calls can be chained.</returns>
        public static LoadBalancingSettings SetSecretKey(this LoadBalancingSettings settings, string key)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.SecretKey = key;
            return(settings);
        }
예제 #5
0
        /// <summary>
        /// Specifies the endpoints available to AWS clients.
        /// </summary>
        /// <param name="settings">The LoadBalancing settings.</param>
        /// <param name="region">The endpoints available to AWS clients.</param>
        /// <returns>The same <see cref="LoadBalancingSettings"/> instance so that multiple calls can be chained.</returns>
        public static LoadBalancingSettings SetRegion(this LoadBalancingSettings settings, string region)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.Region = RegionEndpoint.GetBySystemName(region);
            return(settings);
        }
예제 #6
0
        /// <summary>
        /// Specifies the endpoints available to AWS clients.
        /// </summary>
        /// <param name="settings">The LoadBalancing settings.</param>
        /// <param name="region">The endpoints available to AWS clients.</param>
        /// <returns>The same <see cref="LoadBalancingSettings"/> instance so that multiple calls can be chained.</returns>
        public static LoadBalancingSettings SetRegion(this LoadBalancingSettings settings, RegionEndpoint region)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.Region = region;
            return(settings);
        }
예제 #7
0
        /// <summary>
        /// Specifies the AWS Session Token to use as credentials.
        /// </summary>
        /// <param name="settings">The LoadBalancing settings.</param>
        /// <param name="token">The AWS Session Token.</param>
        /// <returns>The same <see cref="LoadBalancingSettings"/> instance so that multiple calls can be chained.</returns>
        public static LoadBalancingSettings SetSessionToken(this LoadBalancingSettings settings, string token)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            settings.SessionToken = token;
            return(settings);
        }
        private AmazonElasticLoadBalancingClient CreateClient(LoadBalancingSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (settings.Region == null)
            {
                throw new ArgumentNullException("settings.Region");
            }

            if (settings.Credentials == null)
            {
                if (String.IsNullOrEmpty(settings.AccessKey))
                {
                    throw new ArgumentNullException("settings.AccessKey");
                }
                if (String.IsNullOrEmpty(settings.SecretKey))
                {
                    throw new ArgumentNullException("settings.SecretKey");
                }

                if (!String.IsNullOrEmpty(settings.SessionToken))
                {
                    return(new AmazonElasticLoadBalancingClient(settings.AccessKey, settings.SecretKey, settings.SessionToken, settings.Region));
                }
                else
                {
                    return(new AmazonElasticLoadBalancingClient(settings.AccessKey, settings.SecretKey, settings.Region));
                }
            }
            else
            {
                return(new AmazonElasticLoadBalancingClient(settings.Credentials, settings.Region));
            }
        }
 public static bool DisableAvailabilityZones(this ICakeContext context, string loadBalancer, IList<string> zones, LoadBalancingSettings settings)
 {
     return context.CreateManager().DisableAvailabilityZones(loadBalancer, zones, settings);
 }
 public static async Task <bool> EnableAvailabilityZones(this ICakeContext context, string loadBalancer, string zones, LoadBalancingSettings settings)
 {
     return(await context.CreateManager().EnableAvailabilityZones(loadBalancer, zones.Split(','), settings));
 }
 public static bool DeregisterLoadBalancerInstance(this ICakeContext context, string loadBalancer, LoadBalancingSettings settings)
 {
     return context.CreateManager().DeregisterInstances(loadBalancer, EC2Metadata.InstanceId.Split(','), settings);
 }
 public static bool RegisterLoadBalancerInstances(this ICakeContext context, string loadBalancer, IList<string> instances, LoadBalancingSettings settings)
 {
     return context.CreateManager().RegisterInstances(loadBalancer, instances, settings);
 }
 public static async Task <bool> RegisterLoadBalancerInstances(this ICakeContext context, string loadBalancer, IList <string> instances, LoadBalancingSettings settings)
 {
     return(await context.CreateManager().RegisterInstances(loadBalancer, instances, settings));
 }
 public static async Task <bool> DeregisterLoadBalancerInstances(this ICakeContext context, string loadBalancer, string instances, LoadBalancingSettings settings)
 {
     return(await context.CreateManager().DeregisterInstances(loadBalancer, instances.Split(','), settings));
 }
 public static async Task <bool> RegisterLoadBalancerInstance(this ICakeContext context, string loadBalancer, LoadBalancingSettings settings)
 {
     return(await context.CreateManager().RegisterInstances(loadBalancer, EC2InstanceMetadata.InstanceId.Split(','), settings));
 }
 public static bool DisableAvailabilityZone(this ICakeContext context, string loadBalancer, LoadBalancingSettings settings)
 {
     return context.CreateManager().DisableAvailabilityZones(loadBalancer, EC2Metadata.AvailabilityZone.Split(','), settings);
 }
        /// <summary>
        /// Removes the specified Availability Zones from the set of Availability Zones for the specified load balancer.
        /// There must be at least one Availability Zone registered with a load balancer at all times. After an Availability Zone is removed,
        /// all instances registered with the load balancer that are in the removed Availability Zone go into the OutOfService state.
        /// Then, the load balancer attempts to equally balance the traffic among its remaining Availability Zones.
        /// </summary>
        /// <param name="loadBalancer">The name associated with the load balancer.</param>
        /// <param name="zones">The Availability Zones to remove from the load balancer.</param>
        /// <param name="settings">The <see cref="LoadBalancingSettings"/> used during the request to AWS.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        public async Task <bool> DisableAvailabilityZones(string loadBalancer, IList <string> zones, LoadBalancingSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (String.IsNullOrEmpty(loadBalancer))
            {
                throw new ArgumentNullException("loadBalancer");
            }
            if ((zones == null) || (zones.Count == 0))
            {
                throw new ArgumentNullException("zones");
            }



            //Create Request
            AmazonElasticLoadBalancingClient client = this.CreateClient(settings);
            DisableAvailabilityZonesForLoadBalancerRequest request = new DisableAvailabilityZonesForLoadBalancerRequest();

            request.LoadBalancerName = loadBalancer;

            foreach (string zone in zones)
            {
                request.AvailabilityZones.Add(zone);
            }



            //Check Response
            DisableAvailabilityZonesForLoadBalancerResponse response = await client.DisableAvailabilityZonesForLoadBalancerAsync(request, cancellationToken);

            if (response.HttpStatusCode == HttpStatusCode.OK)
            {
                _Log.Verbose("Successfully disabled zones '{0}'", string.Join(",", zones));
                return(true);
            }
            else
            {
                _Log.Error("Failed to disabled zones '{0}'", string.Join(",", zones));
                return(false);
            }
        }
            /// <summary>
            /// Removes instances from the load balancer. Once the instance is deregistered, it will stop receiving traffic from the load balancer. 
            /// </summary>
            /// <param name="loadBalancer">The name associated with the load balancer.</param>
            /// <param name="instances">A list of instance IDs that should be deregistered with the load balancer.</param>
            /// <param name="settings">The <see cref="LoadBalancingSettings"/> used during the request to AWS.</param>
            public bool DeregisterInstances(string loadBalancer, IList<string> instances, LoadBalancingSettings settings)
            {
                if (String.IsNullOrEmpty(loadBalancer))
                {
                    throw new ArgumentNullException("loadBalancer");
                }
                if ((instances == null) || (instances.Count == 0))
                {
                    throw new ArgumentNullException("instances");
                }



                //Create Request
                AmazonElasticLoadBalancingClient client = this.CreateClient(settings);
                DeregisterInstancesFromLoadBalancerRequest request = new DeregisterInstancesFromLoadBalancerRequest();

                request.LoadBalancerName = loadBalancer;

                foreach (string instance in instances)
                {
                    request.Instances.Add(new Instance(instance));
                }



                //Check Response
                DeregisterInstancesFromLoadBalancerResponse response = client.DeregisterInstancesFromLoadBalancer(request);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    _Log.Verbose("Successfully deregistered instances '{0}'", string.Join(",", instances));
                    return true;
                }
                else
                {
                    _Log.Error("Failed to deregister instances '{0}'", string.Join(",", instances));
                    return false;
                }
            }
            /// <summary>
            /// Removes the specified Availability Zones from the set of Availability Zones for the specified load balancer.
            /// There must be at least one Availability Zone registered with a load balancer at all times. After an Availability Zone is removed, 
            /// all instances registered with the load balancer that are in the removed Availability Zone go into the OutOfService state. 
            /// Then, the load balancer attempts to equally balance the traffic among its remaining Availability Zones.
            /// </summary>
            /// <param name="loadBalancer">The name associated with the load balancer.</param>
            /// <param name="zones">The Availability Zones to remove from the load balancer.</param>
            /// <param name="settings">The <see cref="LoadBalancingSettings"/> used during the request to AWS.</param>
            public bool DisableAvailabilityZones(string loadBalancer, IList<string> zones, LoadBalancingSettings settings)
            {
                if (String.IsNullOrEmpty(loadBalancer))
                {
                    throw new ArgumentNullException("loadBalancer");
                }
                if ((zones == null) || (zones.Count == 0))
                {
                    throw new ArgumentNullException("zones");
                }



                //Create Request
                AmazonElasticLoadBalancingClient client = this.CreateClient(settings);
                DisableAvailabilityZonesForLoadBalancerRequest request = new DisableAvailabilityZonesForLoadBalancerRequest();

                request.LoadBalancerName = loadBalancer;

                foreach (string zone in zones)
                {
                    request.AvailabilityZones.Add(zone);
                }



                //Check Response
                DisableAvailabilityZonesForLoadBalancerResponse response = client.DisableAvailabilityZonesForLoadBalancer(request);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    _Log.Verbose("Successfully disabled zones '{0}'", string.Join(",", zones));
                    return true;
                }
                else
                {
                    _Log.Error("Failed to disabled zones '{0}'", string.Join(",", zones));
                    return false;
                }
            }
        /// <summary>
        /// Removes instances from the load balancer. Once the instance is deregistered, it will stop receiving traffic from the load balancer.
        /// </summary>
        /// <param name="loadBalancer">The name associated with the load balancer.</param>
        /// <param name="instances">A list of instance IDs that should be deregistered with the load balancer.</param>
        /// <param name="settings">The <see cref="LoadBalancingSettings"/> used during the request to AWS.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        public async Task <bool> DeregisterInstances(string loadBalancer, IList <string> instances, LoadBalancingSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (String.IsNullOrEmpty(loadBalancer))
            {
                throw new ArgumentNullException("loadBalancer");
            }
            if ((instances == null) || (instances.Count == 0))
            {
                throw new ArgumentNullException("instances");
            }



            //Create Request
            AmazonElasticLoadBalancingClient           client  = this.CreateClient(settings);
            DeregisterInstancesFromLoadBalancerRequest request = new DeregisterInstancesFromLoadBalancerRequest();

            request.LoadBalancerName = loadBalancer;

            foreach (string instance in instances)
            {
                request.Instances.Add(new Instance(instance));
            }



            //Check Response
            DeregisterInstancesFromLoadBalancerResponse response = await client.DeregisterInstancesFromLoadBalancerAsync(request, cancellationToken);

            if (response.HttpStatusCode == HttpStatusCode.OK)
            {
                _Log.Verbose("Successfully deregistered instances '{0}'", string.Join(",", instances));
                return(true);
            }
            else
            {
                _Log.Error("Failed to deregister instances '{0}'", string.Join(",", instances));
                return(false);
            }
        }
 public static async Task <bool> DisableAvailabilityZone(this ICakeContext context, string loadBalancer, LoadBalancingSettings settings)
 {
     return(await context.CreateManager().DisableAvailabilityZones(loadBalancer, EC2InstanceMetadata.AvailabilityZone.Split(','), settings));
 }
 public static async Task <bool> DisableAvailabilityZones(this ICakeContext context, string loadBalancer, IList <string> zones, LoadBalancingSettings settings)
 {
     return(await context.CreateManager().DisableAvailabilityZones(loadBalancer, zones, settings));
 }
 public static bool EnableAvailabilityZones(this ICakeContext context, string loadBalancer, string zones, LoadBalancingSettings settings)
 {
     return context.CreateManager().EnableAvailabilityZones(loadBalancer, zones.Split(','), settings);
 }