├── LICENSE ├── README.md └── main.tf /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Levon Melikbekjan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # terraform-multidecoder-yaml_json 2 | Access multiple YAML and JSON files using their relative paths in a single step. 3 | 4 | ## Usage 5 | Place this module in the location where you need to access multiple YAML and JSON files from various paths. Provide your paths using the **filepaths** parameter, which accepts a set of string values representing the relative paths of the YAML and JSON files. If desired, you can rename the module. 6 | ``` 7 | module "yaml_json_decoder" { 8 | source = "levmel/yaml_json/multidecoder" 9 | version = "0.2.3" 10 | filepaths = ["routes/nsg_rules.yml", "failover/cosmosdb.json", "network/private_endpoints/*.yaml", "network/private_links/config_file.yml", "network/private_endpoints/*.yml", "pipeline/config/*.json"] 11 | } 12 | ``` 13 | 14 | ### Patterns for accessing YAML and JSON files using relative paths:: 15 | 16 | To access all YAML and JSON files within a folder, structure your path as follows: 17 | ```"folder/rest_of_folders/*.yaml"```, ```"folder/rest_of_folders/*.yml"``` 18 | or ```"folder/rest_of_folders/*.json"```. 19 | 20 | For specific YAML or JSON files within a directory, use the format: 21 | ```"folder/rest_of_folders/.yaml"```, ```"folder/rest_of_folders/.yml"``` or ```"folder/rest_of_folders/.json"``` 22 | 23 | To select all YAML and JSON files within a folder, use the following format notation: 24 | ```"*.yml", "*.yaml", "*.json"``` (Refer to the USAGE section above for more details.) 25 | 26 | ### Support for YAML delimiters is introduced in version 0.1.0! 27 | 28 | **WARNING:** Specify only the relative path. Do not include the path.root, as it's already incorporated within the module. Instead, provide everything that follows it. 29 | 30 | ## Access YAML and JSON Entries 31 | You can now access all entries within the selected YAML and JSON files using the format: **"module.yaml_json_decoder.files.[name_of_your_file].entry"**. For instance, if your file is named "name_of_your_config_file", access its entries with **"module.yaml_json_decoder.files.name_of_your_config_file.entry"**. 32 | 33 | 34 | ## Example of accessing multiple YAML and JSON files from various directories: 35 | ### first YAML file: 36 | routes/nsg_rules.yaml 37 | ``` 38 | rdp: 39 | name: rdp 40 | priority: 80 41 | direction: Inbound 42 | access: Allow 43 | protocol: Tcp 44 | source_port_range: "*" 45 | destination_port_range: 3399 46 | source_address_prefix: VirtualNetwork 47 | destination_address_prefix: "*" 48 | 49 | --- 50 | 51 | ssh: 52 | name: ssh 53 | priority: 70 54 | direction: Inbound 55 | access: Allow 56 | protocol: Tcp 57 | source_port_range: "*" 58 | destination_port_range: 24 59 | source_address_prefix: VirtualNetwork 60 | destination_address_prefix: "*" 61 | ``` 62 | ### second YAML file: 63 | services/logging/monitoring.yml 64 | ``` 65 | application_insights: 66 | application_type: other 67 | retention_in_days: 30 68 | daily_data_cap_in_gb: 20 69 | daily_data_cap_notifications_disabled: true 70 | logs: 71 | # Optional fields 72 | - "AppMetrics" 73 | - "AppAvailabilityResults" 74 | - "AppEvents" 75 | - "AppDependencies" 76 | - "AppBrowserTimings" 77 | - "AppExceptions" 78 | - "AppExceptions" 79 | - "AppPerformanceCounters" 80 | - "AppRequests" 81 | - "AppSystemEvents" 82 | - "AppTraces" 83 | ``` 84 | ### first JSON file: 85 | test/config/json_history.json 86 | ``` 87 | { 88 | "glossary": { 89 | "title": "example glossary", 90 | "GlossDiv": { 91 | "title": "S", 92 | "GlossList": { 93 | "GlossEntry": { 94 | "ID": "SGML", 95 | "SortAs": "SGML", 96 | "GlossTerm": "Standard Generalized Markup Language", 97 | "Acronym": "SGML", 98 | "Abbrev": "ISO 8879:1986", 99 | "GlossDef": { 100 | "para": "A meta-markup language, used to create markup languages such as DocBook.", 101 | "GlossSeeAlso": ["GML", "XML"] 102 | }, 103 | "GlossSee": "markup" 104 | } 105 | } 106 | } 107 | } 108 | } 109 | 110 | ``` 111 | 112 | main.tf 113 | ``` 114 | module "yaml_json_multidecoder" { 115 | source = "levmel/yaml_json/multidecoder" 116 | version = "0.2.3" 117 | filepaths = ["routes/nsg_rules.yaml", "services/logging/monitoring.yml", test/config/*.json] 118 | } 119 | 120 | output "nsg_rules_entry" { 121 | value = module.yaml_json_multidecoder.files.nsg_rules.aks.ssh.source_address_prefix 122 | } 123 | 124 | output "application_insights_entry" { 125 | value = module.yaml_json_multidecoder.files.monitoring.application_insights.daily_data_cap_in_gb 126 | } 127 | 128 | output "json_history" { 129 | value = module.yaml_json_multidecoder.files.json_history.glossary.title 130 | } 131 | ``` 132 | 133 | --- 134 | Changes to Outputs: 135 | + nsg_rules_entry = "VirtualNetwork" 136 | + application_insights_entry = 20 137 | + json_history = "example glossary" 138 | 139 | 140 | 141 | Buy me a coffee on PayPal if you find my code useful and it has made your life easier: 142 | ```levonmelikbekjan@yahoo.de``` -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 1.0" 3 | 4 | required_providers { 5 | local = { 6 | source = "hashicorp/local" 7 | version = "~> 2.1" 8 | } 9 | } 10 | } 11 | 12 | variable "filepaths" { 13 | description = "Set of strings representing relative paths to YAML and JSON files, including their file extensions." 14 | type = set(string) 15 | } 16 | 17 | data "local_file" "yaml_json_standard" { 18 | for_each = toset(flatten([for relative_path in var.filepaths : fileset(path.root, relative_path)])) 19 | filename = "${path.root}/${each.value}" 20 | } 21 | 22 | output "files" { 23 | description = "Assignment and formatting of filenames to the corresponding file content." 24 | value = {for i, file in data.local_file.yaml_json_standard : 25 | element(split(".", replace(basename(file.filename), "\\.(yaml|yml|json)$", "")), 0) => 26 | try(yamldecode(join("", split("---", file.content))), {})} 27 | } 28 | --------------------------------------------------------------------------------