Esempio 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'
Esempio 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'
Esempio n. 3
0
def newnode(request):
    error = None
    favcount = 0
    if request.method == 'POST':
        error, form, node = save_new_node(request.POST, request.user)
        if error is None:
            return HttpResponse('<p>success</p>')
    else:
        form = NodeForm(request.GET.get("provider"))
        favcount = Image.objects.filter(provider=request.GET.get("provider"),
                                        favorite=True).count()
    if error == 'form':
        error = None
    return render_to_response('node_form.html', {
        'form': form,
        'favcount': favcount,
        'error': error
    })