public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("application started.."); try { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); log.LogInformation("requestBody : " + requestBody); GetCostAndUsageRequest costAndUsageRequest = JsonConvert.DeserializeObject <GetCostAndUsageRequest>(requestBody); //GetCostAndUsageRequest costAndUsageRequest = BuildAWSCostAndUsageRequest(data); log.LogInformation("Build JSON Completed and AWS call initiated.."); GetCostAndUsageResponse costAndUsageResponse = CallAWSCostAndUsageAPI(costAndUsageRequest); log.LogInformation("AWS call Completed.."); CanonicalResponse canonicalresponse = CreateCanonicalData(costAndUsageResponse); log.LogInformation("Canonical response completed.."); // Insert JSON Response into Cosmos DB ProcessCosmosDB cosmosDB = new ProcessCosmosDB(); log.LogInformation("DB initiated.."); await cosmosDB.GetStartedDemoAsync(canonicalresponse); log.LogInformation("processed response completed.."); return(new OkObjectResult(costAndUsageResponse)); } catch (Exception ex) { log.LogInformation("Error occured in AWS Cost and Usage " + ex.Message.ToString()); return(new BadRequestObjectResult(ex.Message.ToString())); } }
// public static async Task Main(string[] args) // { // try // { // Console.WriteLine("Beginning operations...\n"); // ProcessCosmosDB p = new ProcessCosmosDB(); // await p.GetStartedDemoAsync(response); // } // catch (CosmosException de) // { // Exception baseException = de.GetBaseException(); // Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de); // } // catch (Exception e) // { // Console.WriteLine("Error: {0}", e); // } // finally // { // Console.WriteLine("Application Ended"); // } // } public async Task GetStartedDemoAsync(CanonicalResponse canonicalresponse) { // Create a new instance of the Cosmos Client this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey); await this.CreateDatabaseAsync(); //ADD THIS PART TO YOUR CODE await this.CreateContainerAsync(); await this.AddItemsToContainerAsync(canonicalresponse); }
private static CanonicalResponse CreateCanonicalData(GetCostAndUsageResponse awsResponse) { var config = new MapperConfiguration(cfg => { cfg.CreateMap <Amazon.CostExplorer.Model.ResultByTime, ResultByTime>(); cfg.CreateMap <Amazon.CostExplorer.Model.DateInterval, DateInterval>(); cfg.CreateMap <Amazon.CostExplorer.Model.MetricValue, MetricValue>(); cfg.CreateMap <Amazon.CostExplorer.Model.Group, Group>(); cfg.CreateMap <Amazon.Runtime.ResponseMetadata, ResponseMetadata>(); cfg.CreateMap <GetCostAndUsageResponse, CanonicalResponse>(); }); IMapper mapper = config.CreateMapper(); CanonicalResponse genericUsage = mapper.Map <GetCostAndUsageResponse, CanonicalResponse>(awsResponse); genericUsage.id = Guid.NewGuid(); // genericUsage.id = awsResponse.ResponseMetadata.RequestId; genericUsage.Company = "Advantasure"; genericUsage.Product = "Symphony"; genericUsage.Provider = "AWS"; return(genericUsage); }
private async Task AddItemsToContainerAsync(CanonicalResponse canonicalresponse) { JObject js = JObject.Parse(JsonConvert.SerializeObject(canonicalresponse)); var addjsontoCosmosResponse = await this.container.CreateItemAsync(js, new PartitionKey(js.GetValue("id").ToString())); }