in rostran/providers/ros/template.py [0:0]
def transform(self, target_path: str = None, single_file: bool = False):
"""
transform ros to Terraform
"""
if not target_path:
target_path = os.getcwd() + "/terraform/alicloud"
output_dir = Path(target_path)
if not output_dir.exists():
output_dir.mkdir(parents=True, exist_ok=True)
if not output_dir.is_dir():
typer.secho(
f"{target_path} is not a directory",
fg="red",
)
raise InvalidTargetPath(target_path=target_path, reason=f"{target_path} is not a directory")
typer.secho(f"Transforming ROS template to terraform ...")
conditions = self._transform_conditions()
parameters = self._transform_parameters()
resources = self._transform_resources()
outputs = self._transform_outputs()
if single_file:
file_path = (output_dir / "main.tf").resolve()
with file_path.open("w", encoding="utf-8") as f:
for block in conditions + parameters + resources + outputs:
contents = block.render()
f.write(contents)
f.write("\n\n")
tf_files = [file_path]
else:
tf_blocks = (('local', conditions), ('variable', parameters), ('main', resources), ('output', outputs))
tf_files = []
for block_name, blocks in tf_blocks:
if not blocks:
continue
file_name = f"{block_name}.tf"
file_path = (output_dir / file_name).resolve()
with file_path.open("w", encoding="utf-8") as f:
for block in blocks:
contents = block.render()
f.write(contents)
f.write("\n\n")
tf_files.append(file_path)
typer.secho(f"Transform successful!\n")
try:
tf_command = TerraformCommand(os.path.abspath(output_dir))
tf_command.fmt(check=True, no_color=False, write=True)
if not single_file:
for file_path in tf_files:
with file_path.open("r", encoding="utf-8") as f:
typer.secho(f.read(), fg="green")
except TerraformCommandError:
if not single_file:
typer.secho(f"Terraform fmt failed, the original content will be displayed.\n", fg="yellow")
for item in conditions + parameters + resources + outputs:
typer.secho(item, fg="green")