def list_volumes(query_attrs): """ List Volumes by attribute. Positional arguments: query_attrs (dict): The fields to query on. Each item must be in the form of 'Volume.${attr} ${op}': ${value} where ${attr} is a user attribute, ${op} is ==, !=, >, >=, <, <=, or IN, and ${value} is the value to compare the attribute against. Returns: On success, a list of Volumes that match the query. Raises an exception on error. Authorization: Only an administrator may call this method. """ # don't show deleted volumes query_attrs["Volume.deleted =="] = False return storage.list_volumes(query_attrs)
def myvolumes(request): ''' View that display all of user's owned volumes ''' session = request.session username = session['login_email'] user = db.read_user(username) vols_owned = [] vols_rw = [] vols_ro = [] if user.volumes_o: vols_owned = db.list_volumes({"Volume.volume_id IN": user.volumes_o}) if user.volumes_rw: volumes_rw_without_o = list(set(user.volumes_o) - set(user.volumes_rw)) if volumes_rw_without_o: vols_rw = db.list_volumes( {"Volume.volume_id IN": volumes_rw_without_o}) if user.volumes_r: vols_ro = db.list_Volumes({"Volume.volume_id IN": user.volumes_r}) # filter out volumes that I own that I can also read/write t = loader.get_template('myvolumes.html') c = Context({ 'username': username, 'vols_owned': vols_owned, 'vols_rw': vols_rw, 'vols_ro': vols_ro }) return HttpResponse(t.render(c))
def allvolumes(request): ''' Display all public volumes ''' session = request.session username = session['login_email'] v_attrs = {'Volume.private !=': True} volumes = db.list_volumes(v_attrs) owners = [] for v in volumes: attrs = {"SyndicateUser.owner_id ==": v.owner_id} owners.append(db.get_user(attrs)) vols = zip(volumes, owners) t = loader.get_template('allvolumes.html') c = Context({'username': username, 'vols': vols}) return HttpResponse(t.render(c))
def removevolumes(request, g_id): ''' This handler allows removal of one or many volumes from an Aqcquisition Gateway. It calls multi_update() as a helper method to allow transactional updates to the database. ''' # This is a helper method that isolates the @transactional decorator, speeding # up the code when it doesn't reach update() in this view and allowing for # errors that would break in GAE if the decorator was applied to the entire view. # It updates multiple volumes at once @transactional(xg=True) def multi_update(v_ids, g_id, vfields, gfields): for v_id, vfield in zip(v_ids, vfields): db.update_volume(v_id, **vfield) db.update_acquisition_gateway(g_id, **gfields) session.pop('ag_initial_data' + str(g_id)) session = request.session username = session['login_email'] g_id = int(g_id) VolumeFormSet = formset_factory(gatewayforms.GatewayRemoveVolume, extra=0) formset = VolumeFormSet(request.POST) # This call is not checked because the formset will always be valid (readonly widgets) formset.is_valid() volume_ids_to_be_removed = [] new_ags_set = [] initial_and_forms = zip(session.get('ag_initial_data' + str(g_id), []), formset.forms) for i, f in initial_and_forms: if f.cleaned_data['remove']: attrs = {"Volume.name ==": i['volume_name']} vols = db.list_volumes(attrs, limit=1) vol = vols[0] # update each volume's new AG list new_ags = vol.ag_ids new_ags.remove(int(g_id)) new_ags_set.append({'ag_ids': new_ags}) # update info for AG update volume_ids_to_be_removed.append(vol.volume_id) if not volume_ids_to_be_removed: session['message'] = "You must select at least one volume to remove." return redirect('django_ag.views.viewgateway', g_id) old_vids = set(db.read_acquisition_gateway(g_id).volume_ids) new_vids = list(old_vids - set(volume_ids_to_be_removed)) gfields = {'volume_ids': new_vids} try: multi_update(volume_ids_to_be_removed, g_id, new_ags_set, gfields) except Exception as e: logging.error("Unable to update acquisition gateway %s. Exception %s" % (g_id, e)) session['message'] = "Unable to update gateway." return redirect('django_ag.views.viewgateway', g_id) session['new_change'] = "We've updated your AG's volumes." session['next_url'] = '/syn/AG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks')
def addvolume(request, g_id): ''' Handler for adding a volume to the gateay. ''' # This is a helper method that isolates the @transactional decorator, speeding # up the code when it doesn't reach update() in this view and allowing for # errors that would break in GAE if the decorator was applied to the entire view. @transactional(xg=True) def update(v_id, g_id, vfields, gfields): db.update_volume(v_id, **vfields) db.update_acquisition_gateway(g_id, **gfields) session.pop('ag_initial_data' + str(g_id)) session = request.session username = session['login_email'] g_id = int(g_id) form = gatewayforms.GatewayAddVolume(request.POST) if form.is_valid(): attrs = { "Volume.name ==": form.cleaned_data['volume_name'].strip().replace(" ", "_") } vols = db.list_volumes(attrs, limit=1) if vols: volume = vols[0] logging.info(volume) else: session[ 'message'] = "The volume %s doesn't exist." % form.cleaned_data[ 'volume_name'] return redirect('django_ag.views.viewgateway', g_id) gateway = db.read_acquisition_gateway(g_id) # Prepare upcoming volume state if volume.ag_ids: new_ags = volume.ag_ids new_ags.append(gateway.g_id) else: new_ags = [gateway.g_id] vfields = {'ag_ids': new_ags} # Preare upcoming AG state old_vids = gateway.volume_ids new_vid = volume.volume_id if new_vid in old_vids: session[ 'message'] = "That volume is already attached to this gateway!" return redirect('django_ag.views.viewgateway', g_id) if old_vids: old_vids.append(new_vid) new_vids = old_vids else: new_vids = [new_vid] # Update and redirect try: gfields = {'volume_ids': new_vids} update(volume.volume_id, g_id, vfields, gfields) except Exception as e: logging.error( "Unable to update acquisition gateway %s or volume %s. Exception %s" % (gateway.ms_username, form.cleaned_data['volume_name'], e)) session['message'] = "Unable to update gateway." return redirect('django_ag.views.viewgateway', g_id) session['new_change'] = "We've updated your AG's volumes." session['next_url'] = '/syn/AG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') else: session['message'] = "Invalid entries for adding volumes." return redirect('django_ag.views.viewgateway', g_id)
def removevolumes(request, g_id): ''' This handler allows removal of one or many volumes from an Aqcquisition Gateway. It calls multi_update() as a helper method to allow transactional updates to the database. ''' # This is a helper method that isolates the @transactional decorator, speeding # up the code when it doesn't reach update() in this view and allowing for # errors that would break in GAE if the decorator was applied to the entire view. # It updates multiple volumes at once @transactional(xg=True) def multi_update(v_ids, g_id, vfields, gfields): for v_id, vfield in zip(v_ids, vfields): db.update_volume(v_id, **vfield) db.update_replica_gateway(g_id, **gfields) session.pop('rg_initial_data' + str(g_id)) session = request.session username = session['login_email'] g_id = int(g_id) VolumeFormSet = formset_factory(gatewayforms.GatewayRemoveVolume, extra=0) formset = VolumeFormSet(request.POST) # This call is not checked because the formset will always be valid (readonly widgets) formset.is_valid() volume_ids_to_be_removed = [] new_rgs_set = [] initial_and_forms = zip(session.get('rg_initial_data' + str(g_id), []), formset.forms) for i, f in initial_and_forms: if f.cleaned_data['remove']: attrs = {"Volume.name ==":i['volume_name']} vols = db.list_volumes(attrs, limit=1) vol = vols[0] # update each volumes new RG list new_rgs = vol.rg_ids new_rgs.remove(g_id) new_rgs_set.append({'rg_ids':new_rgs}) # update info for AG update volume_ids_to_be_removed.append(vol.volume_id) if not volume_ids_to_be_removed: session['message'] = "You must select at least one volume to remove." return redirect('django_rg.views.viewgateway', g_id) old_vids = set(db.read_replica_gateway(g_id).volume_ids) new_vids = list(old_vids - set(volume_ids_to_be_removed)) gfields = {'volume_ids':new_vids} try: multi_update(volume_ids_to_be_removed, g_id, new_rgs_set, gfields) except Exception as e: logging.error("Unable to update replica gateway %d. Exception %s" % (g_id, e)) session['message'] = "Unable to update gateway." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've updated your RG's volumes." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks')
def addvolume(request, g_id): ''' Handler for adding a volume to the gateay. ''' # This is a helper method that isolates the @transactional decorator, speeding # up the code when it doesn't reach update() in this view and allowing for # errors that would break in GAE if the decorator was applied to the entire view. @transactional(xg=True) def update(v_id, g_id, vfields, gfields): db.update_volume(v_id, **vfields) db.update_replica_gateway(g_id, **gfields) session.pop('rg_initial_data' + str(g_id)) session = request.session username = session['login_email'] g_id = int(g_id) form = gatewayforms.GatewayAddVolume(request.POST) if form.is_valid(): attrs = {"Volume.name ==":form.cleaned_data['volume_name'].strip().replace(" ", "_")} vols = db.list_volumes(attrs, limit=1) if vols: volume = vols[0] logging.info(volume) else: session['message'] = "The volume %s doesn't exist." % form.cleaned_data['volume_name'] return redirect('django_ag.views.viewgateway', g_id) gateway = db.read_replica_gateway(g_id) # prepare volume state if volume.rg_ids: new_rgs = volume.rg_ids new_rgs.append(gateway.g_id) else: new_rgs = [gateway.g_id] vfields = {'rg_ids':new_rgs} # prepate RG state old_vids = gateway.volume_ids new_vid = volume.volume_id if new_vid in old_vids: session['message'] = "That volume is already attached to this gateway!" return redirect('django_rg.views.viewgateway', g_id) if old_vids: old_vids.append(new_vid) new_vids = old_vids else: new_vids = [new_vid] # Update and redirect try: gfields={'volume_ids':new_vids} update(volume.volume_id, g_id, vfields, gfields) except Exception as e: logging.error("Unable to update replica gateway %s or volume %s. Exception %s" % (rg.ms_username, volume.name, e)) session['message'] = "Unable to update." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've updated your RG's volumes." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return redirect('/syn/thanks') else: session['message'] = "Invalid entries for adding volumes." return redirect('django_rg.views.viewgateway', g_id)
def create(request): ''' View for creating UG's ''' session = request.session username = session['login_email'] user = db.read_user(username) # Helper method that simplifies returning forms after user error. def give_create_form(username, session): message = session.pop('message', "") form = gatewayforms.CreateUG() t = loader.get_template('gateway_templates/create_user_gateway.html') c = RequestContext(request, { 'username': username, 'form': form, 'message': message }) return HttpResponse(t.render(c)) if request.POST: # Validate input forms form = gatewayforms.CreateUG(request.POST) if form.is_valid(): if not form.cleaned_data['volume_name']: logging.info("DLFKJSDF") vol = None else: attrs = { "Volume.name ==": form.cleaned_data['volume_name'].strip().replace(" ", "_") } vols = db.list_volumes(attrs, limit=1) if vols: vol = vols[0] else: session[ 'message'] = "No volume %s exists." % form.cleaned_data[ 'volume_name'] return give_create_form(username, session) if (vol.volume_id not in user.volumes_r) and (vol.volume_id not in user.volumes_rw): session[ 'message'] = "Must have read rights to volume %s to create UG for it." % form.cleaned_data[ 'volume_name'] return give_create_form(username, session) # Force update of UG version attrs = {"UG_version": 1} try: transaction( lambda: db.update_volume(vol.volume_id, **attrs)) except Exception as E: session['message'] = "UG creation error: %s" % E return give_create_form(username, session) try: # Prep kwargs kwargs = {} kwargs['read_write'] = form.cleaned_data['read_write'] kwargs['ms_username'] = form.cleaned_data['g_name'] kwargs['port'] = form.cleaned_data['port'] kwargs['host'] = form.cleaned_data['host'] kwargs['ms_password'] = form.cleaned_data['g_password'] # Create new_ug = db.create_user_gateway(user, vol, **kwargs) except Exception as E: session['message'] = "UG creation error: %s" % E return give_create_form(username, session) session['new_change'] = "Your new gateway is ready." session['next_url'] = '/syn/UG/mygateways' session['next_message'] = "Click here to see your gateways." return HttpResponseRedirect('/syn/thanks/') else: # Prep returned form values (so they don't have to re-enter stuff) if 'g_name' in form.errors: oldname = "" else: oldname = request.POST['g_name'] if 'volume_name' in form.errors: oldvolume_name = "" else: oldvolume_name = request.POST['volume_name'] if 'host' in form.errors: oldhost = "" else: oldhost = request.POST['host'] if 'port' in form.errors: oldport = "" else: oldport = request.POST['port'] # Prep error message message = "Invalid form entry: " for k, v in form.errors.items(): message = message + "\"" + k + "\"" + " -> " for m in v: message = message + m + " " # Give then the form again form = gatewayforms.CreateUG( initial={ 'g_name': oldname, 'volume_name': oldvolume_name, 'host': oldhost, 'port': oldport, }) t = loader.get_template( 'gateway_templates/create_user_gateway.html') c = RequestContext(request, { 'username': username, 'form': form, 'message': message }) return HttpResponse(t.render(c)) else: # Not a POST, give them blank form return give_create_form(username, session)
def changevolume(request, g_id): ''' View to enable changing volume attached to UG ''' session = request.session username = session['login_email'] user = db.read_user(username) g_id = int(g_id) g = db.read_user_gateway(g_id) # Isolate DB calls to allow roll-backs via transactions @transactional(xg=True) def update_ug_and_vol(g_id, gfields, vol1_id, vol2_id): db.update_user_gateway(g_id, **gfields) attrs = {"UG_version": 1} # Force update of UG version db.update_volume(vol1_id, **attrs) db.update_volume(vol2_id, **attrs) form = gatewayforms.ChangeVolume(request.POST) if form.is_valid(): attrs = { "Volume.name ==": form.cleaned_data['volume_name'].strip().replace(" ", "_") } vols = db.list_volumes(attrs, limit=1) if vols: new_vol = vols[0] else: session['message'] = "No volume %s exists." % form.cleaned_data[ 'volume_name'] return redirect('django_ug.views.viewgateway', g_id) if (new_vol.volume_id not in user.volumes_r) and (new_vol.volume_id not in user.volumes_rw): session[ 'message'] = "Must have read rights to volume %s to assign UG to it." % form.cleaned_data[ 'volume_name'] return redirect('django_ug.views.viewgateway', g_id) old_vol = g.volume_id #fields = {"volume_id":new_vol.volume_id, "cert_version": True} try: db.update_user_gateway(g_id, volume_id=new_vol.volume_id, cert_version=True) db.update_volume(new_vol.volume_id, version=True, cert_version=True) if g.is_bound_to_volume(): # update the old Volume db.update_volume(old_vol, version=True, cert_version=True) #update_ug_and_vol(g_id, fields, old_vol, new_vol.volume_id) except Exception, e: logging.error("Unable to update UG with ID %s. Error was %s." % (g_id, e)) session['message'] = "Error. Unable to change user gateway." return redirect('django_ug.views.viewgateway', g_id) session['new_change'] = "We've updated your UG." session['next_url'] = '/syn/UG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your gateway." return HttpResponseRedirect('/syn/thanks')