def _precheck(config, action): """ Run through preflight checklist before running terraform apply/destroy. Args: config: dictionary containing all variable settings required to run terraform with action: string (create | destroy) Returns: Nothing Raises: InvalidCommandException exception on unknown command. """ # Instantiate remote state only if: # - the terraform code isn't already checked out # - and we haven't already run 'terraform remote config...' if not os.path.isfile( os.path.join(config['tf_root'], '.terraform', 'terraform.tfstate')): log_msg = "Configuring terraform remote state in: {}" logger.debug(log_msg.format(config['tf_root'])) tf_command = tf.remote_state(config) return_code = utils.run_command(tf_command, cwd=config['tf_root']) if return_code != 0: raise BaseException("{} failed with code {}.".format( tf_command, return_code)) # Grab all the tf modules required tf_command = tf.get() utils.run_command(tf_command, cwd=config['tf_root']) # validate env_name utils.validate_env_name(config['env_name']) tf_command = tf.validate(config) utils.run_command(tf_command, cwd=config['tf_root']) # Push remote state push_or_pull = { 'create': tf.remote_push, 'plan': tf.remote_pull, 'destroy': tf.remote_pull, } try: tf_command = push_or_pull[action]() except KeyError: raise InvalidCommandException("Invalid Command: {}".format(action)) utils.run_command(tf_command, cwd=config['tf_root']) # Run Plan tf_command = tf.plan(config, action) utils.run_command(tf_command, cwd=config['tf_root']) return
def test_tf_plan_with_create_action(mock_config): expected = [ 'terraform', 'plan', "-var='aws_region=us-east-1'", '-var-file=/tmp/test_tmp_dir/vars.tf', ] mock_action = 'create' result = tf.plan(mock_config, mock_action) assert result == expected return