def on_post(self, req, resp, tenant_id, validated_body): """ This method resets a token when an HTTP POST is received. There is a minimum time limit that must be reached before resetting a token, unless the call is made with the "invalidate_now: true" option. """ body = validated_body['token'] #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() invalidate_now = body['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() #save the tenant document tenant_util.save_tenant(tenant) resp.status = falcon.HTTP_203 resp.set_header('Location', '/v1/{0}/token'.format(tenant_id))
def test_find_event_producer_by_name_returns_instance(self): with patch('meniscus.data.model.tenant_util._db_handler', self.ds_handler): tenant = tenant_util.find_tenant('12345') producer = tenant_util.find_event_producer( tenant, producer_name='system.auth') self.assertIsInstance(producer, EventProducer)
def test_find_event_producer_by_name_returns_none(self): with patch('meniscus.data.model.tenant_util._db_handler', self.ds_handler): tenant = tenant_util.find_tenant('12345') producer = tenant_util.find_event_producer( tenant, producer_name='not_name') self.assertEquals(producer, None)
def test_find_tenant_creates_new_tenant_on_no_tenant_found(self): retrieve_tenant_call = MagicMock(side_effect=[None, self.tenant_obj]) create_tenant_call = MagicMock() with patch('meniscus.data.model.tenant_util.retrieve_tenant', retrieve_tenant_call), patch( 'meniscus.data.model.tenant_util.create_tenant', create_tenant_call): tenant = tenant_util.find_tenant('unknown_tenant_id', create_on_missing=True) self.assertIsInstance(tenant, Tenant) create_tenant_call.assert_called_once_with('unknown_tenant_id')
def test_find_tenant_creates_new_tenant_on_no_tenant_found(self): retrieve_tenant_call = MagicMock(side_effect=[None, self.tenant_obj]) create_tenant_call = MagicMock() with patch('meniscus.data.model.tenant_util.retrieve_tenant', retrieve_tenant_call), patch( 'meniscus.data.model.tenant_util.create_tenant', create_tenant_call): tenant = tenant_util.find_tenant( 'unknown_tenant_id', create_on_missing=True) self.assertIsInstance(tenant, Tenant) create_tenant_call.assert_called_once_with('unknown_tenant_id')
def on_get(self, req, resp, tenant_id): """ Retrieve a specified tenant when a HTTP GET is received """ tenant = tenant_util.find_tenant(tenant_id=tenant_id, create_on_missing=True) if not tenant: _tenant_not_found() resp.status = falcon.HTTP_200 resp.body = api.format_response_body({'tenant': tenant.format()})
def on_get(self, req, resp, tenant_id): """ Retrieve a specified tenant when a HTTP GET is received """ tenant = tenant_util.find_tenant( tenant_id=tenant_id, create_on_missing=True) if not tenant: _tenant_not_found() resp.status = falcon.HTTP_200 resp.body = api.format_response_body({'tenant': tenant.format()})
def on_get(self, req, resp, tenant_id): """ Retrieve a list of all Event Producers for a specified Tenant """ tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() resp.status = falcon.HTTP_200 resp.body = api.format_response_body( {'event_producers': [p.format() for p in tenant.event_producers]})
def on_post(self, req, resp, tenant_id, validated_body): """ Create a a new event Producer for a specified Tenant when an HTTP Post is received """ body = validated_body['event_producer'] tenant = tenant_util.find_tenant(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 if 'sinks' in body.keys(): event_producer_sinks = body['sinks'] else: event_producer_sinks = None # Check if the tenant already has an event producer with this name producer = tenant_util.find_event_producer( tenant, producer_name=event_producer_name) if producer: api.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 producer_id = tenant_util.create_event_producer( tenant, event_producer_name, event_producer_pattern, event_producer_durable, event_producer_encrypted, event_producer_sinks) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/producers/{1}' .format(tenant_id, producer_id))
def on_get(self, req, resp, tenant_id): """ Retrieves Token information for a specified Tenant when an HTTP GET call is received """ #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() resp.status = falcon.HTTP_200 resp.body = api.format_response_body({'token': tenant.token.format()})
def on_put(self, req, resp, tenant_id, event_producer_id, validated_body): """ Make an update to a specified Event Producer's configuration when an HTTP PUT is received """ body = validated_body['event_producer'] #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the event_producer exists and belongs to the tenant event_producer = tenant_util.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 = tenant_util.find_event_producer( tenant, producer_name=body['name']) if duplicate_producer: api.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: event_producer.pattern = str(body['pattern']) if 'durable' in body: event_producer.durable = body['durable'] if 'encrypted' in body: event_producer.encrypted = body['encrypted'] if 'sinks' in body: event_producer.sinks = body['sinks'] #save the tenant document tenant_util.save_tenant(tenant) resp.status = falcon.HTTP_200
def on_put(self, req, resp, tenant_id, event_producer_id, validated_body): """ Make an update to a specified Event Producer's configuration when an HTTP PUT is received """ body = validated_body['event_producer'] #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the event_producer exists and belongs to the tenant event_producer = tenant_util.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 = tenant_util.find_event_producer( tenant, producer_name=body['name']) if duplicate_producer: api.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: event_producer.pattern = str(body['pattern']) if 'durable' in body: event_producer.durable = body['durable'] if 'encrypted' in body: event_producer.encrypted = body['encrypted'] if 'sinks' in body: event_producer.sinks = body['sinks'] #save the tenant document tenant_util.save_tenant(tenant) resp.status = falcon.HTTP_200
def on_post(self, req, resp, tenant_id, validated_body): """ Create a a new event Producer for a specified Tenant when an HTTP Post is received """ body = validated_body['event_producer'] tenant = tenant_util.find_tenant(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 if 'sinks' in body.keys(): event_producer_sinks = body['sinks'] else: event_producer_sinks = None # Check if the tenant already has an event producer with this name producer = tenant_util.find_event_producer( tenant, producer_name=event_producer_name) if producer: api.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 producer_id = tenant_util.create_event_producer( tenant, event_producer_name, event_producer_pattern, event_producer_durable, event_producer_encrypted, event_producer_sinks) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}/producers/{1}'.format(tenant_id, producer_id))
def on_head(self, req, resp, tenant_id): """ Validates a token for a specified tenant when an HTTP HEAD call is received """ #get message token, or abort if token is not in header message_token = req.get_header(MESSAGE_TOKEN, required=True) #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() if not tenant.token.validate_token(message_token): _message_token_is_invalid() resp.status = falcon.HTTP_200
def on_delete(self, req, resp, tenant_id, event_producer_id): """ Delete a specified Event Producer from a Tenant's configuration when an HTTP DELETE is received """ #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the event_producer exists and belongs to the tenant event_producer = tenant_util.find_event_producer( tenant, producer_id=event_producer_id) if not event_producer: _producer_not_found() tenant_util.delete_event_producer(tenant, event_producer) resp.status = falcon.HTTP_200
def on_get(self, req, resp, tenant_id, event_producer_id): """ Retrieve a specified Event Producer from a Tenant when an HTTP GET is received """ #verify the tenant exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if not tenant: _tenant_not_found() #verify the event_producer exists and belongs to the tenant event_producer = tenant_util.find_event_producer( tenant, producer_id=event_producer_id) if not event_producer: _producer_not_found() resp.status = falcon.HTTP_200 resp.body = api.format_response_body( {'event_producer': event_producer.format()})
def on_post(self, req, resp, validated_body): """ Create a new tenant when a HTTP POST is received """ body = validated_body['tenant'] tenant_id = str(body['tenant_id']) tenant_name = body.get('tenant_name', tenant_id) #validate that tenant does not already exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if tenant: api.abort(falcon.HTTP_400, 'Tenant with tenant_id {0} ' 'already exists'.format(tenant_id)) tenant_util.create_tenant(tenant_id=tenant_id, tenant_name=tenant_name) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}'.format(tenant_id))
def on_post(self, req, resp, validated_body): """ Create a new tenant when a HTTP POST is received """ body = validated_body['tenant'] tenant_id = str(body['tenant_id']) tenant_name = body.get('tenant_name', tenant_id) #validate that tenant does not already exists tenant = tenant_util.find_tenant(tenant_id=tenant_id) if tenant: api.abort( falcon.HTTP_400, 'Tenant with tenant_id {0} ' 'already exists'.format(tenant_id)) tenant_util.create_tenant(tenant_id=tenant_id, tenant_name=tenant_name) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}'.format(tenant_id))
def test_find_tenant_returns_instance(self): retrieve_tenant_call = MagicMock(return_value=self.tenant_obj) with patch('meniscus.data.model.tenant_util.retrieve_tenant', retrieve_tenant_call): tenant = tenant_util.find_tenant('12345') self.assertIsInstance(tenant, Tenant)
def test_find_tenant_returns_none(self): retrieve_tenant_call = MagicMock(return_value=None) with patch('meniscus.data.model.tenant_util.retrieve_tenant', retrieve_tenant_call): tenant = tenant_util.find_tenant('12345') self.assertIsNone(tenant)