async def addTrigger(self, tdef): ''' Adds a trigger to the view. ''' iden = tdef.get('iden') if iden is None: tdef['iden'] = s_common.guid() elif self.triggers.get(iden) is not None: raise s_exc.DupIden(mesg='A trigger with this iden already exists') root = await self.core.auth.getUserByName('root') tdef.setdefault('user', root.iden) tdef.setdefault('enabled', True) s_trigger.reqValidTdef(tdef) return await self._push('trigger:add', tdef)
async def addUser(self, name, passwd=None, email=None, iden=None): ''' Add a User to the Hive. Args: name (str): The name of the User. passwd (str): A optional password for the user. email (str): A optional email for the user. iden (str): A optional iden to use as the user iden. Returns: HiveUser: A Hive User. ''' if self.usersbyname.get(name) is not None: raise s_exc.DupUserName(name=name) if iden is None: iden = s_common.guid() else: if not s_common.isguid(iden): raise s_exc.BadArg(name='iden', arg=iden, mesg='Argument it not a valid iden.') if self.usersbyiden.get(iden) is not None: raise s_exc.DupIden(name=name, iden=iden, mesg='User already exists for the iden.') await self._push('user:add', iden, name) user = self.user(iden) if passwd is not None: await user.setPasswd(passwd) if email is not None: await self.setUserInfo(user.iden, 'email', email) # Everyone's a member of 'all' await user.grant(self.allrole.iden) return user
async def _onPushAddTrigger(self, tdef): s_trigger.reqValidTdef(tdef) trig = self.trigdict.get(tdef['iden']) if trig is not None: return self.triggers.get(tdef['iden']).pack() gate = self.core.auth.getAuthGate(tdef['iden']) if gate is not None: raise s_exc.DupIden(mesg='An AuthGate with this iden already exists') user = self.core.auth.user(tdef['user']) await self.core.getStormQuery(tdef['storm']) trig = await self.triggers.load(tdef) await self.trigdict.set(trig.iden, tdef) await self.core.auth.addAuthGate(trig.iden, 'trigger') await user.setAdmin(True, gateiden=tdef.get('iden'), logged=False) return trig.pack()
async def addLayer(self, layriden, indx=None): if any(layriden == layr.iden for layr in self.layers): raise s_exc.DupIden( mesg='May not have the same layer in a view twice') return await self._push('view:addlayer', layriden, indx)
async def add(self, cdef): ''' Persistently adds an appointment Args: cdef (dict): May contain the following keys: creator (str): iden of the creating user iden (str): Iden of the appointment storm (str): storm query to run reqs (Union[None, Dict[TimeUnit, Union[int, Tuple[int]], List[...]): one or more dicts of the fixed aspects of the appointment. dict value may be a single or multiple. May be an empty dict or None. incunit (Union[None, TimeUnit]): the unit that changes for recurring, or None for non-recurring. It is an error for this value to match a key in reqdict. incvals (Union[None, int, Iterable[int]): count of units of incunit or explicit day of week or day of month. Not allowed for incunit == None, required for others (1 would be a typical value) Notes: For values in reqs that are lists and incvals if a list, all combinations of all values (the product) are used Returns: Packed appointment definition ''' iden = cdef['iden'] incunit = cdef.get('incunit') incvals = cdef.get('incvals') reqs = cdef.get('reqs', {}) query = cdef.get('storm') creator = cdef.get('creator') view = cdef.get('view') recur = incunit is not None indx = self._next_indx self._next_indx += 1 if iden in self.appts: raise s_exc.DupIden() if not query: raise ValueError('"query" key of cdef parameter is not present or empty') if not creator: raise ValueError('"creator" key is cdef parameter is not present or empty') if not reqs and incunit is None: raise ValueError('at least one of reqs and incunit must be non-empty') if incunit is not None and incvals is None: raise ValueError('incvals must be non-None if incunit is non-None') if isinstance(reqs, Mapping): reqs = [reqs] # Find all combinations of values in reqdict values and incvals values nexttime = None recs = [] # type: ignore for req in reqs: if TimeUnit.NOW in req: if incunit is not None: mesg = "Recurring jobs may not be scheduled to run 'now'" raise ValueError(mesg) nexttime = self._getNowTick() continue reqdicts = self._dictproduct(req) if not isinstance(incvals, Iterable): incvals = (incvals, ) recs.extend(ApptRec(rd, incunit, v) for (rd, v) in itertools.product(reqdicts, incvals)) appt = _Appt(self, iden, recur, indx, query, creator, recs, nexttime=nexttime, view=view) self._addappt(iden, appt) appt.doc = cdef.get('doc', '') await self._storeAppt(appt) return appt.pack()