Skip to content

Commit 70ea6c6

Browse files
authored
Remove duplicate get_cloud_connection methods (#1114)
Signed-off-by: Christian Berendt <berendt@osism.tech>
1 parent cd5f971 commit 70ea6c6

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

osism/commands/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
# SPDX-License-Identifier: Apache-2.0
2+
3+
from functools import lru_cache
4+
import keystoneauth1
5+
import openstack
6+
7+
8+
@lru_cache
9+
def get_cloud_connection(profile="admin"):
10+
try:
11+
conn = openstack.connect(cloud=profile)
12+
except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions:
13+
pass
14+
15+
return conn
16+
17+
18+
@lru_cache
19+
def get_cloud_project(project_id):
20+
conn = get_cloud_connection()
21+
return conn.identity.get_project(project_id)

osism/commands/compute.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
33
import time
44

55
from cliff.command import Command
6-
import keystoneauth1
76
from loguru import logger
87
import openstack
98
from tabulate import tabulate
109
from prompt_toolkit import prompt
1110

12-
13-
def get_cloud_connection():
14-
try:
15-
conn = openstack.connect(cloud="admin")
16-
except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions:
17-
pass
18-
19-
return conn
11+
from osism.commands import get_cloud_connection, get_cloud_project
2012

2113

2214
class ComputeEnable(Command):
@@ -303,8 +295,8 @@ def take_action(self, parsed_args):
303295
if project and server.project_id == project:
304296
result.append([server.id, server.name, server.status])
305297
elif domain:
306-
project = conn.identity.get_project(server.project_id)
307-
if project.domain_id == domain:
298+
server_project = get_cloud_project(server.project_id)
299+
if server_project.domain_id == domain:
308300
result.append([server.id, server.name, server.status])
309301
else:
310302
result.append([server.id, server.name, server.status])

osism/commands/server.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@
55

66
from cliff.command import Command
77
import dateutil
8-
import keystoneauth1
98
from loguru import logger
10-
import openstack
119
from tabulate import tabulate
1210
from prompt_toolkit import prompt
1311

14-
15-
def get_cloud_connection():
16-
try:
17-
conn = openstack.connect(cloud="admin")
18-
except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions:
19-
pass
20-
21-
return conn
12+
from osism.commands import get_cloud_connection
2213

2314

2415
class ServerMigrate(Command):

osism/commands/volume.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@
44

55
from cliff.command import Command
66
import dateutil
7-
import keystoneauth1
87
from loguru import logger
9-
import openstack
108
import pytz
119
from tabulate import tabulate
1210

13-
# from prompt_toolkit import prompt
14-
15-
16-
def get_cloud_connection():
17-
try:
18-
conn = openstack.connect(cloud="admin")
19-
except keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions:
20-
pass
21-
22-
return conn
11+
from osism.commands import get_cloud_connection
2312

2413

2514
class VolumeList(Command):

0 commit comments

Comments
 (0)