def on_post(self, req, resp, tenant_id): tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) body = load_body(req) hostname = body['hostname'] ip_address = body['ip_address'] # Check if the tenant already has a host with this hostname for host in tenant.hosts: if host.hostname == hostname: abort(400, 'Host with hostname {0} already exists with' ' id={1}'.format(hostname, host.id)) # Create the new host definition new_host = Host(hostname, ip_address) self.db.add(new_host) tenant.hosts.append(new_host) self.db().commit() resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/hosts/{1}' .format(tenant_id, new_host.id))
def on_post(self, req, resp, tenant_id): body = dict() if req.stream: body = load_body(req) self._validate_req_body_on_post(body) #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() invalidate_now = False if 'token' in body.keys() and 'invalidate_now' in body['token']: invalidate_now = body['token']['invalidate_now'] if invalidate_now: #immediately invalidate the token tenant.token.reset_token_now() else: self._validate_token_min_time_limit_reached(tenant.token) tenant.token.reset_token() self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_203 resp.set_header('Location', '/v1/{0}/token'.format(tenant_id))
def on_put(self, req, resp, worker_id): """ updates a worker's status """ #load json payload in body body = load_body(req) self._validate_req_body_on_put(body) #find the worker in db worker_dict = self.db.find_one('worker', {'worker_id': worker_id}) if not worker_dict: _worker_not_found() worker = Worker(**worker_dict) if 'worker_status' in body: worker.status = body['worker_status'] if 'disk_usage' in body: worker.system_info.disk_usage = body['disk_usage'] if 'load_average' in body: worker.system_info.load_average = body['load_average'] self.db.update('worker', worker.format_for_save()) resp.status = falcon.HTTP_200
def on_put(self, req, resp, tenant_id, event_producer_id): #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) #verify the event_producer exists event_producer = find_event_producer(self.db, id=event_producer_id, when_not_found= _producer_not_found) #verify the event_producer belongs to the tenant if not event_producer in tenant.event_producers: _producer_not_found() body = load_body(req) #if a key is present, update the event_producer with the value if 'name' in body.keys(): event_producer.name = body['name'] if 'pattern' in body.keys(): event_producer.pattern = body['pattern'] if 'durable' in body.keys(): event_producer.durable = body['durable'] if 'encrypted' in body.keys(): event_producer.encrypted = body['encrypted'] self.db.commit() resp.status = falcon.HTTP_200
def on_put(self, req, resp, tenant_id, profile_id): profile = find_host_profile(self.db, id=profile_id, when_not_found=_profile_not_found) body = load_body(req) hostname = body['name'] ip_address = body['pattern']
def on_post(self, req, resp, tenant_id): body = load_body(req) self._validate_req_body_on_post(body) #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() hostname = body['hostname'] # Check if the tenant already has a host with this hostname for host in tenant.hosts: if host.hostname == hostname: abort(falcon.HTTP_400, 'Host with hostname {0} already exists with id={1}' .format(hostname, host.get_id())) ip_address_v4 = None if 'ip_address_v4' in body.keys(): ip_address_v4 = body['ip_address_v4'] ip_address_v6 = None if 'ip_address_v6' in body.keys(): ip_address_v6 = body['ip_address_v6'] profile_id = None #if profile id is not in post message, then use a null profile if 'profile_id' in body.keys(): if body['profile_id']: profile_id = body['profile_id'] #if profile id is in post message, then make sure it is valid profile if profile_id: #verify the profile exists and belongs to the tenant profile = find_host_profile(tenant, profile_id=profile_id) if not profile: _profile_not_found() # Create the new host definition new_host = Host( self.db.next_sequence_value(tenant.tenant_id), hostname, ip_address_v4, ip_address_v6, profile_id) tenant.hosts.append(new_host) self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/hosts/{1}' .format(tenant_id, new_host.get_id()))
def on_put(self, req, resp, tenant_id, profile_id): #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) #verify the profile exists profile = find_host_profile(self.db, id=profile_id, when_not_found=_profile_not_found) #verify the profile belongs to the tenant if not profile in tenant.profiles: _profile_not_found() #load the message body = load_body(req) #if attributes are present in message, update the profile if 'name' in body.keys(): profile.name = body['name'] if 'event_producer_ids' in body.keys(): producer_ids = body['event_producer_ids'] # if the put is changing event producers if producer_ids: #abort if any of the event_producers being passed in are not # valid event_producers for this tenant for producer_id in producer_ids: if producer_id not in \ [p.id for p in tenant.event_producers]: _producer_not_found() #remove any event producers from the profile # that aren't in list of event_producers being passed in for producer in profile.event_producers: if producer.id not in producer_ids: profile.event_producers.remove(producer) #see if the incoming event_producers are missing from the # profile, and for pid in producer_ids: if pid not in [p.id for p in profile.event_producers]: profile.event_producers.append( [p for p in tenant.event_producers if p.id == pid][0]) else: profile.event_producers = [] self.db.commit() resp.status = falcon.HTTP_200
def on_post(self, req, resp): body = load_body(req) api_secret = body['api_secret'] coordinator_uri = body['coordinator_uri'] personality = body['personality'] #start pairing on a separate process pairing_process = PairingProcess( api_secret, coordinator_uri, personality) pairing_process.run() resp.status = falcon.HTTP_200
def on_put(self, req, resp, tenant_id, host_id): host = find_host(self.db, host_id, when_not_found=_host_not_found) body = load_body(req) profile_id = body['profile_id'] profile = find_host_profile(self.db, id=profile_id, when_not_found=_profile_not_found) host.profile = profile self.db.commit() resp.status = falcon.HTTP_200
def on_put(self, req, resp): """ Take a broadcast message from a Coordinator and stuff it in the cache for the broadcaster process to handle """ body = load_body(req) broadcast_msg_dict = body['broadcast'] cache = BroadcastCache() cache.set_message_and_targets(broadcast_msg_dict['type'], broadcast_msg_dict['targets']) resp.status = falcon.HTTP_202
def on_post(self, req, resp): body = load_body(req) tenant_id = body['tenant_id'] tenant = find_tenant(self.db, tenant_id=tenant_id) if tenant: abort(falcon.HTTP_400, 'Tenant with tenant_id {0} ' 'already exists'.format(tenant_id)) new_tenant = Tenant(tenant_id) self.db.add(new_tenant) self.db.commit() resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}'.format(tenant_id))
def on_post(self, req, resp, tenant_id): body = load_body(req) self._validate_req_body_on_post(body) tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() event_producer_name = body['name'] event_producer_pattern = body['pattern'] #if durable or encrypted aren't specified, set to False if 'durable' in body.keys(): event_producer_durable = body['durable'] else: event_producer_durable = False if 'encrypted' in body.keys(): event_producer_encrypted = body['encrypted'] else: event_producer_encrypted = False # Check if the tenant already has an event producer with this name producer = find_event_producer(tenant, producer_name=event_producer_name) if producer: abort(falcon.HTTP_400, 'Event producer with name {0} already exists with id={1}.' .format(producer.name, producer.get_id())) # Create the new profile for the host new_event_producer = EventProducer( self.db.next_sequence_value(tenant.tenant_id), event_producer_name, event_producer_pattern, event_producer_durable, event_producer_encrypted) tenant.event_producers.append(new_event_producer) self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/producers/{1}' .format(tenant_id, new_event_producer.get_id()))
def on_put(self, req, resp, tenant_id, host_id): body = load_body(req) self._validate_req_body_on_put(body) #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the hosts exists and belongs to the tenant host = find_host(tenant, host_id=host_id) if not host: _host_not_found() if 'hostname' in body.keys() and host.hostname != body['hostname']: # Check if the tenant already has a host with this hostname hostname = str(body['hostname']) for duplicate_host in tenant.hosts: if duplicate_host.hostname == hostname: abort(falcon.HTTP_400, 'Host with hostname {0} already exists with' ' id={1}'.format(hostname, duplicate_host.get_id())) host.hostname = hostname if 'ip_address_v4' in body.keys(): host.ip_address_v4 = body['ip_address_v4'] if 'ip_address_v6' in body.keys(): host.ip_address_v6 = body['ip_address_v6'] if 'profile_id' in body.keys(): if body['profile_id']: host.profile = int(body['profile_id']) else: host.profile = None if host.profile: #verify the profile exists and belongs to the tenant profile = find_host_profile(tenant, profile_id=host.profile) if not profile: _profile_not_found() self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_200
def on_put(self, req, resp, tenant_id, profile_id): #load the message body = load_body(req) self._validate_req_body_on_put(body) #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the profile exists and belongs to the tenant profile = find_host_profile(tenant, profile_id=profile_id) if not profile: _profile_not_found() #if attributes are present in message, update the profile if 'name' in body.keys() and body['name'] != profile.name: #if the tenant already has a profile with this name then abort duplicate_profile = find_host_profile(tenant, profile_name=body['name']) if duplicate_profile: abort(falcon.HTTP_400, 'Profile with name {0} already exists with id={1}.' .format(duplicate_profile.name, duplicate_profile.get_id())) profile.name = body['name'] if 'event_producer_ids' in body.keys(): producer_ids = body['event_producer_ids'] for producer_id in producer_ids: #abort if any of the event_producers being passed in are not # valid event_producers for this tenant if not find_event_producer(tenant, producer_id=producer_id): _producer_not_found() #update the list of event_producers profile.event_producers = producer_ids self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_200
def on_post(self, req, resp, tenant_id): body = load_body(req) self._validate_req_body_on_post(body) tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() profile_name = body['name'] # Check if the tenant already has a profile with this name profile = find_host_profile(tenant, profile_name=profile_name) if profile: abort(falcon.HTTP_400, 'Profile with name {0} already exists with id={1}.' .format(profile.name, profile.get_id())) # Create the new profile for the host new_host_profile = HostProfile( self.db.next_sequence_value(tenant.tenant_id), profile_name) if 'event_producer_ids' in body.keys(): producer_ids = body['event_producer_ids'] for producer_id in producer_ids: #abort if any of the event_producers being passed in are not # valid event_producers for this tenant if not find_event_producer(tenant, producer_id=producer_id): _producer_not_found() #update the list of event_producers new_host_profile.event_producers = producer_ids tenant.profiles.append(new_host_profile) self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/profiles/{1}' .format(tenant_id, new_host_profile.get_id()))
def on_put(self, req, resp, tenant_id, event_producer_id): body = load_body(req) self._validate_req_body_on_put(body) #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the event_producer exists and belongs to the tenant event_producer = find_event_producer(tenant, producer_id=event_producer_id) if not event_producer: _producer_not_found() #if a key is present, update the event_producer with the value if 'name' in body.keys() and event_producer.name != body['name']: #if the tenant already has a profile with this name then abort duplicate_producer = \ find_event_producer(tenant, producer_name=body['name']) if duplicate_producer: abort(falcon.HTTP_400, 'EventProducer with name {0} already exists with id={1}.' .format(duplicate_producer.name, duplicate_producer.get_id())) event_producer.name = body['name'] if 'pattern' in body.keys(): event_producer.pattern = str(body['pattern']) if 'durable' in body.keys(): event_producer.durable = body['durable'] if 'encrypted' in body.keys(): event_producer.encrypted = body['encrypted'] self.db.update('tenant', tenant.format_for_save()) resp.status = falcon.HTTP_200
def on_post(self, req, resp): body = load_body(req) self._validate_req_body_on_post(body) tenant_id = str(body['tenant_id']) #validate that tenant does not already exists tenant = find_tenant(self.db, tenant_id=tenant_id) if tenant: abort(falcon.HTTP_400, 'Tenant with tenant_id {0} ' 'already exists'.format(tenant_id)) #create new token for the tenant new_token = Token() new_tenant = Tenant(tenant_id, new_token) self.db.put('tenant', new_tenant.format()) self.db.create_sequence(new_tenant.tenant_id) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}'.format(tenant_id))
def on_post(self, req, resp, tenant_id): tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) body = load_body(req) event_producer_name = body['name'] event_producer_pattern = body['pattern'] #if durable or encrypted aren't specified, set to False if 'durable' in body.keys(): event_producer_durable = body['durable'] else: event_producer_durable = False if 'encrypted' in body.keys(): event_producer_encrypted = body['encrypted'] else: event_producer_encrypted = False # Check if the event_producer already has a profile with this name for event_producer in tenant.event_producers: if event_producer.name == event_producer_name: abort(falcon.HTTP_400, 'Producer {0} with name {1} already exists.' .format(event_producer.id, event_producer.name)) # Create the new profile for the host new_event_producer = EventProducer(tenant.id, event_producer_name, event_producer_pattern, event_producer_durable, event_producer_encrypted) self.db.add(new_event_producer) tenant.event_producers.append(new_event_producer) self.db.commit() resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/producers/{1}' .format(tenant_id, new_event_producer.id))
def on_post(self, req, resp, tenant_id): """ This method is passed log event data by a tenant. The request will have a message token and a tenant id which must be validated either by the local cache or by a call to this workers coordinator. """ #read message token from header message_token = req.get_header(MESSAGE_TOKEN, required=True) #Validate the tenant's JSON event log data as valid JSON. body = load_body(req) self._validate_req_body_on_post(body) tenant_identification = TenantIdentification( tenant_id, message_token) try: tenant = tenant_identification.get_validated_tenant() correlator = Correlator(tenant, body) correlator.process_message() try: self.router.route_message(correlator.message) except RoutingException as ex: abort(falcon.HTTP_500, 'error routing message') if correlator.is_durable(): resp.status = falcon.HTTP_202 resp.body = format_response_body( correlator.get_durable_job_info()) else: resp.status = falcon.HTTP_204 except MessageAuthenticationError as ex: abort(falcon.HTTP_401, ex.message) except ResourceNotFoundError as ex: abort(falcon.HTTP_404, ex.message) except CoordinatorCommunicationError: abort(falcon.HTTP_500)
def on_put(self, req, resp, tenant_id, host_id): #verify the tenant exists tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) #verify the hosts exists host = find_host(self.db, host_id, when_not_found=_host_not_found) #verify the host belongs to the tenant if not host in tenant.hosts: _host_not_found() body = load_body(req) if 'hostname' in body.keys(): host.hostname = body['hostname'] if 'ip_address' in body.keys(): host.ip_address = body['ip_address'] if 'profile_id' in body.keys(): profile_id = body['profile_id'] #if profile_id has a new value, # find the profile and assign it to the host if profile_id: profile = find_host_profile(self.db, id=profile_id, when_not_found=_profile_not_found) host.profile = profile #if the profile id key passed an empty value, # then remove the profile form the host else: host.profile = None self.db.commit() resp.status = falcon.HTTP_200
def on_post(self, req, resp, tenant_id): tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) body = load_body(req) hostname = body['hostname'] ip_address = body['ip_address'] #if profile id is not in post message, then use a null profile if 'profile_id' in body.keys(): profile_id = body['profile_id'] else: profile_id = None profile = None #if profile id is in post message, then make sure it is valid profile if profile_id: profile = find_host_profile(self.db, id=profile_id, when_not_found=_profile_not_found) # Check if the tenant already has a host with this hostname for host in tenant.hosts: if host.hostname == hostname: abort(falcon.HTTP_400, 'Host with hostname {0} already exists with' ' id={1}'.format(hostname, host.id)) # Create the new host definition new_host = Host(hostname, ip_address, profile) self.db.add(new_host) tenant.hosts.append(new_host) self.db().commit() resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/hosts/{1}' .format(tenant_id, new_host.id))
def on_post(self, req, resp, tenant_id): tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) body = load_body(req) profile_name = body['name'] # Check if the tenant already has a profile with this name for profile in tenant.profiles: if profile.name == profile_name: abort(falcon.HTTP_400, 'Profile with name {0} already exists.' .format(profile.name, profile.id)) # Create the new profile for the host new_host_profile = HostProfile(tenant.id, profile_name) tenant.profiles.append(new_host_profile) if 'event_producer_ids' in body.keys(): event_producer_ids = body['event_producer_ids'] for event_producer_id in event_producer_ids: event_producer = find_event_producer(self.db, id=event_producer_id, when_not_found= _producer_not_found) if event_producer in tenant.event_producers: new_host_profile.event_producers.append(event_producer) else: _producer_not_found() self.db.add(new_host_profile) self.db.commit() resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/profiles/{1}' .format(tenant_id, new_host_profile.id))
def on_post(self, req, resp, tenant_id): tenant = find_tenant(self.db, tenant_id=tenant_id, when_not_found=_tenant_not_found) body = load_body(req) profile_name = body['name'] # Check if the tenant already has a profile with this name for profile in tenant.profiles: if profile.name == profile_name: abort(400, 'Profile with name {0} already exists.' .format(profile.name, profile.id)) # Create the new profile for the host new_host_profile = HostProfile(tenant.id, profile_name) tenant.profiles.append(new_host_profile) self.db.add(new_host_profile) self.db.commit() resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/profiles/{1}' .format(tenant_id, new_host_profile.id))