コード例 #1
0
        public async Task TestGetProduct_ElastiCache_SingleSkuFromOffer()
        {
            // ARRANGE
            string sku = "HBRQZSXXSY2DXJ77";

            PriceListClient client = new PriceListClient();

            GetProductRequest request = new GetProductRequest("AmazonElastiCache")
            {
                Format = Format.JSON
            };

            GetProductResponse response = await client.GetProductAsync(request);

            ProductOffer ecOffer = response.ProductOffer;

            // ACT

            IEnumerable <IGrouping <string, PricingTerm> > groupedTerms = ecOffer.Terms
                                                                          .SelectMany(x => x.Value) // Get all of the product item dictionaries from on demand and reserved
                                                                                                    //.Where(x => ApplicableProductSkus.Contains(x.Key)) // Only get the pricing terms for products we care about
                                                                          .SelectMany(x => x.Value) // Get all of the pricing term key value pairs
                                                                          .Select(x => x.Value)     // Get just the pricing terms
                                                                          .GroupBy(x => x.Sku);     // Put all of the same skus together

            IGrouping <string, PricingTerm> skuTerms = groupedTerms.First(x => x.Key.Equals(sku));

            // ASSERT
            Assert.True(skuTerms.Where(x => x.TermAttributes.PurchaseOption == PurchaseOption.ON_DEMAND).Count() == 1);
        }
コード例 #2
0
        public async Task TestGetProduct_AmazonRDS()
        {
            // ARRANGE
            PriceListClient   client  = new PriceListClient();
            GetProductRequest request = new GetProductRequest("AmazonRDS");

            // ACT
            GetProductResponse response = await client.GetProductAsync(request);

            // ASSERT
            Assert.True(!response.IsError());
            Assert.True(!String.IsNullOrEmpty(response.ServiceCode));
        }
コード例 #3
0
        public async Task TestGetProduct_ECS_FromJsonContentStream()
        {
            // ARRANGE
            PriceListClient client = new PriceListClient();

            GetProductRequest request = new GetProductRequest("AmazonECS")
            {
                Format = Format.JSON
            };

            // ACT
            GetProductResponse response = await client.GetProductAsync(request);

            ProductOffer offer = ProductOffer.FromJsonStream(response.Content);

            // ASSERT
            Assert.NotNull(offer);
        }
コード例 #4
0
        public async Task TestGetProduct_EC2()
        {
            // ARRANGE
            PriceListClient client = new PriceListClient();

            GetProductRequest request = new GetProductRequest("AmazonEC2")
            {
                Format = Format.JSON
            };

            // ACT
            GetProductResponse response = await client.GetProductAsync(request);

            ProductOffer ec2Offer = response.ProductOffer;

            // ASSERT
            Assert.NotNull(ec2Offer);
        }
コード例 #5
0
        public async Task TestGetProduct_AmazonRDS_CSV()
        {
            // ARRANGE
            PriceListClientConfig config = new PriceListClientConfig();

            PriceListClient client = new PriceListClient(config);

            GetProductRequest request = new GetProductRequest("AmazonRDS")
            {
                Format = Format.CSV
            };

            // ACT
            GetProductResponse response = await client.GetProductAsync(request);

            // ASSERT
            Assert.True(!String.IsNullOrEmpty(response.ServiceCode));
        }
コード例 #6
0
        public async Task TestGetProduct_AmazonDynamoDB_FromJsonContentStream()
        {
            // ARRANGE
            PriceListClient client = new PriceListClient();

            GetProductRequest request = new GetProductRequest("AmazonDynamoDB")
            {
                Format = Format.JSON
            };

            GetProductResponse response = await client.GetProductAsync(request);

            // ACT
            ProductOffer ddbOffer = ProductOffer.FromJsonStream(response.Content);

            // ASSERT
            Assert.NotNull(ddbOffer);
            Assert.True(!String.IsNullOrEmpty(ddbOffer.Version));
        }
