From aafdf7e37f2d11ee0f13ee862c4c67826954ee6c Mon Sep 17 00:00:00 2001 From: Radek Novacek Date: Tue, 29 Apr 2014 13:10:26 +0200 Subject: networking: support deactivation of settings with older provider Older networking provider does not support deactivation setting without device specified. This commit adds a version check and forces use of device when older networking provider is found. --- .../networking/lmi/scripts/networking/__init__.py | 46 +++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/commands/networking/lmi/scripts/networking/__init__.py b/commands/networking/lmi/scripts/networking/__init__.py index 893ca29..160552b 100644 --- a/commands/networking/lmi/scripts/networking/__init__.py +++ b/commands/networking/lmi/scripts/networking/__init__.py @@ -33,7 +33,7 @@ LMI networking provider client library. import time from lmi.scripts.common.errors import LmiFailed, LmiInvalidOptions -from lmi.scripts.common import get_logger +from lmi.scripts.common import get_logger, versioncheck import util @@ -356,6 +356,16 @@ def get_static_routes(ns, setting): ''' return setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent", ResultClass="LMI_IPRouteSettingData") +def _provider_check_required_device(ns): + ''' + Check if provider requires device to be used in ApplySettingToIPNetworkConnection + ''' + # Do a full fetch to obtain qualifiers + if not ns.LMI_IPConfigurationService.is_fetched(True): + ns.LMI_IPConfigurationService.fetch(True) + version = versioncheck.get_class_version(ns.connection, 'LMI_IPConfigurationService', ns.name) + return versioncheck.parser.cmp_version(version, '0.2.3') + def activate(ns, setting, device=None): ''' Activate network setting on given device @@ -419,6 +429,40 @@ def deactivate(ns, setting, device=None): :type device: LMI_IPNetworkConnection or None ''' service = ns.LMI_IPConfigurationService.first_instance() + + if _provider_check_required_device(ns): + # old version of provider that doesn't support activation of setting without device + if device is None: + if setting.classname == 'LMI_BridgingMasterSettingData': + slave_class = 'LMI_BridgingSlaveSettingData' + elif setting.classname == 'LMI_BondingMasterSettingData': + slave_class = 'LMI_BondingSlaveSettingData' + else: + slave_class = None + + # Autodetect and deactivate slaves + if slave_class: + affected_settings = setting.associators(AssocClass='LMI_OrderedIPAssignmentComponent', ResultClass=slave_class) + else: + affected_settings = [setting] + + for slave_setting in affected_settings: + device = slave_setting.first_associator(AssocClass="LMI_IPElementSettingData", ResultClass="LMI_IPNetworkConnection") + result = service.SyncApplySettingToIPNetworkConnection(SettingData=slave_setting, + IPNetworkConnection=device, + Mode=service.ApplySettingToIPNetworkConnection.ModeValues.Mode32769) + if result.errorstr: + raise LmiFailed("Unable to deactivate setting: %s" % result.errorstr) + # TODO: rework using indications + isCurrent = True + while isCurrent: + for esd in slave_setting.references(ResultClass="LMI_IPElementSettingData"): + if esd.IsCurrent == ns.LMI_IPElementSettingData.IsCurrentValues.IsNotCurrent: + isCurrent = False + break + time.sleep(1) + return 0 + if device is not None: LOG().debug('Deactivating setting %s on device %s', setting.Caption, device.ElementName) result = service.SyncApplySettingToIPNetworkConnection(SettingData=setting, -- cgit From c05232b39f0c71d62f30e860bbe5d53330ef126f Mon Sep 17 00:00:00 2001 From: Radek Novacek Date: Tue, 29 Apr 2014 13:46:45 +0200 Subject: networking: add INFOs about performed actions --- commands/networking/lmi/scripts/networking/__init__.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/commands/networking/lmi/scripts/networking/__init__.py b/commands/networking/lmi/scripts/networking/__init__.py index 160552b..585c42b 100644 --- a/commands/networking/lmi/scripts/networking/__init__.py +++ b/commands/networking/lmi/scripts/networking/__init__.py @@ -400,6 +400,7 @@ def activate(ns, setting, device=None): isCurrent = True break time.sleep(1) + LOG().info("Setting %s activated", setting.Caption) return 0 devices = [] @@ -418,6 +419,7 @@ def activate(ns, setting, device=None): Mode=service.ApplySettingToIPNetworkConnection.ModeValues.Mode32768) if result.errorstr: raise LmiFailed("Unable to activate setting: %s" % result.errorstr) + LOG().info("Setting %s activated", setting.Caption) return 0 def deactivate(ns, setting, device=None): @@ -461,6 +463,7 @@ def deactivate(ns, setting, device=None): isCurrent = False break time.sleep(1) + LOG().info("Setting %s deactivated", setting.Caption) return 0 if device is not None: @@ -475,6 +478,7 @@ def deactivate(ns, setting, device=None): Mode=service.ApplySettingToIPNetworkConnection.ModeValues.Mode32769) if result.errorstr: raise LmiFailed("Unable to deactivate setting: %s" % result.errorstr) + LOG().info("Setting %s deactivated", setting.Caption) return 0 def enslave(ns, master_setting, device): @@ -489,6 +493,7 @@ def enslave(ns, master_setting, device): result = capability.LMI_CreateSlaveSetting(MasterSettingData=master_setting) if result.rval != 0: raise LmiFailed("Unable to create setting: %s" % result.errorstr) + LOG().info("Device %s enslaved to setting %s", (device.ElementName, master_setting.Caption)) return 0 def create_setting(ns, caption, device, type, ipv4method, ipv6method): @@ -512,6 +517,7 @@ def create_setting(ns, caption, device, type, ipv4method, ipv6method): IPv6Type=ipv6method) if result.rval != 0: raise LmiFailed("Unable to create setting: %s" % result.errorstr) + LOG().info("Setting %s created", caption) return 0 def delete_setting(ns, setting): @@ -520,7 +526,9 @@ def delete_setting(ns, setting): :param LMI_IPAssignmentSettingData setting: network setting. ''' + caption = setting.Caption setting.delete() + LOG().info("Setting %s deleted", caption) return 0 def add_ip_address(ns, setting, address, prefix, gateway=None): @@ -557,6 +565,7 @@ def add_ip_address(ns, setting, address, prefix, gateway=None): settingData.push() if not found: raise LmiInvalidOptions("Can't add IP address to setting: invalid setting type.") + LOG().info("IP address %s/%d added to setting %s", (address, prefix, setting.Caption)) return 0 def remove_ip_address(ns, setting, address): @@ -589,6 +598,7 @@ def remove_ip_address(ns, setting, address): settingData.push() if not found: raise LmiInvalidOptions("Can't remove IP address from setting: invalid setting type or address doesn't exist.") + LOG().info("IP address %s removed from setting %s", (address, setting.Caption)) return 0 def replace_ip_address(ns, setting, address, prefix, gateway=None): @@ -625,6 +635,7 @@ def replace_ip_address(ns, setting, address, prefix, gateway=None): settingData.push() if not found: raise LmiInvalidOptions("Can't add IP address to setting: invalid setting type.") + LOG().info("Existing addresses replaced with IP address %s/%d in setting %s", (address, prefix, setting.Caption)) return 0 @@ -665,6 +676,7 @@ def add_static_route(ns, setting, address, prefix, metric=None, next_hop=None): if next_hop is not None: route.NextHop = next_hop route.push() + LOG().info("Static route to %s/%d added to setting %s", (address, prefix, setting.Caption)) return 0 def remove_static_route(ns, setting, address): @@ -684,7 +696,7 @@ def remove_static_route(ns, setting, address): settingData.delete() if not found: raise LmiInvalidOptions("No such route: %s" % address) - + LOG().info("Static route to %s removed from setting %s", (address, setting.Caption)) return 0 def replace_static_route(ns, setting, address, prefix, metric=None, next_hop=None): @@ -709,6 +721,7 @@ def replace_static_route(ns, setting, address, prefix, metric=None, next_hop=Non if settingData is None: break settingData.delete() + LOG().info("Static routes replaced with route to %s/%d in setting %s", (address, prefix, setting.Caption)) return add_static_route(ns, setting, address, prefix, metric, next_hop) def add_dns_server(ns, setting, address): @@ -729,6 +742,7 @@ def add_dns_server(ns, setting, address): break else: raise LmiInvalidOptions("Can't assign DNS address to setting %s, invalid setting type" % setting.Caption) + LOG().info("DNS server %s/%d added to setting %s", (address, setting.Caption)) return 0 def remove_dns_server(ns, setting, address): @@ -753,6 +767,7 @@ def remove_dns_server(ns, setting, address): raise LmiInvalidOptions("No DNS with address %s found for setting %s" % (address, setting.Caption)) else: raise LmiInvalidOptions("Can't remove DNS address to setting %s, invalid setting type" % setting.Caption) + LOG().info("DNS server %s removed from setting %s", (address, setting.Caption)) return 0 def replace_dns_server(ns, setting, address): @@ -773,4 +788,5 @@ def replace_dns_server(ns, setting, address): return 0 else: raise LmiInvalidOptions("Can't remove DNS address to setting %s, invalid setting type" % setting.Caption) + LOG().info("Existing DNS servers replaced with %s in setting %s", (address, setting.Caption)) return 0 -- cgit From 04573fca595da773e373de1febc2dd3c0af3ba93 Mon Sep 17 00:00:00 2001 From: Radek Novacek Date: Tue, 29 Apr 2014 15:10:52 +0200 Subject: networking: fix print formatting for INFO messages --- .../networking/lmi/scripts/networking/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/commands/networking/lmi/scripts/networking/__init__.py b/commands/networking/lmi/scripts/networking/__init__.py index 585c42b..9da7110 100644 --- a/commands/networking/lmi/scripts/networking/__init__.py +++ b/commands/networking/lmi/scripts/networking/__init__.py @@ -493,7 +493,7 @@ def enslave(ns, master_setting, device): result = capability.LMI_CreateSlaveSetting(MasterSettingData=master_setting) if result.rval != 0: raise LmiFailed("Unable to create setting: %s" % result.errorstr) - LOG().info("Device %s enslaved to setting %s", (device.ElementName, master_setting.Caption)) + LOG().info("Device %s enslaved to setting %s", device.ElementName, master_setting.Caption) return 0 def create_setting(ns, caption, device, type, ipv4method, ipv6method): @@ -565,7 +565,7 @@ def add_ip_address(ns, setting, address, prefix, gateway=None): settingData.push() if not found: raise LmiInvalidOptions("Can't add IP address to setting: invalid setting type.") - LOG().info("IP address %s/%d added to setting %s", (address, prefix, setting.Caption)) + LOG().info("IP address %s/%d added to setting %s", address, prefix, setting.Caption) return 0 def remove_ip_address(ns, setting, address): @@ -598,7 +598,7 @@ def remove_ip_address(ns, setting, address): settingData.push() if not found: raise LmiInvalidOptions("Can't remove IP address from setting: invalid setting type or address doesn't exist.") - LOG().info("IP address %s removed from setting %s", (address, setting.Caption)) + LOG().info("IP address %s removed from setting %s", address, setting.Caption) return 0 def replace_ip_address(ns, setting, address, prefix, gateway=None): @@ -635,7 +635,7 @@ def replace_ip_address(ns, setting, address, prefix, gateway=None): settingData.push() if not found: raise LmiInvalidOptions("Can't add IP address to setting: invalid setting type.") - LOG().info("Existing addresses replaced with IP address %s/%d in setting %s", (address, prefix, setting.Caption)) + LOG().info("Existing addresses replaced with IP address %s/%d in setting %s", address, prefix, setting.Caption) return 0 @@ -676,7 +676,7 @@ def add_static_route(ns, setting, address, prefix, metric=None, next_hop=None): if next_hop is not None: route.NextHop = next_hop route.push() - LOG().info("Static route to %s/%d added to setting %s", (address, prefix, setting.Caption)) + LOG().info("Static route to %s/%d added to setting %s", address, prefix, setting.Caption) return 0 def remove_static_route(ns, setting, address): @@ -696,7 +696,7 @@ def remove_static_route(ns, setting, address): settingData.delete() if not found: raise LmiInvalidOptions("No such route: %s" % address) - LOG().info("Static route to %s removed from setting %s", (address, setting.Caption)) + LOG().info("Static route to %s removed from setting %s", address, setting.Caption) return 0 def replace_static_route(ns, setting, address, prefix, metric=None, next_hop=None): @@ -721,7 +721,7 @@ def replace_static_route(ns, setting, address, prefix, metric=None, next_hop=Non if settingData is None: break settingData.delete() - LOG().info("Static routes replaced with route to %s/%d in setting %s", (address, prefix, setting.Caption)) + LOG().info("Static routes replaced with route to %s/%d in setting %s", address, prefix, setting.Caption) return add_static_route(ns, setting, address, prefix, metric, next_hop) def add_dns_server(ns, setting, address): @@ -742,7 +742,7 @@ def add_dns_server(ns, setting, address): break else: raise LmiInvalidOptions("Can't assign DNS address to setting %s, invalid setting type" % setting.Caption) - LOG().info("DNS server %s/%d added to setting %s", (address, setting.Caption)) + LOG().info("DNS server %s/%d added to setting %s", address, setting.Caption) return 0 def remove_dns_server(ns, setting, address): @@ -767,7 +767,7 @@ def remove_dns_server(ns, setting, address): raise LmiInvalidOptions("No DNS with address %s found for setting %s" % (address, setting.Caption)) else: raise LmiInvalidOptions("Can't remove DNS address to setting %s, invalid setting type" % setting.Caption) - LOG().info("DNS server %s removed from setting %s", (address, setting.Caption)) + LOG().info("DNS server %s removed from setting %s", address, setting.Caption) return 0 def replace_dns_server(ns, setting, address): @@ -788,5 +788,5 @@ def replace_dns_server(ns, setting, address): return 0 else: raise LmiInvalidOptions("Can't remove DNS address to setting %s, invalid setting type" % setting.Caption) - LOG().info("Existing DNS servers replaced with %s in setting %s", (address, setting.Caption)) + LOG().info("Existing DNS servers replaced with %s in setting %s", address, setting.Caption) return 0 -- cgit