def remove_dns_server(ns, setting, address): ''' Remove dns server from given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value( "IPv%d" % version) for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.classname == "LMI_DNSSettingData" and settingData.ProtocolIFType == protocolIFType): for i in range(len(settingData.DNSServerAddresses)): if util.compare_address(settingData.DNSServerAddresses[i], address): del settingData.DNSServerAddresses[i] settingData.push() return 0 else: 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_static_route(ns, setting, address, prefix, metric=None, next_hop=None): ''' Remove all static routes and add given static route to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param metric: metric for the route or None :type gateway: int or None :param next_hop: IPv4 or IPv6 address for the next hop of the route or None :type next_hop: str or None ''' # Check the IP address address, version = util.address_check(address) # this is workaround for crashing provider, see: # https://bugzilla.redhat.com/show_bug.cgi?id=1067487 while 1: settingData = setting.first_associator( AssocClass="LMI_OrderedIPAssignmentComponent", ResultClass="LMI_IPRouteSettingData") 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 replace_dns_server(ns, setting, address): ''' Remove all dns servers and add given dns server to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value( "IPv%d" % version) for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.classname == "LMI_DNSSettingData" and settingData.ProtocolIFType == protocolIFType): settingData.DNSServerAddresses = [address] settingData.push() 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
def remove_dns_server(ns, setting, address): ''' Remove dns server from given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value("IPv%d" % version) for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.classname == "LMI_DNSSettingData" and settingData.ProtocolIFType == protocolIFType): dns = [] for addr in settingData.DNSServerAddresses: if not util.compare_address(addr, address): dns.append(addr) if len(dns) == len(settingData.DNSServerAddresses): raise LmiInvalidOptions("No DNS with address %s found for setting %s" % (address, setting.Caption)) settingData.DNSServerAddresses = dns settingData.push() return 0 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_ip_address(ns, setting, address, prefix, gateway=None): ''' Remove all IP addresses from given static setting and add new IP address. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param gateway: default gateway or None :type gateway: str or None ''' address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) gateway = _gateway_check(gateway, version) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict()["IPv%s" % version] found = False for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): settingData.IPAddresses = [address] if version == 4: settingData.SubnetMasks = [util.netmask_from_prefix(prefix)] else: settingData.IPv6SubnetPrefixLengths = [prefix] if gateway: settingData.GatewayAddresses = [gateway] else: settingData.GatewayAddresses = [""] found = True settingData.push() if not found: raise LmiInvalidOptions("Can't add IP address to setting: invalid setting type.") return 0
def remove_ip_address(ns, setting, address): ''' Remove the IP address from given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' address, version = util.address_check(address) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict()["IPv%s" % version] found = False for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): i = 0 while i < len(settingData.IPAddresses): if util.compare_address(settingData.IPAddresses[i], address): del settingData.IPAddresses[i] if version == 4: del settingData.SubnetMasks[i] else: del settingData.IPv6SubnetPrefixLengths[i] del settingData.GatewayAddresses[i] found = True i += 1 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 remove_ip_address(ns, setting, address): ''' Remove the IP address from given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' address, version = util.address_check(address) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict( )["IPv%s" % version] found = False for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): i = 0 while i < len(settingData.IPAddresses): if util.compare_address(settingData.IPAddresses[i], address): del settingData.IPAddresses[i] if version == 4: del settingData.SubnetMasks[i] else: del settingData.IPv6SubnetPrefixLengths[i] del settingData.GatewayAddresses[i] found = True i += 1 settingData.push() if not found: raise LmiInvalidOptions( "Can't remove IP address from setting: invalid setting type or address doesn't exist." ) return 0
def add_dns_server(ns, setting, address): ''' Add a dns server to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value( "IPv%d" % version) for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.classname == "LMI_DNSSettingData" and settingData.ProtocolIFType == protocolIFType): settingData.DNSServerAddresses.append(address) settingData.push() 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 _gateway_check(gateway, version): if gateway is None: return None try: gw, gateway_version = util.address_check(gateway) except util.IPCheckFailed: raise LmiInvalidOptions("Invalid gateway: %s" % gateway) if gateway_version != version: raise LmiInvalidOptions("Invalid gateway, should be IPv%d: %s" % (version, gateway)) return gw
def remove_ip_address(ns, setting, address): ''' Remove the IP address from given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' address, version = util.address_check(address) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict( )["IPv%s" % version] found = False for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): i = 0 # lmishell doesn't handle in-place editing of array parameters properly, # we need to create new arrays and then copy them back addresses = [] masks = [] gateways = [] while i < len(settingData.IPAddresses): if not util.compare_address(settingData.IPAddresses[i], address): addresses.append(settingData.IPAddresses[i]) if version == 4: masks.append(settingData.SubnetMasks[i]) else: masks.append(settingData.IPv6SubnetPrefixLengths[i]) gateways.append(settingData.GatewayAddresses[i]) else: found = True i += 1 settingData.IPAddresses = addresses if version == 4: settingData.SubnetMasks = masks else: settingData.IPv6SubnetPrefixLengths = masks settingData.GatewayAddresses = gateways 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 add_ip_address(ns, setting, address, prefix, gateway=None): ''' Add an IP address to the given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param gateway: default gateway or None :type gateway: str or None ''' address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) gateway = _gateway_check(gateway, version) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict( )["IPv%s" % version] found = False for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): # lmishell doesn't handle in-place editing of array parameters properly, # we need to create new arrays and then copy them back settingData.IPAddresses = settingData.IPAddresses + [address] if version == 4: settingData.SubnetMasks = settingData.SubnetMasks + [ util.netmask_from_prefix(prefix) ] else: settingData.IPv6SubnetPrefixLengths = settingData.IPv6SubnetPrefixLengths + [ str(prefix) ] if gateway: settingData.GatewayAddresses = settingData.GatewayAddresses + [ gateway ] else: settingData.GatewayAddresses = settingData.GatewayAddresses + [ "" ] found = True 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): ''' Remove the IP address from given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' address, version = util.address_check(address) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict()["IPv%s" % version] found = False for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): i = 0 # lmishell doesn't handle in-place editing of array parameters properly, # we need to create new arrays and then copy them back addresses = [] masks = [] gateways = [] while i < len(settingData.IPAddresses): if not util.compare_address(settingData.IPAddresses[i], address): addresses.append(settingData.IPAddresses[i]) if version == 4: masks.append(settingData.SubnetMasks[i]) else: masks.append(settingData.IPv6SubnetPrefixLengths[i]) gateways.append(settingData.GatewayAddresses[i]) else: found = True i += 1 settingData.IPAddresses = addresses if version == 4: settingData.SubnetMasks = masks else: settingData.IPv6SubnetPrefixLengths = masks settingData.GatewayAddresses = gateways 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_dns_server(ns, setting, address): ''' Remove all dns servers and add given dns server to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value("IPv%d" % version) for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.classname == "LMI_DNSSettingData" and settingData.ProtocolIFType == protocolIFType): settingData.DNSServerAddresses = [address] settingData.push() return 0 else: raise LmiInvalidOptions("Can't remove DNS address to setting %s, invalid setting type" % setting.Caption) return 0
def remove_static_route(ns, setting, address): ''' Remove static route to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) found = False for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent", ResultClass="LMI_IPRouteSettingData"): if util.compare_address(settingData.DestinationAddress, address): found = True 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 add_dns_server(ns, setting, address): ''' Add a dns server to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value("IPv%d" % version) for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.classname == "LMI_DNSSettingData" and settingData.ProtocolIFType == protocolIFType): settingData.DNSServerAddresses.append(address) settingData.push() 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 replace_ip_address(ns, setting, address, prefix, gateway=None): ''' Remove all IP addresses from given static setting and add new IP address. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param gateway: default gateway or None :type gateway: str or None ''' address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) gateway = _gateway_check(gateway, version) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict( )["IPv%s" % version] found = False for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): settingData.IPAddresses = [address] if version == 4: settingData.SubnetMasks = [util.netmask_from_prefix(prefix)] else: settingData.IPv6SubnetPrefixLengths = [prefix] if gateway: settingData.GatewayAddresses = [gateway] else: settingData.GatewayAddresses = [""] found = True 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
def add_static_route(ns, setting, address, prefix, metric=None, next_hop=None): ''' Add a static route to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param metric: metric for the route or None :type gateway: int or None :param next_hop: IPv4 or IPv6 address for the next hop of the route or None :type next_hop: str or None ''' # Check the IP address address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) if version == 4: result = setting.LMI_AddStaticIPRoute( AddressType=setting.LMI_AddStaticIPRoute.AddressTypeValues.IPv4, DestinationAddress=address, DestinationMask=util.netmask_from_prefix(prefix)) elif version == 6: result = setting.LMI_AddStaticIPRoute( AddressType=setting.LMI_AddStaticIPRoute.AddressTypeValues.IPv6, DestinationAddress=address, PrefixLength=prefix) else: raise LmiInvalidOptions("Invalid IP address: %s" % address) if result.rval != 0: raise LmiFailed("Unable to add static route: %s" % (result.errorstr or "unknown error")) route = result.rparams["Route"].to_instance() if metric is not None or next_hop is not None: if metric is not None: route.RouteMetric = metric 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): ''' Remove static route to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. ''' # Check the IP address address, version = util.address_check(address) found = False for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent", ResultClass="LMI_IPRouteSettingData"): if util.compare_address(settingData.DestinationAddress, address): found = True settingData.delete() if not found: raise LmiInvalidOptions("No such route: %s" % address) return 0
def add_ip_address(ns, setting, address, prefix, gateway=None): ''' Add an IP address to the given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param gateway: default gateway or None :type gateway: str or None ''' address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) gateway = _gateway_check(gateway, version) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict( )["IPv%s" % version] found = False for settingData in setting.associators( AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): settingData.IPAddresses.append(address) if version == 4: settingData.SubnetMasks.append( util.netmask_from_prefix(prefix)) else: settingData.IPv6SubnetPrefixLengths.append(str(prefix)) if gateway: settingData.GatewayAddresses.append(gateway) else: settingData.GatewayAddresses.append("") found = True settingData.push() if not found: raise LmiInvalidOptions( "Can't add IP address to setting: invalid setting type.") return 0
def add_ip_address(ns, setting, address, prefix, gateway=None): ''' Add an IP address to the given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param gateway: default gateway or None :type gateway: str or None ''' address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) gateway = _gateway_check(gateway, version) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict()["IPv%s" % version] found = False for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): # lmishell doesn't handle in-place editing of array parameters properly, # we need to create new arrays and then copy them back settingData.IPAddresses = settingData.IPAddresses + [address] if version == 4: settingData.SubnetMasks = settingData.SubnetMasks + [util.netmask_from_prefix(prefix)] else: settingData.IPv6SubnetPrefixLengths = settingData.IPv6SubnetPrefixLengths + [str(prefix)] if gateway: settingData.GatewayAddresses = settingData.GatewayAddresses + [gateway] else: settingData.GatewayAddresses = settingData.GatewayAddresses + [""] found = True 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 replace_static_route(ns, setting, address, prefix, metric=None, next_hop=None): ''' Remove all static routes and add given static route to the given setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param metric: metric for the route or None :type gateway: int or None :param next_hop: IPv4 or IPv6 address for the next hop of the route or None :type next_hop: str or None ''' # Check the IP address address, version = util.address_check(address) # this is workaround for crashing provider, see: # https://bugzilla.redhat.com/show_bug.cgi?id=1067487 while 1: settingData = setting.first_associator(AssocClass="LMI_OrderedIPAssignmentComponent", ResultClass="LMI_IPRouteSettingData") if settingData is None: break settingData.delete() return add_static_route(ns, setting, address, prefix, metric, next_hop)
def add_ip_address(ns, setting, address, prefix, gateway=None): ''' Add an IP address to the given static setting. :param LMI_IPAssignmentSettingData setting: network setting. :param str address: IPv4 or IPv6 address. :param int prefix: network prefix. :param gateway: default gateway or None :type gateway: str or None ''' address, version = util.address_check(address) prefix = util.prefix_check(prefix, version) gateway = _gateway_check(gateway, version) protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict()["IPv%s" % version] found = False for settingData in setting.associators(AssocClass="LMI_OrderedIPAssignmentComponent"): if (settingData.ProtocolIFType is not None and int(settingData.ProtocolIFType) == protocol and hasattr(settingData, "IPAddresses")): settingData.IPAddresses.append(address) if version == 4: settingData.SubnetMasks.append(util.netmask_from_prefix(prefix)) else: settingData.IPv6SubnetPrefixLengths.append(str(prefix)) if gateway: settingData.GatewayAddresses.append(gateway) else: settingData.GatewayAddresses.append("") found = True 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