Merge pull request #21 from beelit94/feature/minior_refactor

minor refactor
This commit is contained in:
beelit94 2017-08-30 11:06:37 -07:00 committed by GitHub
commit 042868bada

View file

@ -84,7 +84,8 @@ class Terraform(object):
return wrapper
def apply(self, dir_or_plan=None, input=False, no_color=IsFlagged, **kwargs):
def apply(self, dir_or_plan=None, input=False, no_color=IsFlagged,
**kwargs):
"""
refer to https://terraform.io/docs/commands/apply.html
no-color is flagged by default
@ -143,21 +144,19 @@ class Terraform(object):
return self.cmd('plan', *args, **options)
def init(self, dir_or_plan=None, backend_config=None,
reconfigure=IsFlagged, force_copy=IsNotFlagged, backend=True,
**kwargs):
reconfigure=IsFlagged, backend=True, **kwargs):
"""
refer to https://www.terraform.io/docs/commands/init.html
By default, this assumes you want to use backend config, and tries to
init fresh. The flags -reconfigure and -backend=true are default.
:param dir_or_plan: relative path to the folder want to init
:param backend_config: a dictionary of backend config options. eg.
t = Terraform()
t.init(backend_config={'access_key': 'myaccesskey',
'secret_key': 'mysecretkey', 'bucket': 'mybucketname'})
:param reconfigure: whether or not to force reconfiguration of backend
:param force_copy: whether or not to migrate from the previous backend
settings to the new backend settings
:param backend: whether or not to use backend settings for init
:param kwargs: options
:return: ret_code, stdout, stderr
@ -165,7 +164,6 @@ class Terraform(object):
options = kwargs
options['backend_config'] = backend_config
options['reconfigure'] = reconfigure
options['force_copy'] = force_copy
options['backend'] = backend
options = self._generate_default_options(options)
args = self._generate_default_args(dir_or_plan)
@ -198,40 +196,40 @@ class Terraform(object):
cmds = cmd.split()
cmds = [self.terraform_bin_path] + cmds
for k, v in kwargs.items():
if '_' in k:
k = k.replace('_', '-')
for option, value in kwargs.items():
if '_' in option:
option = option.replace('_', '-')
if type(v) is list:
for sub_v in v:
cmds += ['-{k}={v}'.format(k=k, v=sub_v)]
if type(value) is list:
for sub_v in value:
cmds += ['-{k}={v}'.format(k=option, v=sub_v)]
continue
if type(v) is dict:
if 'backend-config' in k:
for bk, bv in v.items():
if type(value) is dict:
if 'backend-config' in option:
for bk, bv in value.items():
cmds += ['-backend-config={k}={v}'.format(k=bk, v=bv)]
continue
# since map type sent in string won't work, create temp var file for
# variables, and clean it up later
else:
filename = self.temp_var_files.create(v)
filename = self.temp_var_files.create(value)
cmds += ['-var-file={0}'.format(filename)]
continue
# simple flag,
if v is IsFlagged:
cmds += ['-{k}'.format(k=k)]
if value is IsFlagged:
cmds += ['-{k}'.format(k=option)]
continue
if v is None or v is IsNotFlagged:
if value is None or value is IsNotFlagged:
continue
if type(v) is bool:
v = 'true' if v else 'false'
if type(value) is bool:
value = 'true' if value else 'false'
cmds += ['-{k}={v}'.format(k=k, v=v)]
cmds += ['-{k}={v}'.format(k=option, v=value)]
cmds += args
return cmds
@ -321,7 +319,8 @@ class Terraform(object):
file_path = file_path or self.state or ''
if not file_path:
backend_path = os.path.join(file_path, '.terraform', 'terraform.tfstate')
backend_path = os.path.join(file_path, '.terraform',
'terraform.tfstate')
if os.path.exists(os.path.join(working_dir, backend_path)):
file_path = backend_path
@ -344,7 +343,8 @@ class VariableFiles(object):
with tempfile.NamedTemporaryFile('w+t', delete=False) as temp:
log.debug('{0} is created'.format(temp.name))
self.files.append(temp)
log.debug('variables wrote to tempfile: {0}'.format(str(variables)))
log.debug(
'variables wrote to tempfile: {0}'.format(str(variables)))
temp.write(json.dumps(variables))
file_name = temp.name