fix most pylints

This commit is contained in:
jerger 2023-01-27 11:19:03 +01:00
parent c1c25a016f
commit a3d97a0d04
4 changed files with 21 additions and 16 deletions

View file

@ -30,7 +30,7 @@ pylint:
stage: lint stage: lint
allow_failure: true allow_failure: true
script: script:
- pylint -d C0301 dda_python_terraform/*.py - pylint -d C0112,C0115,C0301,R0913,R0903,R0902,R0914,R1705,R1732,W0622 dda_python_terraform/*.py
test-0.13.7: test-0.13.7:

View file

@ -1,3 +1,4 @@
"""Module providing wrapper for terraform."""
from .terraform import ( from .terraform import (
IsFlagged, IsFlagged,
IsNotFlagged, IsNotFlagged,

View file

@ -1,3 +1,4 @@
"""Module providing wrapper for terraform."""
import json import json
import logging import logging
import os import os
@ -30,8 +31,9 @@ CommandOutput = Tuple[Optional[int], Optional[str], Optional[str]]
class TerraformCommandError(subprocess.CalledProcessError): class TerraformCommandError(subprocess.CalledProcessError):
"""Class representing a terraform error"""
def __init__(self, ret_code: int, cmd: str, out: Optional[str], err: Optional[str]): def __init__(self, ret_code: int, cmd: str, out: Optional[str], err: Optional[str]):
super(TerraformCommandError, self).__init__(ret_code, cmd) super().__init__(ret_code, cmd)
self.out = out self.out = out
self.err = err self.err = err
logger.error("Error with command %s. Reason: %s", self.cmd, self.err) logger.error("Error with command %s. Reason: %s", self.cmd, self.err)
@ -76,7 +78,7 @@ class Terraform:
self.working_dir = working_dir self.working_dir = working_dir
self.state = state self.state = state
self.targets = [] if targets is None else targets self.targets = [] if targets is None else targets
self.variables = dict() if variables is None else variables self.variables = {} if variables is None else variables
self.parallelism = parallelism self.parallelism = parallelism
self.terraform_bin_path = ( self.terraform_bin_path = (
terraform_bin_path if terraform_bin_path else "terraform" terraform_bin_path if terraform_bin_path else "terraform"
@ -313,15 +315,15 @@ class Terraform:
if self.is_env_vars_included: if self.is_env_vars_included:
environ_vars = os.environ.copy() environ_vars = os.environ.copy()
p = subprocess.Popen( proc = subprocess.Popen(
cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars
) )
if not synchronous: if not synchronous:
return None, None, None return None, None, None
out, err = p.communicate() out, err = proc.communicate()
ret_code = p.returncode ret_code = proc.returncode
logger.info("output: %s", out) logger.info("output: %s", out)
if ret_code == 0: if ret_code == 0:
@ -486,8 +488,6 @@ class Terraform:
} }
def _generate_cmd_options(self, **kwargs) -> List[str]: def _generate_cmd_options(self, **kwargs) -> List[str]:
"""
"""
result = [] result = []
for option, value in kwargs.items(): for option, value in kwargs.items():
@ -501,13 +501,12 @@ class Terraform:
if isinstance(value, dict): if isinstance(value, dict):
if "backend-config" in option: if "backend-config" in option:
for bk, bv in value.items(): for backend_key, backend_value in value.items():
result += [f"-backend-config={bk}={bv}"] result += [f"-backend-config={backend_key}={backend_value}"]
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
elif option == "var": if option == "var":
# We do not create empty var-files if there is no var passed. # We do not create empty var-files if there is no var passed.
# An empty var-file would result in an error: An argument or block definition is required here # An empty var-file would result in an error: An argument or block definition is required here
if value: if value:
@ -546,10 +545,12 @@ class Terraform:
class VariableFiles: class VariableFiles:
"""Class representing a terraform var files"""
def __init__(self): def __init__(self):
self.files = [] self.files = []
def create(self, variables: Dict[str, str]) -> str: def create(self, variables: Dict[str, str]) -> str:
"""create var file in temp"""
with tempfile.NamedTemporaryFile( with tempfile.NamedTemporaryFile(
"w+t", suffix=".tfvars.json", delete=False "w+t", suffix=".tfvars.json", delete=False
) as temp: ) as temp:
@ -562,7 +563,8 @@ class VariableFiles:
return file_name return file_name
def clean_up(self): def clean_up(self):
for f in self.files: """cleanup the var file"""
os.unlink(f.name) for fle in self.files:
os.unlink(fle.name)
self.files = [] self.files = []

View file

@ -1,3 +1,4 @@
"""Helper Module providing wrapper for terraform state."""
import json import json
import logging import logging
import os import os
@ -7,6 +8,7 @@ logger = logging.getLogger(__name__)
class Tfstate: class Tfstate:
"""Class representing a terraform state"""
def __init__(self, data: Optional[Dict[str, str]] = None): def __init__(self, data: Optional[Dict[str, str]] = None):
self.tfstate_file: Optional[str] = None self.tfstate_file: Optional[str] = None
self.native_data = data self.native_data = data
@ -21,8 +23,8 @@ class Tfstate:
""" """
logger.debug("read data from %s", file_path) logger.debug("read data from %s", file_path)
if os.path.exists(file_path): if os.path.exists(file_path):
with open(file_path) as f: with open(file_path, encoding="utf-8") as fle:
json_data = json.load(f) json_data = json.load(fle)
tf_state = Tfstate(json_data) tf_state = Tfstate(json_data)
tf_state.tfstate_file = file_path tf_state.tfstate_file = file_path