From a453d5fa874acdbd78f265a6195910c7572cdfa2 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Wed, 24 Jan 2024 12:04:36 +0100 Subject: views: Check HTTP GET response from ogserver Check that GET response is ok. Otherwise, show error message to the user and redirect --- ogcp/views.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'ogcp') diff --git a/ogcp/views.py b/ogcp/views.py index e8267a0..41576af 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -671,6 +671,9 @@ def action_image_restore(): image_id = form.image.data server = get_server_from_clients(ips) r = server.get('/images') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) images_list = r.json()['images'] image = search_image(images_list, int(image_id)) if not image: @@ -773,6 +776,9 @@ def action_hardware(): form.ips.data = ' '.join(ips) server = get_server_from_clients(ips) r = server.get('/hardware', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) hardware = r.json()['hardware'] return render_template('actions/hardware.html', form=form, hardware=hardware, scopes=scopes) @@ -814,6 +820,9 @@ def action_software(): form.ips.data = ' '.join(ips) server = get_server_from_clients(ips) r = server.get('/client/setup', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) if not r.json()['partitions']: flash(_('Software inventory is not available. Boot client in ogLive mode to obtain it'), category='error') @@ -851,6 +860,10 @@ def action_session(): server = get_server_from_clients(list(ips)) form.ips.data = ' '.join(ips) r = server.get('/session', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + sessions = r.json()['sessions'] if not sessions: flash(_('ogServer returned an empty session list'), @@ -879,6 +892,10 @@ def action_client_info(): payload = {'client': list(ips)} r = server.get('/client/info', payload) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + db_client = r.json() form.name.data = db_client['name'] @@ -915,10 +932,18 @@ def action_client_info(): form.livedir.choices.append(choice) r = server.get('/mode') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + available_modes = [(mode, mode) for mode in r.json()['modes']] form.boot.choices = list(available_modes) r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + rooms = parse_scopes_from_tree(r.json(), 'room') rooms = [(room['id'], room['name']) for room in rooms if room['id'] == int(db_client['room'])] @@ -927,6 +952,10 @@ def action_client_info(): form.submit.render_kw = {"style": "visibility:hidden;"} r = server.get('/images') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + images = r.json()['images'] ip = list(ips)[0] @@ -985,6 +1014,10 @@ def action_client_update(): payload = {'client': list(ips)} r = server.get('/client/info', payload) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + db_client = r.json() form.mac.render_kw = {'readonly': True} @@ -1000,6 +1033,9 @@ def action_client_update(): current_mode = db_client['boot'] r = server.get('/mode') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) available_modes = [(current_mode, current_mode)] available_modes.extend([(mode, mode) for mode in r.json()['modes'] if mode != current_mode]) @@ -1007,6 +1043,10 @@ def action_client_update(): form.boot.render_kw = {'readonly': True} r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + room_id = db_client['room'] rooms = parse_scopes_from_tree(r.json(), 'room') rooms = [(room['id'], room['name']) for room in rooms @@ -1170,11 +1210,19 @@ def action_client_add(): form.server.data = params['scope-server'] server = get_server_from_ip_port(params['scope-server']) r = server.get('/mode') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + available_modes = [(mode, mode) for mode in r.json()['modes']] form.boot.choices = list(available_modes) form.mac.render_kw = {'placeholder': 'aabbccddeeaa'} r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + room_id = params['scope-room'] rooms = parse_scopes_from_tree(r.json(), 'room') rooms = [(room['id'], room['name']) for room in rooms @@ -1214,6 +1262,10 @@ def action_clients_import_get(): form.server.data = params['scope-server'] server = get_server_from_ip_port(params['scope-server']) r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + rooms = parse_scopes_from_tree(r.json(), 'room') selected_room_id = params['scope-room'] selected_room = [(room['id'], room['name'] + " (" + room['parent'] + ")") @@ -1433,6 +1485,10 @@ def action_image_create(): ip = form.ip.data server = get_server_from_clients([ip]) r = server.get('/client/info', payload={"client": [ip]}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + disk, partition, code = form.os.data.split(' ') payload = {"clients": [ip], "disk": disk, @@ -1456,6 +1512,9 @@ def action_image_create(): server = get_server_from_clients(ips) r = server.get('/client/setup', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) for part in r.json()['partitions'][1:]: form.os.choices.append( @@ -1465,6 +1524,10 @@ def action_image_create(): f"{FS_CODES[part.get('filesystem')]}") ) r = server.get('/client/info', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + client_repo_id = r.json()['repo_id'] repositories = get_repositories(server) form.repository.choices = [ (repo['id'], repo['name']) for repo in repositories @@ -1486,6 +1549,10 @@ def action_image_update(): image_id = form.image.data server = get_server_from_clients([ip]) r = server.get('/images') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + images_list = r.json()['images'] image = search_image(images_list, int(image_id)) if not image: @@ -1518,6 +1585,10 @@ def action_image_update(): server = get_server_from_clients(ips) r = server.get('/client/info', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + repo_id = r.json()['repo_id'] images = get_images_grouped_by_repos_from_server(server) if repo_id not in images: @@ -1528,6 +1599,10 @@ def action_image_update(): form.image.choices.append((image['id'], image['name'])) r = server.get('/client/setup', payload={'client': list(ips)}) + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('commands')) + for part in r.json()['partitions'][1:]: form.os.choices.append( (f"{part.get('disk')} {part.get('partition')} {part.get('code')}", @@ -1636,6 +1711,10 @@ def action_center_delete(): return redirect(url_for('scopes')) server = get_server_from_ip_port(params['scope-server']) r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + selected_center_id = params['scope-center'] centers = parse_scopes_from_tree(r.json(), 'center') selected_center = [(center['id'], center['name']) for center in centers @@ -1671,6 +1750,10 @@ def action_room_add(): return redirect(url_for('scopes')) server = get_server_from_ip_port(params['scope-server']) r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + selected_center_id = params['scope-center'] centers = parse_scopes_from_tree(r.json(), 'center') selected_center = [(center['id'], center['name']) for center in centers @@ -1709,6 +1792,10 @@ def action_room_delete(): return redirect(url_for('scopes')) server = get_server_from_ip_port(params['scope-server']) r = server.get('/scopes') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('scopes')) + rooms = parse_scopes_from_tree(r.json(), 'room') selected_room_id = params['scope-room'] selected_room = [(room['id'], room['name'] + " (" + room['parent'] + ")") @@ -2230,6 +2317,9 @@ def action_image_info(): id = ids.pop() server = get_server_from_ip_port(params['image-server']) r = server.get('/images') + if r.status_code != requests.codes.ok: + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for('images')) images = r.json()['images'] image = next(img for img in images if img['id'] == int(id)) -- cgit v1.2.3-18-g5258