def test_get_service_unavailable(self, mock_session): mock_session.return_value.get.return_value = MockResponse( HTTPStatus.SERVICE_UNAVAILABLE) result = RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services() self.assertTrue(result is None)
def test_get_success(self, mock_session): response = [RangerService()] mock_session.return_value.get.return_value = MockResponse( HTTPStatus.OK, response=response, content='Success') result = RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services() self.assertEqual(response, result)
def test_unexpected_http_method(self, mock_api): mock_api.method.return_value = "PATCH" try: RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({}) except Exception as e: self.assertTrue('Unsupported HTTP Method' in repr(e))
def test_get_success(self, mock_session): response = RangerService() mock_session.return_value.get.return_value = MockResponse( HTTPStatus.OK, response=response, content='Success') result = RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({}) self.assertTrue(response.__repr__() in result)
def test_unexpected_http_method(self, mock_api): mock_api.method.return_value = "PATCH" mock_api.url = TestRangerClient.URL mock_api.path = RangerClient.URI_SERVICE try: RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services() except RangerServiceException as e: self.assertTrue('Unsupported HTTP Method' in repr(e))
def test_get_unexpected_status_code(self, mock_response, mock_session): content = 'Internal Server Error' mock_response.text = content mock_response.content = content mock_response.status_code = HTTPStatus.INTERNAL_SERVER_ERROR mock_session.return_value.get.return_value = mock_response try: RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services() except RangerServiceException as e: self.assertTrue(HTTPStatus.INTERNAL_SERVER_ERROR, e.statusCode)
def test_get_unexpected_status_code(self, mock_response, mock_session): content = 'Internal Server Error' mock_response.text = content mock_response.content = content mock_response.status_code = HTTPStatus.INTERNAL_SERVER_ERROR mock_session.return_value.get.return_value = mock_response try: RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({}) except Exception as e: self.assertTrue(content in repr(e))
from apache_ranger.model.ranger_service import RangerService from apache_ranger.client.ranger_client import RangerClient ranger_client = RangerClient('http://ranger:6080', 'admin', 'rangerR0cks!') service = RangerService(name='dev_hbase', type='hbase', configs={'username':'******', 'password':'******', 'hadoop.security.authentication': 'simple', 'hbase.security.authentication': 'simple', 'hadoop.security.authorization': 'true', 'hbase.zookeeper.property.clientPort': '16181', 'hbase.zookeeper.quorum': 'ranger-hbase', 'zookeeper.znode.parent': '/hbase'}) ranger_client.create_service(service)
if __name__ == "__main__": parser = argparse.ArgumentParser(description='Parameters required for connection to Ranger Admin!') parser.add_argument('--url', default='http://localhost:6080') parser.add_argument('--username') parser.add_argument('--password') args = parser.parse_args() service_def_name = "sampleServiceDef" service_name = "sampleService" policy_name = "samplePolicy" role_name = "sampleRole" ranger_client = RangerClient(args.url, args.username, args.password) # create a new service definition config = RangerServiceConfigDef(itemId=1, name='sampleConfig', type='string') access_type = RangerAccessTypeDef(itemId=1, name='sampleAccess') resource_def = RangerResourceDef(itemId=1, name='root', type='string') service_def = RangerServiceDef(name=service_def_name, configs=[config], accessTypes=[access_type], resources=[resource_def]) created_service_def = ranger_client.create_service_def(service_def) log.info('New Service Definition created successfully {}'.format(created_service_def)) # create a new service