예제 #1
0
파일: common.py 프로젝트: AthinaB/synnefo
def get_resource(name, value, for_update=False):
    """Get object from DB based by it's ID

    Helper function for getting an object from DB by it's DB and raising
    appropriate command line errors if the object does not exist or the
    ID is invalid.

    """
    objects = RESOURCE_MAP[name]
    if name == "floating-ip":
        capital_name = "Floating IP"
    else:
        capital_name = name.capitalize()
    PREFIXED_RESOURCES = ["server", "network", "port", "volume"]

    if isinstance(value, basestring) and name in PREFIXED_RESOURCES:
        if value.startswith(settings.BACKEND_PREFIX_ID):
            try:
                if name == "server":
                    value = id_from_instance_name(value)
                elif name == "network":
                    value = id_from_network_name(value)
                elif name == "port":
                    value = id_from_nic_name(value)
                elif name == "volume":
                    value = id_from_disk_name(value)
            except ValueError:
                raise CommandError("Invalid {} ID: {}".format(capital_name,
                                                              value))

    if for_update:
        objects = objects.select_for_update()
    try:
        return objects.get(id=value)
    except ObjectDoesNotExist:
        msg = ("{0} with ID {1} does not exist. Use {2}-list to find out"
               " available {2} IDs.")
        raise CommandError(msg.format(capital_name, value, name))
    except (ValueError, TypeError):
        raise CommandError("Invalid {} ID: {}".format(capital_name, value))
예제 #2
0
def get_resource(name, value, for_update=False):
    """Get object from DB based by it's ID

    Helper function for getting an object from DB by it's DB and raising
    appropriate command line errors if the object does not exist or the
    ID is invalid.

    """
    objects = RESOURCE_MAP[name]
    if name == "floating-ip":
        capital_name = "Floating IP"
    else:
        capital_name = name.capitalize()
    PREFIXED_RESOURCES = ["server", "network", "port", "volume"]

    if isinstance(value, basestring) and name in PREFIXED_RESOURCES:
        if value.startswith(settings.BACKEND_PREFIX_ID):
            try:
                if name == "server":
                    value = id_from_instance_name(value)
                elif name == "network":
                    value = id_from_network_name(value)
                elif name == "port":
                    value = id_from_nic_name(value)
                elif name == "volume":
                    value = id_from_disk_name(value)
            except ValueError:
                raise CommandError("Invalid {} ID: {}".format(
                    capital_name, value))

    if for_update:
        objects = objects.select_for_update()
    try:
        return objects.get(id=value)
    except ObjectDoesNotExist:
        msg = ("{0} with ID {1} does not exist. Use {2}-list to find out"
               " available {2} IDs.")
        raise CommandError(msg.format(capital_name, value, name))
    except (ValueError, TypeError):
        raise CommandError("Invalid {} ID: {}".format(capital_name, value))
예제 #3
0
def process_ganeti_nics(ganeti_nics):
    """Process NIC dict from ganeti"""
    new_nics = []
    for index, gnic in enumerate(ganeti_nics):
        nic_name = gnic.get("name", None)
        if nic_name is not None:
            nic_id = utils.id_from_nic_name(nic_name)
        else:
            # Put as default value the index. If it is an unknown NIC to
            # synnefo it will be created automaticaly.
            nic_id = UNKNOWN_NIC_PREFIX + str(index)
        network_name = gnic.get('network', '')
        network_id = utils.id_from_network_name(network_name)
        network = Network.objects.get(id=network_id)

        # Get the new nic info
        mac = gnic.get('mac')
        ipv4 = gnic.get('ip')
        subnet6 = network.subnet6
        ipv6 = mac2eui64(mac, subnet6.cidr) if subnet6 else None

        firewall = gnic.get('firewall')
        firewall_profile = _reverse_tags.get(firewall)
        if not firewall_profile and network.public:
            firewall_profile = settings.DEFAULT_FIREWALL_PROFILE

        nic_info = {
            'index': index,
            'network': network,
            'mac': mac,
            'ipv4_address': ipv4,
            'ipv6_address': ipv6,
            'firewall_profile': firewall_profile,
            'state': 'ACTIVE'
        }

        new_nics.append((nic_id, nic_info))
    return dict(new_nics)
예제 #4
0
파일: backend.py 프로젝트: cstavr/synnefo
def process_ganeti_nics(ganeti_nics):
    """Process NIC dict from ganeti hooks."""
    new_nics = []
    for i, new_nic in enumerate(ganeti_nics):
        network_name = new_nic.get('network', '')
        network_id = utils.id_from_network_name(network_name)
        net = Network.objects.get(id=network_id)

        nic_name = new_nic.get("name", None)
        nic_id = None
        if nic_name is not None:
            nic_id = utils.id_from_nic_name(nic_name)

        # Get the new nic info
        mac = new_nic.get('mac', '')
        ipv4 = new_nic.get('ip', '')
        if net.subnet6:
            ipv6 = mac2eui64(mac, net.subnet6)
        else:
            ipv6 = ''

        firewall = new_nic.get('firewall', '')
        firewall_profile = _reverse_tags.get(firewall, '')
        if not firewall_profile and net.public:
            firewall_profile = settings.DEFAULT_FIREWALL_PROFILE

        nic = {
            'index': i,
            'network': net,
            'mac': mac,
            'ipv4': ipv4,
            'ipv6': ipv6,
            'firewall_profile': firewall_profile,
            'state': 'ACTIVE',
            'id': nic_id}

        new_nics.append(nic)
    return new_nics
예제 #5
0
파일: backend.py 프로젝트: apyrgio/synnefo
def process_ganeti_nics(ganeti_nics):
    """Process NIC dict from ganeti"""
    new_nics = []
    for index, gnic in enumerate(ganeti_nics):
        nic_name = gnic.get("name", None)
        if nic_name is not None:
            nic_id = utils.id_from_nic_name(nic_name)
        else:
            # Put as default value the index. If it is an unknown NIC to
            # synnefo it will be created automaticaly.
            nic_id = UNKNOWN_NIC_PREFIX + str(index)
        network_name = gnic.get('network', '')
        network_id = utils.id_from_network_name(network_name)
        network = Network.objects.get(id=network_id)

        # Get the new nic info
        mac = gnic.get('mac')
        ipv4 = gnic.get('ip')
        subnet6 = network.subnet6
        ipv6 = mac2eui64(mac, subnet6.cidr) if subnet6 else None

        firewall = gnic.get('firewall')
        firewall_profile = _reverse_tags.get(firewall)
        if not firewall_profile and network.public:
            firewall_profile = settings.DEFAULT_FIREWALL_PROFILE

        nic_info = {
            'index': index,
            'network': network,
            'mac': mac,
            'ipv4_address': ipv4,
            'ipv6_address': ipv6,
            'firewall_profile': firewall_profile,
            'state': 'ACTIVE'}

        new_nics.append((nic_id, nic_info))
    return dict(new_nics)