Skip to content

Commit

Permalink
Merge pull request #36 from CallyCo-io/fix/terraform-passthrough
Browse files Browse the repository at this point in the history
fix: Terraform Passthrough
  • Loading branch information
techman83 authored Dec 1, 2024
2 parents 63542a4 + a30fa57 commit b614cb5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/cally/cli/commands/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,35 @@ def write_template(config: CallyStackServiceConfig, output: Path):
default='/usr/bin/terraform',
help='Path to the terraform binary',
)
@click.option(
'--terraform-cache',
envvar='CALLY_TERRAFORM_CACHE',
type=click.Path(path_type=Path),
default=Path(Path.home().as_posix(), '.cache', 'cally'),
help='Path to the services\' terraform plan/provider cache',
)
@click.argument('args', nargs=-1, type=click.UNPROCESSED)
@click.pass_obj
def run_terraform(
config: CallyStackServiceConfig, terraform_path: str, args: Tuple[str, ...]
config: CallyStackServiceConfig,
terraform_path: str,
terraform_cache: Path,
args: Tuple[str, ...],
):
tf_cmd = terraform.Command(terraform_path=terraform_path, arguments=args)
with terraform.Action(service=config.config) as action:
action.synth_stack()
terraform_cache.mkdir(exist_ok=True)
with terraform.Command(
service=config.config,
terraform_path=terraform_path,
terraform_cache=terraform_cache,
arguments=args,
) as tf_cmd:
with terraform.Action(service=config.config) as action:
action.synth_stack()
Path('cdk.tf.json').write_text(action.print(), encoding='utf-8')
if not tf_cmd.success:
click.secho(message=tf_cmd.stderr, fg='red')
sys.exit(tf_cmd.returncode)
click.secho(tf_cmd.stdout)
click.secho(tf_cmd.stdout)


tf.add_command(print_template)
Expand Down
27 changes: 26 additions & 1 deletion src/cally/cli/tools/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,37 @@ def print(self) -> str:

class Command:
arguments: Tuple[str, ...]
service: CallyStackService
terraform_path: str
terraform_cache: Path
_result: subprocess.CompletedProcess

def __init__(self, terraform_path: str, arguments: Tuple[str, ...]) -> None:
def __init__(
self,
service: CallyStackService,
terraform_path: str,
terraform_cache: Path,
arguments: Tuple[str, ...],
) -> None:
self.terraform_path = terraform_path
self.terraform_cache = terraform_cache
self.arguments = arguments
self.service = service

def __enter__(self) -> 'Action':
self._cache_dir = Path(self.terraform_cache, self.service.name)
self._cache_dir.mkdir(exist_ok=True)
self._cwd = Path().cwd()
os.chdir(self._cache_dir)
return self

def __exit__(
self,
exc_type: Type[BaseException],
exc_value: BaseException,
traceback: TracebackType,
) -> None:
os.chdir(self._cwd)

@property
def result(self) -> subprocess.CompletedProcess:
Expand Down
19 changes: 19 additions & 0 deletions tests/cli/test_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ def test_empty_write(self):
},
)
class TfCommandTests(CallyTestHarness):
def setUp(self):
super().setUp()
self.env_patcher = mock.patch.dict(
os.environ,
{
**self.env_patcher.values,
"CALLY_TERRAFORM_CACHE": Path(self.working.name, '.cache').as_posix(),
},
)
self.env_patcher.start()

def test_terraform_version(self):
result = CliRunner().invoke(
tf, ['run', 'version', '--environment', 'test', '--service', 'test']
Expand All @@ -74,3 +85,11 @@ def test_terraform_error(self):
self.assertTrue(
result.output.startswith('Terraform has no command named "invalid-foo".')
)

def test_cached_output(self):
CliRunner().invoke(
tf, ['run', 'version', '--environment', 'test', '--service', 'test']
)
self.assertTrue(
Path(Path.home().as_posix(), '.cache', 'test', 'cdk.tf.json').exists()
)

0 comments on commit b614cb5

Please sign in to comment.