From 4caf653b317dbf1c93916fffad806ca09e8c1e2e Mon Sep 17 00:00:00 2001 From: jeanluc Date: Thu, 26 Sep 2024 19:19:47 +0200 Subject: [PATCH] Fix Pylint, run pre-commit --- src/saltext/azurerm/clouds/azurerm.py | 33 ++-- src/saltext/azurerm/fileserver/azurefs.py | 1 + src/saltext/azurerm/loader.py | 1 + .../azurerm/modules/azurerm_compute.py | 2 +- .../azurerm_compute_availability_set.py | 9 +- .../azurerm/modules/azurerm_compute_disk.py | 5 +- .../azurerm/modules/azurerm_compute_image.py | 7 +- .../azurerm_compute_virtual_machine.py | 16 +- ...urerm_compute_virtual_machine_extension.py | 5 +- .../azurerm_compute_virtual_machine_image.py | 1 + src/saltext/azurerm/modules/azurerm_dns.py | 5 +- .../azurerm/modules/azurerm_keyvault_key.py | 11 +- .../modules/azurerm_keyvault_secret.py | 11 +- .../azurerm/modules/azurerm_keyvault_vault.py | 4 +- .../azurerm/modules/azurerm_network.py | 13 +- .../azurerm/modules/azurerm_resource.py | 16 +- src/saltext/azurerm/states/azurerm_compute.py | 3 +- .../azurerm_compute_availability_set.py | 9 +- .../states/azurerm_compute_virtual_machine.py | 11 +- src/saltext/azurerm/states/azurerm_dns.py | 33 ++-- .../azurerm/states/azurerm_keyvault_key.py | 13 +- .../azurerm/states/azurerm_keyvault_secret.py | 22 +-- .../azurerm/states/azurerm_keyvault_vault.py | 9 +- src/saltext/azurerm/states/azurerm_network.py | 146 +++++++++--------- .../azurerm/states/azurerm_resource.py | 53 +++---- src/saltext/azurerm/utils/azurerm.py | 17 +- tests/unit/utils/test_azurerm.py | 11 +- 27 files changed, 241 insertions(+), 226 deletions(-) diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index 555bb6a..95489b0 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -89,6 +89,7 @@ **Note:** review the details of Service Principals. Owner role is more than you normally need, and you can restrict scope to a resource group or individual resources. """ + import importlib import logging import os.path @@ -97,27 +98,27 @@ from multiprocessing import cpu_count from multiprocessing.pool import ThreadPool -import salt.cache # pylint: disable=import-error -import salt.config as config # pylint: disable=import-error -import salt.utils.cloud # pylint: disable=import-error -import salt.utils.files # pylint: disable=import-error -import salt.utils.stringutils # pylint: disable=import-error -import salt.utils.yaml # pylint: disable=import-error -import salt.version # pylint: disable=import-error -import saltext.azurerm.utils.azurerm -from salt.exceptions import SaltCloudConfigError # pylint: disable=import-error -from salt.exceptions import SaltCloudExecutionFailure # pylint: disable=import-error -from salt.exceptions import SaltCloudExecutionTimeout # pylint: disable=import-error -from salt.exceptions import SaltCloudSystemExit # pylint: disable=import-error +import salt.cache +import salt.utils.cloud +import salt.utils.files +import salt.utils.stringutils +import salt.utils.yaml +import salt.version +from salt import config +from salt.exceptions import SaltCloudConfigError +from salt.exceptions import SaltCloudExecutionFailure +from salt.exceptions import SaltCloudExecutionTimeout +from salt.exceptions import SaltCloudSystemExit +import saltext.azurerm.utils.azurerm HAS_LIBS = False try: import azure.mgmt.compute.models as compute_models import azure.mgmt.network.models as network_models - - from azure.storage.blob import BlobServiceClient, ContainerClient from azure.core.exceptions import HttpResponseError + from azure.storage.blob import BlobServiceClient + from azure.storage.blob import ContainerClient HAS_LIBS = True except ImportError: @@ -126,7 +127,7 @@ try: __salt__ # pylint: disable=used-before-assignment except NameError: - import salt.loader # pylint: disable=import-error + import salt.loader __opts__ = salt.config.minion_config("/etc/salt/minion") __utils__ = salt.loader.utils(__opts__) @@ -1706,7 +1707,7 @@ def create_or_update_vmextension(call=None, kwargs=None): # pylint: disable=unu if not isinstance(settings, dict): raise SaltCloudSystemExit("VM extension settings are not valid") - elif "commandToExecute" not in settings and "script" not in settings: + if "commandToExecute" not in settings and "script" not in settings: raise SaltCloudSystemExit( "VM extension settings are not valid. Either commandToExecute or script" " must be specified." diff --git a/src/saltext/azurerm/fileserver/azurefs.py b/src/saltext/azurerm/fileserver/azurefs.py index 9420bf7..e768bfe 100644 --- a/src/saltext/azurerm/fileserver/azurefs.py +++ b/src/saltext/azurerm/fileserver/azurefs.py @@ -44,6 +44,7 @@ Do not include the leading ? for sas_token if generated from the web """ + import base64 import logging import os diff --git a/src/saltext/azurerm/loader.py b/src/saltext/azurerm/loader.py index 0b81ee7..3299ae0 100644 --- a/src/saltext/azurerm/loader.py +++ b/src/saltext/azurerm/loader.py @@ -2,6 +2,7 @@ Define the required entry-points functions in order for Salt to know what and from where it should load this extension's loaders """ + from . import PACKAGE_ROOT # pylint: disable=unused-import diff --git a/src/saltext/azurerm/modules/azurerm_compute.py b/src/saltext/azurerm/modules/azurerm_compute.py index 9738b96..6d2f07b 100644 --- a/src/saltext/azurerm/modules/azurerm_compute.py +++ b/src/saltext/azurerm/modules/azurerm_compute.py @@ -33,10 +33,10 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging - # Azure libs HAS_LIBS = False try: diff --git a/src/saltext/azurerm/modules/azurerm_compute_availability_set.py b/src/saltext/azurerm/modules/azurerm_compute_availability_set.py index a03e410..87ac655 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_availability_set.py +++ b/src/saltext/azurerm/modules/azurerm_compute_availability_set.py @@ -33,6 +33,7 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -42,11 +43,9 @@ HAS_LIBS = False try: import azure.mgmt.compute.models # pylint: disable=unused-import - from azure.core.exceptions import ( - ResourceNotFoundError, - HttpResponseError, - SerializationError, - ) + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_compute_disk.py b/src/saltext/azurerm/modules/azurerm_compute_disk.py index 1825192..e6c48c3 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_disk.py +++ b/src/saltext/azurerm/modules/azurerm_compute_disk.py @@ -33,6 +33,7 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -42,9 +43,7 @@ HAS_LIBS = False try: import azure.mgmt.compute.models # pylint: disable=unused-import - from azure.core.exceptions import ( - HttpResponseError, - ) + from azure.core.exceptions import HttpResponseError HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_compute_image.py b/src/saltext/azurerm/modules/azurerm_compute_image.py index e6bbb64..9025c80 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_image.py +++ b/src/saltext/azurerm/modules/azurerm_compute_image.py @@ -33,6 +33,7 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -42,10 +43,8 @@ HAS_LIBS = False try: import azure.mgmt.compute.models # pylint: disable=unused-import - from azure.core.exceptions import ( - HttpResponseError, - SerializationError, - ) + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import SerializationError from azure.mgmt.core.tools import is_valid_resource_id HAS_LIBS = True diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py index 0599fc7..eee73ef 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py @@ -33,6 +33,7 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging import os @@ -43,12 +44,11 @@ HAS_LIBS = False try: import azure.mgmt.compute.models # pylint: disable=unused-import - from azure.core.exceptions import ( - ResourceNotFoundError, - HttpResponseError, - SerializationError, - ) - from azure.mgmt.core.tools import is_valid_resource_id, parse_resource_id + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError + from azure.mgmt.core.tools import is_valid_resource_id + from azure.mgmt.core.tools import parse_resource_id HAS_LIBS = True except ImportError: @@ -109,7 +109,7 @@ def create_or_update( host_group=None, extensions_time_budget=None, **kwargs, -): +): # pylint: disable=too-many-arguments """ .. versionadded:: 2.1.0 @@ -660,7 +660,7 @@ def create_or_update( "password", ) connection_profile = {x: kwargs[x] for x in auth_kwargs if x in kwargs} - is_linux = True if result["storage_profile"]["os_disk"]["os_type"] == "Linux" else False + is_linux = result["storage_profile"]["os_disk"]["os_type"] == "Linux" extension_info = {} # attach custom script extension for userdata diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py index 44bf771..c89fe05 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py @@ -33,6 +33,7 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -42,9 +43,7 @@ HAS_LIBS = False try: import azure.mgmt.compute.models # pylint: disable=unused-import - from azure.core.exceptions import ( - HttpResponseError, - ) + from azure.core.exceptions import HttpResponseError HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_image.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_image.py index b283d43..f6ef27c 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_image.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_image.py @@ -33,6 +33,7 @@ * ``AZURE_US_GOV_CLOUD`` * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging diff --git a/src/saltext/azurerm/modules/azurerm_dns.py b/src/saltext/azurerm/modules/azurerm_dns.py index e12b362..f1f5ee5 100644 --- a/src/saltext/azurerm/modules/azurerm_dns.py +++ b/src/saltext/azurerm/modules/azurerm_dns.py @@ -37,6 +37,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -47,7 +48,9 @@ try: import azure.mgmt.dns.models # pylint: disable=unused-import import azure.mgmt.privatedns.models # pylint: disable=unused-import - from azure.core.exceptions import ResourceNotFoundError, HttpResponseError, SerializationError + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_keyvault_key.py b/src/saltext/azurerm/modules/azurerm_keyvault_key.py index 8537c36..ac2d3fc 100644 --- a/src/saltext/azurerm/modules/azurerm_keyvault_key.py +++ b/src/saltext/azurerm/modules/azurerm_keyvault_key.py @@ -34,6 +34,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import datetime import logging @@ -43,13 +44,11 @@ # Azure libs HAS_LIBS = False try: + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceExistsError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError from azure.keyvault.keys import KeyClient - from azure.core.exceptions import ( - ResourceNotFoundError, - HttpResponseError, - ResourceExistsError, - SerializationError, - ) HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_keyvault_secret.py b/src/saltext/azurerm/modules/azurerm_keyvault_secret.py index 8fc8596..a173e39 100644 --- a/src/saltext/azurerm/modules/azurerm_keyvault_secret.py +++ b/src/saltext/azurerm/modules/azurerm_keyvault_secret.py @@ -34,6 +34,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import datetime import logging @@ -43,13 +44,11 @@ # Azure libs HAS_LIBS = False try: + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceExistsError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError from azure.keyvault.secrets import SecretClient - from azure.core.exceptions import ( - ResourceNotFoundError, - HttpResponseError, - ResourceExistsError, - SerializationError, - ) HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_keyvault_vault.py b/src/saltext/azurerm/modules/azurerm_keyvault_vault.py index 0fa89ee..29f8941 100644 --- a/src/saltext/azurerm/modules/azurerm_keyvault_vault.py +++ b/src/saltext/azurerm/modules/azurerm_keyvault_vault.py @@ -34,6 +34,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -43,7 +44,8 @@ HAS_LIBS = False try: import azure.mgmt.keyvault.models # pylint: disable=unused-import - from azure.core.exceptions import HttpResponseError, ResourceNotFoundError + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceNotFoundError HAS_LIBS = True except ImportError: diff --git a/src/saltext/azurerm/modules/azurerm_network.py b/src/saltext/azurerm/modules/azurerm_network.py index b506856..881a1eb 100644 --- a/src/saltext/azurerm/modules/azurerm_network.py +++ b/src/saltext/azurerm/modules/azurerm_network.py @@ -33,18 +33,22 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging import salt.config # pylint: disable=import-error, unused-import import salt.loader # pylint: disable=import-error, unused-import + import saltext.azurerm.utils.azurerm # Azure libs HAS_LIBS = False try: import azure.mgmt.network.models # pylint: disable=unused-import - from azure.core.exceptions import ResourceNotFoundError, HttpResponseError, SerializationError + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError from azure.mgmt.core.tools import is_valid_resource_id HAS_LIBS = True @@ -1500,10 +1504,9 @@ def network_interface_create_or_update( errmsg = "The provided Backend Pool ID is not a valid resource ID string." log.error(errmsg) - elif ( - "load_balancer_name" - and "backend_address_pool_name" - in ipconfig["load_balancer_backend_address_pools"][idx] + elif all( + key in ipconfig["load_balancer_backend_address_pools"][idx] + for key in ("load_balancer_name", "backend_address_pool_name") ): try: lbbep_data = ( diff --git a/src/saltext/azurerm/modules/azurerm_resource.py b/src/saltext/azurerm/modules/azurerm_resource.py index e8f8e2a..e47d1fc 100644 --- a/src/saltext/azurerm/modules/azurerm_resource.py +++ b/src/saltext/azurerm/modules/azurerm_resource.py @@ -33,19 +33,23 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging from json.decoder import JSONDecodeError import salt.utils.files # pylint: disable=import-error import salt.utils.json # pylint: disable=import-error + import saltext.azurerm.utils.azurerm # Azure libs HAS_LIBS = False try: import azure.mgmt.resource.resources.models # pylint: disable=unused-import - from azure.core.exceptions import ResourceNotFoundError, HttpResponseError, SerializationError + from azure.core.exceptions import HttpResponseError + from azure.core.exceptions import ResourceNotFoundError + from azure.core.exceptions import SerializationError HAS_LIBS = True except ImportError: @@ -939,12 +943,10 @@ def policy_assignment_create(name, scope, definition_name, **kwargs): # Delete this section when the ticket above is resolved. # BEGIN definition_list = policy_definitions_list(**kwargs) - if definition_name in definition_list: - definition = definition_list[definition_name] - else: - definition = { - "error": f'The policy definition named "{definition_name}" could not be found.' - } + definition = definition_list.get( + definition_name, + {"error": f'The policy definition named "{definition_name}" could not be found.'}, + ) # END if "error" not in definition: diff --git a/src/saltext/azurerm/states/azurerm_compute.py b/src/saltext/azurerm/states/azurerm_compute.py index 04f0704..d9a2c09 100644 --- a/src/saltext/azurerm/states/azurerm_compute.py +++ b/src/saltext/azurerm/states/azurerm_compute.py @@ -73,6 +73,7 @@ - connection_auth: {{ profile }} """ + # Python libs import logging @@ -99,7 +100,7 @@ def availability_set_present( virtual_machines=None, sku=None, connection_auth=None, - **kwargs + **kwargs, ): """ .. versionadded:: 2019.2.0 diff --git a/src/saltext/azurerm/states/azurerm_compute_availability_set.py b/src/saltext/azurerm/states/azurerm_compute_availability_set.py index d8bae61..67456da 100644 --- a/src/saltext/azurerm/states/azurerm_compute_availability_set.py +++ b/src/saltext/azurerm/states/azurerm_compute_availability_set.py @@ -55,6 +55,7 @@ """ + # Python libs import logging @@ -233,10 +234,10 @@ def present( ret["comment"] = f"Availability set {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create availability set {}! ({})".format( # pylint: disable=consider-using-f-string - name, aset.get("error") + ret["comment"] = ( + "Failed to create availability set {}! ({})".format( # pylint: disable=consider-using-f-string + name, aset.get("error") + ) ) return ret diff --git a/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py b/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py index 1921718..48627c7 100644 --- a/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py +++ b/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py @@ -51,6 +51,7 @@ password: 123pass """ + # Python libs import logging @@ -120,7 +121,7 @@ def present( tags=None, connection_auth=None, **kwargs, -): +): # pylint: disable=too-many-arguments """ .. versionadded:: 2.1.0 @@ -622,10 +623,10 @@ def present( ret["comment"] = f"Virtual machine {name} has been {action}d." return ret - ret[ - "comment" - ] = "Failed to {} virtual machine {}! ({})".format( # pylint: disable=consider-using-f-string - action, name, virt_mach.get("error") + ret["comment"] = ( + "Failed to {} virtual machine {}! ({})".format( # pylint: disable=consider-using-f-string + action, name, virt_mach.get("error") + ) ) if not ret["result"]: ret["changes"] = {} diff --git a/src/saltext/azurerm/states/azurerm_dns.py b/src/saltext/azurerm/states/azurerm_dns.py index cadb9be..b27af16 100644 --- a/src/saltext/azurerm/states/azurerm_dns.py +++ b/src/saltext/azurerm/states/azurerm_dns.py @@ -95,6 +95,7 @@ - connection_auth: {{ profile }} """ + import logging import salt.utils.dictdiffer # pylint: disable=import-error @@ -298,10 +299,10 @@ def zone_present( ret["comment"] = f"DNS zone {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create DNS zone {}! ({})".format( # pylint: disable=consider-using-f-string - name, zone.get("error") + ret["comment"] = ( + "Failed to create DNS zone {}! ({})".format( # pylint: disable=consider-using-f-string + name, zone.get("error") + ) ) return ret @@ -562,10 +563,10 @@ def record_set_present( continue if record_str[-1] != "s": if not isinstance(record, dict): - ret[ - "comment" - ] = "{} record information must be specified as a dictionary!".format( # pylint: disable=consider-using-f-string - record_str + ret["comment"] = ( + "{} record information must be specified as a dictionary!".format( # pylint: disable=consider-using-f-string + record_str + ) ) return ret for key, val in record.items(): @@ -573,10 +574,10 @@ def record_set_present( ret["changes"] = {"new": {record_str: record}} elif record_str[-1] == "s": if not isinstance(record, list): - ret[ - "comment" - ] = "{} record information must be specified as a list of dictionaries!".format( # pylint: disable=consider-using-f-string - record_str + ret["comment"] = ( + "{} record information must be specified as a list of dictionaries!".format( # pylint: disable=consider-using-f-string + record_str + ) ) return ret local, remote = (sorted(config) for config in (record, rec_set[record_str])) @@ -657,10 +658,10 @@ def record_set_present( ret["comment"] = f"Record set {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create record set {}! ({})".format( # pylint: disable=consider-using-f-string - name, rec_set.get("error") + ret["comment"] = ( + "Failed to create record set {}! ({})".format( # pylint: disable=consider-using-f-string + name, rec_set.get("error") + ) ) return ret diff --git a/src/saltext/azurerm/states/azurerm_keyvault_key.py b/src/saltext/azurerm/states/azurerm_keyvault_key.py index 52b2d37..3560017 100644 --- a/src/saltext/azurerm/states/azurerm_keyvault_key.py +++ b/src/saltext/azurerm/states/azurerm_keyvault_key.py @@ -35,6 +35,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -108,9 +109,9 @@ def present( action = "create" if not isinstance(connection_auth, dict): - ret[ - "comment" - ] = "Connection information must be specified via acct or connection_auth dictionary!" + ret["comment"] = ( + "Connection information must be specified via acct or connection_auth dictionary!" + ) return ret key = __salt__["azurerm_keyvault_key.get_key"]( @@ -242,9 +243,9 @@ def absent(name, vault_url, connection_auth=None): ret = {"name": name, "result": False, "comment": "", "changes": {}} if not isinstance(connection_auth, dict): - ret[ - "comment" - ] = "Connection information must be specified via acct or connection_auth dictionary!" + ret["comment"] = ( + "Connection information must be specified via acct or connection_auth dictionary!" + ) return ret key = __salt__["azurerm_keyvault_key.get_key"]( diff --git a/src/saltext/azurerm/states/azurerm_keyvault_secret.py b/src/saltext/azurerm/states/azurerm_keyvault_secret.py index 6eba502..6cd6a2d 100644 --- a/src/saltext/azurerm/states/azurerm_keyvault_secret.py +++ b/src/saltext/azurerm/states/azurerm_keyvault_secret.py @@ -35,6 +35,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging @@ -105,9 +106,9 @@ def present( action = "create" if not isinstance(connection_auth, dict): - ret[ - "comment" - ] = "Connection information must be specified via acct or connection_auth dictionary!" + ret["comment"] = ( + "Connection information must be specified via acct or connection_auth dictionary!" + ) return ret secret = __salt__["azurerm_keyvault_secret.get_secret"]( @@ -216,10 +217,10 @@ def present( ret["comment"] = f"Secret {name} has been {action}d." return ret - ret[ - "comment" - ] = "Failed to {} Secret {}! ({})".format( # pylint: disable=consider-using-f-string - action, name, secret.get("error") + ret["comment"] = ( + "Failed to {} Secret {}! ({})".format( # pylint: disable=consider-using-f-string + action, name, secret.get("error") + ) ) if not ret["result"]: ret["changes"] = {} @@ -259,11 +260,12 @@ def absent(name, vault_url, purge=False, wait=False, connection_auth=None): """ ret = {"name": name, "result": False, "comment": "", "changes": {}} action = "delete" + deleted = False if not isinstance(connection_auth, dict): - ret[ - "comment" - ] = "Connection information must be specified via acct or connection_auth dictionary!" + ret["comment"] = ( + "Connection information must be specified via acct or connection_auth dictionary!" + ) return ret secret = __salt__["azurerm_keyvault_secret.get_secret"]( diff --git a/src/saltext/azurerm/states/azurerm_keyvault_vault.py b/src/saltext/azurerm/states/azurerm_keyvault_vault.py index db41fdf..51774b4 100644 --- a/src/saltext/azurerm/states/azurerm_keyvault_vault.py +++ b/src/saltext/azurerm/states/azurerm_keyvault_vault.py @@ -35,6 +35,7 @@ * ``AZURE_GERMAN_CLOUD`` """ + # Python libs import logging from operator import itemgetter @@ -377,10 +378,10 @@ def present( ret["comment"] = f"Key Vault {name} has been {action}d." return ret - ret[ - "comment" - ] = "Failed to {} Key Vault {}! ({})".format( # pylint: disable=consider-using-f-string - action, name, vault.get("error") + ret["comment"] = ( + "Failed to {} Key Vault {}! ({})".format( # pylint: disable=consider-using-f-string + action, name, vault.get("error") + ) ) if not ret["result"]: ret["changes"] = {} diff --git a/src/saltext/azurerm/states/azurerm_network.py b/src/saltext/azurerm/states/azurerm_network.py index f416c9d..7ad0e54 100644 --- a/src/saltext/azurerm/states/azurerm_network.py +++ b/src/saltext/azurerm/states/azurerm_network.py @@ -75,9 +75,11 @@ - connection_auth: {{ profile }} """ + import logging import salt.utils.dictdiffer # pylint: disable=import-error + import saltext.azurerm.utils.azurerm __virtualname__ = "azurerm_network" @@ -240,10 +242,10 @@ def virtual_network_present( ret["comment"] = f"Virtual network {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create virtual network {}! ({})".format( # pylint: disable=consider-using-f-string - name, vnet.get("error") + ret["comment"] = ( + "Failed to create virtual network {}! ({})".format( # pylint: disable=consider-using-f-string + name, vnet.get("error") + ) ) return ret @@ -436,10 +438,10 @@ def subnet_present( ret["comment"] = f"Subnet {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create subnet {}! ({})".format( # pylint: disable=consider-using-f-string - name, snet.get("error") + ret["comment"] = ( + "Failed to create subnet {}! ({})".format( # pylint: disable=consider-using-f-string + name, snet.get("error") + ) ) return ret @@ -585,10 +587,10 @@ def network_security_group_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"security_rules" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"security_rules" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -637,10 +639,10 @@ def network_security_group_present( ret["comment"] = f"Network security group {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create network security group {}! ({})".format( # pylint: disable=consider-using-f-string - name, nsg.get("error") + ret["comment"] = ( + "Failed to create network security group {}! ({})".format( # pylint: disable=consider-using-f-string + name, nsg.get("error") + ) ) return ret @@ -826,10 +828,10 @@ def security_rule_present( for params in exclusive_params: # pylint: disable=eval-used if not eval(params[0]) and not eval(params[1]): - ret[ - "comment" - ] = "Either the {} or {} parameter must be provided!".format( # pylint: disable=consider-using-f-string - params[0], params[1] + ret["comment"] = ( + "Either the {} or {} parameter must be provided!".format( # pylint: disable=consider-using-f-string + params[0], params[1] + ) ) return ret # pylint: disable=eval-used @@ -1024,10 +1026,10 @@ def security_rule_present( ret["comment"] = f"Security rule {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create security rule {}! ({})".format( # pylint: disable=consider-using-f-string - name, rule.get("error") + ret["comment"] = ( + "Failed to create security rule {}! ({})".format( # pylint: disable=consider-using-f-string + name, rule.get("error") + ) ) return ret @@ -1302,10 +1304,10 @@ def load_balancer_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"frontend_ip_configurations" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"frontend_ip_configurations" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -1319,10 +1321,10 @@ def load_balancer_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"backend_address_pools" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"backend_address_pools" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -1353,10 +1355,10 @@ def load_balancer_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"load_balancing_rules" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"load_balancing_rules" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -1372,10 +1374,10 @@ def load_balancer_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"inbound_nat_rules" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"inbound_nat_rules" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -1391,10 +1393,10 @@ def load_balancer_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"inbound_nat_pools" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"inbound_nat_pools" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -1410,10 +1412,10 @@ def load_balancer_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"outbound_nat_rules" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"outbound_nat_rules" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -1475,10 +1477,10 @@ def load_balancer_present( ret["comment"] = f"Load balancer {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create load balancer {}! ({})".format( # pylint: disable=consider-using-f-string - name, load_bal.get("error") + ret["comment"] = ( + "Failed to create load balancer {}! ({})".format( # pylint: disable=consider-using-f-string + name, load_bal.get("error") + ) ) return ret @@ -1727,10 +1729,10 @@ def public_ip_address_present( ret["comment"] = f"Public IP address {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create public IP address {}! ({})".format( # pylint: disable=consider-using-f-string - name, pub_ip.get("error") + ret["comment"] = ( + "Failed to create public IP address {}! ({})".format( # pylint: disable=consider-using-f-string + name, pub_ip.get("error") + ) ) return ret @@ -1981,10 +1983,10 @@ def network_interface_present( ) if comp_ret.get("comment"): - ret[ - "comment" - ] = '"ip_configurations" {}'.format( # pylint: disable=consider-using-f-string - comp_ret["comment"] + ret["comment"] = ( + '"ip_configurations" {}'.format( # pylint: disable=consider-using-f-string + comp_ret["comment"] + ) ) return ret @@ -2048,10 +2050,10 @@ def network_interface_present( ret["comment"] = f"Network interface {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create network interface {}! ({})".format( # pylint: disable=consider-using-f-string - name, iface.get("error") + ret["comment"] = ( + "Failed to create network interface {}! ({})".format( # pylint: disable=consider-using-f-string + name, iface.get("error") + ) ) return ret @@ -2252,10 +2254,10 @@ def route_table_present( ret["comment"] = f"Route table {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create route table {}! ({})".format( # pylint: disable=consider-using-f-string - name, rt_tbl.get("error") + ret["comment"] = ( + "Failed to create route table {}! ({})".format( # pylint: disable=consider-using-f-string + name, rt_tbl.get("error") + ) ) return ret @@ -2444,10 +2446,10 @@ def route_present( ret["comment"] = f"Route {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create route {}! ({})".format( # pylint: disable=consider-using-f-string - name, route.get("error") + ret["comment"] = ( + "Failed to create route {}! ({})".format( # pylint: disable=consider-using-f-string + name, route.get("error") + ) ) return ret diff --git a/src/saltext/azurerm/states/azurerm_resource.py b/src/saltext/azurerm/states/azurerm_resource.py index 9872003..a019d88 100644 --- a/src/saltext/azurerm/states/azurerm_resource.py +++ b/src/saltext/azurerm/states/azurerm_resource.py @@ -69,6 +69,7 @@ - connection_auth: {{ profile }} """ + import json import logging @@ -181,10 +182,10 @@ def resource_group_present( ret["changes"] = {"old": {}, "new": group} return ret - ret[ - "comment" - ] = "Failed to create resource group {}! ({})".format( # pylint: disable=consider-using-f-string - name, group.get("error") + ret["comment"] = ( + "Failed to create resource group {}! ({})".format( # pylint: disable=consider-using-f-string + name, group.get("error") + ) ) return ret @@ -360,15 +361,15 @@ def policy_definition_present( return ret if not policy_rule and not policy_rule_json and not policy_rule_file: - ret[ - "comment" - ] = 'One of "policy_rule", "policy_rule_json", or "policy_rule_file" is required!' + ret["comment"] = ( + 'One of "policy_rule", "policy_rule_json", or "policy_rule_file" is required!' + ) return ret if sum(x is not None for x in [policy_rule, policy_rule_json, policy_rule_file]) > 1: - ret[ - "comment" - ] = 'Only one of "policy_rule", "policy_rule_json", or "policy_rule_file" is allowed!' + ret["comment"] = ( + 'Only one of "policy_rule", "policy_rule_json", or "policy_rule_file" is allowed!' + ) return ret if (policy_rule_json or policy_rule_file) and ( @@ -406,10 +407,10 @@ def policy_definition_present( **kwargs, ) except Exception as exc: # pylint: disable=broad-except - ret[ - "comment" - ] = 'Unable to locate policy rule file "{}"! ({})'.format( # pylint: disable=consider-using-f-string - policy_rule_file, exc + ret["comment"] = ( + 'Unable to locate policy rule file "{}"! ({})'.format( # pylint: disable=consider-using-f-string + policy_rule_file, exc + ) ) return ret @@ -421,10 +422,10 @@ def policy_definition_present( with salt.utils.files.fopen(sfn, "r") as prf: temp_rule = json.load(prf) except Exception as exc: # pylint: disable=broad-except - ret[ - "comment" - ] = 'Unable to load policy rule file "{}"! ({})'.format( # pylint: disable=consider-using-f-string - policy_rule_file, exc + ret["comment"] = ( + 'Unable to load policy rule file "{}"! ({})'.format( # pylint: disable=consider-using-f-string + policy_rule_file, exc + ) ) return ret @@ -541,10 +542,10 @@ def policy_definition_present( ret["comment"] = f"Policy definition {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create policy definition {}! ({})".format( # pylint: disable=consider-using-f-string - name, policy.get("error") + ret["comment"] = ( + "Failed to create policy definition {}! ({})".format( # pylint: disable=consider-using-f-string + name, policy.get("error") + ) ) return ret @@ -742,10 +743,10 @@ def policy_assignment_present( ret["comment"] = f"Policy assignment {name} has been created." return ret - ret[ - "comment" - ] = "Failed to create policy assignment {}! ({})".format( # pylint: disable=consider-using-f-string - name, policy.get("error") + ret["comment"] = ( + "Failed to create policy assignment {}! ({})".format( # pylint: disable=consider-using-f-string + name, policy.get("error") + ) ) return ret diff --git a/src/saltext/azurerm/utils/azurerm.py b/src/saltext/azurerm/utils/azurerm.py index 9f54bb7..375518a 100644 --- a/src/saltext/azurerm/utils/azurerm.py +++ b/src/saltext/azurerm/utils/azurerm.py @@ -19,6 +19,7 @@ :platform: linux """ + import importlib import logging import os @@ -33,17 +34,13 @@ from salt.exceptions import SaltSystemExit # pylint: disable=import-error try: - from azure.identity import ( - AzureAuthorityHosts, - DefaultAzureCredential, - KnownAuthorities, - ) from azure.core.exceptions import ClientAuthenticationError - from msrestazure.azure_cloud import ( - MetadataEndpointError, - get_cloud_from_metadata_endpoint, - ) from azure.core.pipeline.policies import UserAgentPolicy + from azure.identity import AzureAuthorityHosts + from azure.identity import DefaultAzureCredential + from azure.identity import KnownAuthorities + from msrestazure.azure_cloud import MetadataEndpointError + from msrestazure.azure_cloud import get_cloud_from_metadata_endpoint HAS_AZURE = True except ImportError: @@ -197,8 +194,6 @@ def log_cloud_error(client, message, **kwargs): message, ) - return - def paged_object_to_list(paged_object): """ diff --git a/tests/unit/utils/test_azurerm.py b/tests/unit/utils/test_azurerm.py index 868068d..475f307 100644 --- a/tests/unit/utils/test_azurerm.py +++ b/tests/unit/utils/test_azurerm.py @@ -3,12 +3,14 @@ from unittest.mock import patch import pytest -import saltext.azurerm.utils.azurerm from azure.mgmt.resource.resources import ResourceManagementClient +import saltext.azurerm.utils.azurerm + try: from salt._logging.impl import SaltLoggingClass - from salt.exceptions import SaltSystemExit, SaltInvocationError + from salt.exceptions import SaltInvocationError + from salt.exceptions import SaltSystemExit except ImportError: pass @@ -21,9 +23,8 @@ class FakeCredential: """ def get_token(self, *scopes, **kwargs): # pylint: disable=unused-argument - from azure.core.credentials import ( # pylint: disable=import-outside-toplevel - AccessToken, - ) + # pylint: disable=import-outside-toplevel + from azure.core.credentials import AccessToken return AccessToken("fake_token", 2527537086)