Example #1
0
        public string Build(string tfstate, string res)
        {
            var state = new TerraformState(new StateFileContent(tfstate));
            var sqlServerResourceId = state.GetDependencies(res).First(z => z.Contains("azurerm_sql_server"));
            var server       = state.GetResourceByPath(sqlServerResourceId + ".fully_qualified_domain_name");
            var user         = state.GetResourceByPath(sqlServerResourceId + ".administrator_login");
            var pwd          = state.GetResourceByPath(sqlServerResourceId + ".administrator_login_password");
            var databaseName = state.GetResourceByPath(res + ".name");

            return($"Server=tcp:{server},1433;Initial Catalog={databaseName};Persist Security Info=False;User ID={user};Password={pwd};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
        }
        public void Execute(CommandLineApplication command)
        {
            //public async System.Threading.Tasks.Task GetFileContent(string path, string container,string localFolder)
            command.Description = "Get attribute from terraform config";
            command.HelpOption("-?|-h|--help");
            var attribute = command.Argument("attribute", "Attribute from terraform resource");

            var tfstate = command.Option("-t|--tfstate", "Teraform state file", CommandOptionType.SingleValue);
            var res     = command.Option("-k|--resourcekey", "Teraform resource key file", CommandOptionType.SingleValue);
            var file    = command.Option("-s|--save", "Save as file", CommandOptionType.SingleValue);

            command.OnExecute(async() =>
            {
                var state          = new TerraformState(new StateFileContent(tfstate.HasValue() ? tfstate.Value() : null));
                var attributeValue = attribute.Value;
                if (res.HasValue())
                {
                    attributeValue = res.Value() + attribute.Value;
                }
                var content = state.GetResourceByPath(attributeValue);
                if (file.HasValue())
                {
                    await File.WriteAllTextAsync(file.Value(), content);
                }
                else
                {
                    Console.WriteLine(content);
                }
                return(0);
            });
        }
Example #3
0
        public void Replace(string path, TerraformState state, string tag, bool print_on_output)
        {
            var file         = File.ReadAllText(path);
            var matches      = Regex.Matches(file, tag + "([0-9a-z_.-]*)");
            var replacements = new List <Tuple <string, string> >();

            if (matches.Count == 0)
            {
                return;
            }
            if (print_on_output == false)
            {
                Console.WriteLine("Replacing " + path);
            }
            string itemToReplace = "";

            foreach (Match item in matches)
            {
                try
                {
                    itemToReplace = item.Groups[1].Value;
                    var res = state.GetResourceByPath(itemToReplace);
                    replacements.Add(new Tuple <string, string>(tag + itemToReplace, res));
                }catch (Exception exc)
                {
                    if (print_on_output == false)
                    {
                        Console.WriteLine($"Provided key = {itemToReplace} doesn't exists");
                    }
                }
            }
            foreach (var rep in replacements)
            {
                file = file.Replace(rep.Item1, rep.Item2);
            }
            if (print_on_output == false)
            {
                File.WriteAllText(path, file);
            }
        }