From efa8c6815da6a4f2744142a8b3160b838f2c7563 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 12 Nov 2020 19:47:46 +0100 Subject: [PATCH 1/3] Fix AttributeErrors in nic methods --- pyvcloud/vcd/vm.py | 57 ++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/pyvcloud/vcd/vm.py b/pyvcloud/vcd/vm.py index acb2d2a4..a140484f 100644 --- a/pyvcloud/vcd/vm.py +++ b/pyvcloud/vcd/vm.py @@ -717,13 +717,14 @@ def delete_nic(self, index): indices = [None] * 10 nic_not_found = True # find the nic with the given index - for nc in net_conn_section.NetworkConnection: - if int(nc.NetworkConnectionIndex.text) == index: - net_conn_section.remove(nc) - nic_not_found = False - else: - indices[int(nc.NetworkConnectionIndex. - text)] = nc.NetworkConnectionIndex.text + if hasattr(net_conn_section, 'NetworkConnection'): + for nc in net_conn_section.NetworkConnection: + if int(nc.NetworkConnectionIndex.text) == index: + net_conn_section.remove(nc) + nic_not_found = False + else: + indices[int(nc.NetworkConnectionIndex. + text)] = nc.NetworkConnectionIndex.text if nic_not_found: raise InvalidParameterException( @@ -1381,27 +1382,29 @@ def update_nic(self, network_name, nic_id=0, # get network connection section. net_conn_section = self.get_resource().NetworkConnectionSection nic_index = 0 - nic_found = False - for network in net_conn_section.NetworkConnection: - if network.get('network') == network_name: - if network.NetworkConnectionIndex == nic_id: - nic_found = True - if ip_address is not None: - network.IpAddress = E.IpAddress(ip_address) - network.IsConnected = E.IsConnected(is_connected) - if ip_address_mode is not None: - network.IpAddressAllocationMode = \ - E.IpAddressAllocationMode(ip_address_mode) - if adapter_type is not None: - network.NetworkAdapterType = E.NetworkAdapterType( - adapter_type) - if is_primary: - nic_index = network.NetworkConnectionIndex - break - - if nic_found is False: + nic_not_found = True + if hasattr(net_conn_section, 'NetworkConnection'): + for nc in net_conn_section.NetworkConnection: + if nc.get('network') == network_name: + if int(nc.NetworkConnectionIndex.text) == nic_id: + nic_not_found = False + if ip_address is not None: + nc.IpAddress = E.IpAddress(ip_address) + nc.IsConnected = E.IsConnected(is_connected) + if ip_address_mode is not None: + nc.IpAddressAllocationMode = \ + E.IpAddressAllocationMode(ip_address_mode) + if adapter_type is not None: + nc.NetworkAdapterType = E.NetworkAdapterType( + adapter_type) + if is_primary: + nic_index = nc.NetworkConnectionIndex + break + + if nic_not_found: raise EntityNotFoundException( - 'VM Network with name \'%s\' not found.' % network_name) + 'Nic with name \'%s\' and index \'%s\' is not found in the VM \'%s\'' % + (network_name, nic_id, self.get_resource().get('name'))) if is_primary: nic_index = int(nic_index.text) From d483f656c0cb239af31af4d968a809cc114bf828 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 12 Nov 2020 23:16:35 +0100 Subject: [PATCH 2/3] Move update_nic to other nic methods --- pyvcloud/vcd/vm.py | 114 ++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/pyvcloud/vcd/vm.py b/pyvcloud/vcd/vm.py index a140484f..65640b14 100644 --- a/pyvcloud/vcd/vm.py +++ b/pyvcloud/vcd/vm.py @@ -698,6 +698,63 @@ def list_nics(self): nics.append(nic) return nics + def update_nic(self, network_name, nic_id=0, + is_connected=False, + is_primary=False, + ip_address_mode=None, + ip_address=None, + adapter_type=None): + """Updates a nic of the VM. + + :param str network_name: name of the network to be modified. + :param bool is_connected: True, if the nic has to be connected. + :param bool is_primary: True, if its a primary nic of the VM. + :param str ip_address_mode: One of DHCP|POOL|MANUAL|NONE. + :param str ip_address: to be set an ip in case of MANUAL mode. + :param str adapter_type: nic adapter type.One of NetworkAdapterType + values. + + :return: an object containing EntityType.TASK XML data which represents + the asynchronous task adding a nic. + + :rtype: lxml.objectify.ObjectifiedElement + """ + # get network connection section. + net_conn_section = self.get_resource().NetworkConnectionSection + nic_index = 0 + nic_not_found = True + if hasattr(net_conn_section, 'NetworkConnection'): + for nc in net_conn_section.NetworkConnection: + if nc.get('network') == network_name: + if int(nc.NetworkConnectionIndex.text) == nic_id: + nic_not_found = False + if ip_address is not None: + nc.IpAddress = E.IpAddress(ip_address) + nc.IsConnected = E.IsConnected(is_connected) + if ip_address_mode is not None: + nc.IpAddressAllocationMode = \ + E.IpAddressAllocationMode(ip_address_mode) + if adapter_type is not None: + nc.NetworkAdapterType = E.NetworkAdapterType( + adapter_type) + if is_primary: + nic_index = nc.NetworkConnectionIndex + break + + if nic_not_found: + raise EntityNotFoundException( + 'Nic with name \'%s\' and index \'%s\' is not found in the VM \'%s\'' % + (network_name, nic_id, self.get_resource().get('name'))) + + if is_primary: + nic_index = int(nic_index.text) + net_conn_section.PrimaryNetworkConnectionIndex = \ + E.PrimaryNetworkConnectionIndex(nic_index) + + return self.client.put_linked_resource( + net_conn_section, RelationType.EDIT, + EntityType.NETWORK_CONNECTION_SECTION.value, net_conn_section) + def delete_nic(self, index): """Deletes a nic from the VM. @@ -1358,63 +1415,6 @@ def relocate(self, datastore_id): EntityType.RELOCATE_PARAMS.value, relocate_params) - def update_nic(self, network_name, nic_id=0, - is_connected=False, - is_primary=False, - ip_address_mode=None, - ip_address=None, - adapter_type=None): - """Updates a nic of the VM. - - :param str network_name: name of the network to be modified. - :param bool is_connected: True, if the nic has to be connected. - :param bool is_primary: True, if its a primary nic of the VM. - :param str ip_address_mode: One of DHCP|POOL|MANUAL|NONE. - :param str ip_address: to be set an ip in case of MANUAL mode. - :param str adapter_type: nic adapter type.One of NetworkAdapterType - values. - - :return: an object containing EntityType.TASK XML data which represents - the asynchronous task adding a nic. - - :rtype: lxml.objectify.ObjectifiedElement - """ - # get network connection section. - net_conn_section = self.get_resource().NetworkConnectionSection - nic_index = 0 - nic_not_found = True - if hasattr(net_conn_section, 'NetworkConnection'): - for nc in net_conn_section.NetworkConnection: - if nc.get('network') == network_name: - if int(nc.NetworkConnectionIndex.text) == nic_id: - nic_not_found = False - if ip_address is not None: - nc.IpAddress = E.IpAddress(ip_address) - nc.IsConnected = E.IsConnected(is_connected) - if ip_address_mode is not None: - nc.IpAddressAllocationMode = \ - E.IpAddressAllocationMode(ip_address_mode) - if adapter_type is not None: - nc.NetworkAdapterType = E.NetworkAdapterType( - adapter_type) - if is_primary: - nic_index = nc.NetworkConnectionIndex - break - - if nic_not_found: - raise EntityNotFoundException( - 'Nic with name \'%s\' and index \'%s\' is not found in the VM \'%s\'' % - (network_name, nic_id, self.get_resource().get('name'))) - - if is_primary: - nic_index = int(nic_index.text) - net_conn_section.PrimaryNetworkConnectionIndex = \ - E.PrimaryNetworkConnectionIndex(nic_index) - - return self.client.put_linked_resource( - net_conn_section, RelationType.EDIT, - EntityType.NETWORK_CONNECTION_SECTION.value, net_conn_section) - def get_operating_system_section(self): """Get operating system section of VM. From f78cd213338a8d8519ab68217eabb37f3e0a95bb Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Fri, 13 Nov 2020 11:33:11 +0100 Subject: [PATCH 3/3] Fix linting errors --- pyvcloud/vcd/vm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyvcloud/vcd/vm.py b/pyvcloud/vcd/vm.py index 65640b14..f24cd383 100644 --- a/pyvcloud/vcd/vm.py +++ b/pyvcloud/vcd/vm.py @@ -743,7 +743,8 @@ def update_nic(self, network_name, nic_id=0, if nic_not_found: raise EntityNotFoundException( - 'Nic with name \'%s\' and index \'%s\' is not found in the VM \'%s\'' % + 'Nic with name ' + '\'%s\' and index \'%s\' is not found in the VM \'%s\'' % (network_name, nic_id, self.get_resource().get('name'))) if is_primary: