Skip to content

Commit

Permalink
Add terraform version controls
Browse files Browse the repository at this point in the history
Add globals to set minimum and latest Terraform versions.

TERRAFORM_VERSION_MINIMUM: The minimum required version. If there is an
existing binary that matches the minor version and is equal to or
greater than this value then a new binary will not be downloaded.

TERRAFORM_VERSION_LATEST:  The version that will be downloaded if there
is no existing binary, or if the existing binary does not meet the
minimum version requirement.
  • Loading branch information
jaywcarman committed May 19, 2021
1 parent e9ab41f commit cad0b2a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions plugins/module_utils/ibmcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,12 @@
from ansible.module_utils.six import iteritems
from ansible.module_utils.six import ensure_str
from ansible.module_utils.six import string_types
from packaging import version

DEFAULT_TF_DIR = '/var/tmp/ansible/ibmcloud/'
RM_OBJECT_SUBDIRS = True
TERRAFORM_VERSION_MINIMUM = '0.12.20'
TERRAFORM_VERSION_LATEST = '0.12.31'


def ibmcloud_terraform(
Expand Down Expand Up @@ -600,9 +603,7 @@ class Terraform:
parameters (dict): Resource parameter dictionary
terraform_dir (str): Terraform working directory
ibm_provider_version (str): IBM Cloud Terraform provider version
terraform_version (str, optional): Terraform version. TODO: If
not specified version is set
via table lookup.
terraform_version (str, optional): Terraform version.
env (dict, optional): Mapping of environment variables
"""
IBM_PROVIDER_BASE_URL = (
Expand Down Expand Up @@ -632,7 +633,7 @@ def __init__(
parameters,
terraform_dir,
ibm_provider_version,
terraform_version='0.12.20',
terraform_version=None,
env=None):

self.generation = None
Expand All @@ -649,7 +650,6 @@ def __init__(
self.function_namespace = parameters['function_namespace']
self.terraform_dir = terraform_dir
self.ibm_provider_version = ibm_provider_version
self.terraform_version = terraform_version
self.executable = os.path.join(terraform_dir, 'terraform')
self.env = env
self.platform = sys.platform
Expand Down Expand Up @@ -681,6 +681,20 @@ def tf_subdir_path():
except IndexError:
existing_version = None

# Set terraform version based on existing, minimum and latest values
if terraform_version is not None:
self.terraform_version = terraform_version
elif existing_version is None:
self.terraform_version = TERRAFORM_VERSION_LATEST
elif (version.parse(existing_version).minor !=
version.parse(TERRAFORM_VERSION_MINIMUM).minor):
self.terraform_version = TERRAFORM_VERSION_LATEST
elif (version.parse(existing_version) <
version.parse(TERRAFORM_VERSION_MINIMUM)):
self.terraform_version = TERRAFORM_VERSION_LATEST
else:
self.terraform_version = existing_version

# Download and install Terraform if desired version not found
if (not os.path.isfile(self.executable) or
existing_version != self.terraform_version):
Expand Down

0 comments on commit cad0b2a

Please sign in to comment.