diff --git a/app/network/forms.py b/app/network/forms.py index d0fe5c212a59104ad6f0ca9998436304e3d71962..aa66f2340e3820d17951b83893f1a16ac0ffd41d 100644 --- a/app/network/forms.py +++ b/app/network/forms.py @@ -250,8 +250,12 @@ class AnsibleGroupForm(CSEntryForm): default=models.AnsibleGroupType.STATIC, coerce=models.AnsibleGroupType.coerce, ) + children = SelectMultipleField("Children", coerce=utils.coerce_to_str_or_none) hosts = SelectMultipleField("Hosts", coerce=utils.coerce_to_str_or_none) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.children.choices = utils.get_model_choices( + models.AnsibleGroup, attr="name" + ) self.hosts.choices = utils.get_model_choices(models.Host, attr="name") diff --git a/app/network/views.py b/app/network/views.py index 5932fa7c24b0aa6f207c8ec754ac7fe0763e6179..96c1f2ec149ad460b8a74914f9595227931236ae 100644 --- a/app/network/views.py +++ b/app/network/views.py @@ -346,11 +346,17 @@ def edit_ansible_group(name): # See https://stackoverflow.com/questions/5519729/wtforms-how-to-select-options-in-selectmultiplefield form.hosts.default = [host.id for host in group.hosts] form.hosts.process(request.form) + # Same for AnsibleGroup children + form.children.default = [child.id for child in group.children] + form.children.process(request.form) if form.validate_on_submit(): group.name = form.name.data group.vars = form.vars.data or None group.type = form.type.data group.hosts = [models.Host.query.get(id_) for id_ in form.hosts.data] + group.children = [ + models.AnsibleGroup.query.get(id_) for id_ in form.children.data + ] current_app.logger.debug(f"Trying to update: {group!r}") try: db.session.commit() @@ -370,11 +376,13 @@ def create_ansible_group(): form = AnsibleGroupForm() if form.validate_on_submit(): hosts = [models.Host.query.get(id_) for id_ in form.hosts.data] + children = [models.AnsibleGroup.query.get(id_) for id_ in form.children.data] group = models.AnsibleGroup( name=form.name.data, vars=form.vars.data or None, type=form.type.data, hosts=hosts, + children=children, ) current_app.logger.debug(f"Trying to create: {group!r}") db.session.add(group) diff --git a/app/static/js/groups.js b/app/static/js/groups.js index ec1bbf3721b36f7383507f88320e0d5c0a90fa79..e660d72b301951e1004e4e596d8d658df6b90549 100644 --- a/app/static/js/groups.js +++ b/app/static/js/groups.js @@ -58,6 +58,7 @@ $(document).ready(function() { } }, { data: 'type' }, + { data: 'children' }, { data: 'hosts' } ] }); diff --git a/app/templates/network/create_group.html b/app/templates/network/create_group.html index 89161907e0bc0ea6aa67ea17c9eb9a38bb0fbf3c..cd1504dc3581aa3213918a8e34e0114c201d5080 100644 --- a/app/templates/network/create_group.html +++ b/app/templates/network/create_group.html @@ -9,6 +9,7 @@ {{ render_field(form.name, class_="text-lowercase") }} {{ render_field(form.vars) }} {{ render_field(form.type) }} + {{ render_field(form.children, class_="selectpicker") }} {{ render_field(form.hosts, class_="selectpicker") }} <div class="form-group row"> <div class="col-sm-10"> diff --git a/app/templates/network/edit_group.html b/app/templates/network/edit_group.html index f19aaca4d2cca315c035d2e3b47a40e40f7e86ac..2b1ddd203bc37c3b1dd88ccafdc0fab387bf74ee 100644 --- a/app/templates/network/edit_group.html +++ b/app/templates/network/edit_group.html @@ -18,6 +18,7 @@ {{ render_field(form.name, class_="text-lowercase") }} {{ render_field(form.vars) }} {{ render_field(form.type) }} + {{ render_field(form.children, class_="selectpicker") }} {{ render_field(form.hosts, class_="selectpicker") }} <div class="form-group row"> <div class="col-sm-10"> diff --git a/app/templates/network/groups.html b/app/templates/network/groups.html index af1f517ecfd4c87c02917447bad2418005a3fb78..7d82d9d1a7570319af2a7aa2f6461461320c25bf 100644 --- a/app/templates/network/groups.html +++ b/app/templates/network/groups.html @@ -24,6 +24,7 @@ <th>Name</th> <th>Vars</th> <th>Type</th> + <th>Children</th> <th>Hosts</th> </tr> </thead> diff --git a/app/templates/network/view_group.html b/app/templates/network/view_group.html index 457879ef5f891af1d52c764b23822a27750fde7b..31e73e7a624260a4dfac80dcc80ff9318cdefb52 100644 --- a/app/templates/network/view_group.html +++ b/app/templates/network/view_group.html @@ -1,5 +1,5 @@ {% extends "network/groups.html" %} -{% from "_helpers.html" import link_to_hosts %} +{% from "_helpers.html" import link_to_hosts, link_to_ansible_groups %} {% block title %}View Ansible group - CSEntry{% endblock %} @@ -22,6 +22,14 @@ <dd class="col-sm-9"><pre>{{ group.vars | toyaml }}</pre></dd> <dt class="col-sm-3">Type</dt> <dd class="col-sm-9">{{ group.type.name }}</dd> + {% if group.parents %} + <dt class="col-sm-3">Parents</dt> + <dd class="col-sm-9">{{ link_to_ansible_groups(group.parents) }}</dd> + {% endif %} + {% if group.children %} + <dt class="col-sm-3">Children</dt> + <dd class="col-sm-9">{{ link_to_ansible_groups(group.children) }}</dd> + {% endif %} <dt class="col-sm-3">hosts</dt> <dd class="col-sm-9">{{ link_to_hosts(group.hosts) }}</dd> <dt class="col-sm-3">Created by</dt>