Commit d72aa9c8 by Szabolcs Gelencser

Add template share by groups to UpdateTemplateShares view

parent 554c8261
...@@ -640,8 +640,7 @@ class UpdateSharedTemplates(View): ...@@ -640,8 +640,7 @@ class UpdateSharedTemplates(View):
return InstanceTemplate.objects.filter(image_id__in=snapshot_ids) return InstanceTemplate.objects.filter(image_id__in=snapshot_ids)
def __list_users_of_group(self, group_id): def __list_users_of_group(self, group_id):
keystone = self.__get_keystone_admin_client() return self.keystone.users.list(group=group_id)
return keystone.users.list(group=group_id)
def __is_member_of_groups(self, template): def __is_member_of_groups(self, template):
for group in template.groups.all(): for group in template.groups.all():
...@@ -660,7 +659,10 @@ class UpdateSharedTemplates(View): ...@@ -660,7 +659,10 @@ class UpdateSharedTemplates(View):
if not self.__is_member_of_groups(template): if not self.__is_member_of_groups(template):
snapshot_owner_glance = self.__get_glance_admin_client(template.owner_id) snapshot_owner_glance = self.__get_glance_admin_client(template.owner_id)
try:
snapshot_owner_glance.image_members.delete(template.image_id, self.project_id) snapshot_owner_glance.image_members.delete(template.image_id, self.project_id)
except:
pass # TODO: silent fail, but should log in case of some error
def __cleanup_snapshots(self): def __cleanup_snapshots(self):
templates_of_existing_snaps = self.__get_templates_of_snapshots() templates_of_existing_snaps = self.__get_templates_of_snapshots()
...@@ -670,18 +672,30 @@ class UpdateSharedTemplates(View): ...@@ -670,18 +672,30 @@ class UpdateSharedTemplates(View):
def __accept_membership(self, template): def __accept_membership(self, template):
self.glance.image_members.update(template.image_id, self.project_id, 'accepted') self.glance.image_members.update(template.image_id, self.project_id, 'accepted')
def __assign_by_users(self): def __share_template(self, template):
templates = InstanceTemplate.objects.filter(users__project_id=self.project_id) snapshot_owner_glance = self.__get_glance_admin_client(template.owner_id)
for t in templates:
snapshot_owner_glance = self.__get_glance_admin_client(t.owner_id)
try: try:
snapshot_owner_glance.image_members.create(t.image_id, self.project_id) snapshot_owner_glance.image_members.create(template.image_id, self.project_id)
self.__accept_membership(t) self.__accept_membership(template)
except: except:
pass # TODO: silent fail, but should log in case of some error pass # TODO: silent fail, but should log in case of some error
def __share_templates(self, templates):
for t in templates:
self.__share_template(t)
def __assign_by_users(self):
templates = InstanceTemplate.objects.filter(users__project_id=self.project_id)
self.__share_templates(templates)
def __get_group_ids_of_user(self):
generator = self.keystone.groups.list(user=self.user_id)
return [g.id for g in generator]
def __assign_by_groups(self): def __assign_by_groups(self):
pass group_ids = self.__get_group_ids_of_user()
templates = InstanceTemplate.objects.filter(groups__group_id__in=group_ids)
self.__share_templates(templates)
def __assign_snapshots(self): def __assign_snapshots(self):
self.__assign_by_users() self.__assign_by_users()
...@@ -693,7 +707,8 @@ class UpdateSharedTemplates(View): ...@@ -693,7 +707,8 @@ class UpdateSharedTemplates(View):
def post(self, request): def post(self, request):
if not 'secret' in request.POST or \ if not 'secret' in request.POST or \
not 'project_id' in request.POST: not 'project_id' in request.POST or \
not 'user_id' in request.POST:
return HttpResponse(status=400) return HttpResponse(status=400)
secret = request.POST['secret'] secret = request.POST['secret']
...@@ -701,7 +716,9 @@ class UpdateSharedTemplates(View): ...@@ -701,7 +716,9 @@ class UpdateSharedTemplates(View):
return HttpResponse(status=401) return HttpResponse(status=401)
self.project_id = request.POST['project_id'] self.project_id = request.POST['project_id']
self.user_id = request.POST['user_id']
self.glance = self.__get_glance_admin_client(self.project_id) self.glance = self.__get_glance_admin_client(self.project_id)
self.keystone = self.__get_keystone_admin_client()
self.__update_shared_templates() self.__update_shared_templates()
return HttpResponse(status=200) return HttpResponse(status=200)
......
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