示例#1
0
 def get_tenants_for_tenants_api(self):
     """
     This method computes the tenants and sites for the tenants service only. Note that the tenants service is a
     special case because it must retrieve the sites and tenants from its own DB, not from
     """
     logger.debug(
         "this is the tenants service, pulling sites and tenants from db..."
     )
     # NOTE: only in the case of the tenants service will we be able to import this function; so this import needs to
     # stay guarded in this method.
     if not conf.service_name == 'tenants':
         raise errors.BaseTapisError(
             "get_tenants_for_tenants_api called by a service other than tenants."
         )
     from service.models import get_tenants as tenants_api_get_tenants
     from service.models import get_sites as tenants_api_get_sites
     # in the case where the tenants api migrations are running, this call will fail with a sqlalchemy.exc.ProgrammingError
     # because the tenants table will not exist yet.
     tenants = []
     result = []
     logger.info("calling the tenants api's get_sites() function...")
     try:
         sites = tenants_api_get_sites()
     except Exception as e:
         logger.info(
             "WARNING - got an exception trying to compute the sites.. "
             "this better be the tenants migration container.")
         return tenants
     logger.info("calling the tenants api's get_tenants() function...")
     try:
         tenants = tenants_api_get_tenants()
     except Exception as e:
         logger.info(
             "WARNING - got an exception trying to compute the tenants.. "
             "this better be the tenants migration container.")
         return tenants
     # for each tenant, look up its corresponding site record and save it on the tenant record--
     for t in tenants:
         # Remove datetime objects --
         t.pop('create_time')
         t.pop('last_update_time')
         # convert the tenants to TapisResult objects, and then append the sites object.
         tn = TapisResult(**t)
         for s in sites:
             if 'primary' in s.keys() and s['primary']:
                 self.primary_site = TapisResult(**s)
             if s['site_id'] == tn.site_id:
                 tn.site = TapisResult(**s)
                 result.append(tn)
                 break
     return result
示例#2
0
def test_tapisresult_self_in_response():
    result = [{
        "self": "use 'self' in the response and you know you're foobar.",
        "a_key": "a_value"
    }]
    tr_list = [TapisResult(r) for r in result]
    assert len(tr_list) == 1
示例#3
0
def test_tapisresult_list_simple():
    result = ['a', 1, 'b', True, None, 3.14159, b'some bytes']
    tr = TapisResult(result)
    r = tr.result
    assert len(r) == 7
    assert r[0] == 'a'
    assert r[1] == 1
    assert r[2] == 'b'
    assert r[3] == True
    assert r[4] == None
    assert r[5] == 3.14159
    assert r[6] == b'some bytes'
示例#4
0
def test_tapisresult_dict():
    result = {
        'a': 1,
        'b': 'bee',
        'c': b'bytes',
        'd': True,
        'e': 3.14159,
        'f': None
    }
    tr = TapisResult(**result)
    assert tr.a == 1
    assert tr.b == 'bee'
    assert tr.c == b'bytes'
    assert tr.d is True
    assert tr.e == 3.14159
    assert tr.f is None
示例#5
0
def test_tapisresult_list_o_dict():
    result = [
        {
            'a': 1,
            'b': 'bee',
            'c': b'bytes',
            'd': True,
            'e': 3.14159,
            'f': None
        },
        {
            'a': 10,
            'b': 'foo',
            'c': b'bytes',
            'd': False,
            'e': 3.14159,
            'f': None
        },
    ]
    tr_list = [TapisResult(**r) for r in result]
    assert len(tr_list) == 2
    # first item -
    tr_1 = tr_list[0]
    assert tr_1.a == 1
    assert tr_1.b == 'bee'
    assert tr_1.c == b'bytes'
    assert tr_1.d is True
    assert tr_1.e == 3.14159
    assert tr_1.f is None
    # 2nd item -
    tr_2 = tr_list[1]
    assert tr_2.a == 10
    assert tr_2.b == 'foo'
    assert tr_2.c == b'bytes'
    assert tr_2.d is False
    assert tr_2.e == 3.14159
    assert tr_2.f is None
示例#6
0
def test_tapisresult_nested_dicts():
    result = [{
        'a': [{
            'bb': 10,
            'cc': True
        }, {
            'dd': 5
        }],
        'b': [{
            'ee': b'bytes'
        }]
    }, {
        'time_1': [{
            'x_0': 'abc',
            'x_1': 'def'
        }, {
            'y_0': 0,
            'y_1': 3.14
        }]
    }]
    tr_list = [TapisResult(**r) for r in result]
    assert len(tr_list) == 2
    # first item -
    tr_1 = tr_list[0]
    assert type(tr_1.a) == list
    assert tr_1.a[0].bb == 10
    assert tr_1.a[0].cc is True
    assert tr_1.a[1].dd == 5

    # 2nd item -
    tr_2 = tr_list[1]
    assert type(tr_2.time_1) == list
    assert tr_2.time_1[0].x_0 == 'abc'
    assert tr_2.time_1[0].x_1 == 'def'
    assert tr_2.time_1[1].y_0 == 0
    assert tr_2.time_1[1].y_1 == 3.14