Skip to content

Commit 3aff444

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Migrate hypervisor stats commands to SDK"
2 parents 240bbe7 + 992cfdf commit 3aff444

4 files changed

Lines changed: 82 additions & 73 deletions

File tree

openstackclient/compute/v2/hypervisor_stats.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,49 @@
1111
# under the License.
1212
#
1313

14-
1514
"""Hypervisor Stats action implementations"""
1615

1716
from osc_lib.command import command
17+
from osc_lib import utils
1818

1919
from openstackclient.i18n import _
2020

2121

22+
def _get_hypervisor_stat_columns(item):
23+
column_map = {
24+
# NOTE(gtema): If we decide to use SDK names - empty this
25+
'disk_available': 'disk_available_least',
26+
'local_disk_free': 'free_disk_gb',
27+
'local_disk_size': 'local_gb',
28+
'local_disk_used': 'local_gb_used',
29+
'memory_free': 'free_ram_mb',
30+
'memory_size': 'memory_mb',
31+
'memory_used': 'memory_mb_used',
32+
33+
}
34+
hidden_columns = ['id', 'links', 'location', 'name']
35+
return utils.get_osc_show_columns_for_sdk_resource(
36+
item, column_map, hidden_columns)
37+
38+
2239
class ShowHypervisorStats(command.ShowOne):
2340
_description = _("Display hypervisor stats details")
2441

2542
def take_action(self, parsed_args):
26-
compute_client = self.app.client_manager.compute
27-
hypervisor_stats = compute_client.hypervisors.statistics().to_dict()
28-
29-
return zip(*sorted(hypervisor_stats.items()))
43+
# The command is deprecated since it is being dropped in Nova.
44+
self.log.warning(
45+
_("This command is deprecated.")
46+
)
47+
compute_client = self.app.client_manager.sdk_connection.compute
48+
# We do API request directly cause this deprecated method is not and
49+
# will not be supported by OpenStackSDK.
50+
response = compute_client.get(
51+
'/os-hypervisors/statistics',
52+
microversion='2.1')
53+
hypervisor_stats = response.json().get('hypervisor_statistics')
54+
55+
display_columns, columns = _get_hypervisor_stat_columns(
56+
hypervisor_stats)
57+
data = utils.get_dict_properties(
58+
hypervisor_stats, columns)
59+
return (display_columns, data)

openstackclient/tests/unit/compute/v2/fakes.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -341,67 +341,6 @@ def create_one_extension(attrs=None):
341341
return extension
342342

343343

344-
class FakeHypervisorStats(object):
345-
"""Fake one or more hypervisor stats."""
346-
347-
@staticmethod
348-
def create_one_hypervisor_stats(attrs=None):
349-
"""Create a fake hypervisor stats.
350-
351-
:param dict attrs:
352-
A dictionary with all attributes
353-
:return:
354-
A FakeResource object, with count, current_workload, and so on
355-
"""
356-
attrs = attrs or {}
357-
358-
# Set default attributes.
359-
stats_info = {
360-
'count': 2,
361-
'current_workload': 0,
362-
'disk_available_least': 50,
363-
'free_disk_gb': 100,
364-
'free_ram_mb': 23000,
365-
'local_gb': 100,
366-
'local_gb_used': 0,
367-
'memory_mb': 23800,
368-
'memory_mb_used': 1400,
369-
'running_vms': 3,
370-
'vcpus': 8,
371-
'vcpus_used': 3,
372-
}
373-
374-
# Overwrite default attributes.
375-
stats_info.update(attrs)
376-
377-
# Set default method.
378-
hypervisor_stats_method = {'to_dict': stats_info}
379-
380-
hypervisor_stats = fakes.FakeResource(
381-
info=copy.deepcopy(stats_info),
382-
methods=copy.deepcopy(hypervisor_stats_method),
383-
loaded=True)
384-
return hypervisor_stats
385-
386-
@staticmethod
387-
def create_hypervisors_stats(attrs=None, count=2):
388-
"""Create multiple fake hypervisors stats.
389-
390-
:param dict attrs:
391-
A dictionary with all attributes
392-
:param int count:
393-
The number of hypervisors to fake
394-
:return:
395-
A list of FakeResource objects faking the hypervisors
396-
"""
397-
hypervisors = []
398-
for i in range(0, count):
399-
hypervisors.append(
400-
FakeHypervisorStats.create_one_hypervisor_stats(attrs))
401-
402-
return hypervisors
403-
404-
405344
class FakeSecurityGroup(object):
406345
"""Fake one or more security groups."""
407346

openstackclient/tests/unit/compute/v2/test_hypervisor_stats.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
#
15+
from unittest import mock
1516

1617
from openstackclient.compute.v2 import hypervisor_stats
1718
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
19+
from openstackclient.tests.unit import fakes
1820

1921

2022
class TestHypervisorStats(compute_fakes.TestComputev2):
@@ -23,20 +25,55 @@ def setUp(self):
2325
super(TestHypervisorStats, self).setUp()
2426

2527
# Get a shortcut to the compute client hypervisors mock
26-
self.hypervisors_mock = self.app.client_manager.compute.hypervisors
27-
self.hypervisors_mock.reset_mock()
28+
self.app.client_manager.sdk_connection = mock.Mock()
29+
self.app.client_manager.sdk_connection.compute = mock.Mock()
30+
self.sdk_client = self.app.client_manager.sdk_connection.compute
31+
self.sdk_client.get = mock.Mock()
32+
33+
34+
# Not in fakes.py because hypervisor stats has been deprecated
35+
36+
def create_one_hypervisor_stats(attrs=None):
37+
"""Create a fake hypervisor stats.
38+
39+
:param dict attrs:
40+
A dictionary with all attributes
41+
:return:
42+
A dictionary that contains hypervisor stats information keys
43+
"""
44+
attrs = attrs or {}
45+
46+
# Set default attributes.
47+
stats_info = {
48+
'count': 2,
49+
'current_workload': 0,
50+
'disk_available_least': 50,
51+
'free_disk_gb': 100,
52+
'free_ram_mb': 23000,
53+
'local_gb': 100,
54+
'local_gb_used': 0,
55+
'memory_mb': 23800,
56+
'memory_mb_used': 1400,
57+
'running_vms': 3,
58+
'vcpus': 8,
59+
'vcpus_used': 3,
60+
}
61+
62+
# Overwrite default attributes.
63+
stats_info.update(attrs)
64+
65+
return stats_info
2866

2967

3068
class TestHypervisorStatsShow(TestHypervisorStats):
3169

70+
_stats = create_one_hypervisor_stats()
71+
3272
def setUp(self):
3373
super(TestHypervisorStatsShow, self).setUp()
3474

35-
self.hypervisor_stats = \
36-
compute_fakes.FakeHypervisorStats.create_one_hypervisor_stats()
37-
38-
self.hypervisors_mock.statistics.return_value =\
39-
self.hypervisor_stats
75+
self.sdk_client.get.return_value = fakes.FakeResponse(
76+
data={'hypervisor_statistics': self._stats})
4077

4178
self.cmd = hypervisor_stats.ShowHypervisorStats(self.app, None)
4279

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
features:
3+
- Switch hypervisor operations to consume OpenStackSDK

0 commit comments

Comments
 (0)