コード例 #7
0
        public async Task TestGetProduct_ECS_FromJsonString()
        {
            // ARRANGE
            PriceListClient client = new PriceListClient();

            GetProductRequest request = new GetProductRequest("AmazonECS")
            {
                Format = Format.JSON
            };

            // ACT
            GetProductResponse response = await client.GetProductAsync(request);

            bool success = response.TryGetResponseContentAsString(out string productInfo);

            // ASSERT
            Assert.True(success);

            ProductOffer offer = ProductOffer.FromJson(productInfo);

            Assert.NotNull(offer);
        }
コード例 #8
0
        /// <summary>
        /// Executes the lambda function to get the price list data for the
        /// set of services we can buy reserved instances for
        /// </summary>
        /// <param name="ev"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task RunForServiceAsync(ServiceRequest req, ILambdaContext context)
        {
            _context = context;

            if (req == null || String.IsNullOrEmpty(req.Service))
            {
                string message = "No service was provided in the service request.";
                context.LogError(message);
                await SNSNotify(message, context);

                throw new Exception(message);
            }

            // Get the product price data for the service
            context.LogInfo($"Getting product data for {req.Service}");

            string bucket      = System.Environment.GetEnvironmentVariable("BUCKET");
            string delimiter   = System.Environment.GetEnvironmentVariable("DELIMITER");
            string inputFormat = System.Environment.GetEnvironmentVariable("PRICELIST_FORMAT");

            if (String.IsNullOrEmpty(inputFormat))
            {
                inputFormat = "csv";
            }

            inputFormat = inputFormat.ToLower().Trim();

            context.LogInfo($"Using price list format: {inputFormat}");

            if (String.IsNullOrEmpty(delimiter))
            {
                delimiter = defaultDelimiter;
            }

            // This holds the disposable stream and writer objects
            // that need to be disposed at the end
            List <IDisposable> disposables = new List <IDisposable>();

            try
            {
                // Will hold the stream of price data content that the
                // transfer utility will send
                MemoryStream memoryStreamOut = new MemoryStream();
                disposables.Add(memoryStreamOut);

                // Provided to the csv writer to write to the memory stream
                TextWriter streamWriter = new StreamWriter(memoryStreamOut);
                disposables.Add(streamWriter);

                // The csv writer to write the price data objects
                var config = new CsvConfiguration(CultureInfo.InvariantCulture)
                {
                    Delimiter = delimiter
                };

                CsvWriter csvWriter = new CsvWriter(streamWriter, config);
                disposables.Add(csvWriter);

                // Write the header to the csv
                csvWriter.WriteHeader <ReservedInstancePricingTerm>();
                csvWriter.NextRecord();

                // Create the product request with the right format
                GetProductRequest productRequest = new GetProductRequest(req.Service)
                {
                    Format = inputFormat.Equals("json", StringComparison.OrdinalIgnoreCase) ? Format.JSON : Format.CSV
                };

                context.LogInfo("Getting price list offer file.");

                // Retrieve the finished get product price data response
                GetProductResponse response = await priceListClient.GetProductAsync(productRequest);

                string service = response.ServiceCode;

                context.LogInfo("Parsing price list data.");

                // Fill the output stream
                await this.FillOutputStreamWriter(response.Content, csvWriter, productRequest.Format);

                // Make sure everything is written out since we don't dispose
                // of these till later, if the textwriter isn't flushed
                // you will lose content from the csv file
                csvWriter.Flush();
                streamWriter.Flush();

                response.Dispose();
                response = null;

                await UploadCsvToS3(memoryStreamOut, bucket, service, context);

                context.LogInfo("Completed upload");
            }
            catch (Exception e)
            {
                context.LogError(e);
                string message = $"[ERROR] {DateTime.Now} {{{context.AwsRequestId}}} : There was a problem executing lambda for service {req.Service} - {e.Message}\n{e.StackTrace}";
                await SNSNotify(message, context);

                throw e;
            }
            finally
            {
                // Dispose all of the streams and writers used to
                // write the CSV content, we need to dispose of these here
                // so the memory stream doesn't get closed by disposing
                // of the writers too early, which will cause the transfer utility
                // to fail the upload
                foreach (IDisposable item in disposables)
                {
                    try
                    {
                        item.Dispose();
                    }
                    catch { }
                }

                // Make sure memory is cleaned up
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }