Commit 7f83651c by Szabolcs Gelencser

Extract glance and template to c members on AclUpdateView

parent af399a32
...@@ -459,9 +459,8 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin): ...@@ -459,9 +459,8 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin):
return None return None
def __accept_membership(self, project_id_of_user): def __accept_membership(self, project_id_of_user):
template = self.get_object() other_users_glance = self.__get_glance_admin_client(project_id_of_user)
glance = self.__get_glance_admin_client(project_id_of_user) other_users_glance.image_members.update(self.template.image_id, project_id_of_user, 'accepted')
glance.image_members.update(template.image_id, project_id_of_user, 'accepted')
def __list_users_of_group(self, group_id): def __list_users_of_group(self, group_id):
keystone = self.__get_keystone_admin_client() keystone = self.__get_keystone_admin_client()
...@@ -476,11 +475,10 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin): ...@@ -476,11 +475,10 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin):
def __handle_group_assignment(self): def __handle_group_assignment(self):
new_group_name = self.request.POST['name'] new_group_name = self.request.POST['name']
template = self.get_object()
new_group = self.__get_group_by_name(new_group_name) new_group = self.__get_group_by_name(new_group_name)
old_groups = TemplateGroupMember.objects.filter( old_groups = TemplateGroupMember.objects.filter(
instancetemplate__image_id=template.image_id) instancetemplate__image_id=self.template.image_id)
if new_group.id in old_groups: if new_group.id in old_groups:
msg = _("Already shared with group %s" % new_group_name) msg = _("Already shared with group %s" % new_group_name)
messages.warning(self.request, msg) messages.warning(self.request, msg)
...@@ -490,37 +488,34 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin): ...@@ -490,37 +488,34 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin):
group.save() group.save()
except IntegrityError: except IntegrityError:
group = TemplateGroupMember.objects.get(group_id=new_group.id) group = TemplateGroupMember.objects.get(group_id=new_group.id)
template.groups.add(group) self.template.groups.add(group)
template.save() self.template.save()
msg = _("Successfully shared with group %s" % new_group_name) msg = _("Successfully shared with group %s" % new_group_name)
messages.success(self.request, msg) messages.success(self.request, msg)
def __get_members_of_snapshot(self, glance): def __get_members_of_snapshot(self):
template = self.get_object() old_members_generator = self.glance.image_members.list(self.template.image_id)
old_members_generator = glance.image_members.list(template.image_id)
return [m.member_id for m in old_members_generator] return [m.member_id for m in old_members_generator]
def __handle_user_assignment(self): def __handle_user_assignment(self):
glance = self.__get_glance_admin_client(self.request.user.tenant_id)
template = self.get_object()
new_template_user = self.request.POST['name'] new_template_user = self.request.POST['name']
project_id_of_user = self.__get_project_id_by_name(new_template_user) project_id_of_user = self.__get_project_id_by_name(new_template_user)
old_members = self.__get_members_of_snapshot(glance) old_members = self.__get_members_of_snapshot()
if project_id_of_user in old_members: if project_id_of_user in old_members:
msg = _("Template is already shared with %s" % new_template_user) msg = _("Template is already shared with %s" % new_template_user)
messages.warning(self.request, msg) messages.warning(self.request, msg)
elif project_id_of_user is not None: elif project_id_of_user is not None:
glance.image_members.create(template.image_id, project_id_of_user) self.glance.image_members.create(self.template.image_id, project_id_of_user)
self.__accept_membership(project_id_of_user) self.__accept_membership(project_id_of_user)
try: try:
user = TemplateUserMember(project_id=project_id_of_user, name=new_template_user) user = TemplateUserMember(project_id=project_id_of_user, name=new_template_user)
user.save() user.save()
except IntegrityError: except IntegrityError:
user = TemplateUserMember.objects.get(project_id=project_id_of_user) user = TemplateUserMember.objects.get(project_id=project_id_of_user)
template.users.add(user) self.template.users.add(user)
template.save() self.template.save()
msg = _("Successfully shared with %s" % new_template_user) msg = _("Successfully shared with %s" % new_template_user)
messages.success(self.request, msg) messages.success(self.request, msg)
else: else:
...@@ -554,15 +549,13 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin): ...@@ -554,15 +549,13 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin):
return removes return removes
def __remove_user_from_db(self, member_id): def __remove_user_from_db(self, member_id):
template = self.get_object()
user = TemplateUserMember.objects.get(project_id=member_id) user = TemplateUserMember.objects.get(project_id=member_id)
template.users.remove(user) self.template.users.remove(user)
template.save() self.template.save()
def __remove_group_from_db(self, member_id): def __remove_group_from_db(self, member_id):
template = self.get_object() self.template.groups.remove(member_id)
template.groups.remove(member_id) self.template.save()
template.save()
def __handle_removes_from_db(self): def __handle_removes_from_db(self):
removes = self.__get_removes() removes = self.__get_removes()
...@@ -574,8 +567,7 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin): ...@@ -574,8 +567,7 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin):
messages.success(self.request, _("Successfully removed group")) messages.success(self.request, _("Successfully removed group"))
def __is_member_of_groups(self, member_id): def __is_member_of_groups(self, member_id):
template = self.get_object() for group in self.template.groups.all():
for group in template.groups.all():
group_members = self.__list_users_of_group(group.group_id) group_members = self.__list_users_of_group(group.group_id)
for group_member in group_members: for group_member in group_members:
if group_member.default_project_id == member_id: if group_member.default_project_id == member_id:
...@@ -583,33 +575,30 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin): ...@@ -583,33 +575,30 @@ class AclUpdateView(LoginRequiredMixin, View, SingleObjectMixin):
return False return False
def __cleanup_member(self, member_id): def __cleanup_member(self, member_id):
template = self.get_object()
try: try:
template.users.get(project_id=member_id) self.template.users.get(project_id=member_id)
return # member is assigned as user return # member is assigned as user
except TemplateUserMember.DoesNotExist: except TemplateUserMember.DoesNotExist:
pass pass
if not self.__is_member_of_groups(member_id): if not self.__is_member_of_groups(member_id):
glance = self.__get_glance_admin_client(self.request.user.tenant_id) self.glance.image_members.delete(self.template.image_id, member_id)
glance.image_members.delete(template.image_id, member_id)
def __cleanup_assigned_members_of_snapshot(self): def __cleanup_assigned_members_of_snapshot(self):
template = self.get_object() member_ids = self.__get_members_of_snapshot()
glance = self.__get_glance_admin_client(self.request.user.tenant_id)
member_ids = self.__get_members_of_snapshot(glance)
for member_id in member_ids: for member_id in member_ids:
self.__cleanup_member(member_id) self.__cleanup_member(member_id)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
template = self.get_object() self.template = self.get_object()
self.glance = self.__get_glance_admin_client(self.request.user.tenant_id)
openstack_api.glance.image_update(request, template.image_id, visibility="shared") openstack_api.glance.image_update(request, self.template.image_id, visibility="shared")
self.__handle_removes_from_db() self.__handle_removes_from_db()
self.__handle_assignments() self.__handle_assignments()
self.__cleanup_assigned_members_of_snapshot() self.__cleanup_assigned_members_of_snapshot()
return redirect("%s#access" % template.get_absolute_url()) return redirect("%s#access" % self.template.get_absolute_url())
class GraphMixin(object): class GraphMixin(object):
......
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