2016-11-18 07:35:14 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2020-10-19 22:30:02 +00:00
|
|
|
import os
|
2021-01-21 01:01:34 +00:00
|
|
|
from typing import Dict, Optional
|
2016-11-18 07:35:14 +00:00
|
|
|
|
2020-10-19 21:57:22 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-11-18 07:35:14 +00:00
|
|
|
|
|
|
|
|
2020-10-19 21:57:22 +00:00
|
|
|
class Tfstate:
|
2021-01-21 01:01:34 +00:00
|
|
|
def __init__(self, data: Optional[Dict[str, str]] = None):
|
|
|
|
self.tfstate_file: Optional[str] = None
|
2016-11-18 07:35:14 +00:00
|
|
|
self.native_data = data
|
|
|
|
if data:
|
|
|
|
self.__dict__ = data
|
|
|
|
|
|
|
|
@staticmethod
|
2021-01-21 01:01:34 +00:00
|
|
|
def load_file(file_path: str) -> "Tfstate":
|
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.
|
2016-11-18 07:35:14 +00:00
|
|
|
"""
|
2020-10-19 22:30:02 +00:00
|
|
|
logger.debug("read data from %s", file_path)
|
2016-11-18 07:35:14 +00:00
|
|
|
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
|
|
|
|
|
2021-02-28 13:44:58 +00:00
|
|
|
logger.debug("%s does not exist", file_path)
|
2016-11-18 07:35:14 +00:00
|
|
|
|
2020-10-19 21:57:22 +00:00
|
|
|
return Tfstate()
|