From 73a1350b111b1f3cf5758ca912775f76c850cd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Gajdu=C5=A1ek?= Date: Wed, 17 Apr 2024 15:00:34 +0200 Subject: [PATCH] Switch from black & reorder-python-imports to ruff --- .gitignore | 1 + .pre-commit-config.yaml | 19 +++---- pyproject.toml | 22 ++++++++ tests/test_hawkular.py | 6 +- tests/test_vm_and_template_systems.py | 6 +- wrapanapi/clients/__init__.py | 3 +- wrapanapi/entities/__init__.py | 20 +++---- wrapanapi/entities/base.py | 5 +- wrapanapi/entities/instance.py | 4 +- wrapanapi/entities/network.py | 10 ++-- wrapanapi/entities/physical_container.py | 1 + wrapanapi/entities/server.py | 4 +- wrapanapi/entities/stack.py | 10 ++-- wrapanapi/entities/template.py | 10 ++-- wrapanapi/entities/vm.py | 14 ++--- wrapanapi/entities/volume.py | 10 ++-- wrapanapi/systems/base.py | 5 +- wrapanapi/systems/container/rhopenshift.py | 10 +--- wrapanapi/systems/ec2.py | 26 ++++----- wrapanapi/systems/google.py | 17 +++--- wrapanapi/systems/hawkular.py | 3 +- wrapanapi/systems/lenovo.py | 1 + wrapanapi/systems/msazure.py | 17 ++---- wrapanapi/systems/nuage.py | 1 + wrapanapi/systems/openstack.py | 27 +++++---- wrapanapi/systems/openstack_infra.py | 1 - wrapanapi/systems/redfish.py | 8 +-- wrapanapi/systems/rhevm.py | 32 +++++------ wrapanapi/systems/scvmm.py | 64 ++++++---------------- wrapanapi/systems/vcloud.py | 3 +- wrapanapi/systems/virtualcenter.py | 41 ++++++-------- wrapanapi/utils/__init__.py | 4 +- wrapanapi/utils/logger_mixin.py | 1 + 33 files changed, 172 insertions(+), 234 deletions(-) diff --git a/.gitignore b/.gitignore index 1d138b35..26bc7744 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ dist/ .tox/ AUTHORS ChangeLog +vcd_sdk.log diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c090a6c0..29f4896b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,18 +1,15 @@ ci: autofix_prs: false repos: - - repo: https://github.com/asottile/reorder-python-imports - rev: v3.12.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.3.7 hooks: - - id: reorder-python-imports - args: - - --application-directories=.:wrapanapi - - repo: https://github.com/psf/black - rev: 23.12.1 - hooks: - - id: black - args: [--safe, --quiet, --line-length, "100"] - require_serial: true + # Run the linter. + - id: ruff + args: [ --fix ] + # Run the formatter. + - id: ruff-format - repo: https://github.com/PyCQA/flake8 rev: 7.0.0 hooks: diff --git a/pyproject.toml b/pyproject.toml index efeb7bba..2bb722a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,3 +87,25 @@ test = [ "pytest-variables", "coveralls", ] + +[tool.ruff] +line-length = 100 +indent-width = 4 + +[tool.ruff.format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +[tool.ruff.lint] +select = ["I"] + +ignore = [] diff --git a/tests/test_hawkular.py b/tests/test_hawkular.py index ae868ef8..5b9e0570 100644 --- a/tests/test_hawkular.py +++ b/tests/test_hawkular.py @@ -1,4 +1,5 @@ """Unit tests for Hawkular client.""" + import json import os from random import sample @@ -8,10 +9,7 @@ import pytest from wrapanapi.systems import HawkularSystem -from wrapanapi.systems.hawkular import CanonicalPath -from wrapanapi.systems.hawkular import Resource -from wrapanapi.systems.hawkular import ResourceData -from wrapanapi.systems.hawkular import ResourceType +from wrapanapi.systems.hawkular import CanonicalPath, Resource, ResourceData, ResourceType def fake_urlopen(c_client, url, headers, params): diff --git a/tests/test_vm_and_template_systems.py b/tests/test_vm_and_template_systems.py index 4dd08d53..f750a98d 100644 --- a/tests/test_vm_and_template_systems.py +++ b/tests/test_vm_and_template_systems.py @@ -6,6 +6,7 @@ If running within a cfme venv, disable the cfme plugins like so: $ pytest test_vm_and_template_systems.py -p no:cfme -s """ + import datetime import logging @@ -17,10 +18,7 @@ from wrapanapi import VmState from wrapanapi.entities import StackMixin from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.systems.ec2 import EC2Image -from wrapanapi.systems.ec2 import EC2Instance -from wrapanapi.systems.ec2 import StackStates - +from wrapanapi.systems.ec2 import EC2Image, EC2Instance, StackStates log = logging.getLogger("wrapanapi.tests.test_vm_and_template_systems") diff --git a/wrapanapi/clients/__init__.py b/wrapanapi/clients/__init__.py index 79207210..54f9d537 100644 --- a/wrapanapi/clients/__init__.py +++ b/wrapanapi/clients/__init__.py @@ -1,5 +1,4 @@ from .rest_client import ContainerClient -from .websocket_client import HawkularWebsocketClient -from .websocket_client import WebsocketClient +from .websocket_client import HawkularWebsocketClient, WebsocketClient __all__ = ["ContainerClient", "WebsocketClient", "HawkularWebsocketClient"] diff --git a/wrapanapi/entities/__init__.py b/wrapanapi/entities/__init__.py index 08986470..bca99ba1 100644 --- a/wrapanapi/entities/__init__.py +++ b/wrapanapi/entities/__init__.py @@ -1,21 +1,15 @@ """ wrapanapi.entities """ + from .instance import Instance -from .network import Network -from .network import NetworkMixin +from .network import Network, NetworkMixin from .physical_container import PhysicalContainer -from .server import Server -from .server import ServerState -from .stack import Stack -from .stack import StackMixin -from .template import Template -from .template import TemplateMixin -from .vm import Vm -from .vm import VmMixin -from .vm import VmState -from .volume import Volume -from .volume import VolumeMixin +from .server import Server, ServerState +from .stack import Stack, StackMixin +from .template import Template, TemplateMixin +from .vm import Vm, VmMixin, VmState +from .volume import Volume, VolumeMixin __all__ = [ "Template", diff --git a/wrapanapi/entities/base.py b/wrapanapi/entities/base.py index d6ae644b..8e60a2a3 100644 --- a/wrapanapi/entities/base.py +++ b/wrapanapi/entities/base.py @@ -3,9 +3,8 @@ Provides method/class definitions for handling any entity on a provider """ -from abc import ABCMeta -from abc import abstractmethod -from abc import abstractproperty + +from abc import ABCMeta, abstractmethod, abstractproperty from reprlib import aRepr from wrapanapi.exceptions import NotFoundError diff --git a/wrapanapi/entities/instance.py b/wrapanapi/entities/instance.py index 6664c4ee..43f3fa1e 100644 --- a/wrapanapi/entities/instance.py +++ b/wrapanapi/entities/instance.py @@ -3,8 +3,8 @@ Instances which run on cloud providers """ -from abc import ABCMeta -from abc import abstractproperty + +from abc import ABCMeta, abstractproperty from .vm import Vm diff --git a/wrapanapi/entities/network.py b/wrapanapi/entities/network.py index b112ed0a..8874457e 100644 --- a/wrapanapi/entities/network.py +++ b/wrapanapi/entities/network.py @@ -3,13 +3,11 @@ Networks """ -from abc import ABCMeta -from abc import abstractmethod -from wrapanapi.entities.base import Entity -from wrapanapi.entities.base import EntityMixin -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError +from abc import ABCMeta, abstractmethod + +from wrapanapi.entities.base import Entity, EntityMixin +from wrapanapi.exceptions import MultipleItemsError, NotFoundError class Network(Entity, metaclass=ABCMeta): diff --git a/wrapanapi/entities/physical_container.py b/wrapanapi/entities/physical_container.py index a853572a..6a6a7782 100644 --- a/wrapanapi/entities/physical_container.py +++ b/wrapanapi/entities/physical_container.py @@ -5,6 +5,7 @@ blocks, sleds, chassis or enclosures that contain other physical entities such as physical servers. """ + from abc import ABCMeta from wrapanapi.entities.base import Entity diff --git a/wrapanapi/entities/server.py b/wrapanapi/entities/server.py index d688570c..933e33fb 100644 --- a/wrapanapi/entities/server.py +++ b/wrapanapi/entities/server.py @@ -3,8 +3,8 @@ Implements classes and methods related to actions performed on (physical) servers """ -from abc import ABCMeta -from abc import abstractmethod + +from abc import ABCMeta, abstractmethod from wrapanapi.entities.base import Entity diff --git a/wrapanapi/entities/stack.py b/wrapanapi/entities/stack.py index 035b53a0..ea7130b5 100644 --- a/wrapanapi/entities/stack.py +++ b/wrapanapi/entities/stack.py @@ -3,13 +3,11 @@ Orchestration stacks """ -from abc import ABCMeta -from abc import abstractmethod -from wrapanapi.entities.base import Entity -from wrapanapi.entities.base import EntityMixin -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError +from abc import ABCMeta, abstractmethod + +from wrapanapi.entities.base import Entity, EntityMixin +from wrapanapi.exceptions import MultipleItemsError, NotFoundError class Stack(Entity, metaclass=ABCMeta): diff --git a/wrapanapi/entities/template.py b/wrapanapi/entities/template.py index 8aff7b9b..b7575572 100644 --- a/wrapanapi/entities/template.py +++ b/wrapanapi/entities/template.py @@ -3,13 +3,11 @@ Methods/classes pertaining to performing actions on a template """ -from abc import ABCMeta -from abc import abstractmethod -from wrapanapi.entities.base import Entity -from wrapanapi.entities.base import EntityMixin -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError +from abc import ABCMeta, abstractmethod + +from wrapanapi.entities.base import Entity, EntityMixin +from wrapanapi.exceptions import MultipleItemsError, NotFoundError class Template(Entity, metaclass=ABCMeta): diff --git a/wrapanapi/entities/vm.py b/wrapanapi/entities/vm.py index 7c54e75a..bc8e15db 100644 --- a/wrapanapi/entities/vm.py +++ b/wrapanapi/entities/vm.py @@ -3,20 +3,16 @@ Methods/classes pertaining to performing actions on a VM/instance """ + import time -from abc import ABCMeta -from abc import abstractmethod -from abc import abstractproperty +from abc import ABCMeta, abstractmethod, abstractproperty from cached_property import cached_property_with_ttl -from wait_for import TimedOutError -from wait_for import wait_for +from wait_for import TimedOutError, wait_for from wrapanapi.const import CACHED_PROPERTY_TTL -from wrapanapi.entities.base import Entity -from wrapanapi.entities.base import EntityMixin -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError +from wrapanapi.entities.base import Entity, EntityMixin +from wrapanapi.exceptions import MultipleItemsError, NotFoundError class VmState: diff --git a/wrapanapi/entities/volume.py b/wrapanapi/entities/volume.py index 424a2aee..6a6e13dd 100644 --- a/wrapanapi/entities/volume.py +++ b/wrapanapi/entities/volume.py @@ -3,13 +3,11 @@ Volumes """ -from abc import ABCMeta -from abc import abstractmethod -from wrapanapi.entities.base import Entity -from wrapanapi.entities.base import EntityMixin -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError +from abc import ABCMeta, abstractmethod + +from wrapanapi.entities.base import Entity, EntityMixin +from wrapanapi.exceptions import MultipleItemsError, NotFoundError class Volume(Entity, metaclass=ABCMeta): diff --git a/wrapanapi/systems/base.py b/wrapanapi/systems/base.py index 6a2148cb..b4e4a544 100644 --- a/wrapanapi/systems/base.py +++ b/wrapanapi/systems/base.py @@ -2,9 +2,8 @@ Used to communicate with providers without using CFME facilities """ -from abc import ABCMeta -from abc import abstractmethod -from abc import abstractproperty + +from abc import ABCMeta, abstractmethod, abstractproperty from wrapanapi.utils import LoggerMixin diff --git a/wrapanapi/systems/container/rhopenshift.py b/wrapanapi/systems/container/rhopenshift.py index 1e02e20a..87965198 100644 --- a/wrapanapi/systems/container/rhopenshift.py +++ b/wrapanapi/systems/container/rhopenshift.py @@ -2,23 +2,19 @@ import json import string from collections.abc import Iterable -from functools import partial -from functools import wraps +from functools import partial, wraps from random import choice import inflection import yaml from kubernetes import client as kubeclient from kubernetes.client.rest import ApiException -from miq_version import TemplateName -from miq_version import Version +from miq_version import TemplateName, Version from openshift import client as ociclient -from wait_for import TimedOutError -from wait_for import wait_for +from wait_for import TimedOutError, wait_for from wrapanapi.systems.base import System - # this service allows to access db outside of openshift common_service = """ { diff --git a/wrapanapi/systems/ec2.py b/wrapanapi/systems/ec2.py index 24268bbe..35c4db09 100644 --- a/wrapanapi/systems/ec2.py +++ b/wrapanapi/systems/ec2.py @@ -8,19 +8,19 @@ from botocore.config import Config from botocore.exceptions import ClientError -from wrapanapi.entities import Instance -from wrapanapi.entities import Network -from wrapanapi.entities import NetworkMixin -from wrapanapi.entities import Stack -from wrapanapi.entities import StackMixin -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState -from wrapanapi.entities import Volume -from wrapanapi.exceptions import ActionTimedOutError -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError +from wrapanapi.entities import ( + Instance, + Network, + NetworkMixin, + Stack, + StackMixin, + Template, + TemplateMixin, + VmMixin, + VmState, + Volume, +) +from wrapanapi.exceptions import ActionTimedOutError, MultipleItemsError, NotFoundError from wrapanapi.systems.base import System diff --git a/wrapanapi/systems/google.py b/wrapanapi/systems/google.py index 485b1706..f6aaf560 100644 --- a/wrapanapi/systems/google.py +++ b/wrapanapi/systems/google.py @@ -1,6 +1,7 @@ """ Defines System and Entity classes related to the Google Cloud platform """ + import os import random import time @@ -15,15 +16,13 @@ from oauth2client.service_account import ServiceAccountCredentials from wait_for import wait_for -from wrapanapi.entities import Instance -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState -from wrapanapi.exceptions import ImageNotFoundError -from wrapanapi.exceptions import MultipleInstancesError -from wrapanapi.exceptions import NotFoundError -from wrapanapi.exceptions import VMInstanceNotFound +from wrapanapi.entities import Instance, Template, TemplateMixin, VmMixin, VmState +from wrapanapi.exceptions import ( + ImageNotFoundError, + MultipleInstancesError, + NotFoundError, + VMInstanceNotFound, +) from wrapanapi.systems.base import System # Retry transport and file IO errors. diff --git a/wrapanapi/systems/hawkular.py b/wrapanapi/systems/hawkular.py index 86bdc36a..b8bb5acf 100644 --- a/wrapanapi/systems/hawkular.py +++ b/wrapanapi/systems/hawkular.py @@ -10,8 +10,7 @@ from packaging import version -from wrapanapi.clients import ContainerClient -from wrapanapi.clients import HawkularWebsocketClient +from wrapanapi.clients import ContainerClient, HawkularWebsocketClient from wrapanapi.systems.base import System """ diff --git a/wrapanapi/systems/lenovo.py b/wrapanapi/systems/lenovo.py index 8987c4af..938f1338 100644 --- a/wrapanapi/systems/lenovo.py +++ b/wrapanapi/systems/lenovo.py @@ -1,6 +1,7 @@ """Backend management system classes Used to communicate with providers without using CFME facilities """ + import json import requests diff --git a/wrapanapi/systems/msazure.py b/wrapanapi/systems/msazure.py index fa8527fa..949ed44a 100644 --- a/wrapanapi/systems/msazure.py +++ b/wrapanapi/systems/msazure.py @@ -2,9 +2,9 @@ Used to communicate with providers without using CFME facilities """ + import os -from datetime import datetime -from datetime import timedelta +from datetime import datetime, timedelta import pytz from azure.common import AzureConflictHttpError @@ -13,8 +13,7 @@ from azure.mgmt.compute import ComputeManagementClient from azure.mgmt.iothub import IotHubClient from azure.mgmt.network import NetworkManagementClient -from azure.mgmt.network.models import NetworkSecurityGroup -from azure.mgmt.network.models import SecurityRule +from azure.mgmt.network.models import NetworkSecurityGroup, SecurityRule from azure.mgmt.resource import SubscriptionClient from azure.mgmt.resource.resources import ResourceManagementClient from azure.mgmt.resource.subscriptions.models import SubscriptionState @@ -24,14 +23,8 @@ from dateutil import parser from wait_for import wait_for -from wrapanapi.entities import Instance -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState -from wrapanapi.exceptions import ImageNotFoundError -from wrapanapi.exceptions import MultipleImagesError -from wrapanapi.exceptions import VMInstanceNotFound +from wrapanapi.entities import Instance, Template, TemplateMixin, VmMixin, VmState +from wrapanapi.exceptions import ImageNotFoundError, MultipleImagesError, VMInstanceNotFound from wrapanapi.systems.base import System diff --git a/wrapanapi/systems/nuage.py b/wrapanapi/systems/nuage.py index fb044349..820c098b 100644 --- a/wrapanapi/systems/nuage.py +++ b/wrapanapi/systems/nuage.py @@ -1,6 +1,7 @@ """Backend management system classes Used to communicate with providers without using CFME facilities """ + from wrapanapi.systems.base import System from wrapanapi.utils.random import random_name diff --git a/wrapanapi/systems/openstack.py b/wrapanapi/systems/openstack.py index ba0fb8aa..4f5909db 100644 --- a/wrapanapi/systems/openstack.py +++ b/wrapanapi/systems/openstack.py @@ -2,6 +2,7 @@ Used to communicate with providers without using CFME facilities """ + import json import os import time @@ -28,20 +29,18 @@ from swiftclient.exceptions import ClientException as SwiftException from wait_for import wait_for -from wrapanapi.entities import Instance -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState -from wrapanapi.exceptions import ActionTimedOutError -from wrapanapi.exceptions import ImageNotFoundError -from wrapanapi.exceptions import ItemNotFound -from wrapanapi.exceptions import KeystoneVersionNotSupported -from wrapanapi.exceptions import MultipleImagesError -from wrapanapi.exceptions import MultipleInstancesError -from wrapanapi.exceptions import NetworkNameNotFound -from wrapanapi.exceptions import NoMoreFloatingIPs -from wrapanapi.exceptions import VMInstanceNotFound +from wrapanapi.entities import Instance, Template, TemplateMixin, VmMixin, VmState +from wrapanapi.exceptions import ( + ActionTimedOutError, + ImageNotFoundError, + ItemNotFound, + KeystoneVersionNotSupported, + MultipleImagesError, + MultipleInstancesError, + NetworkNameNotFound, + NoMoreFloatingIPs, + VMInstanceNotFound, +) from wrapanapi.systems.base import System # TODO The following monkeypatch nonsense is criminal, and would be diff --git a/wrapanapi/systems/openstack_infra.py b/wrapanapi/systems/openstack_infra.py index 7c36ebf3..679e9f48 100644 --- a/wrapanapi/systems/openstack_infra.py +++ b/wrapanapi/systems/openstack_infra.py @@ -11,7 +11,6 @@ from wrapanapi.exceptions import KeystoneVersionNotSupported from wrapanapi.systems.base import System - Node = namedtuple("Node", ["uuid", "name", "power_state", "provision_state"]) diff --git a/wrapanapi/systems/redfish.py b/wrapanapi/systems/redfish.py index 41d8378c..d592acb2 100644 --- a/wrapanapi/systems/redfish.py +++ b/wrapanapi/systems/redfish.py @@ -1,14 +1,12 @@ """Backend management system classes Used to communicate with providers without using CFME facilities """ + import redfish_client -from wrapanapi.entities import PhysicalContainer -from wrapanapi.entities import Server -from wrapanapi.entities import ServerState +from wrapanapi.entities import PhysicalContainer, Server, ServerState from wrapanapi.entities.base import Entity -from wrapanapi.exceptions import InvalidValueException -from wrapanapi.exceptions import ItemNotFound +from wrapanapi.exceptions import InvalidValueException, ItemNotFound from wrapanapi.systems.base import System diff --git a/wrapanapi/systems/rhevm.py b/wrapanapi/systems/rhevm.py index 1a42bbe1..9382afe0 100644 --- a/wrapanapi/systems/rhevm.py +++ b/wrapanapi/systems/rhevm.py @@ -2,27 +2,23 @@ Used to communicate with providers without using CFME facilities """ + import fauxfactory import pytz -from ovirtsdk4 import Connection -from ovirtsdk4 import Error +from ovirtsdk4 import Connection, Error, types from ovirtsdk4 import NotFoundError as OVirtNotFoundError -from ovirtsdk4 import types -from wait_for import TimedOutError -from wait_for import wait_for - -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import Vm -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState -from wrapanapi.exceptions import ItemNotFound -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import NotFoundError -from wrapanapi.exceptions import ResourceAlreadyExistsException -from wrapanapi.exceptions import VMInstanceNotFound -from wrapanapi.exceptions import VMInstanceNotSuspended -from wrapanapi.exceptions import VMNotFoundViaIP +from wait_for import TimedOutError, wait_for + +from wrapanapi.entities import Template, TemplateMixin, Vm, VmMixin, VmState +from wrapanapi.exceptions import ( + ItemNotFound, + MultipleItemsError, + NotFoundError, + ResourceAlreadyExistsException, + VMInstanceNotFound, + VMInstanceNotSuspended, + VMNotFoundViaIP, +) from wrapanapi.systems.base import System diff --git a/wrapanapi/systems/scvmm.py b/wrapanapi/systems/scvmm.py index f1ddb14a..3e288411 100644 --- a/wrapanapi/systems/scvmm.py +++ b/wrapanapi/systems/scvmm.py @@ -2,6 +2,7 @@ Used to communicate with providers without using CFME facilities """ + import json import re import time @@ -14,17 +15,10 @@ from cached_property import cached_property from wait_for import wait_for -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import Vm -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState -from wrapanapi.exceptions import ImageNotFoundError -from wrapanapi.exceptions import MultipleItemsError -from wrapanapi.exceptions import VMInstanceNotFound +from wrapanapi.entities import Template, TemplateMixin, Vm, VmMixin, VmState +from wrapanapi.exceptions import ImageNotFoundError, MultipleItemsError, VMInstanceNotFound from wrapanapi.systems.base import System - WINDOWS_TZ_INFO = { "AUS Central Standard Time": "Australia/Darwin", "AUS Eastern Standard Time": "Australia/Sydney", @@ -325,9 +319,7 @@ def clone(self, vm_name, vm_host, path, start_vm=True): $vm_new = Get-SCVirtualMachine -ID "{src_vm}" -VMMServer $scvmm_server $vm_host = Get-SCVMHost -VMMServer $scvmm_server -ComputerName "{vm_host}" New-SCVirtualMachine -Name "{vm_name}" -VM $vm_new -VMHost $vm_host -Path "{path}" - """.format( - vm_name=vm_name, src_vm=self._id, vm_host=vm_host, path=path - ) + """.format(vm_name=vm_name, src_vm=self._id, vm_host=vm_host, path=path) if start_vm: script = f"{script} -StartVM" self._run_script(script) @@ -357,9 +349,7 @@ def create_snapshot(self, check_type="Standard"): script = """ $vm = Get-SCVirtualMachine -ID "{scvmm_vm_id}" New-SCVMCheckpoint -VM $vm - """.format( - scvmm_vm_id=self._id - ) + """.format(scvmm_vm_id=self._id) self.system.run_script(script) def set_checkpoint_type(self, check_type="Standard"): @@ -398,9 +388,7 @@ def disconnect_dvd_drives(self): $DVDDrives = Get-SCVirtualDVDDrive -VM $VM foreach ($drive in $DVDDrives) {{$drive | Remove-SCVirtualDVDDrivce}} Write-Host "number_dvds_disconnected: " + $DVDDrives.length - """.format( - self._id - ) + """.format(self._id) output = self._run_script(script) output = output.splitlines() num_removed_line = [line for line in output if "number_dvds_disconnected:" in line] @@ -416,9 +404,7 @@ def mark_as_template(self, library_server, library_share, template_name=None, ** script = """ $VM = Get-SCVirtualMachine -ID \"{id}\" -VMMServer $scvmm_server New-SCVMTemplate -Name \"{name}\" -VM $VM -LibraryServer \"{ls}\" -SharePath \"{lp}\" - """.format( - id=self._id, name=name, ls=library_server, lp=library_share - ) + """.format(id=self._id, name=name, ls=library_server, lp=library_share) self.logger.info("Creating SCVMM Template '%s' from VM '%s'", name, self._log_str) self._run_script(script) self.system.update_scvmm_library() @@ -482,9 +468,7 @@ def deploy(self, vm_name, host_group, timeout=900, vm_cpu=None, vm_ram=None, **k $vmc = New-SCVMConfiguration -VMTemplate $tpl -Name "{vm_name}" -VMHostGroup $vm_hg Update-SCVMConfiguration -VMConfiguration $vmc New-SCVirtualMachine -Name "{vm_name}" -VMConfiguration $vmc - """.format( - id=self._id, vm_name=vm_name, host_group=host_group - ) + """.format(id=self._id, vm_name=vm_name, host_group=host_group) if kwargs: self.logger.warn("deploy() ignored kwargs: %s", kwargs) if vm_cpu: @@ -509,9 +493,7 @@ def delete(self): script = """ $Template = Get-SCVMTemplate -ID \"{id}\" -VMMServer $scvmm_server Remove-SCVMTemplate -VMTemplate $Template -Force - """.format( - id=self._id - ) + """.format(id=self._id) self.logger.info("Removing SCVMM VM Template '%s'", self._log_str) self._run_script(script) self.system.update_scvmm_library() @@ -575,9 +557,7 @@ def pre_script(self): $secpasswd = ConvertTo-SecureString "{}" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ("{}\\{}", $secpasswd) $scvmm_server = Get-SCVMMServer -Computername localhost -Credential $mycreds - """.format( - self.password, self.domain, self.user - ) + """.format(self.password, self.domain, self.user) ) @cached_property @@ -752,9 +732,7 @@ def update_scvmm_library(self, path="VHDs"): script = """ $lib = Get-SCLibraryShare Read-SCLibraryShare -LibraryShare $lib[0] -Path {path} -RunAsynchronously - """.format( - path=path - ) + """.format(path=path) self.run_script(script) def unzip_archive(self, path, dest): @@ -765,9 +743,7 @@ def unzip_archive(self, path, dest): $dest = "{dest}" Add-Type -assembly "system.io.compression.filesystem" [io.compression.zipfile]::ExtractToDirectory($path, $dest) - """.format( - path=path, dest=dest - ) + """.format(path=path, dest=dest) self.run_script(script) def download_file(self, url, name, dest="L:\\Library\\VHDs\\", unzip=False): @@ -778,9 +754,7 @@ def download_file(self, url, name, dest="L:\\Library\\VHDs\\", unzip=False): $output = "{dest}{name}" $wc = New-Object System.Net.WebClient $wc.DownloadFile($url, $output) - """.format( - url=url, name=name, dest=dest - ) + """.format(url=url, name=name, dest=dest) self.run_script(script) if unzip: self.unzip_archive(f"{dest}{name}", dest) @@ -793,9 +767,7 @@ def delete_file(self, name, dest="L:\\Library\\VHDs\\"): script = """ $fname = "{dest}{name}" Remove-Item -Path $fname - """.format( - name=name, dest=dest - ) + """.format(name=name, dest=dest) self.run_script(script) self.update_scvmm_library(dest) @@ -804,9 +776,7 @@ def delete_app_package(self, name): script = """ $app_package = Get-SCApplicationPackage -Name "{}" Remove-SCApplicationPackage -ApplicationPackage $app_package - """.format( - name - ) + """.format(name) self.run_script(script) def delete_vhd(self, name): @@ -815,9 +785,7 @@ def delete_vhd(self, name): script = """ $vhd = Get-SCVirtualHardDisk -Name "{}" Remove-SCVirtualHardDisk -VirtualHardDisk $vhd - """.format( - name - ) + """.format(name) self.run_script(script) class PowerShellScriptError(Exception): diff --git a/wrapanapi/systems/vcloud.py b/wrapanapi/systems/vcloud.py index 3a83a6f1..71734cb9 100644 --- a/wrapanapi/systems/vcloud.py +++ b/wrapanapi/systems/vcloud.py @@ -1,5 +1,4 @@ -from pyvcloud.vcd.client import BasicLoginCredentials -from pyvcloud.vcd.client import Client +from pyvcloud.vcd.client import BasicLoginCredentials, Client from pyvcloud.vcd.org import Org from pyvcloud.vcd.vapp import VApp from pyvcloud.vcd.vdc import VDC diff --git a/wrapanapi/systems/virtualcenter.py b/wrapanapi/systems/virtualcenter.py index 2090898f..1c033aee 100644 --- a/wrapanapi/systems/virtualcenter.py +++ b/wrapanapi/systems/virtualcenter.py @@ -2,6 +2,7 @@ Used to communicate with providers without using CFME facilities """ + import atexit import operator import re @@ -9,36 +10,30 @@ import threading import time from datetime import datetime +from distutils.version import LooseVersion from functools import partial import pytz from cached_property import threaded_cached_property -from distutils.version import LooseVersion -from pyVim.connect import Disconnect -from pyVim.connect import SmartConnect -from pyVmomi import vim -from pyVmomi import vmodl -from wait_for import TimedOutError -from wait_for import wait_for - -from wrapanapi.entities import Template -from wrapanapi.entities import TemplateMixin -from wrapanapi.entities import Vm -from wrapanapi.entities import VmMixin -from wrapanapi.entities import VmState +from pyVim.connect import Disconnect, SmartConnect +from pyVmomi import vim, vmodl +from wait_for import TimedOutError, wait_for + +from wrapanapi.entities import Template, TemplateMixin, Vm, VmMixin, VmState from wrapanapi.entities.base import Entity -from wrapanapi.exceptions import DatastoreNotFoundError -from wrapanapi.exceptions import HostNotRemoved -from wrapanapi.exceptions import NotFoundError -from wrapanapi.exceptions import VMCreationDateError -from wrapanapi.exceptions import VMInstanceNotCloned -from wrapanapi.exceptions import VMInstanceNotFound -from wrapanapi.exceptions import VMInstanceNotStopped -from wrapanapi.exceptions import VMInstanceNotSuspended -from wrapanapi.exceptions import VMNotFoundViaIP +from wrapanapi.exceptions import ( + DatastoreNotFoundError, + HostNotRemoved, + NotFoundError, + VMCreationDateError, + VMInstanceNotCloned, + VMInstanceNotFound, + VMInstanceNotStopped, + VMInstanceNotSuspended, + VMNotFoundViaIP, +) from wrapanapi.systems.base import System - SELECTION_SPECS = [ "resource_pool_traversal_spec", "resource_pool_vm_traversal_spec", diff --git a/wrapanapi/utils/__init__.py b/wrapanapi/utils/__init__.py index b7b96d89..7b46cb69 100644 --- a/wrapanapi/utils/__init__.py +++ b/wrapanapi/utils/__init__.py @@ -1,6 +1,4 @@ -from .json_utils import eval_strings -from .json_utils import json_load_byteified -from .json_utils import json_loads_byteified +from .json_utils import eval_strings, json_load_byteified, json_loads_byteified from .logger_mixin import LoggerMixin __all__ = ["LoggerMixin", "json_load_byteified", "json_loads_byteified", "eval_strings"] diff --git a/wrapanapi/utils/logger_mixin.py b/wrapanapi/utils/logger_mixin.py index dfd00a43..6699eb89 100644 --- a/wrapanapi/utils/logger_mixin.py +++ b/wrapanapi/utils/logger_mixin.py @@ -2,6 +2,7 @@ Contains utils used by the base classes """ + import logging