Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Objects in Variables #71

Open
cfisher440 opened this issue Mar 24, 2023 · 2 comments
Open

Nested Objects in Variables #71

cfisher440 opened this issue Mar 24, 2023 · 2 comments

Comments

@cfisher440
Copy link

I'm creating a projects.auto.tfvars file.
I used the following command to create an empty object
hcledit -f test.tf -u attribute append testing '{}'
This successfully creates
testing = {}
But now I'd like to create the following

testing = {
  testing2 = {}
}

I tried this with the following command but get no output
hcledit -f test.tf -u attribute append testing.testing2 '{}'
Am I possibly just doing something wrong or is this a feature you'd need to implement into the code?

@thelumlaa
Copy link

@cfisher440 @minamijoyo did you manage to find a solution?
I am trying to manipulate a tfvars file that contains tags in the following format:
rg_tags = { cost_center = "123" rg_description = "text" app_contact = "12" tech_contact = "23" }

the following query returns rg_tags value:

hcledit attribute get -f .\file.tfvars rg_tags

But this returns an empty string
hcledit attribute get -f .\file.tfvars rg_tags.tech_contact

@cfisher440
Copy link
Author

cfisher440 commented Dec 4, 2023

@thelumlaa In my case, since the hcledit tool was not working for me, I ended up going a different route. What I did is I created what I wanted the tfvars file to look like in a json format. I then created a Python script that converts a json file to a tfvars file. Here is the script you can use:

import json

tfvars_json_file = 'projects.auto.tfvars.json'
tfvars_file = 'projects.auto.tfvars'

def format_dict(data, indent=0):
    result = ""
    for key, value in data.items():
        if isinstance(value, dict):
            result += " " * (indent * 2) + "{} = {{\n".format(key)
            result += format_dict(value, indent + 1)
            result += " " * (indent * 2) + "}\n"
        else:
            result += " " * (indent * 2) + "{} = {}\n".format(key, json.dumps(value))
    return result

with open(tfvars_json_file, 'r') as f:
    data = json.load(f)

formatted_data = format_dict(data)

with open(tfvars_file, 'w') as f:
    f.write(formatted_data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants