private AmazonCloudFrontClient GetClient(CloudFrontSettings 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 AmazonCloudFrontClient(settings.AccessKey, settings.SecretKey, settings.Region);
                }
                else
                {
                    return new AmazonCloudFrontClient(settings.Credentials, settings.Region);
                }
            }
        /// <summary>
        /// Helper method to get the AWS Credentials from environment variables
        /// </summary>
        /// <param name="environment">The cake environment.</param>
        /// <returns>A new <see cref="CloudFrontSettings"/> instance to be used in calls to the <see cref="ICloudFrontManager"/>.</returns>
        public static CloudFrontSettings CreateCloudFrontSettings(this ICakeEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            CloudFrontSettings settings = new CloudFrontSettings();

            //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;
        }
Пример #3
0
        /// <summary>
        /// Helper method to get the AWS Credentials from environment variables
        /// </summary>
        /// <param name="environment">The cake environment.</param>
        /// <returns>A new <see cref="CloudFrontSettings"/> instance to be used in calls to the <see cref="ICloudFrontManager"/>.</returns>
        public static CloudFrontSettings CreateCloudFrontSettings(this ICakeEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            CloudFrontSettings settings = new CloudFrontSettings();

            //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);
        }
Пример #4
0
        /// <summary>
        /// Specifies the endpoints available to AWS clients.
        /// </summary>
        /// <param name="settings">The CloudFront settings.</param>
        /// <param name="region">The endpoints available to AWS clients.</param>
        /// <returns>The same <see cref="CloudFrontSettings"/> instance so that multiple calls can be chained.</returns>
        public static CloudFrontSettings SetRegion(this CloudFrontSettings settings, RegionEndpoint region)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.Region = region;
            return(settings);
        }
Пример #5
0
        /// <summary>
        /// Specifies the endpoints available to AWS clients.
        /// </summary>
        /// <param name="settings">The CloudFront settings.</param>
        /// <param name="region">The endpoints available to AWS clients.</param>
        /// <returns>The same <see cref="CloudFrontSettings"/> instance so that multiple calls can be chained.</returns>
        public static CloudFrontSettings SetRegion(this CloudFrontSettings settings, string region)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.Region = RegionEndpoint.GetBySystemName(region);
            return(settings);
        }
Пример #6
0
        /// <summary>
        /// Specifies the AWS Secret Key to use as credentials.
        /// </summary>
        /// <param name="settings">The CloudFront settings.</param>
        /// <param name="key">The AWS Secret Key</param>
        /// <returns>The same <see cref="CloudFrontSettings"/> instance so that multiple calls can be chained.</returns>
        public static CloudFrontSettings SetSecretKey(this CloudFrontSettings settings, string key)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            settings.SecretKey = key;
            return(settings);
        }
