예제 #1
0
        public static async Task Run(
            [TimerTrigger("0 0 */1 * * *")] TimerInfo timerInfo,
            [CosmosDB(
                 databaseName: Webhook.DatabaseName,
                 collectionName: Webhook.CollectionName,
                 ConnectionStringSetting = "CosmosDBConnectionString")] DocumentClient documentClient,
            ILogger log,
            ExecutionContext context)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var dimensionConfigs = JsonConvert.DeserializeObject <DimensionConfig[]>(config["DIMENSION_CONFIG"]);

            log.LogTrace($"Dimension configs: {JsonConvert.SerializeObject(dimensionConfigs)}");

            using (var httpClient = HttpClientFactory.Create())
            {
                var token = await ChronJob.GetToken(httpClient, config, log).ConfigureAwait(continueOnCapturedContext: false);

                httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

                using (var queryable = documentClient.CreateDocumentQuery <BillingEntry>(UriFactory.CreateDocumentCollectionUri(Webhook.DatabaseName, Webhook.CollectionName)).AsDocumentQuery())
                {
                    while (queryable.HasMoreResults)
                    {
                        foreach (var billingEntry in await queryable.ExecuteNextAsync <BillingEntry>().ConfigureAwait(continueOnCapturedContext: false))
                        {
                            foreach (var dimensionConfig in dimensionConfigs)
                            {
                                var response = await ChronJob.EmitUsageEvents(config, httpClient, dimensionConfig, billingEntry).ConfigureAwait(continueOnCapturedContext: false);

                                var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);

                                if (response.IsSuccessStatusCode)
                                {
                                    log.LogTrace($"Successfully emitted a usage event. Reponse body: {responseBody}");
                                }
                                else
                                {
                                    log.LogError($"Failed to emit a usage event. Error code: {response.StatusCode}. Failure cause: {response.ReasonPhrase}. Response body: {responseBody}");
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        public static async Task Run(
            [TimerTrigger("0 0 */1 * * *")] TimerInfo timerInfo,
            ILogger log,
            ExecutionContext context)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var dimensionConfigs = JsonConvert.DeserializeObject <DimensionConfig[]>(config["DIMENSION_CONFIG"]);

            log.LogTrace($"Dimension configs: {JsonConvert.SerializeObject(dimensionConfigs)}");

            using (var armHttpClient = HttpClientFactory.Create())
            {
                var armToken = await ChronJob.GetToken(config, armHttpClient, log, "https://management.core.windows.net/").ConfigureAwait(continueOnCapturedContext: false);

                armHttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {armToken}");

                var applicationResourceId = await ChronJob.GetResourceGroupManagedBy(config, armHttpClient, log).ConfigureAwait(continueOnCapturedContext: false);

                var application = await ChronJob.GetApplication(applicationResourceId, config, armHttpClient, log).ConfigureAwait(continueOnCapturedContext: false);

                if (application != null)
                {
                    log.LogInformation($"Authorization bearer token: {armToken}");
                    log.LogInformation($"Resource usage id: {application.Properties.BillingDetails?.ResourceUsageId}");
                    log.LogInformation($"Plan name: {application.Plan.Name}");

                    foreach (var dimensionConfig in dimensionConfigs)
                    {
                        var response = await ChronJob.EmitUsageEvents(config, armHttpClient, dimensionConfig, application.Properties.BillingDetails?.ResourceUsageId, application.Plan.Name).ConfigureAwait(continueOnCapturedContext: false);

                        var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);

                        if (response.IsSuccessStatusCode)
                        {
                            log.LogTrace($"Successfully emitted a usage event. Reponse body: {responseBody}");
                        }
                        else
                        {
                            log.LogError($"Failed to emit a usage event. Error code: {response.StatusCode}. Failure cause: {response.ReasonPhrase}. Response body: {responseBody}");
                        }
                    }
                }
            }
        }