Ejemplo n.º 1
0
 def setUp(self):
     self.handles = list()
     self.list_tables_requests = list()
     self.num_handles = 1 if is_prod_pod() or is_onprem() else 2
     for handle in range(self.num_handles):
         tenant = tenant_id + ('' if handle == 0 else str(handle))
         self.handles.append(get_handle(tenant))
         self.list_tables_requests.append(
             ListTablesRequest().set_timeout(timeout))
     self.list_tables_request = ListTablesRequest().set_timeout(timeout)
Ejemplo n.º 2
0
 def tearDownClass(cls):
     for handle in range(len(cls.handles)):
         try:
             ltr = ListTablesRequest()
             result = cls.handles[handle].list_tables(ltr)
             for table in result.get_tables():
                 if table.startswith(table_prefix):
                     drop_request = TableRequest().set_statement(
                         'DROP TABLE IF EXISTS ' + table)
                     cls.table_request(drop_request, cls.handles[handle])
         finally:
             cls.handles[handle].close()
             delete_tenant(tenant_id + str(handle))
     delete_tier()
    def testAccessTokenProviderOldPath(self):
        account_at = 'account-at'
        service_at = 'service-at'

        class TokenHandler(SimpleHTTPRequestHandler):
            def do_GET(self):
                rawpath = self.path.split('?')[0]
                if rawpath == APP_ENDPOINT:
                    self.send_response(codes.ok)
                    self.send_header('Content-Type', 'application/json')
                    self.send_header('Content-Length', str(len(PSM_INFO)))
                    self.end_headers()
                    self.wfile.write(PSM_INFO.encode())

            def do_POST(self):
                rawpath = self.path.split('?')[0]
                if rawpath == TOKEN_ENDPOINT:
                    content = self.rfile.read(
                        int(self.headers['Content-Length']))
                    content = unquote(content.decode())
                    if 'andc' in content:
                        res = str.format(TOKEN_RESULT, service_at)
                    else:
                        res = str.format(TOKEN_RESULT, account_at)
                    self.send_response(codes.ok)
                    self.send_header('Content-Type', 'application/json')
                    self.send_header('Content-Length', str(len(res)))
                    self.end_headers()
                    self.wfile.write(res.encode())

        httpd, port = self.__find_port_start_server(TokenHandler)

        self.base = 'http://localhost:' + str(port)
        self.token_provider = DefaultAccessTokenProvider(
            idcs_url=self.base,
            entitlement_id='123456789',
            creds_provider=self.creds_provider)
        # get authorization string for ListTablesRequest
        result = self.token_provider.get_authorization_string(
            ListTablesRequest())
        self.assertIsNotNone(result)
        self.assertEqual(result, 'Bearer ' + account_at)
        # get authorization string for TableRequest
        result = self.token_provider.get_authorization_string(TableRequest())
        self.assertIsNotNone(result)
        self.assertEqual(result, 'Bearer ' + service_at)
        self.__stop_server(httpd)
 def testRealCloudGetAuthorizationStringAndToken(self):
     generate_properties_file(idcs_url(), credentials_file)
     self.token_provider = DefaultAccessTokenProvider(
         idcs_props_file=properties_file)
     # get authorization string for ListTablesRequest
     result = self.token_provider.get_authorization_string(
         ListTablesRequest())
     self.assertIsNotNone(result)
     # get authorization string for TableRequest
     result = self.token_provider.get_authorization_string(
         TableRequest())
     self.assertIsNotNone(result)
     # get account access token
     result = self.token_provider.get_account_access_token()
     self.assertIsNotNone(result)
     # get service access token
     result = self.token_provider.get_service_access_token()
     self.assertIsNotNone(result)
Ejemplo n.º 5
0
    def testAccessTokenProviderGetAuthorizationString(self):
        global GET_INFO, POST_INFO
        GET_INFO = CLIENT_INFO
        POST_INFO = None
        httpd, port = self._find_port_start_server(TokenHandler)

        self.base = 'http://localhost:' + str(port)
        self.token_provider = DefaultAccessTokenProvider(
            idcs_url=self.base, creds_provider=self.creds_provider)
        # get authorization string for ListTablesRequest
        result = self.token_provider.get_authorization_string(
            ListTablesRequest())
        self.assertIsNotNone(result)
        self.assertEqual(result, 'Bearer ' + ACCOUNT_AT)
        # get authorization string for TableRequest
        result = self.token_provider.get_authorization_string(TableRequest())
        self.assertIsNotNone(result)
        self.assertEqual(result, 'Bearer ' + SERVICE_AT)
        self._stop_server(httpd)
Ejemplo n.º 6
0
 def drop_all_tables(cls):
     ltr = ListTablesRequest()
     result = cls.handle.list_tables(ltr)
     for table in result.get_tables():
         if table.startswith(table_prefix):
             cls.drop_table(table)
