Ejemplo n.º 1
0
def host_edit(hostname):
    db_session = DBSession()

    host = get_host(db_session, hostname)
    if host is None:
        abort(404)

    form = HostForm(request.form)
    fill_jump_hosts(db_session, form.jump_host.choices)
    fill_regions(db_session, form.region.choices)
    fill_software_profiles(db_session, form.software_profile.choices)

    if request.method == 'POST' and form.validate():
        if not can_edit(current_user):
            abort(401)

        # Editing a hostname which has already existed in the database.
        if hostname != form.hostname.data and get_host(
                db_session, form.hostname.data) is not None:
            return render_template('host/edit.html',
                                   form=form,
                                   duplicate_error=True)

        create_or_update_host(
            db_session=db_session,
            hostname=form.hostname.data,
            region_id=form.region.data,
            location=form.location.data,
            roles=form.roles.data,
            software_profile_id=form.software_profile.data,
            connection_type=form.connection_type.data,
            host_or_ip=form.host_or_ip.data,
            username=form.username.data,
            password=form.password.data if len(form.password.data) > 0 else
            host.connection_param[0].password,
            enable_password=form.enable_password.data
            if len(form.enable_password.data) > 0 else
            host.connection_param[0].enable_password,
            port_number=form.port_number.data,
            jump_host_id=form.jump_host.data,
            created_by=current_user.username,
            host=host)

        return_url = get_return_url(request, 'home')

        if return_url is None:
            return redirect(url_for('home'))
        else:
            return redirect(url_for(return_url, hostname=hostname))
    else:
        # Assign the values to form fields
        form.hostname.data = host.hostname
        form.region.data = host.region_id
        form.software_profile.data = host.software_profile_id
        form.location.data = host.location
        form.roles.data = host.roles
        form.host_or_ip.data = host.connection_param[0].host_or_ip
        form.username.data = host.connection_param[0].username
        form.jump_host.data = host.connection_param[0].jump_host_id
        form.connection_type.data = host.connection_param[0].connection_type
        form.port_number.data = host.connection_param[0].port_number
        if not is_empty(host.connection_param[0].password):
            form.password_placeholder = 'Use Password on File'
        else:
            form.password_placeholder = 'No Password Specified'

        if not is_empty(host.connection_param[0].enable_password):
            form.enable_password_placeholder = 'Use Password on File'
        else:
            form.enable_password_placeholder = 'No Password Specified'

        return render_template('host/edit.html', form=form)