From 70ea6c6b7a5883b05e66107e0461f32341683d18 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Sun, 3 Nov 2024 18:50:29 +0100 Subject: [PATCH] Remove duplicate get_cloud_connection methods (#1114) Signed-off-by: Christian Berendt --- osism/commands/__init__.py | 20 ++++++++++++++++++++ osism/commands/compute.py | 14 +++----------- osism/commands/server.py | 11 +---------- osism/commands/volume.py | 13 +------------ 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/osism/commands/__init__.py b/osism/commands/__init__.py index 98813136..c1581917 100644 --- a/osism/commands/__init__.py +++ b/osism/commands/__init__.py @@ -1 +1,21 @@ # SPDX-License-Identifier: Apache-2.0 + +from functools import lru_cache +import keystoneauth1 +import openstack + + +@lru_cache +def get_cloud_connection(profile="admin"): + try: + conn = openstack.connect(cloud=profile) + except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions: + pass + + return conn + + +@lru_cache +def get_cloud_project(project_id): + conn = get_cloud_connection() + return conn.identity.get_project(project_id) diff --git a/osism/commands/compute.py b/osism/commands/compute.py index 747b4c9c..a60f0a54 100644 --- a/osism/commands/compute.py +++ b/osism/commands/compute.py @@ -3,20 +3,12 @@ import time from cliff.command import Command -import keystoneauth1 from loguru import logger import openstack from tabulate import tabulate from prompt_toolkit import prompt - -def get_cloud_connection(): - try: - conn = openstack.connect(cloud="admin") - except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions: - pass - - return conn +from osism.commands import get_cloud_connection, get_cloud_project class ComputeEnable(Command): @@ -303,8 +295,8 @@ def take_action(self, parsed_args): if project and server.project_id == project: result.append([server.id, server.name, server.status]) elif domain: - project = conn.identity.get_project(server.project_id) - if project.domain_id == domain: + server_project = get_cloud_project(server.project_id) + if server_project.domain_id == domain: result.append([server.id, server.name, server.status]) else: result.append([server.id, server.name, server.status]) diff --git a/osism/commands/server.py b/osism/commands/server.py index 3eb6d669..14463341 100644 --- a/osism/commands/server.py +++ b/osism/commands/server.py @@ -5,20 +5,11 @@ from cliff.command import Command import dateutil -import keystoneauth1 from loguru import logger -import openstack from tabulate import tabulate from prompt_toolkit import prompt - -def get_cloud_connection(): - try: - conn = openstack.connect(cloud="admin") - except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions: - pass - - return conn +from osism.commands import get_cloud_connection class ServerMigrate(Command): diff --git a/osism/commands/volume.py b/osism/commands/volume.py index 7d4726bf..3768d275 100644 --- a/osism/commands/volume.py +++ b/osism/commands/volume.py @@ -4,22 +4,11 @@ from cliff.command import Command import dateutil -import keystoneauth1 from loguru import logger -import openstack import pytz from tabulate import tabulate -# from prompt_toolkit import prompt - - -def get_cloud_connection(): - try: - conn = openstack.connect(cloud="admin") - except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions: - pass - - return conn +from osism.commands import get_cloud_connection class VolumeList(Command):