def setHost(name, url_back): """edit host Edit an existing host Arguments: name - name of the host to be edited url_back - url for redirecting when finished Return: if form validates edit the host, if doesn't show a page with the errors """ form = HostForm(request.form) host = APIObject(name, 'host') host_to_edit = host.show() if request.method == 'POST' and form.validate(): host.edit(new_name=app.config['ID_COLE'] + 'HOST_' + form.name.data, ipv4_address=form.ipv4_address.data) api.api_call('publish') flash('Edited team') return redirect(url_for(url_back)) return render_template('edit-host.html', form=form, host_to_edit=host_to_edit, url_back=url_back)
def addHost(group_name, url_back): """add host Add a new host inside a group Arguments: group_nam - the name of the group where the host has to be added url_back - url for redirecting when finished Return: if form validates, create the host and add it to the group, if doesn't render a page with the errors """ form = HostForm(request.form) if request.method == 'POST' and form.validate(): host = APIObject('HOST_' + form.name.data, 'host') add = host.add(ipv4_address=form.ipv4_address.data) if not add.success: if 'errors' in add.res_obj['data']: message = add.res_obj['data']['errors'][0]['message'] if message[:26] == 'More than one object named': flash('A computer with that name already exists') if 'warnings' in add.res_obj['data']: message = add.res_obj['data']['warnings'][0]['message'] if message[:37] == 'More than one object have the same IP': flash('A computer with this IP already exists') else: host.add_to_group('set-group', group_name) api.api_call('publish') flash('Equipment added') return redirect(url_for(url_back)) else: return render_template('form-errors.html', form=form, url_back=url_back)
def host_create(): if not can_create(current_user): abort(401) form = HostForm(request.form) db_session = DBSession() 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(): db_session = DBSession() try: host = get_host(db_session, form.hostname.data) if host 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, enable_password=form.enable_password.data, port_number=form.port_number.data, jump_host_id=form.jump_host.data, created_by=current_user.username) finally: db_session.rollback() return redirect(url_for('home')) return render_template('host/edit.html', form=form)
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)