Exemplo n.º 1
0
def save_new_node(data, user):
    provider_id = data.get("provider")
    if not provider_id:
        return 'Incorrect provider id', None, None
    error = None
    form = None
    try:
        provider = Provider.objects.get(id=provider_id)
        form = NodeForm(provider_id, data)
    except Provider.DoesNotExist:
        error = 'Incorrect provider id'
    
    if form is not None:
        if form.is_valid():
            try:
                node = Node.objects.get(
                    provider=provider, name=form.cleaned_data['name']
                )
                error = 'A node with that name already exists'
            except Node.DoesNotExist:
                error, data_from_provider = provider.create_node(form)
                if error is None:
                    node = form.save(commit = False)
                    node.node_id    = str(data_from_provider['node_id'])
                    node.public_ip  = data_from_provider['public_ip']
                    node.private_ip = data_from_provider.get('private_ip', '')
                    node.state      = get_state(data_from_provider['state'])
                    node.created_by = user.username
                    node.save_extra_data(data_from_provider.get('extra', ''))
                    try:
                        node.save()
                        logging.info('New node created %s' % node)
                        # Mark image as recently used by saving it
                        if node.image is not None:
                            node.image.save()
                        return None, form, node
                    except Exception, e:
                        error = e
                        logging.error('Could not create node: %s' % e)
        else:
            error = 'form'
Exemplo n.º 2
0
def save_new_node(data, user):
    provider_id = data.get("provider")
    if not provider_id:
        return 'Incorrect provider id', None, None
    error = None
    form = None
    try:
        provider = Provider.objects.get(id=provider_id)
        form = NodeForm(provider_id, data)
    except Provider.DoesNotExist:
        error = 'Incorrect provider id'

    if form is not None:
        if form.is_valid():
            try:
                node = Node.objects.get(provider=provider,
                                        name=form.cleaned_data['name'])
                error = 'A node with that name already exists'
            except Node.DoesNotExist:
                error, data_from_provider = provider.create_node(form)
                if error is None:
                    node = form.save(commit=False)
                    node.node_id = str(data_from_provider['node_id'])
                    node.public_ip = data_from_provider['public_ip']
                    node.private_ip = data_from_provider.get('private_ip', '')
                    node.state = get_state(data_from_provider['state'])
                    node.created_by = user.username
                    node.save_extra_data(data_from_provider.get('extra', ''))
                    try:
                        node.save()
                        logging.info('New node created %s' % node)
                        # Mark image as recently used by saving it
                        if node.image is not None:
                            node.image.save()
                        return None, form, node
                    except Exception, e:
                        error = e
                        logging.error('Could not create node: %s' % e)
        else:
            error = 'form'