Skip to content

Commit 7252a7a

Browse files
committed
compute: Migrate 'host set' to SDK
This was the sole outstanding command to be migrated to SDK. We also clean up the old in-tree wrappers we have in the process and add missing error checks for the 'host list' and 'host show' commands. Change-Id: I5635469b63ab3370fb5118e4f8a1758002381aa5 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 0f07c97 commit 7252a7a

6 files changed

Lines changed: 122 additions & 319 deletions

File tree

openstackclient/api/compute_v2.py

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from osc_lib.i18n import _
2020

2121

22-
# TODO(dtroyer): Mingrate this to osc-lib
22+
# TODO(dtroyer): Migrate this to osc-lib
2323
class InvalidValue(Exception):
2424
"""An argument value is not valid: wrong type, out of range, etc"""
2525

@@ -269,80 +269,6 @@ def floating_ip_pool_list(
269269

270270
return self.list(url)["floating_ip_pools"]
271271

272-
# Hosts
273-
274-
def host_list(
275-
self,
276-
zone=None,
277-
):
278-
"""Lists hypervisor Hosts
279-
280-
https://docs.openstack.org/api-ref/compute/#list-hosts
281-
Valid for Compute 2.0 - 2.42
282-
283-
:param string zone:
284-
Availability zone
285-
:returns: A dict of the floating IP attributes
286-
"""
287-
288-
url = "/os-hosts"
289-
if zone:
290-
url = '/os-hosts?zone=%s' % zone
291-
292-
return self.list(url)["hosts"]
293-
294-
def host_set(
295-
self, host=None, status=None, maintenance_mode=None, **params
296-
):
297-
"""Modify host properties
298-
299-
https://docs.openstack.org/api-ref/compute/#update-host-status
300-
Valid for Compute 2.0 - 2.42
301-
302-
status
303-
maintenance_mode
304-
"""
305-
306-
url = "/os-hosts"
307-
308-
params = {}
309-
if status:
310-
params['status'] = status
311-
if maintenance_mode:
312-
params['maintenance_mode'] = maintenance_mode
313-
if params == {}:
314-
# Don't bother calling if nothing given
315-
return None
316-
else:
317-
return self._request(
318-
"PUT",
319-
f"/{url}/{host}",
320-
json=params,
321-
).json()
322-
323-
def host_show(
324-
self,
325-
host=None,
326-
):
327-
"""Show host
328-
329-
https://docs.openstack.org/api-ref/compute/#show-host-details
330-
Valid for Compute 2.0 - 2.42
331-
"""
332-
333-
url = "/os-hosts"
334-
335-
r_host = self.find(
336-
url,
337-
attr='host_name',
338-
value=host,
339-
)
340-
341-
data = []
342-
for h in r_host:
343-
data.append(h['resource'])
344-
return data
345-
346272
# Networks
347273

348274
def network_create(

openstackclient/compute/v2/host.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
"""Host action implementations"""
1717

18+
from openstack import exceptions as sdk_exceptions
1819
from osc_lib.command import command
1920
from osc_lib import utils
2021

@@ -35,34 +36,26 @@ def get_parser(self, prog_name):
3536

3637
def take_action(self, parsed_args):
3738
compute_client = self.app.client_manager.sdk_connection.compute
38-
columns = ("Host Name", "Service", "Zone")
3939

4040
self.log.warning(
41-
"API has been deprecated. "
42-
"Please consider using 'hypervisor list' instead."
41+
"API has been deprecated; "
42+
"consider using 'hypervisor list' instead."
4343
)
4444

4545
# doing this since openstacksdk has decided not to support this
4646
# deprecated command
47-
hosts = (
48-
compute_client.get('/os-hosts', microversion='2.1')
49-
.json()
50-
.get('hosts')
51-
)
52-
47+
response = compute_client.get('/os-hosts', microversion='2.1')
48+
sdk_exceptions.raise_from_response(response)
49+
hosts = response.json().get('hosts')
5350
if parsed_args.zone is not None:
54-
filtered_hosts = []
55-
for host in hosts:
56-
if host['zone'] == parsed_args.zone:
57-
filtered_hosts.append(host)
58-
59-
hosts = filtered_hosts
51+
hosts = [h for h in hosts if h['zone'] == parsed_args.zone]
6052

53+
columns = ("Host Name", "Service", "Zone")
6154
return columns, (utils.get_dict_properties(s, columns) for s in hosts)
6255

6356

6457
class SetHost(command.Command):
65-
_description = _("Set host properties")
58+
_description = _("DEPRECATED: Set host properties")
6659

6760
def get_parser(self, prog_name):
6861
parser = super().get_parser(prog_name)
@@ -90,20 +83,33 @@ def get_parser(self, prog_name):
9083
return parser
9184

9285
def take_action(self, parsed_args):
93-
kwargs = {}
86+
compute_client = self.app.client_manager.sdk_connection.compute
9487

88+
self.log.warning(
89+
"API has been deprecated; "
90+
"consider using 'compute service set' instead."
91+
)
92+
93+
data = {}
9594
if parsed_args.enable:
96-
kwargs['status'] = 'enable'
95+
data['status'] = 'enable'
9796
if parsed_args.disable:
98-
kwargs['status'] = 'disable'
97+
data['status'] = 'disable'
9998
if parsed_args.enable_maintenance:
100-
kwargs['maintenance_mode'] = 'enable'
99+
data['maintenance_mode'] = 'enable'
101100
if parsed_args.disable_maintenance:
102-
kwargs['maintenance_mode'] = 'disable'
101+
data['maintenance_mode'] = 'disable'
103102

104-
compute_client = self.app.client_manager.compute
103+
if not data:
104+
# don't bother calling if nothing given
105+
return
105106

106-
compute_client.api.host_set(parsed_args.host, **kwargs)
107+
# doing this since openstacksdk has decided not to support this
108+
# deprecated command
109+
response = compute_client.put(
110+
f'/os-hosts/{parsed_args.host}', json=data, microversion='2.1'
111+
)
112+
sdk_exceptions.raise_from_response(response)
107113

108114

109115
class ShowHost(command.Lister):
@@ -116,26 +122,25 @@ def get_parser(self, prog_name):
116122

117123
def take_action(self, parsed_args):
118124
compute_client = self.app.client_manager.sdk_connection.compute
119-
columns = ("Host", "Project", "CPU", "Memory MB", "Disk GB")
120125

121126
self.log.warning(
122-
"API has been deprecated. "
123-
"Please consider using 'hypervisor show' instead."
127+
"API has been deprecated; "
128+
"consider using 'hypervisor show' instead."
124129
)
125130

126131
# doing this since openstacksdk has decided not to support this
127132
# deprecated command
128-
resources = (
129-
compute_client.get(
130-
'/os-hosts/' + parsed_args.host, microversion='2.1'
131-
)
132-
.json()
133-
.get('host')
133+
response = compute_client.get(
134+
f'/os-hosts/{parsed_args.host}', microversion='2.1'
134135
)
136+
sdk_exceptions.raise_from_response(response)
137+
resources = response.json().get('host')
135138

136139
data = []
137140
if resources is not None:
138141
for resource in resources:
139142
data.append(resource['resource'])
140143

144+
columns = ("Host", "Project", "CPU", "Memory MB", "Disk GB")
145+
141146
return columns, (utils.get_dict_properties(s, columns) for s in data)

openstackclient/tests/unit/api/test_compute_v2.py

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -227,114 +227,6 @@ def test_floating_ip_pool_list(self):
227227
self.assertEqual(self.LIST_FLOATING_IP_POOL_RESP, ret)
228228

229229

230-
class TestHost(TestComputeAPIv2):
231-
FAKE_HOST_RESP_1 = {
232-
"zone": "internal",
233-
"host_name": "myhost",
234-
"service": "conductor",
235-
}
236-
237-
FAKE_HOST_RESP_2 = {
238-
"zone": "internal",
239-
"host_name": "myhost",
240-
"service": "scheduler",
241-
}
242-
243-
FAKE_HOST_RESP_3 = {
244-
"zone": "nova",
245-
"host_name": "myhost",
246-
"service": "compute",
247-
}
248-
249-
LIST_HOST_RESP = [
250-
FAKE_HOST_RESP_1,
251-
FAKE_HOST_RESP_2,
252-
FAKE_HOST_RESP_3,
253-
]
254-
255-
def test_host_list_no_options(self):
256-
self.requests_mock.register_uri(
257-
'GET',
258-
FAKE_URL + '/os-hosts',
259-
json={'hosts': self.LIST_HOST_RESP},
260-
status_code=200,
261-
)
262-
ret = self.api.host_list()
263-
self.assertEqual(self.LIST_HOST_RESP, ret)
264-
265-
def test_host_list_zone(self):
266-
self.requests_mock.register_uri(
267-
'GET',
268-
FAKE_URL + '/os-hosts?zone=nova',
269-
json={'hosts': [self.FAKE_HOST_RESP_3]},
270-
status_code=200,
271-
)
272-
self.requests_mock.register_uri(
273-
'GET',
274-
FAKE_URL + '/os-hosts',
275-
json={'hosts': [self.FAKE_HOST_RESP_3]},
276-
status_code=200,
277-
)
278-
ret = self.api.host_list(zone='nova')
279-
self.assertEqual([self.FAKE_HOST_RESP_3], ret)
280-
281-
def test_host_set_none(self):
282-
ret = self.api.host_set(host='myhost')
283-
self.assertIsNone(ret)
284-
285-
def test_host_set(self):
286-
self.requests_mock.register_uri(
287-
'PUT',
288-
FAKE_URL + '/os-hosts/myhost',
289-
json={},
290-
status_code=200,
291-
)
292-
ret = self.api.host_set(host='myhost', status='enabled')
293-
self.assertEqual({}, ret)
294-
295-
def test_host_show(self):
296-
FAKE_RESOURCE_1 = {
297-
"cpu": 2,
298-
"disk_gb": 1028,
299-
"host": "c1a7de0ac9d94e4baceae031d05caae3",
300-
"memory_mb": 8192,
301-
"project": "(total)",
302-
}
303-
FAKE_RESOURCE_2 = {
304-
"cpu": 0,
305-
"disk_gb": 0,
306-
"host": "c1a7de0ac9d94e4baceae031d05caae3",
307-
"memory_mb": 512,
308-
"project": "(used_now)",
309-
}
310-
FAKE_RESOURCE_3 = {
311-
"cpu": 0,
312-
"disk_gb": 0,
313-
"host": "c1a7de0ac9d94e4baceae031d05caae3",
314-
"memory_mb": 0,
315-
"project": "(used_max)",
316-
}
317-
FAKE_HOST_RESP = [
318-
{'resource': FAKE_RESOURCE_1},
319-
{'resource': FAKE_RESOURCE_2},
320-
{'resource': FAKE_RESOURCE_3},
321-
]
322-
FAKE_HOST_LIST = [
323-
FAKE_RESOURCE_1,
324-
FAKE_RESOURCE_2,
325-
FAKE_RESOURCE_3,
326-
]
327-
328-
self.requests_mock.register_uri(
329-
'GET',
330-
FAKE_URL + '/os-hosts/myhost',
331-
json={'host': FAKE_HOST_RESP},
332-
status_code=200,
333-
)
334-
ret = self.api.host_show(host='myhost')
335-
self.assertEqual(FAKE_HOST_LIST, ret)
336-
337-
338230
class TestNetwork(TestComputeAPIv2):
339231
FAKE_NETWORK_RESP = {
340232
'id': '1',

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ def __init__(self, **kwargs):
127127
self.keypairs = mock.Mock()
128128
self.keypairs.resource_class = fakes.FakeResource(None, {})
129129

130-
self.hosts = mock.Mock()
131-
self.hosts.resource_class = fakes.FakeResource(None, {})
132-
133130
self.server_groups = mock.Mock()
134131
self.server_groups.resource_class = fakes.FakeResource(None, {})
135132

@@ -1012,56 +1009,6 @@ def get_networks(networks=None, count=2):
10121009
return mock.Mock(side_effect=networks)
10131010

10141011

1015-
def create_one_host(attrs=None):
1016-
"""Create a fake host.
1017-
1018-
:param dict attrs:
1019-
A dictionary with all attributes
1020-
:return:
1021-
A FakeResource object, with uuid and other attributes
1022-
"""
1023-
attrs = attrs or {}
1024-
1025-
# Set default attributes.
1026-
host_info = {
1027-
"service_id": 1,
1028-
"host": "host1",
1029-
"uuid": 'host-id-' + uuid.uuid4().hex,
1030-
"vcpus": 10,
1031-
"memory_mb": 100,
1032-
"local_gb": 100,
1033-
"vcpus_used": 5,
1034-
"memory_mb_used": 50,
1035-
"local_gb_used": 10,
1036-
"hypervisor_type": "xen",
1037-
"hypervisor_version": 1,
1038-
"hypervisor_hostname": "devstack1",
1039-
"free_ram_mb": 50,
1040-
"free_disk_gb": 50,
1041-
"current_workload": 10,
1042-
"running_vms": 1,
1043-
"cpu_info": "",
1044-
"disk_available_least": 1,
1045-
"host_ip": "10.10.10.10",
1046-
"supported_instances": "",
1047-
"metrics": "",
1048-
"pci_stats": "",
1049-
"extra_resources": "",
1050-
"stats": "",
1051-
"numa_topology": "",
1052-
"ram_allocation_ratio": 1.0,
1053-
"cpu_allocation_ratio": 1.0,
1054-
"zone": 'zone-' + uuid.uuid4().hex,
1055-
"host_name": 'name-' + uuid.uuid4().hex,
1056-
"service": 'service-' + uuid.uuid4().hex,
1057-
"cpu": 4,
1058-
"disk_gb": 100,
1059-
'project': 'project-' + uuid.uuid4().hex,
1060-
}
1061-
host_info.update(attrs)
1062-
return host_info
1063-
1064-
10651012
def create_one_usage(attrs=None):
10661013
"""Create a fake usage.
10671014

0 commit comments

Comments
 (0)