Commit e2523d6e authored by Élie Bouttier's avatar Élie Bouttier

wireguard

parent 2baf8daa
......@@ -358,11 +358,17 @@ class ServiceAdmin(admin.ModelAdmin):
('service_type', admin.RelatedOnlyFieldListFilter),
)
search_fields = ('=id', 'service_type__name', 'label', 'notes',)
fields = ('adhesion', 'service_type', 'label', 'notes', 'loan_equipment', 'get_contribution_link', 'is_active',)
readonly_fields = ('get_contribution_link', 'is_active',)
raw_id_fields = ('adhesion',)
form = ServiceForm
def get_fields(self, request, obj=None):
fields = ['adhesion', 'service_type', 'label']
if obj and obj.service_type.name == 'wireguard':
fields += ['wireguard_pubkey']
fields += ['notes', 'loan_equipment', 'get_contribution_link', 'is_active']
return fields
def save_model(self, request, srv, form, change):
if srv.pk and 'adhesion' in form.changed_data:
with transaction.atomic():
......@@ -720,7 +726,8 @@ class TunnelAdmin(admin.ModelAdmin):
class ServiceTypeAdmin(admin.ModelAdmin):
fields = ('name', 'contact')
list_display = ('name', 'contact',)
fields = ('name', 'contact',)
def get_actions(self, request):
actions = super().get_actions(request)
......
# Generated by Django 2.2 on 2019-05-19 19:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('services', '0055_auto_20190422_1648'),
]
operations = [
migrations.AddField(
model_name='service',
name='wireguard_pubkey',
field=models.CharField(blank=True, default='', max_length=64, verbose_name='Clé public WireGuard'),
),
]
......@@ -176,6 +176,9 @@ class Service(models.Model):
created = models.DateTimeField(auto_now_add=True)
loan_equipment = models.BooleanField(default=False, verbose_name='Matériel en prêt')
# WireGuard
wireguard_pubkey = models.CharField(max_length=64, verbose_name='Clé public WireGuard', blank=True, default='')
contribution = models.OneToOneField(RecurringPayment, on_delete=models.CASCADE)
objects = ActiveServiceManager()
......
......@@ -10,4 +10,5 @@ urlpatterns = [
re_path(r'^api/routes/(?:(?P<route>[\w-]+)/)?$', views.routes, name='routes'), # TODO: path_re => path
path('api/architecture/switch/', views.architecture_switch, name='archi-switch'),
path('api/architecture/ip/', views.architecture_ip, name='archi-ip'),
path('api/wireguard/', views.wireguard, name='wireguard'),
]
......@@ -14,10 +14,9 @@ from datetime import timedelta, datetime
from djadhere.utils import get_active_filter
from djadhere.decorators import api_key_required
from .models import Service, Route, IPResource, ServiceAllocation
from .models import Service, Route, IPResource, IPResourceState, ServiceAllocation
from .utils.fastpinger import fastpinger_update
from .utils.architecture import export_switch, export_ip
from services.models import IPResource, IPResourceState, ServiceAllocation
class Status(LoginRequiredMixin, TemplateView):
......@@ -108,3 +107,23 @@ def architecture_switch(request):
@api_key_required
def architecture_ip(request):
return HttpResponse(export_ip(), content_type='text/plain; charset=utf-8')
@require_GET
@api_key_required
def wireguard(request):
allocations = ServiceAllocation.objects \
.filter(
get_active_filter() \
& models.Q(service__service_type__name='wireguard')
) \
.annotate(
wireguard=Concat(
'resource__ip',
models.Value(' '),
'service__wireguard_pubkey',
output_field=models.CharField()
)
)
content = '\n'.join(allocations.values_list('wireguard', flat=True))
return HttpResponse(content, content_type='text/plain; charset=utf-8')
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 to comment