dda-python-terraform/python_terraform/tfstate.py

33 lines
790 B
Python
Raw Normal View History

import json
import os
import logging
2020-10-19 21:57:22 +00:00
logger = logging.getLogger(__name__)
2020-10-19 21:57:22 +00:00
class Tfstate:
def __init__(self, data=None):
self.tfstate_file = None
self.native_data = data
if data:
self.__dict__ = data
@staticmethod
def load_file(file_path):
2020-10-19 21:57:22 +00:00
"""Read the tfstate file and load its contents.
Parses then as JSON and put the result into the object.
"""
2020-10-19 21:57:22 +00:00
logger.debug('read data from %s', file_path)
if os.path.exists(file_path):
with open(file_path) as f:
json_data = json.load(f)
tf_state = Tfstate(json_data)
tf_state.tfstate_file = file_path
return tf_state
2020-10-19 21:57:22 +00:00
logger.debug('%s is not exist', file_path)
2020-10-19 21:57:22 +00:00
return Tfstate()