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 ...@@ -11,5 +11,5 @@ from . import views
urlpatterns = [ urlpatterns = [
# url(r'^', include(router.urls)), # url(r'^', include(router.urls)),
url(r'^deployments', views.DeploymentsView.as_view()), 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 @@ ...@@ -2,6 +2,7 @@
from rest_framework import generics from rest_framework import generics
from rest_framework.authentication import TokenAuthentication from rest_framework.authentication import TokenAuthentication
from .. import rabbitmq
from . import serializers, models from . import serializers, models
# authentication class for the client api # authentication class for the client api
...@@ -25,9 +26,15 @@ class DeploymentsView(generics.RetrieveAPIView): ...@@ -25,9 +26,15 @@ class DeploymentsView(generics.RetrieveAPIView):
return d 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 authentication_classes = authentication_classes
serializer_class = serializers.ServiceSerializer serializer_class = serializers.ServiceSerializer
def get_queryset(self): 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): ...@@ -58,7 +58,9 @@ class Site(models.Model):
) )
name = models.CharField(max_length=150, unique=True) name = models.CharField(max_length=150, unique=True)
description = models.TextField(max_length=300, blank=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): def __str__(self):
return self.name return self.name
......
...@@ -43,12 +43,10 @@ class RabbitMQInstance: ...@@ -43,12 +43,10 @@ class RabbitMQInstance:
'read': '^service\.({})$'.format(services), 'read': '^service\.({})$'.format(services),
} }
# TODO might fail
r = requests.put( r = requests.put(
set_topic_permission_uri, set_topic_permission_uri,
json=set_topic_permission_data, auth=self.auth) json=set_topic_permission_data, auth=self.auth)
r.raise_for_status()
print('RabbitMQ: set_topic_permissions {}'.format(r.status_code))
# set permissions for the user # set permissions for the user
def set_permissions(self, site): def set_permissions(self, site):
...@@ -65,10 +63,9 @@ class RabbitMQInstance: ...@@ -65,10 +63,9 @@ class RabbitMQInstance:
'read': permission, 'read': permission,
} }
# TODO might fail
r = requests.put( r = requests.put(
set_permission_uri, json=set_permission_data, auth=self.auth) 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 # create user at the rabbitmq instance
def create_user(self, site): def create_user(self, site):
...@@ -78,16 +75,14 @@ class RabbitMQInstance: ...@@ -78,16 +75,14 @@ class RabbitMQInstance:
username username
) )
print('registerring client {}'.format(site.client.username))
user_creation_data = { user_creation_data = {
'password': str(site.client.auth_token.key), 'password': str(site.client.auth_token.key),
'tags': '', 'tags': '',
} }
# TODO might fail
r = requests.put( r = requests.put(
user_creation_uri, json=user_creation_data, auth=self.auth) 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 # delete user at the rabbitmq instance
def delete_user(self, site): def delete_user(self, site):
...@@ -98,15 +93,26 @@ class RabbitMQInstance: ...@@ -98,15 +93,26 @@ class RabbitMQInstance:
'password': str(site.client.auth_token.key), 'password': str(site.client.auth_token.key),
'tags': '', 'tags': '',
} }
# TODO might fail
r = requests.delete( r = requests.delete(
user_creation_uri, json=user_creation_data, auth=self.auth) 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): def register_site(self, site):
print('RabbitMQ: register: {} / {}'.format(
site.name, site.client.username))
self.create_user(site) self.create_user(site)
self.set_permissions(site) self.set_permissions(site)
self.set_topic_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): def deregister_site(self, site):
print('RabbitMQ: deregister: {} / {}'.format(
site.name, site.client.username))
self.delete_user(site) 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