dda-python-terraform/dda_python_terraform/tfstate.py

36 lines
1,015 B
Python
Raw Permalink Normal View History

2023-01-27 10:19:03 +00:00
"""Helper Module providing wrapper for terraform state."""
import json
import logging
import os
from typing import Dict, Optional
logger = logging.getLogger(__name__)
class Tfstate:
2023-01-27 10:19:03 +00:00
"""Class representing a terraform state"""
def __init__(self, data: Optional[Dict[str, str]] = None):
self.tfstate_file: Optional[str] = None
self.native_data = data
if data:
self.__dict__ = data
@staticmethod
def load_file(file_path: str) -> "Tfstate":
"""Read the tfstate file and load its contents.
Parses then as JSON and put the result into the object.
"""
logger.debug("read data from %s", file_path)
if os.path.exists(file_path):
2023-01-27 10:19:03 +00:00
with open(file_path, encoding="utf-8") as fle:
json_data = json.load(fle)
tf_state = Tfstate(json_data)
tf_state.tfstate_file = file_path
return tf_state
logger.debug("%s does not exist", file_path)
return Tfstate()