Commit 3b116b81 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add updating of rabbitmq permissions

parent 6a17098f
......@@ -11,5 +11,5 @@ from . import views
urlpatterns = [
# url(r'^', include(router.urls)),
url(r'^deployments', views.DeploymentsView.as_view()),
url(r'^services', views.ServiceView.as_view()),
url(r'^config', views.ConfigurationView.as_view()),
]
......@@ -2,6 +2,7 @@
from rest_framework import generics
from rest_framework.authentication import TokenAuthentication
from .. import rabbitmq
from . import serializers, models
# authentication class for the client api
......@@ -25,9 +26,15 @@ class DeploymentsView(generics.RetrieveAPIView):
return d
class ServiceView(generics.ListAPIView):
# the client has to fetch the configuration (like services etc.) here
class ConfigurationView(generics.ListAPIView):
authentication_classes = authentication_classes
serializer_class = serializers.ServiceSerializer
def get_queryset(self):
return self.request.user.site.services.all()
site = self.request.user.site
# we update the rabbitmq permission here, so the
# client can access all of his services, even new ones
rabbitmq.RabbitMQInstance().update_site(site)
return site.services.all()
......@@ -58,7 +58,9 @@ class Site(models.Model):
)
name = models.CharField(max_length=150, unique=True)
description = models.TextField(max_length=300, blank=True)
last_fetch = models.DateTimeField(default=datetime.utcfromtimestamp(0))
last_fetch = models.DateTimeField(
default=make_aware(datetime.utcfromtimestamp(0)),
editable=False)
def __str__(self):
return self.name
......
......@@ -43,12 +43,10 @@ class RabbitMQInstance:
'read': '^service\.({})$'.format(services),
}
# TODO might fail
r = requests.put(
set_topic_permission_uri,
json=set_topic_permission_data, auth=self.auth)
print('RabbitMQ: set_topic_permissions {}'.format(r.status_code))
r.raise_for_status()
# set permissions for the user
def set_permissions(self, site):
......@@ -65,10 +63,9 @@ class RabbitMQInstance:
'read': permission,
}
# TODO might fail
r = requests.put(
set_permission_uri, json=set_permission_data, auth=self.auth)
print('RabbitMQ: set_permissions {}'.format(r.status_code))
r.raise_for_status()
# create user at the rabbitmq instance
def create_user(self, site):
......@@ -78,16 +75,14 @@ class RabbitMQInstance:
username
)
print('registerring client {}'.format(site.client.username))
user_creation_data = {
'password': str(site.client.auth_token.key),
'tags': '',
}
# TODO might fail
r = requests.put(
user_creation_uri, json=user_creation_data, auth=self.auth)
print('RabbitMQ: create_user {}'.format(r.status_code))
r.raise_for_status()
# delete user at the rabbitmq instance
def delete_user(self, site):
......@@ -98,15 +93,26 @@ class RabbitMQInstance:
'password': str(site.client.auth_token.key),
'tags': '',
}
# TODO might fail
r = requests.delete(
user_creation_uri, json=user_creation_data, auth=self.auth)
print('RabbitMQ: delete_user {}'.format(r.status_code))
r.raise_for_status()
# PUBLIC API
def register_site(self, site):
print('RabbitMQ: register: {} / {}'.format(
site.name, site.client.username))
self.create_user(site)
self.set_permissions(site)
self.set_topic_permissions(site)
def update_site(self, site):
print('RabbitMQ: update: {} / {}'.format(
site.name, site.client.username))
self.set_topic_permissions(site)
def deregister_site(self, site):
print('RabbitMQ: deregister: {} / {}'.format(
site.name, site.client.username))
self.delete_user(site)
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