Skip to content

Commit 7ede1db

Browse files
committed
address-comments
1 parent e9e51f8 commit 7ede1db

1 file changed

Lines changed: 43 additions & 26 deletions

File tree

cloudify_libvirt/common.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,47 @@ def get_libvirt_params(**kwargs):
5050
return libvirt_auth, template_params
5151

5252

53+
def handle_device_type(d_type, device):
54+
handled_device = {}
55+
serial_values = ['source_path', 'target_port']
56+
usb_values = ['vendor_id', 'product_id']
57+
pci_values = ['bus', 'slot', 'function']
58+
tpm_values = ['path']
59+
valid_values = None
60+
if d_type == 'serial':
61+
valid_values = serial_values
62+
elif d_type == 'usb':
63+
valid_values = usb_values
64+
elif d_type == 'pci':
65+
valid_values = pci_values
66+
elif d_type == 'tpm':
67+
valid_values = tpm_values
68+
if valid_values:
69+
for k in device:
70+
if k in valid_values:
71+
handled_device[k] = device.get(k)
72+
return handled_device
73+
74+
75+
def handle_devices(devices):
76+
serial_devices = []
77+
usb_devices = []
78+
pci_devices = []
79+
tpm_devices = []
80+
for device in devices:
81+
to_append = handle_device_type(device.get('type'), device)
82+
if to_append:
83+
if device.get('type') == 'serial':
84+
serial_devices.append(to_append)
85+
elif device.get('type') == 'usb':
86+
usb_devices.append(to_append)
87+
elif device.get('type') == 'pci':
88+
pci_devices.append(to_append)
89+
elif device.get('type') == 'tpm':
90+
tpm_devices.append(to_append)
91+
return serial_devices, usb_devices, pci_devices, tpm_devices
92+
93+
5394
def gen_xml_template(kwargs, template_params, default_template):
5495
# templates
5596
template_resource = kwargs.get('template_resource')
@@ -73,32 +114,8 @@ def gen_xml_template(kwargs, template_params, default_template):
73114
# let's handle devices properly
74115
passthrough_devices = params.pop('devices', None)
75116
if passthrough_devices:
76-
serial_devices = []
77-
usb_devices = []
78-
pci_devices = []
79-
tpm_devices = []
80-
for device in passthrough_devices:
81-
if device.get('type') == 'serial':
82-
if 'source_path' in device and 'target_port' in device:
83-
serial_devices.append({
84-
'source_path': device.get('source_path'),
85-
'target_port': device.get('target_port')})
86-
elif device.get('type') == 'usb':
87-
if 'vendor_id' in device and 'product_id' in device:
88-
usb_devices.append({
89-
'vendor_id': device.get('vendor_id'),
90-
'product_id': device.get('product_id')})
91-
elif device.get('type') == 'pci':
92-
if 'bus' in device and 'slot' in device and \
93-
'function' in device:
94-
pci_devices.append({
95-
'bus': device.get('bus'),
96-
'slot': device.get('slot'),
97-
'function': device.get('function')})
98-
elif device.get('type') == 'tpm':
99-
if 'path' in device:
100-
tpm_devices.append({
101-
'path': device.get('path')})
117+
serial_devices, usb_devices, pci_devices, tpm_devices = \
118+
handle_devices(passthrough_devices)
102119
if serial_devices:
103120
params.update({'serial_devices': serial_devices})
104121
if usb_devices:

0 commit comments

Comments
 (0)