def _parse_tfvars()

in blueprints/third-party-solutions/openshift/prepare.py [0:0]


def _parse_tfvars(tfvars=None, tfdir=None):
  'Parse vars and tfvars files and return variables.'
  logging.info('parsing tf variables')
  result = {}
  try:
    with open(os.path.join(tfdir, 'variables.tf')) as f:
      result = {k: v.get('default') for k, v in hcl.load(f)['variable'].items()}
    if tfvars:
      with open(os.path.join(tfdir, tfvars)) as f:
        result.update(hcl.load(f))
    else:
      logging.info('no tfvars file used')
  except (KeyError, ValueError) as e:
    raise Error(f'Wrong variable files syntax: {e}')
  except (IOError, OSError) as e:
    raise Error(f'Cannot open variable files: {e}')
  for k, v in result.items():
    if k == 'post_bootstrap_config':
      continue
    if v is None:
      raise Error(f'Terraform variable {k} not set.')
  return result