Exemplo n.º 1
0
        public async Task <IActionResult> PreviewConfiguration([FromBody] PreviewContainer previewOptions)
        {
            if (previewOptions is null)
            {
                return(BadRequest("no preview-data received"));
            }

            if (previewOptions.Environment is null)
            {
                return(BadRequest("no environment-data received"));
            }

            if (previewOptions.Structure is null)
            {
                return(BadRequest("no structure-data received"));
            }

            var environmentInfo = new EnvironmentCompilationInfo
            {
                Name = "Intermediate-Preview-Environment",
                Keys = await ResolveEnvironmentPreview(previewOptions.Environment)
            };

            var(structKeys, varKeys) = await ResolveStructurePreview(previewOptions.Structure);

            var structureInfo = new StructureCompilationInfo
            {
                Name      = "Intermediate-Preview-Structure",
                Keys      = structKeys,
                Variables = varKeys
            };

            var compiled = _compiler.Compile(environmentInfo,
                                             structureInfo,
                                             _parser);

            return(Ok(new PreviewResult
            {
                Map = compiled.CompiledConfiguration.ToDictionary(_ => _.Key, _ => _.Value),
                Json = _translator.ToJson(compiled.CompiledConfiguration),
                UsedKeys = compiled.GetUsedKeys().Where(key => environmentInfo.Keys.ContainsKey(key))
            }));
        }
Exemplo n.º 2
0
        private void CheckCompilationResult(
            IDictionary <string, string> keys,
            IDictionary <string, string> structKeys,
            IDictionary <string, string> structVars,
            IDictionary <string, string> secrets,
            Action <CompilationResult> assertions,
            Func <IConfigurationCompiler, IConfigurationParser, EnvironmentCompilationInfo, StructureCompilationInfo, CompilationResult> compileFunc = null,
            [CallerMemberName] string testName = null)
        {
            var provider = new ServiceCollection().AddLogging(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Warning))
                           .AddTransient <ISecretConfigValueProvider, TestSecretProvider>(p => new TestSecretProvider(secrets))
                           .BuildServiceProvider();

            var compilerLogger = provider.GetRequiredService <ILogger <ConfigurationCompiler> >();
            var resolverLogger = provider.GetRequiredService <ILogger <IValueResolver> >();
            var secretProvider = provider.GetRequiredService <ISecretConfigValueProvider>();

            IConfigurationCompiler compiler = new ConfigurationCompiler(secretProvider, compilerLogger, resolverLogger);
            IConfigurationParser   parser   = new AntlrConfigurationParser();

            var env = new EnvironmentCompilationInfo
            {
                Keys = new ReadOnlyDictionary <string, string>(keys),
                Name = $"{testName}-Environment"
            };

            var structure = new StructureCompilationInfo
            {
                Keys      = new ReadOnlyDictionary <string, string>(structKeys),
                Variables = new ReadOnlyDictionary <string, string>(structVars),
                Name      = $"{testName}-Structure"
            };

            var compiled = compileFunc?.Invoke(compiler, parser, env, structure) ?? compiler.Compile(env, structure, parser);

            assertions(compiled);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PreviewConfiguration([FromRoute] string environmentCategory,
                                                               [FromRoute] string environmentName,
                                                               [FromRoute] string structureName,
                                                               [FromRoute] int structureVersion)
        {
            var envId    = new EnvironmentIdentifier(environmentCategory, environmentName);
            var structId = new StructureIdentifier(structureName, structureVersion);

            var structureKeyResult = await _store.Structures.GetKeys(structId, QueryRange.All);

            if (structureKeyResult.IsError)
            {
                return(ProviderError(structureKeyResult));
            }

            var structureVariableResult = await _store.Structures.GetVariables(structId, QueryRange.All);

            if (structureVariableResult.IsError)
            {
                return(ProviderError(structureVariableResult));
            }

            var environmentResult = await _store.Environments.GetKeys(new EnvironmentKeyQueryParameters
            {
                Environment = envId,
                Range       = QueryRange.All
            });

            if (environmentResult.IsError)
            {
                return(ProviderError(environmentResult));
            }

            var structureSnapshot   = structureKeyResult.Data;
            var variableSnapshot    = structureVariableResult.Data;
            var environmentSnapshot = environmentResult.Data;

            var environmentInfo = new EnvironmentCompilationInfo
            {
                Name = $"{envId.Category}/{envId.Name}",
                Keys = environmentSnapshot
            };
            var structureInfo = new StructureCompilationInfo
            {
                Name      = $"{structId.Name}/{structId.Version}",
                Keys      = structureSnapshot,
                Variables = variableSnapshot
            };

            try
            {
                var compiled = _compiler.Compile(environmentInfo,
                                                 structureInfo,
                                                 _parser);

                var json = _translator.ToJson(compiled.CompiledConfiguration);

                return(Ok(json));
            }
            catch (Exception e)
            {
                KnownMetrics.Exception.WithLabels(e.GetType().Name).Inc();
                Logger.LogError(e, "failed to add new environment at (" +
                                $"{nameof(environmentCategory)}: {environmentCategory}; " +
                                $"{nameof(environmentName)}: {environmentName}; " +
                                $"{nameof(structureName)}: {structureName}; " +
                                $"{nameof(structureVersion)}: {structureVersion})");
                return(Ok(JsonDocument.Parse("{}").RootElement));
            }
        }