From c5c0766d2358516ccf0907e7b562d8444ab82bf0 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 12 Nov 2020 19:47:46 +0100 Subject: [PATCH] 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 5ad1ec4a0..b5013f12f 100644 --- a/pyvcloud/vcd/vm.py +++ b/pyvcloud/vcd/vm.py @@ -716,13 +716,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( @@ -1380,27 +1381,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( - 'Vapp 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)