Skip to content
Snippets Groups Projects
Commit f3206d0a authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add button to delete hosts

- set cascade to all and delete-orphan to delete interfaces when
  deleting a host
- button added on the view host page to delete host (admin only)

JIRA INFRA-557 #action In Progress
parent 8471ec5b
No related branches found
No related tags found
No related merge requests found
......@@ -899,7 +899,11 @@ class Host(CreatedMixin, db.Model):
)
ansible_vars = db.Column(postgresql.JSONB)
interfaces = db.relationship("Interface", backref="host")
# Set cascade to all (to add delete) and delete-orphan to delete all interfaces
# when deleting a host
interfaces = db.relationship(
"Interface", backref="host", cascade="all, delete-orphan"
)
items = db.relationship("Item", backref="host")
ansible_groups = db.relationship(
"AnsibleGroup",
......
......@@ -107,6 +107,19 @@ def create_host():
return render_template("network/create_host.html", form=form)
@bp.route("/hosts/delete", methods=["POST"])
@login_groups_accepted("admin")
def delete_host():
host = models.Host.query.get_or_404(request.form["host_id"])
# Deleting the host will also delete all
# associated interfaces due to the cascade delete option
# defined on the model
db.session.delete(host)
db.session.commit()
flash(f"Host {host.name} has been deleted", "success")
return redirect(url_for("network.list_hosts"))
@bp.route("/hosts/view/<name>", methods=("GET", "POST"))
@login_required
def view_host(name):
......
......@@ -22,7 +22,22 @@
<div class="col-sm-9">
<dl class="row">
<dt class="col-sm-3">Hostname</dt>
<dd class="col-sm-9">{{ host.fqdn }}</dd>
<dd class="col-sm-9">
<div class="row">
<div class="col-sm-10">
{{ host.fqdn }}
</div>
{% if current_user.is_admin %}
<div class="col-sm-2 text-right">
<form method="POST" action="/network/hosts/delete">
<input id="host_id" name="host_id" type="hidden" value="{{ host.id }}">
{{ delete_button_with_confirmation("Delete host", "deleteConfirmation-%s" | format(host.id),
"Are you sure you want to delete the host %s?" | format(host.name)) }}
</form>
</div>
{% endif %}
</div>
</dd>
<dt class="col-sm-3">Device Type</dt>
<dd class="col-sm-9">{{ host.device_type }}</dd>
{% if host.items %}
......
docs/_static/network/delete_host_confirmation.png

161 KiB

docs/_static/network/view_host_delete.png

159 KiB

......@@ -49,6 +49,23 @@ If you need more than one interface, you should go to the *Add interface* pane f
.. image:: _static/add_interface.png
Delete a host
-------------
Note that this is restricted to admin users.
To delete a host, just click on the **trash** icon next to the hostname on the *View host* page.
.. image:: _static/network/view_host_delete.png
A confirmation window will be opened. You can confirm or cancel the action.
.. image:: _static/network/delete_host_confirmation.png
When deleting a host, all its interfaces are deleted as well.
The host is only deleted from CSEntry. Virtual machines are not automatically deleted.
DNS and DHCP update
-------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment