예제 #1
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
예제 #2
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
예제 #3
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
예제 #4
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
예제 #5
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
예제 #6
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
예제 #7
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
예제 #8
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.")
    LOG().info("IP address %s/%d added to setting %s", address, prefix, setting.Caption)
    return 0