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