我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用mongoengine.DoesNotExist()。
def _on_deregister(self, ch, method, properties, message): """ Event method that is called when a de-registration request is received. Removes the given plugin from the internal data model. :param properties: request properties :param message: request body (contains UUID to identify plugin) :return: response message """ message = json.loads(str(message)) try: p = model.Plugin.objects.get(uuid=message.get("uuid")) p.delete() except DoesNotExist: LOG.debug("Couldn't find plugin with UUID %r in DB" % pid) LOG.info("DE-REGISTERED: %r" % message.get("uuid")) # broadcast a plugin status update to the other plugin self.send_plugin_status_update() # return result response = { "status": "OK" } return json.dumps(response)
def put(self, plugin_uuid=None): LOG.debug("PUT plugin lifecycle: %r" % plugin_uuid) try: p = model.Plugin.objects.get(uuid=plugin_uuid) # get target state from request body ts = json.loads(request.json).get("target_state") if ts is None: LOG.error("Malformed request: %r" % request.json) return {"message": "malformed request"}, 500 if ts == "start": PM.send_start_notification(p) elif ts == "pause": PM.send_pause_notification(p) elif ts == "stop": PM.send_stop_notification(p) else: return {"message": "Malformed request"}, 500 return {}, 200 except DoesNotExist as e: LOG.error("Lookup error: %r" % plugin_uuid) return {}, 404 # reference to plugin manager
def get_process(self, process): """ Get current process. If not exists, create it. :param process: Process data. :type process: dict. :return: Process :rtype: :class:`audit_tools.audit.Process` """ from audit_tools.audit.models import Process p = getattr(self.namespace, "audit_current_process", None) if p is None: try: p = Process.objects.get(pid=process['pid'], machine=process['machine'], creation_time=process['creation_time']) except DoesNotExist: p = Process(**process) p.save() self.set_process(p) return p
def fetch(retry=0): proxy = 'http://{}'.format(Proxy.get_random()['address']) headers = {'user-agent': get_user_agent()} conn = aiohttp.ProxyConnector(proxy=proxy) url = 'http://httpbin.org/ip' try: with aiohttp.ClientSession(connector=conn) as session: with aiohttp.Timeout(TIMEOUT): async with session.get(url, headers=headers) as resp: return await resp.json() except (ProxyConnectionError, TimeoutError): try: p = Proxy.objects.get(address=proxy) if p: p.delete() except DoesNotExist: pass retry += 1 if retry > 5: raise TimeoutError() await asyncio.sleep(1) return await fetch(retry=retry)
def save_document(cls, zip_code, address, neighborhood, city, state): """ Create or update a document returns True if created and False if only updated Update condition is based on zipcode. If zipcode already exists in db the the document is only updated. Otherwise the document is created """ try: object_ = cls.objects.get(zip_code=zip_code) object_.update( address=address, neighborhood=neighborhood, city=city, state=state ) return False except DoesNotExist: cls( zip_code=zip_code, address=address, neighborhood=neighborhood, city=city, state=state).save() return True
def removeAssociation(self, server_url, handle): """ This method removes the matching association if it's found, and returns whether the association was removed or not. """ try: mist_associations = MistAssociation.objects(server_url=server_url, handle=handle.encode('hex')) except me.DoesNotExist: return False for assoc in mist_associations: assoc.delete() return len(mist_associations) > 0
def useNonce(self, server_url, timestamp, salt): """Called when using a nonce. This method should return C{True} if the nonce has not been used before, and store it for a while to make sure nobody tries to use the same value again. If the nonce has already been used or the timestamp is not current, return C{False}. You may use L{openid.store.nonce.SKEW} for your timestamp window. """ if is_nonce_old(timestamp): return False try: mist_nonces = MistNonce.objects(server_url=server_url, salt=salt, timestamp=timestamp) except me.DoesNotExist: mist_nonces = [] if len(mist_nonces) == 0: print "Timestamp = %s" % timestamp MistNonce(server_url=server_url, salt=salt, timestamp=timestamp).save() return True return False
def cleanupNonces(self): """Remove expired nonces from the store. Discards any nonce from storage that is old enough that its timestamp would not pass L{useNonce}. This method is not called in the normal operation of the library. It provides a way for store admins to keep their storage from filling up with expired data. @return: the number of nonces expired. @returntype: int """ try: mist_nonces = MistNonce.objects() except me.DoesNotExist: mist_nonces = [] counter = 0 for nonce in mist_nonces: if nonce.is_old(): nonce.delete() counter += 1 return counter
def cleanupAssociations(self): """Remove expired associations from the store. This method is not called in the normal operation of the library. It provides a way for store admins to keep their storage from filling up with expired data. @return: the number of associations expired. @returntype: int """ try: mist_associations = MistAssociation.objects() except me.DoesNotExist: mist_associations = [] counter = 0 for assoc in mist_associations: if assoc.is_expired(): assoc.delete() counter += 1 return counter
def list_dns_records(request): """ List all DNS zone records for a particular zone. --- """ auth_context = auth_context_from_request(request) cloud_id = request.matchdict['cloud'] zone_id = request.matchdict['zone'] try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id) except me.DoesNotExist: raise CloudNotFoundError try: zone = Zone.objects.get(owner=auth_context.owner, cloud=cloud, id=zone_id) except Zone.DoesNotExist: raise NotFoundError('Zone does not exist') return filter_list_records(auth_context, zone)
def delete_dns_zone(request): """ Delete a specific DNS zone under a cloud. --- """ auth_context = auth_context_from_request(request) cloud_id = request.matchdict['cloud'] zone_id = request.matchdict['zone'] # Do we need the cloud here, now that the models have been created? try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id) except me.DoesNotExist: raise CloudNotFoundError try: zone = Zone.objects.get(owner=auth_context.owner, id=zone_id) except Zone.DoesNotExist: raise NotFoundError('Zone does not exist') auth_context.check_perm("zone", "remove", zone_id) zone.ctl.delete_zone() # Schedule a UI update trigger_session_update(auth_context.owner, ['zones']) return OK
def add(cls, machine, interval=None, ttl=300): try: schedule = cls.objects.get(machine_id=machine.id) except cls.DoesNotExist: schedule = cls(machine_id=machine.id) try: schedule.save() except me.NotUniqueError: # Work around race condition where schedule was created since # last time we checked. schedule = cls.objects.get(machine_id=machine.id) schedule.set_default_interval(60 * 60 * 2) if interval is not None: schedule.add_interval(interval, ttl) schedule.run_immediately = True schedule.cleanup_expired_intervals() schedule.save() return schedule
def get_object_with_id(owner, rid, rtype, *args, **kwargs): query = {} if rtype in ['machine', 'network', 'image', 'location']: if 'cloud_id' not in kwargs: raise RequiredParameterMissingError('No cloud id provided') else: query.update({'cloud': kwargs['cloud_id']}) if rtype == 'machine': query.update({'machine_id': rid}) else: query.update({'id': rid, 'deleted': None}) if rtype not in ['machine', 'image']: query.update({'owner': owner}) try: resource_obj = get_resource_model(rtype).objects.get(**query) except DoesNotExist: raise NotFoundError('Resource with this id could not be located') return resource_obj
def get_singleton(cls): """Return (and create if missing) the single Portal document""" try: portal = cls.objects.get() log.debug("Loaded portal info from db.") except me.DoesNotExist: log.info("No portal info found in db, will try to initialize.") try: portal = cls() portal.save() log.info("Initialized portal info.") except me.NotUniqueError: log.warning("Probable race condition while initializing " "portal info, will try to reload.") portal = cls.objects.get() log.debug("Loaded portal info from db.") except me.MultipleObjectsReturned: log.error("Multiple Portal info found in database.") portal = cls.objects.first() return portal
def get_avatar(request): """ Returns the requested avatar --- avatar: description: 'Avatar Id' in: path required: true type: string """ avatar_id = request.matchdict['avatar'] try: avatar = Avatar.objects.get(id=avatar_id) except me.DoesNotExist: raise NotFoundError() return Response(content_type=str(avatar.content_type), body=str(avatar.body))
def delete_dev_user(request): if not config.ENABLE_DEV_USERS: raise NotFoundError() params = params_from_request(request) email = params.get('email', '') if not email: raise RequiredParameterMissingError('email') try: user = User.objects.get(email=email) user.delete() log.warning("[DEV ENDPOINT]: Delete user with email: %s", email) except User.DoesNotExist: # If user does not exist we are okay log.warning("[DEV ENDPOINT]: User with email: %s is already absent", email) return OK
def add(self, fail_on_error=True, fail_on_invalid_params=True, **kwargs): """This is a hack to associate a key with the VM hosting this cloud""" super(LibvirtMainController, self).add( fail_on_error=fail_on_error, fail_on_invalid_params=fail_on_invalid_params, add=True, **kwargs ) # FIXME: Don't use self.cloud.host as machine_id, this prevents us from # changing the cloud's host. # FIXME: Add type field to differentiate between actual vm's and the # host. try: machine = Machine.objects.get(cloud=self.cloud, machine_id=self.cloud.host) except me.DoesNotExist: machine = Machine.objects(cloud=self.cloud, machine_id=self.cloud.host).save() if self.cloud.key: machine.ctl.associate_key(self.cloud.key, username=self.cloud.username, port=self.cloud.port)
def set_default_key(request): """ Set default key Sets a new default key EDIT permission required on key. --- key: description: The key id in: path required: true type: string """ key_id = request.matchdict['key'] auth_context = auth_context_from_request(request) try: key = Key.objects.get(owner=auth_context.owner, id=key_id, deleted=None) except me.DoesNotExist: raise NotFoundError('Key id does not exist') auth_context.check_perm('key', 'edit', key.id) key.ctl.set_default() return OK
def get_public_key(request): """ Get public key Gets public key from key name. READ permission required on key. --- key: description: The key id in: path required: true type: string """ key_id = request.matchdict['key'] if not key_id: raise RequiredParameterMissingError("key_id") auth_context = auth_context_from_request(request) try: key = SSHKey.objects.get(owner=auth_context.owner, id=key_id, deleted=None) except me.DoesNotExist: raise NotFoundError('Key id does not exist') auth_context.check_perm('key', 'read', key.id) return key.public
def show_schedule_entry(request): """ Show a schedule details of a user READ permission required on schedule --- schedule_id: type: string """ schedule_id = request.matchdict['schedule_id'] auth_context = auth_context_from_request(request) if not schedule_id: raise RequiredParameterMissingError('No schedule id provided') try: schedule = Schedule.objects.get(id=schedule_id, deleted=None, owner=auth_context.owner) except me.DoesNotExist: raise ScheduleTaskNotFound() # SEC require READ permission on schedule auth_context.check_perm('schedule', 'read', schedule_id) return schedule.as_dict()
def user_toggle(user_id=None, action=None): try: user = umodels.User.get_user(user_id) if action == "remove": user.delete() flash.success("User \"{}\" was successfully deleted!" "".format(str(user))) elif action == "is_admin": user.is_admin = not user.is_admin user.save() flash.success("User \"{}\" field \"{}\" was successfully " "updated to \"{}\"!".format(str(user), action, user.is_admin)) except (me.DoesNotExist, me.ValidationError) as e: flash.warning("User with id \"{}\" does not exist." "".format(user_id)) return redirect(url_for("admin.home"))
def index_stock(stockid): try: page = int(request.args.get('page')) if request.args.get('page') else 1 stock = models.Stock.objects.filter(id=stockid, blacklisted=False).first() if not stock: return redirect(url_for('index')) leaders = models.get_leaders() return render_template('index.html', view="market", base_url=settings.SERVER_NAME, leaders=leaders, stock=stock, stocks=api_views.get_paged_stocks(page), page=page) except DoesNotExist as e: return redirect(url_for('index')) except ValidationError as e: return redirect(url_for('index'))
def get_journal_object(journal_number): ''' If the journal exists in database, return the object. Else construct the object in database, and return it. :param journal_number: :return: ''' try: journal = Journal.objects.get(entry_number=str(journal_number)) except DoesNotExist: journal = Journal() journal.entry_number = str(journal_number) url = 'http://ieeexplore.ieee.org/xpl/RecentIssue.jsp' payload = { 'punumber': journal_number } r = requests.get(url, params=payload) query = PyQuery(r.text) journal.name = query('#journal-page-hdr h1').text().strip() journal.save() return journal
def _on_heartbeat(self, ch, method, properties, message): message = json.loads(str(message)) pid = message.get("uuid") try: p = model.Plugin.objects.get(uuid=pid) # update heartbeat timestamp p.last_heartbeat_at = datetime.datetime.now() change = False # TODO ugly: state management of plugins should be hidden with plugin class if message.get("state") == "READY" and p.state != "READY": # a plugin just announced that it is ready, lets start it self.send_start_notification(p) change = True elif message.get("state") != p.state: # lets keep track of the reported state update p.state = message.get("state") change = True p.save() if change: # there was a state change lets schedule an plugin status update notification self.send_plugin_status_update() except DoesNotExist: LOG.debug("Couldn't find plugin with UUID %r in DB" % pid)
def get(self, plugin_uuid=None): LOG.debug("GET plugin info for: %r" % plugin_uuid) try: p = model.Plugin.objects.get(uuid=plugin_uuid) return p.to_dict(), 200 except DoesNotExist as e: LOG.error("Lookup error: %r" % plugin_uuid) return {}, 404
def delete(self, plugin_uuid=None): LOG.debug("DELETE plugin: %r" % plugin_uuid) try: p = model.Plugin.objects.get(uuid=plugin_uuid) # send lifecycle stop event to plugin PM.send_stop_notification(p) # TODO ensure that record is deleted even if plugin does not deregister itself (use a timeout?) return {}, 200 except DoesNotExist as e: LOG.error("Lookup error: %r" % plugin_uuid) return {}, 404
def save_search_result(p, queue, retry=0): proxy = Proxy.get_random()['address'] url = SEARCH_URL.format(SEARCH_TEXT, p) try: r = fetch(url, proxy=proxy) except (Timeout, ConnectionError): sleep(0.1) retry += 1 if retry > 5: queue.put(url) raise GreenletExit() try: p = Proxy.objects.get(address=proxy) if p: p.delete() except DoesNotExist: pass return save_search_result(url, queue, retry) soup = BeautifulSoup(r.text, 'lxml') results = soup.find(class_='results') if results is None: # ???????, ?????? sleep(0.1) retry += 1 if retry > 5: queue.put(url) raise GreenletExit() return save_search_result(url, queue, retry) articles = results.find_all( 'div', lambda x: 'wx-rb' in x) for article in articles: save_article(article)
def get_or_404(cls, *args, **kwargs): message = kwargs.pop('message', None) try: return cls.objects.get(*args, **kwargs) except (MultipleObjectsReturned, DoesNotExist, ValidationError): if message is not None: abort(404, message=message) abort(404)
def getAssociation(self, server_url, handle=None): """ Gets a server url and the handle and finds a matching association that has not expired. Expired associations are deleted. The association returned is the one with the most recent issuing timestamp. """ query = {'server_url': server_url} if handle: query.update({'handle': handle.encode('hex')}) try: mist_associations = MistAssociation.objects(**query) except me.DoesNotExist: mist_associations = [] filtered_mist_assocs = [] for assoc in mist_associations: if assoc.is_expired(): assoc.delete() else: filtered_mist_assocs.append(assoc) filtered_mist_assocs = sorted(filtered_mist_assocs, key=lambda assoc: assoc.issued, reverse=True) if len(filtered_mist_assocs) > 0: mist_assoc = filtered_mist_assocs[0] association = Association(handle=mist_assoc.handle.decode('hex'), secret=mist_assoc.secret.decode('hex'), issued=mist_assoc.issued, lifetime=mist_assoc.lifetime, assoc_type=mist_assoc.assoc_type) return association return None
def su(request): """ Impersonate another user. This allows an admin to take the identity of any other user. It is meant to be used strictly for debugging. You can return to your regular user simply by logging out. This won't affect the last login time of the actual user. An email should be immediately sent out to the team, notifying of the 'su' action for security reasons. """ # SEC raise exception if user not admin user = user_from_request(request, admin=True) session = request.environ['session'] if isinstance(session, ApiToken): raise ForbiddenError('Cannot do su when authenticated with api token') real_email = user.email params = params_from_request(request) email = params.get('email') if not email: raise RequiredParameterMissingError('email') try: user = User.objects.get(email=email) except (UserNotFoundError, User.DoesNotExist): raise UserUnauthorizedError() reissue_cookie_session(request, real_email, su=user.id) # alert admins subject = "Some admin used su" body = "Admin: %s\nUser: %s\nServer: %s" % (real_email, user.email, config.CORE_URI) send_email(subject, body, config.NOTIFICATION_EMAIL['ops']) return HTTPFound('/')
def get_user(self, effective=True): """Return `su` user, if `effective` else `user`""" if self.user_id: try: if effective and self.su: user = User.objects.get(id=self.su) else: user = User.objects.get(id=self.user_id) return user except me.DoesNotExist: pass return None
def user_from_session_id(session_id): """Returns user associated with given cookie session id""" try: user = SessionToken.objects.get(token=session_id).get_user() if user is not None: return user except DoesNotExist: pass raise UserUnauthorizedError()
def create_dns_zone(request): """ Create a new DNS zone under a specific cloud. --- """ auth_context = auth_context_from_request(request) cloud_id = request.matchdict['cloud'] auth_context.check_perm("cloud", "read", cloud_id) auth_context.check_perm("cloud", "create_resources", cloud_id) tags = auth_context.check_perm("zone", "add", None) # Try to get the specific cloud for which we will create the zone. try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id) except me.DoesNotExist: raise CloudNotFoundError params = params_from_request(request) new_zone = Zone.add(owner=cloud.owner, cloud=cloud, **params).as_dict() if tags: resolve_id_and_set_tags(auth_context.owner, 'zone', new_zone['id'], tags, cloud_id=cloud_id) # Schedule a UI update trigger_session_update(auth_context.owner, ['zones']) return new_zone
def create_dns_record(request): """ Create a new record under a specific zone --- """ auth_context = auth_context_from_request(request) cloud_id = request.matchdict['cloud'] # Try to get the specific cloud for which we will create the zone. try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id) except me.DoesNotExist: raise CloudNotFoundError zone_id = request.matchdict['zone'] try: zone = Zone.objects.get(owner=auth_context.owner, id=zone_id) except Zone.DoesNotExist: raise NotFoundError('Zone does not exist') auth_context.check_perm("cloud", "read", cloud_id) auth_context.check_perm("zone", "read", zone_id) auth_context.check_perm("zone", "create_records", zone_id) tags = auth_context.check_perm("record", "add", None) # Get the params and create the new record params = params_from_request(request) dns_cls = RECORDS[params['type']] rec = dns_cls.add(owner=auth_context.owner, zone=zone, **params).as_dict() if tags: resolve_id_and_set_tags(auth_context.owner, 'record', rec['id'], tags, cloud_id=cloud_id, zone_id=zone_id) # Schedule a UI update trigger_session_update(auth_context.owner, ['zones']) return rec
def delete_dns_record(request): """ Delete a specific DNS record under a zone. --- """ auth_context = auth_context_from_request(request) cloud_id = request.matchdict['cloud'] zone_id = request.matchdict['zone'] record_id = request.matchdict['record'] try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id) except me.DoesNotExist: raise CloudNotFoundError try: zone = Zone.objects.get(owner=auth_context.owner, id=zone_id) except Zone.DoesNotExist: raise NotFoundError('Zone does not exist') try: record = Record.objects.get(zone=zone, id=record_id) except Record.DoesNotExist: raise NotFoundError('Record does not exist') auth_context.check_perm("record", "remove", record_id) record.ctl.delete_record() # Schedule a UI update trigger_session_update(auth_context.owner, ['zones']) return OK
def enabled(self): try: return (super(CloudPollingSchedule, self).enabled and self.cloud.enabled and not self.cloud.deleted) except me.DoesNotExist: log.error('Cannot get cloud for polling schedule.') return False
def interval(self): try: if self.default_interval.every != self.cloud.polling_interval: log.warning("Schedule has different interval from cloud, " "fixing") self.default_interval.every = self.cloud.polling_interval self.save() return super(CloudPollingSchedule, self).interval except me.DoesNotExist: log.error('Cannot get interval. Cloud is missing') return PollingInterval(every=0)
def show_script(request): """ Show script details and job history. READ permission required on script. --- script_id: type: string required: true in: path """ script_id = request.matchdict['script_id'] auth_context = auth_context_from_request(request) if not script_id: raise RequiredParameterMissingError('No script id provided') try: script = Script.objects.get(owner=auth_context.owner, id=script_id, deleted=None) except me.DoesNotExist: raise NotFoundError('Script id not found') # SEC require READ permission on SCRIPT auth_context.check_perm('script', 'read', script_id) ret_dict = script.as_dict() jobs = get_stories('job', auth_context.owner.id, script_id=script_id) ret_dict['jobs'] = [job['job_id'] for job in jobs] return ret_dict
def download_script(request): """ Download script file or archive. READ permission required on script. --- script_id: type: string required: true in: path """ script_id = request.matchdict['script_id'] auth_context = auth_context_from_request(request) if not script_id: raise RequiredParameterMissingError('No script id provided') try: script = Script.objects.get(owner=auth_context.owner, id=script_id, deleted=None) except me.DoesNotExist: raise NotFoundError('Script id not found') # SEC require READ permission on SCRIPT auth_context.check_perm('script', 'read', script_id) try: return script.ctl.get_file() except BadRequestError(): return Response("Unable to find: {}".format(request.path_info)) # SEC
def edit_script(request): """ Edit script (rename only as for now) EDIT permission required on script. --- script_id: in: path required: true type: string new_name: type: string required: true new_description: type: string """ script_id = request.matchdict['script_id'] params = params_from_request(request) new_name = params.get('new_name') new_description = params.get('new_description') auth_context = auth_context_from_request(request) # SEC require EDIT permission on script auth_context.check_perm('script', 'edit', script_id) try: script = Script.objects.get(owner=auth_context.owner, id=script_id, deleted=None) except me.DoesNotExist: raise NotFoundError('Script id not found') if not new_name: raise RequiredParameterMissingError('No new name provided') script.ctl.edit(new_name, new_description) ret = {'new_name': new_name} if isinstance(new_description, basestring): ret['new_description'] = new_description return ret # SEC
def url_script(request): """ Returns to a mist authenticated user, a self-auth/signed url for fetching a script's file. READ permission required on script. --- script_id: in: path required: true type: string """ auth_context = auth_context_from_request(request) script_id = request.matchdict['script_id'] try: Script.objects.get(owner=auth_context.owner, id=script_id, deleted=None) except Script.DoesNotExist: raise NotFoundError('Script does not exist.') # SEC require READ permission on script auth_context.check_perm('script', 'read', script_id) # build HMAC and inject into the `curl` command hmac_params = {'action': 'fetch_script', 'object_id': script_id} expires_in = 60 * 15 mac_sign(hmac_params, expires_in) url = "%s/api/v1/fetch" % config.CORE_URI encode_params = urllib.urlencode(hmac_params) r_url = url + '?' + encode_params return r_url
def fetch_script(script_id): """Used by mist.api.views.fetch""" try: script = Script.objects.get(id=script_id, deleted=None) except Script.DoesNotExist: raise NotFoundError('Script does not exist') return script.ctl.get_file()
def request_whitelist_ip(request): """ User logs in successfully but it's from a non-whitelisted ip. They click on a link 'whitelist current ip', which sends an email to their account. """ try: email = user_from_request(request).email except UserUnauthorizedError: email = params_from_request(request).get('email', '') try: user = User.objects.get(email=email) except (UserNotFoundError, me.DoesNotExist): # still return OK so that there's no leak on valid email return OK token = get_secure_rand_token() user.whitelist_ip_token = token user.whitelist_ip_token_created = time() user.whitelist_ip_token_ip_addr = ip_from_request(request) log.debug("will now save (whitelist_ip)") user.save() subject = config.WHITELIST_IP_EMAIL_SUBJECT body = config.WHITELIST_IP_EMAIL_BODY body = body % ( (user.first_name or "") + " " + (user.last_name or ""), config.CORE_URI, encrypt("%s:%s" % (token, email), config.SECRET), user.whitelist_ip_token_ip_addr, config.CORE_URI) if not send_email(subject, body, email): log.info("Failed to send email to user %s for whitelist IP link" % user.email) raise ServiceUnavailableError() log.info("Sent email to user %s\n%s" % (email, body)) return OK # SEC
def list_subnets(request): """ List subnets of a cloud Currently supports the EC2, GCE and OpenStack clouds. For other providers this returns an empty list. READ permission required on cloud. --- cloud: in: path required: true type: string network_id: in: path required: true description: The DB ID of the network whose subnets will be returned type: string """ cloud_id = request.matchdict['cloud'] network_id = request.matchdict['network'] auth_context = auth_context_from_request(request) auth_context.check_perm("cloud", "read", cloud_id) try: cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id) except Cloud.DoesNotExist: raise CloudNotFoundError try: network = Network.objects.get(cloud=cloud, id=network_id) except Network.DoesNotExist: raise NetworkNotFoundError subnets = methods.list_subnets(cloud, network=network) return subnets
def find_metrics(request): """ Get metrics of a machine Get all metrics associated with specific machine READ permission required on cloud. READ permission required on machine. --- cloud: in: path required: true type: string machine: in: path required: true type: string """ raise NotImplementedError() cloud_id = request.matchdict['cloud'] machine_id = request.matchdict['machine'] auth_context = auth_context_from_request(request) auth_context.check_perm("cloud", "read", cloud_id) try: machine = Machine.objects.get(cloud=cloud_id, machine_id=machine_id) machine_uuid = machine.id except me.DoesNotExist: machine_uuid = "" auth_context.check_perm("machine", "read", machine_uuid) return methods.find_metrics(auth_context.owner, cloud_id, machine_id)
def assoc_metric(request): """ Associate metric with machine Associate metric with specific machine READ permission required on cloud. EDIT_GRAPHS permission required on machine. --- cloud: in: path required: true type: string machine: in: path required: true type: string metric_id: description: ' Metric_id ' type: string """ raise NotImplementedError() cloud_id = request.matchdict['cloud'] machine_id = request.matchdict['machine'] params = params_from_request(request) metric_id = params.get('metric_id') if not metric_id: raise RequiredParameterMissingError('metric_id') auth_context = auth_context_from_request(request) auth_context.check_perm("cloud", "read", cloud_id) try: machine = Machine.objects.get(cloud=cloud_id, machine_id=machine_id) machine_uuid = machine.id except me.DoesNotExist: machine_uuid = "" auth_context.check_perm("machine", "edit_graphs", machine_uuid) methods.assoc_metric(auth_context.owner, cloud_id, machine_id, metric_id) return {}
def delete_avatar(request): """ Deletes the requested avatar --- avatar: description: 'Avatar Id' in: path required: true type: string """ avatar_id = request.matchdict['avatar'] auth_context = auth_context_from_request(request) try: avatar = Avatar.objects.get(id=avatar_id, owner=auth_context.user) except me.DoesNotExist: raise NotFoundError() try: org = Owner.objects.get(avatar=avatar_id) org.avatar = '' org.save() except me.DoesNotExist: pass avatar.delete() trigger_session_update(auth_context.owner, ["org"]) return OK
def list_user_organizations(request): """ List user's organizations List all the organizations where user is a member """ try: user = user_from_request(request) except me.DoesNotExist: raise UnauthorizedError() return [{'id': org.id, 'name': org.name} for org in Organization.objects(members=user)] # SEC
def show_team(request): """ Show team. Only available to organization owners. --- org_id: description: The team's org id type: string required: true team_id: description: The team's id type: string """ auth_context = auth_context_from_request(request) org_id = request.matchdict['org_id'] team_id = request.matchdict['team_id'] # SEC check if owner if not (auth_context.org and auth_context.is_owner() and auth_context.org.id == org_id): raise OrganizationAuthorizationFailure() # Check if team entry exists try: team = auth_context.org.get_team_by_id(team_id) except me.DoesNotExist: raise TeamNotFound() return team.as_dict() # SEC
def delete(self): super(Machine, self).delete() mist.api.tag.models.Tag.objects(resource=self).delete() try: self.owner.mapper.remove(self) except (AttributeError, me.DoesNotExist) as exc: log.error(exc)