Ejemplo n.º 7
0
def main():

    handle = None
    try:
        #
        # Create a handle
        #
        handle = get_handle(tenant_id)

        #
        # List any existing tables for this tenant
        #
        print('Listing tables')
        ltr = ListTablesRequest()
        lr_result = handle.list_tables(ltr)
        print('Existing tables: ' + str(lr_result))

        #
        # Create a table
        #
        statement = 'Create table if not exists ' + table_name + '(id integer, \
sid integer, name string, primary key(shard(sid), id))'

        print('Creating table: ' + statement)
        request = TableRequest().set_statement(statement).set_table_limits(
            TableLimits(30, 10, 1))
        result = handle.table_request(request)

        #
        # Table creation can take time, depending on the state of the system.
        # If if fails after 40s, re-run the program
        #
        result.wait_for_state(handle, table_name, State.ACTIVE, 40000, 3000)
        print('After create table')

        #
        # Create an index
        #
        statement = ('Create index if not exists ' + index_name + ' on ' +
                     table_name + '(name)')
        print('Creating index: ' + statement)
        request = TableRequest().set_statement(statement)
        result = handle.table_request(request)

        #
        # Index creation can take time, depending on the state of the system.
        # If if fails after 40s, re-run the program
        #
        result.wait_for_state(handle, table_name, State.ACTIVE, 40000, 3000)
        print('After create index')

        #
        # Get the table
        #
        request = GetTableRequest().set_table_name(table_name)
        result = handle.get_table(request)
        print('After get table: ' + str(result))

        #
        # Get the indexes
        #
        request = GetIndexesRequest().set_table_name(table_name)
        result = handle.get_indexes(request)
        print('The indexes for: ' + table_name)
        for idx in result.get_indexes():
            print('\t' + str(idx))

        #
        # Get the table usage information
        #
        request = TableUsageRequest().set_table_name(table_name)
        result = handle.get_table_usage(request)
        print('The table usage information for: ' + table_name)
        for record in result.get_usage_records():
            print('\t' + str(record))

        #
        # Drop the table
        #
        if drop_table:
            request = TableRequest().set_statement('drop table if exists ' +
                                                   table_name)
            result = handle.table_request(request)

            #
            # Table drop can take time, depending on the state of the system.
            # If this wait fails the table will still probably been dropped
            #
            result.wait_for_state(handle, table_name, State.DROPPED, 30000,
                                  2000)
            print('After drop table')
        else:
            print('Not dropping table')

        print('Example is complete')
    except Exception as e:
        print(e)
        traceback.print_exc()
    finally:
        # If the handle isn't closed Python will not exit properly
        if handle is not None:
            handle.close()
Ejemplo n.º 8
0
def main():

    handle = None
    try:
        #
        # Create a handle
        #
        handle = get_handle(tenant_id)

        #
        # List any existing tables for this tenant
        #
        print('Listing tables')
        ltr = ListTablesRequest()
        lr_result = handle.list_tables(ltr)
        print('Existing tables: ' + str(lr_result))

        #
        # Create a table
        #
        statement = 'Create table if not exists ' + table_name + '(id integer, \
sid integer, name string, primary key(shard(sid), id))'
        print('Creating table: ' + statement)
        request = TableRequest().set_statement(statement).set_table_limits(
            TableLimits(30, 10, 1))
        handle.do_table_request(request, 40000, 3000)
        print('After create table')

        #
        # Create an index
        #
        statement = ('Create index if not exists ' + index_name + ' on ' +
                     table_name + '(name)')
        print('Creating index: ' + statement)
        request = TableRequest().set_statement(statement)
        handle.do_table_request(request, 40000, 3000)
        print('After create index')

        #
        # Get the table
        #
        request = GetTableRequest().set_table_name(table_name)
        result = handle.get_table(request)
        print('After get table: ' + str(result))

        #
        # Get the indexes
        #
        request = GetIndexesRequest().set_table_name(table_name)
        result = handle.get_indexes(request)
        print('The indexes for: ' + table_name)
        for idx in result.get_indexes():
            print('\t' + str(idx))

        #
        # Get the table usage information, on-prem mode not supported
        #
        if not using_on_prem:
            request = TableUsageRequest().set_table_name(table_name)
            result = handle.get_table_usage(request)
            print('The table usage information for: ' + table_name)
            for record in result.get_usage_records():
                print('\t' + str(record))

        #
        # Drop the index
        #
        request = TableRequest().set_statement(
            'drop index ' + index_name + ' on ' + table_name)
        handle.do_table_request(request, 30000, 2000)
        print('After drop index')

        #
        # Drop the table
        #
        if drop_table:
            request = TableRequest().set_statement(
                'drop table if exists ' + table_name)
            handle.do_table_request(request, 30000, 2000)
            print('After drop table')
        else:
            print('Not dropping table')

        print('Example is complete')
    except Exception as e:
        print(e)
        traceback.print_exc()
    finally:
        # If the handle isn't closed Python will not exit properly
        if handle is not None:
            handle.close()
Ejemplo n.º 9
0
 def setUp(self):
     self.handles = list()
     self.num_handles = 1 if is_pod() else 2
     for handle in range(self.num_handles):
         self.handles.append(get_handle(tenant_id + str(handle)))
     self.list_tables_request = ListTablesRequest().set_timeout(timeout)