Skip to content

Instantly share code, notes, and snippets.

@tormath1
Created December 7, 2020 15:12
Show Gist options
  • Save tormath1/a30a1b6c13540f4daa764ba65dedbe73 to your computer and use it in GitHub Desktop.
Save tormath1/a30a1b6c13540f4daa764ba65dedbe73 to your computer and use it in GitHub Desktop.
import json
import sys
if len(sys.argv) < 3:
print(f"usage: {sys.argv[0]} resources.tfstate resources12.tfstate")
exit(1)
source = sys.argv[1]
dest = sys.argv[2]
# lookup table of resources to convert
conversions = {
"azurerm_logic_app_action_custom": "aws_instance",
"azurerm_logic_app_trigger_custom": "aws_instance",
"azurerm_logic_app_workflow": "aws_instance",
"azurerm_virtual_desktop_application_group": "aws_instance",
"azurerm_virtual_desktop_host_pool": "aws_instance",
"azurerm_virtual_desktop_workspace": "aws_instance",
"azurerm_virtual_desktop_workspace_application_group_association": "aws_instance"
}
with open(source, "rb") as _fh:
resources = json.loads(_fh.read())
# replace the TF version from 13 to 12
resources['terraform_version'] = "0.12.28"
# in TF13, all dependencies modules starts with "module.<module-name>
module = "module.vdi."
for resource in resources.get("resources"):
# remove the keyword "module", it does not exist in TF12
del(resource['module'])
provider = resource.get("provider")
# remove "unknown provider" which makes fail the BE
if "random" in resource.get("provider", ""):
resources.get("resources").remove(resource)
continue
rt = resource.get("type")
if rt and conversions.get(rt):
resource["type"] = conversions.get(rt)
# TODO: use regex to extract only the "provider.<provider>"
resource["provider"] = "provider.azurerm"
instances = resource.get("instances", [])
for instance in instances:
dependencies = instance.get("dependencies", [])
for i in range(0, len(dependencies)):
dep = dependencies[i]
# in TF13, all dependencies modules starts with
# "module.<module-name>"
if dep.startswith(module):
dep = dep[len(module):]
rt = dep.split(".")[0]
if conversions.get(rt):
dep = f"{conversions.get(rt)}.{dep.split('.')[1]}"
elif dep.startswith("module.network."):
dep = dep[len("module.network."):]
# remove "random" dependencies
elif "random" in dep:
dependencies.remove(i)
continue
dependencies[i] = dep
with open(dest, "w") as _fh:
_fh.write(json.dumps(resources))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment