Пример #1
0
            private async Task ProcessTaskAsync(
                AssessmentTask taskDefinition,
                TelemetryContext telemetryContext,
                string accessToken)
            {
                var telemetryManager = TelemetryHelper.CreateTelemetryManager(telemetryContext);

                var context = AssessmentHelper.GetEnvironmentContext(
                    telemetryManager,
                    accessToken,
                    ConfigHelper.ResourceManagerEndpoint,
                    taskDefinition.SubscriptionId);

                var assessmentService = new AssessmentService();

                var assessmentReport = await assessmentService.GenerateReportAsync(
                    context,
                    taskDefinition.TargetRegion);

                var reportId = DataAccess.SaveReport(
                    taskDefinition.TenantId,
                    taskDefinition.UserObjectId,
                    assessmentReport.ReportFilePath);

                File.Delete(assessmentReport.ReportFilePath);

                TelemetryHelper.LogInformation(
                    FormattableString.Invariant($"Saved report {reportId} to database and storage account."),
                    telemetryContext);

                DataAccess.UpdateTaskStatusCompleted(
                    taskDefinition.Id,
                    reportId);

                TokenStore.Instance.RemoveTokenWrapperByTaskId(taskDefinition.Id);

                TelemetryHelper.LogInformation(
                    @"Updated task status to completed.",
                    telemetryContext);

                TelemetryHelper.WriteEvent(
                    TelemetryEventNames.TaskEnd,
                    telemetryContext);

                TelemetryHelper.LogVerbose(@"Telemetry flush.", telemetryContext);
                TelemetryHelper.Flush();

                var flushWaitingTime = TimeSpan.FromSeconds(60);

                TelemetryHelper.LogVerbose(
                    FormattableString.Invariant($"Waiting flush for {flushWaitingTime}."),
                    telemetryContext);
                Thread.Sleep(flushWaitingTime);
            }
Пример #2
0
        private static async Task RunAsync(Options opts, string usageReportContent)
        {
            var azureEnvironmentHelper = new AzureEnvironmentHelper(opts.AzureEnvironmentName);

            ParsedUsageReport usageReport = null;
            string            accessToken = null;

            if (usageReportContent == null)
            {
                var authenticationResult = await TokenProvider.AcquireTokenAsync(
                    azureEnvironmentHelper.AuthenticationEndpoint,
                    opts.TenantId,
                    azureEnvironmentHelper.ResourceManagerEndpoint);

                accessToken = authenticationResult.AccessToken;
            }
            else
            {
                usageReport = UsageFileHelper.Parse(usageReportContent);
            }

            var context = new AssessmentContext(
                usageReport: usageReport,
                subscriptionNames: opts.SubscriptionNames,
                subscriptionIds: opts.SubscriptionIds,
                resourceGroupNames: opts.ResourceGroupNames,
                accessToken: accessToken,
                armBaseUri: azureEnvironmentHelper.ResourceManagerEndpoint,
                resourceCachePath: opts.ResourceCachePath,
                configManager: ConfigManagerFactory.CreateStorageAccountConfigManager(Settings.Default.ConnectionString),
                telemetryManager: new TelemetryManager(
                    new AppInsightsTelemetryProvider(ConfigurationManager.AppSettings["ApplicationInsightsKey"]),
                    new Dictionary <string, string>
            {
                { "DeploymentId", $"console_debug_{Environment.MachineName}" }
            }));

            var assessmentService = new AssessmentService();
            var assessmentReport  = await assessmentService.GenerateReportAsync(context, opts.TargetLocation);

            if (File.Exists(assessmentReport.ReportFilePath))
            {
                Process.Start(assessmentReport.ReportFilePath);
            }
        }
Пример #3
0
        public async Task <ActionResult> Run(AssessmentWithUsageReportModel model)
        {
            try
            {
                if (model.UsageReportFile == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Please upload a file"));
                }

                var inputFileName  = model.UsageReportFile.FileName;
                var outputFileName = $"{Path.GetFileNameWithoutExtension(inputFileName)}-AssessmentReport.pdf";

                string inputContent;
                using (var reader = new StreamReader(model.UsageReportFile.InputStream))
                {
                    inputContent = await reader.ReadToEndAsync();
                }

                var context = AssessmentHelper.GetEnvironmentContext(
                    TelemetryHelper.CreateTelemetryManager(new TelemetryContext
                {
                    Properties = new Dictionary <string, string>
                    {
                        { "UsageReportFileName", model.UsageReportFile.FileName },
                        { "TargetRegion", model.SelectedTargetRegion }
                    }
                }),
                    UsageFileHelper.Parse(inputContent));

                var assessmentService = new AssessmentService();
                var assessmentReport  = await assessmentService.GenerateReportAsync(context, model.SelectedTargetRegion);

                var outputContent = System.IO.File.ReadAllBytes(assessmentReport.ReportFilePath);
                System.IO.File.Delete(assessmentReport.ReportFilePath);

                return(this.File(outputContent, "application/pdf", outputFileName));
            }
            catch (Exception ex)
            {
                Trace.TraceError(FormattableString.Invariant($"Assessment with usage report failed: {ex}"));
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, ex.Message));
            }
        }