Skip to content

Commit

Permalink
RD-5504-Enhance-Infracost-Workflow (#208)
Browse files Browse the repository at this point in the history
* RD-5504-Enhance-Infracost-Workflow

* RD-5504-Add-More-Comments
  • Loading branch information
ahmadiesa-abu authored Aug 2, 2022
1 parent d00b96e commit fa102e8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.19.1:
- Change the infracost_config workflow param to not be required
- hide the api_key inside infracost_config runtime_property
- handle executable path sent as None
- resolve terraform root module as it could have source_path as relative `.`
0.19.0:
- Bump Infracost version to bring back tfvars json format support.
- New update workflow.
Expand Down
43 changes: 37 additions & 6 deletions cloudify_tf/terraform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from cloudify import exceptions as cfy_exc
from cloudify_common_sdk.cli_tool_base import CliTool
from cloudify_common_sdk.utils import run_subprocess, update_dict_values
from cloudify_common_sdk.secure_property_management import get_stored_property

from .. import utils

Expand Down Expand Up @@ -574,6 +575,29 @@ def from_ctx(ctx, terraform_source, skip_tf=False, **kwargs):
if not terraform_version and not skip_tf:
ctx.instance.runtime_properties['terraform_version'] = \
tf.version
# handle api_key property in runtime
if 'infracost_config' in kwargs:
result = ''
api_key = \
get_stored_property(ctx, 'infracost_config',
force_node=True).get("api_key", {})
infracost_config_from_params = \
ctx.workflow_parameters.get('infracost_config', {})
api_key_param = ''
if isinstance(infracost_config_from_params, dict):
api_key_param = infracost_config_from_params.get('api_key')
# first check node property
if api_key:
result = \
utils.resolve_dict_intrinsic_vals(
api_key, ctx.deployment.id)
# override that from the workflow parameter
if api_key_param:
result = \
utils.resolve_dict_intrinsic_vals(
api_key_param, ctx.deployment.id)
kwargs['infracost_config']['api_key'] = result

setup_config_tf(ctx, tf, **kwargs)
return tf

Expand Down Expand Up @@ -660,7 +684,7 @@ def setup_config_tf(ctx,
tf.tfsec.export_config()

# Terratag
terratag_config_from_props = ctx.node.properties.get('terratag_config', )
terratag_config_from_props = ctx.node.properties.get('terratag_config', {})
original_terratag_config = \
ctx.instance.runtime_properties.get('terratag_config', {}) or \
terratag_config_from_props
Expand All @@ -682,13 +706,20 @@ def setup_config_tf(ctx,
ctx.instance.runtime_properties['terratag_config'] = \
tf.terratag.export_config()

# infracost
infracost_config_from_props = \
ctx.node.properties.get('infracost_config', {})
original_infracost_config = \
ctx.instance.runtime_properties.get('infracost_config', {}) or \
infracost_config_from_props
new_infracost_config = update_dict_values(
original_infracost_config, infracost_config)
if infracost_config or infracost_config_from_props.get('enable', False):
tf.infracost = Infracost.from_ctx(_ctx=ctx,
infracost_config=infracost_config,
variables=tf.variables,
env=tf.env,
tfvars=tf.tfvars)
tf.infracost = Infracost.from_ctx(
_ctx=ctx,
infracost_config=new_infracost_config,
variables=tf.variables,
env=tf.env,
tfvars=tf.tfvars)
ctx.instance.runtime_properties['infracost_config'] = \
tf.infracost.export_config()
10 changes: 7 additions & 3 deletions cloudify_tf/terraform/infracost.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import yaml
from os import path, remove
from pathlib import Path
from contextlib import contextmanager
from tempfile import NamedTemporaryFile

Expand Down Expand Up @@ -205,7 +206,8 @@ def infracost(self):
json_result = '{}'
command = [self.executable_path, 'breakdown', '--config-file']
with self.runtime_file() as f:
relative_path = f.replace(self.terraform_root_module, '')
root_path = str(Path(self.terraform_root_module).resolve())
relative_path = f.replace(root_path, '')
self.config = {
'version': 0.1,
'projects': [{
Expand All @@ -217,13 +219,15 @@ def infracost(self):
with self.config_file() as cf:
command.extend([cf, '--show-skipped', '--no-color'])
result = self.execute(command, self.terraform_root_module,
self.env, return_output=True)
convert_secrets(self.env),
return_output=True)
# injecting the executable_path because execute pops item 0
command.insert(0, self.executable_path)
command.remove('--show-skipped')
command.extend(['--format json'])
json_result = self.execute(command, self.terraform_root_module,
self.env, return_output=True)
convert_secrets(self.env),
return_output=True)
return result, json.loads(json_result)

def export_config(self):
Expand Down
9 changes: 7 additions & 2 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins:
tf:
executor: central_deployment_agent
package_name: cloudify-terraform-plugin
package_version: '0.19.0'
package_version: '0.19.1'

dsl_definitions:

Expand Down Expand Up @@ -571,5 +571,10 @@ workflows:
<<: *terraform_plan_params
infracost_config:
description: >
infracost specific config
infracost specific config, the value here could be one of these choices:
- `{}`: it will take the values from runtime-properties/properties
- default_value: it will check the changes against the runtime-properties/properties
if it has override values it will take them
Note about enable property it is not considered here as it is enabled by default
type: cloudify.types.terraform.infracost
default: {}
9 changes: 7 additions & 2 deletions plugin_1_4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins:
tf:
executor: central_deployment_agent
package_name: cloudify-terraform-plugin
package_version: '0.19.0'
package_version: '0.19.1'

dsl_definitions:

Expand Down Expand Up @@ -581,8 +581,13 @@ workflows:
<<: *terraform_plan_params
infracost_config:
description: >
infracost specific config
infracost specific config, the value here could be one of these choices:
- `{}`: it will take the values from runtime-properties/properties
- default_value: it will check the changes against the runtime-properties/properties
if it has override values it will take them
Note about enable property it is not considered here as it is enabled by default
type: cloudify.types.terraform.infracost
default: {}


blueprint_labels:
Expand Down
10 changes: 7 additions & 3 deletions v2_plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins:
tf:
executor: central_deployment_agent
package_name: cloudify-terraform-plugin
package_version: '0.19.0'
package_version: '0.19.1'

dsl_definitions:

Expand Down Expand Up @@ -571,9 +571,13 @@ workflows:
<<: *terraform_plan_params
infracost_config:
description: >
infracost specific config
infracost specific config, the value here could be one of these choices:
- `{}`: it will take the values from runtime-properties/properties
- default_value: it will check the changes against the runtime-properties/properties
if it has override values it will take them
Note about enable property it is not considered here as it is enabled by default
type: cloudify.types.terraform.infracost

default: {}

blueprint_labels:
obj-type:
Expand Down

0 comments on commit fa102e8

Please sign in to comment.