Commit 0d815cd3 by Bence Dányi

firewall_gui: allow to save new hosts

parent e6e4c489
...@@ -118,7 +118,7 @@ urlpatterns = patterns('', ...@@ -118,7 +118,7 @@ urlpatterns = patterns('',
url(r'^firewall/(?P<name>\w+)/(?P<id>\d+)/delete/', 'firewall_gui.views.delete_entity'), url(r'^firewall/(?P<name>\w+)/(?P<id>\d+)/delete/', 'firewall_gui.views.delete_entity'),
url(r'^firewall/rules/new/$', 'firewall_gui.views.show_rule'), url(r'^firewall/rules/new/$', 'firewall_gui.views.show_rule'),
# url(r'^firewall/hosts/save/$', 'firewall_gui.views.save_host'), url(r'^firewall/hosts/new/$', 'firewall_gui.views.show_host'),
# url(r'^firewall/vlans/save/$', 'firewall_gui.views.save_vlan'), # url(r'^firewall/vlans/save/$', 'firewall_gui.views.save_vlan'),
# url(r'^firewall/vlangroups/save/$', 'firewall_gui.views.save_vlangroup'), # url(r'^firewall/vlangroups/save/$', 'firewall_gui.views.save_vlangroup'),
# url(r'^firewall/hostgroups/save/$', 'firewall_gui.views.save_hostgroup'), # url(r'^firewall/hostgroups/save/$', 'firewall_gui.views.save_hostgroup'),
......
...@@ -163,7 +163,7 @@ ...@@ -163,7 +163,7 @@
</span> </span>
</div> </div>
<div class="input-append has-tooltip" title="Hostgroups of this host"> <div class="input-append has-tooltip" title="Hostgroups of this host">
<input class="span2 hostgroup" id="hostgroup" type="text" ng-model="newGroup"> <input class="span2 hostgroup" id="groups" type="text" ng-model="newGroup">
<button class="btn" type="button" ng-click="addHostGroup(newGroup)">Add</button> <button class="btn" type="button" ng-click="addHostGroup(newGroup)">Add</button>
<span class="help-inline" ng-bind="getError('groups')"></span> <span class="help-inline" ng-bind="getError('groups')"></span>
</div> </div>
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<form class="navbar-search" style="margin: 20px"> <form class="navbar-search" style="margin: 20px">
<input type="text" class="search-query" placeholder="Search" ng-model="query"> <input type="text" class="search-query" placeholder="Search" ng-model="query">
</form> </form>
<a style="margin-top: 19px" href="#/hosts/new" class="btn">New host</a> <a style="margin-top: 20px" href="#/hosts/new" class="btn">New host</a>
</div> </div>
</div> </div>
<table class="table table-striped"> <table class="table table-striped">
......
...@@ -252,46 +252,71 @@ def show_rule(request, id=None): ...@@ -252,46 +252,71 @@ def show_rule(request, id=None):
return HttpResponse(json.dumps(rule), content_type='application/json') return HttpResponse(json.dumps(rule), content_type='application/json')
def show_host(request, id): def show_host(request, id=None):
host = get_object_or_404(Host, id=id) try:
host = { host = Host.objects.get(id=id)
'id': host.id, host = {
'reverse': host.reverse, 'id': host.id,
'name': host.hostname, 'reverse': host.reverse,
'mac': host.mac, 'name': host.hostname,
'ipv4': host.ipv4, 'mac': host.mac,
'ipv6': host.ipv6, 'ipv4': host.ipv4,
'pub_ipv4': host.pub_ipv4, 'ipv6': host.ipv6,
'shared_ip': host.shared_ip, 'pub_ipv4': host.pub_ipv4,
'description': host.description, 'shared_ip': host.shared_ip,
'comment': host.comment, 'description': host.description,
'location': host.location, 'comment': host.comment,
'vlan': { 'location': host.location,
'name': host.vlan.name, 'vlan': {
'id': host.vlan.id 'name': host.vlan.name,
}, 'id': host.vlan.id
'owner': { },
'name': str(host.owner),
'id': host.owner.id
},
'created_at': host.created_at.isoformat(),
'modified_at': host.modified_at.isoformat(),
'groups': [{
'name': group.name,
'id': group.id,
} for group in host.groups.all()],
'rules': [{
'id': rule.id,
'direction': rule.get_direction_display(),
'proto': rule.proto,
'owner': { 'owner': {
'id': rule.owner.id, 'name': str(host.owner),
'name': str(rule.owner), 'id': host.owner.id
}, },
'accept': rule.accept, 'created_at': host.created_at.isoformat(),
'nat': rule.nat 'modified_at': host.modified_at.isoformat(),
} for rule in host.rules.all()] 'groups': [{
} 'name': group.name,
'id': group.id,
} for group in host.groups.all()],
'rules': [{
'id': rule.id,
'direction': rule.get_direction_display(),
'proto': rule.proto,
'owner': {
'id': rule.owner.id,
'name': str(rule.owner),
},
'accept': rule.accept,
'nat': rule.nat
} for rule in host.rules.all()]
}
except:
host = {
'id': None,
'reverse': None,
'name': None,
'mac': None,
'ipv4': None,
'ipv6': None,
'pub_ipv4': None,
'shared_ip': False,
'description': '',
'comment': '',
'location': '',
'vlan': {
'name': None,
},
'owner': {
'name': None,
},
'created_at': None,
'modified_at': None,
'groups': [],
'rules': []
}
return HttpResponse(json.dumps(host), content_type='application/json') return HttpResponse(json.dumps(host), content_type='application/json')
...@@ -516,10 +541,10 @@ def save_rule(request): ...@@ -516,10 +541,10 @@ def save_rule(request):
@user_passes_test(req_staff) @user_passes_test(req_staff)
def save_host(request): def save_host(request):
data = json.loads(request.body) data = json.loads(request.body)
if data['id']: if 'id' in data and data['id']:
host = get_object_or_404(Host, id=data['id']) host = get_object_or_404(Host, id=data['id'])
else: else:
host = Host.objects.create() host = Host()
errors = {} errors = {}
host.reverse = data['reverse'] host.reverse = data['reverse']
host.hostname = data['name'] host.hostname = data['name']
...@@ -533,17 +558,17 @@ def save_host(request): ...@@ -533,17 +558,17 @@ def save_host(request):
host.location = data['location'] host.location = data['location']
set_field(host, 'vlan', errors, name=data['vlan']['name']) set_field(host, 'vlan', errors, name=data['vlan']['name'])
set_field(host, 'owner', errors, username=data['owner']['name']) set_field(host, 'owner', errors, username=data['owner']['name'])
for host in data['vlans']: for group in data['groups']:
try: try:
if '__destroyed' in host and host['__destroyed']: if '__destroyed' in group and group['__destroyed']:
host_object = Vlan.objects.get(name = host['name']) group_object = Group.objects.get(name = group['name'])
vlan.snat_to.remove(host_object) host.groups.remove(group_object)
elif '__created' in host and host['__created']: elif '__created' in group and group['__created']:
host_object = Vlan.objects.get(name = host['name']) group_object = Group.objects.get(name = group['name'])
vlan.snat_to.add(host_object) host.groups.add(group_object)
except Exception as e: except Exception as e:
errors['vlans'] = ('Host with the name "%(name)s" does not exists!') % { errors['groups'] = ('Group with the name "%(name)s" does not exists!') % {
'name': host['name'] 'name': group['name']
} }
try: try:
host.full_clean() host.full_clean()
...@@ -552,7 +577,7 @@ def save_host(request): ...@@ -552,7 +577,7 @@ def save_host(request):
if len(errors) > 0: if len(errors) > 0:
return HttpResponse(json.dumps(errors), content_type='application/json', status=400) return HttpResponse(json.dumps(errors), content_type='application/json', status=400)
host.save() host.save()
return HttpResponse('KTHXBYE') return HttpResponse(host.id)
@user_passes_test(req_staff) @user_passes_test(req_staff)
def save_vlan(request): def save_vlan(request):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment