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

Variables are not serializable #65

Open
rgreinho opened this issue May 9, 2020 · 0 comments
Open

Variables are not serializable #65

rgreinho opened this issue May 9, 2020 · 0 comments
Assignees

Comments

@rgreinho
Copy link

rgreinho commented May 9, 2020

I am trying to add a secret to Google Secret Manager (GCP).

The secret will come from a variable defined in Terraform Enterprise (see https://www.hashicorp.com/blog/variable-management-in-terraform-cloud/ for more details).

However, referring to a Variable object prevents the stack to be serialized:

$ terraformpy
terraformpy - Processing: main.tf.py
terraformpy - Writing main.tf.json
Traceback (most recent call last):
  File "/private/tmp/terraformpy-bug/.venv/bin/terraformpy", line 10, in <module>
    sys.exit(main())
  File "/private/tmp/terraformpy-bug/.venv/lib/python3.8/site-packages/terraformpy/cli.py", line 46, in main
    json.dump(compile(), fd, indent=4, sort_keys=True)
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 179, in dump
    for chunk in iterable:
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  [Previous line repeated 1 more time]
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Variable is not JSON serializable

Context

Using python3 on OSX:

$ python3 --version
Python 3.8.2
$ system_profiler SPSoftwareDataType|grep macOS | xargs
System Version: macOS 10.15.4 (19E287)
$ pip list | grep -i terraformpy
terraformpy 1.3.0

Repro steps

The script:

from terraformpy import Resource
from terraformpy import Variable

v = Variable("my_secret", type="string", description="secret for my project")
s = Resource(
    "google_secret_manager_secret",
    "my_project_secret_id",
    provider="google-beta",
    secret_id="my_project_secret_id",
    replication=dict(automatic=True),
)
Resource(
    "google_secret_manager_secret_version",
    "my_project",
    provider="google-beta",
    secret=s.id,
    secret_data=v,
)

The steps:

mkdir -p /tmp/terraformpy-bug
cd /tmp/terraformpy-bug
pbpaste > main.tf.py
python3 -m venv .venv
source .venv/bin/activate
pip install terraformpy
terraformpy

Workaround

The workaround I am using is to manually "serialize" the variable:

secret_data="${var.my_secret}",

But that defeats the purpose of using a python object.

@borgstrom borgstrom self-assigned this May 11, 2020
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