Пример #7
0
        /// <summary>
        /// Specifies the AWS Session Token to use as credentials.
        /// </summary>
        /// <param name="settings">The CloudFront settings.</param>
        /// <param name="token">The AWS Session Token.</param>
        /// <returns>The same <see cref="CloudFrontSettings"/> instance so that multiple calls can be chained.</returns>
        public static CloudFrontSettings SetSessionToken(this CloudFrontSettings settings, string token)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            settings.SessionToken = token;
            return(settings);
        }
        private AmazonCloudFrontClient GetClient(CloudFrontSettings 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 AmazonCloudFrontClient(settings.AccessKey, settings.SecretKey, settings.SessionToken, settings.Region));
                }
                else
                {
                    return(new AmazonCloudFrontClient(settings.AccessKey, settings.SecretKey, settings.Region));
                }
            }
            else
            {
                return(new AmazonCloudFrontClient(settings.Credentials, settings.Region));
            }
        }
            /// <summary>
            /// Invalidates objects from a CloudFront distribution.
            /// </summary>
            /// <param name="distributionId">The distribution to invalidate objects from.</param>
            /// <param name="items">The path of the objects to invalidate.</param>
            /// <param name="reference">A unique name that ensures the request can't be replayed.</param>
            /// <param name="settings">The <see cref="CloudFrontSettings"/> required to connect to Amazon CloudFront.</param>
            public string CreateInvalidation(string distributionId, IList<string> items, string reference, CloudFrontSettings settings)
            {
                //Get Reference
                if (String.IsNullOrEmpty(reference))
                {
                    reference = DateTime.Now.Ticks.ToString();
                }



                //Correct Paths
                List<string> paths = new List<string>();

                foreach (string item in items)
                {
                    if (!item.StartsWith("/"))
                    {
                        paths.Add("/" + item);
                    }
                }



                //Create Request
                InvalidationBatch batch = new InvalidationBatch()
                {
                    Paths = new Paths()
                    {
                        Items = paths.ToList(),
                        Quantity = items.Count
                    },

                    CallerReference = reference
                };

                CreateInvalidationRequest request = new CreateInvalidationRequest()
                {
                     DistributionId = distributionId,
                     InvalidationBatch = batch
                };



                //Send Request
                _Log.Verbose("Create Invalidation {0}", distributionId);

                AmazonCloudFrontClient client = this.GetClient(settings);

                CreateInvalidationResponse response = client.CreateInvalidation(request);

                if (response.HttpStatusCode == System.Net.HttpStatusCode.Created)
                {
                    return response.Invalidation.Id;
                }
                else
                {
                    _Log.Error("Error invalidating object {0}", distributionId);

                    return "";
                }
            }
 public static void CreateInvalidation(this ICakeContext context, string distributionId, IList<string> items, string reference, CloudFrontSettings settings)
 {
     context.CreateManager().CreateInvalidation(distributionId, items, reference, settings);
 }
 public static void CreateInvalidation(this ICakeContext context, string distributionId, string item, CloudFrontSettings settings)
 {
     context.CreateManager().CreateInvalidation(distributionId, new List<string>() { item }, "", settings);
 }
 public static async Task <string> CreateInvalidation(this ICakeContext context, string distributionId, IList <string> items, string reference, CloudFrontSettings settings)
 {
     return(await context.CreateManager().CreateInvalidation(distributionId, items, reference, settings));
 }
 public static async Task <string> CreateInvalidation(this ICakeContext context, string distributionId, string item, CloudFrontSettings settings)
 {
     return(await context.CreateManager().CreateInvalidation(distributionId, new List <string>()
     {
         item
     }, "", settings));
 }
        /// <summary>
        /// Invalidates objects from a CloudFront distribution.
        /// </summary>
        /// <param name="distributionId">The distribution to invalidate objects from.</param>
        /// <param name="items">The path of the objects to invalidate.</param>
        /// <param name="reference">A unique name that ensures the request can't be replayed.</param>
        /// <param name="settings">The <see cref="CloudFrontSettings"/> required to connect to Amazon CloudFront.</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 <string> CreateInvalidation(string distributionId, IList <string> items, string reference, CloudFrontSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            //Get Reference
            if (String.IsNullOrEmpty(reference))
            {
                reference = DateTime.Now.Ticks.ToString();
            }



            //Correct Paths
            List <string> paths = new List <string>();

            foreach (string item in items)
            {
                if (!item.StartsWith("/"))
                {
                    paths.Add("/" + item);
                }
                else
                {
                    paths.Add(item);
                }
            }



            //Create Request
            InvalidationBatch batch = new InvalidationBatch()
            {
                Paths = new Paths()
                {
                    Items    = paths.ToList(),
                    Quantity = items.Count
                },

                CallerReference = reference
            };

            CreateInvalidationRequest request = new CreateInvalidationRequest()
            {
                DistributionId    = distributionId,
                InvalidationBatch = batch
            };



            //Send Request
            _Log.Verbose("Create Invalidation {0}", distributionId);

            AmazonCloudFrontClient client = this.GetClient(settings);

            CreateInvalidationResponse response = await client.CreateInvalidationAsync(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.Created)
            {
                return(response.Invalidation.Id);
            }
            else
            {
                _Log.Error("Error invalidating object {0}", distributionId);

                return("");
            }
        }