def test_get_tacc_username_api_problem(self, cassette): """Make sure we don't return the Atmosphere username when we have trouble connecting to the TAS API. It should fail. TODO: Figure out how to handle it gracefully. """ from jetstream.allocation import TASAPIDriver tas_driver = TASAPIDriver() tas_driver.clear_cache() self.assertDictEqual(tas_driver.username_map, {}) user = UserFactory.create(username='******') tacc_username = tas_driver.get_tacc_username(user) self.assertIsNone(tacc_username) self.assertDictEqual(tas_driver.username_map, {}) assert_cassette_playback_length(cassette, 1)
def validate_user(self, user): """ Validates an account based on the business logic assigned by jetstream. In this example: * Accounts are *ONLY* valid if they have 1+ 'jetstream' allocations. * All other allocations are ignored. """ driver = TASAPIDriver() try: project_allocations = fill_user_allocation_source_for(driver, user) if not project_allocations: return False return True except (NoTaccUserForXsedeException, NoAccountForUsernameException): logger.exception('User is invalid: %s', user) return False except TASAPIException: logger.exception( 'Some other error happened while trying to validate user: %s', user) active_allocation_count = UserAllocationSource.objects.filter( only_current_user_allocations() & Q(user=user)).count() logger.debug('user: %s, active_allocation_count: %d', user, active_allocation_count) return active_allocation_count > 0
def test_get_all_projects(self, cassette): """Test retrieving projects for a Jetstream user""" from jetstream.allocation import TASAPIDriver tas_driver = TASAPIDriver() projects = tas_driver.get_all_projects() self.assertEquals(len(projects), 2) self.assertEquals(projects[0]['chargeCode'], u'CH-916862') self.assertEquals(projects[-1]['chargeCode'], u'TG-MCB960139') self.maxDiff = None response = json.loads(cassette.data[0][1]['body']['string']) result = response['result'] self.assertEquals(len(projects), len(result)) self.assertEquals(projects[0], result[0]) self.assertEquals(projects[-1], result[-1]) assert_cassette_playback_length(cassette, 1)
class XsedeGroup(AtmosphereAccountCreationPlugin): """ For Jetstream, AccountCreation respects the "Directory" between User and Project. NOTE: This requires some communication with the TAS API Driver and can take some time to generate a list... Due to memoization, it is best to "Batch" these requests. For Each project listed for tacc_username(user.username): - 'username' == tacc_username(user.username) - 'project_name' == tacc_project_name """ def __init__(self): self.tas_driver = TASAPIDriver() if not self.tas_driver.tacc_api: raise Exception( "Attempting to use the XsedeGroup CreationPlugin without TAS driver. Fix your configuration to continue" ) def get_credentials_list(self, provider, username): credentials_list = [] tacc_username = self.tas_driver._xsede_to_tacc_username(username) if not tacc_username: logger.warn( "TAS Driver found no TACC Username for XUP User %s" % username ) tacc_username = username tacc_projects = self.tas_driver.find_projects_for(tacc_username) for tacc_project in tacc_projects: tacc_projectname = tacc_project['chargeCode'] # TODO: should is_leader be used in the dict created below? # tacc_leader_username = tacc_project.get('pi', {})\ # .get('username', '') # is_leader = tacc_leader_username == tacc_username credentials_list.append( { 'account_user': username, 'username': tacc_username, 'project_name': tacc_projectname, 'group_name': tacc_projectname, 'is_leader': False, } ) return credentials_list
def validate_user(self, user): """ Validates an account based on the business logic assigned by jetstream. In this example: * Accounts are *ONLY* valid if they have 1+ 'jetstream' allocations. * All other allocations are ignored. """ tas_driver = TASAPIDriver() try: tacc_username = tas_driver.get_tacc_username(user) project_allocations = tas_driver.get_user_allocations( tacc_username) if not project_allocations: return False return True except TASAPIException: logger.exception("Could not validate user: %s" % user) return False
def _create_reports_for(user, allocation_name, end_date): driver = TASAPIDriver() tacc_username = user.username # driver.get_tacc_username(user) if not tacc_username: logger.error( "No TACC username for user: '******' which came from allocation id: {}" .format(user, allocation_name)) return project_name = allocation_name # driver.get_allocation_project_name(allocation_name) try: project_report = _create_tas_report_for(user, tacc_username, project_name, end_date) return project_report except TASPluginException: logger.exception( "Could not create the report because of the error below") return
def get_default_quota(self, user, provider): """ If a user is only in a special allocation, then give them a reduced quota """ driver = TASAPIDriver() special_allocation_sources = getattr(django.conf.settings, 'SPECIAL_ALLOCATION_SOURCES', {}) default_quota = None try: project_allocations = fill_user_allocation_source_for(driver, user) if project_allocations and len( project_allocations) == 1 and project_allocations[ 0].name in special_allocation_sources: import core.models sub_allocation_quota = special_allocation_sources[ project_allocations[0].name].get('default_quota') if sub_allocation_quota: default_quota = core.models.Quota.objects.get_or_create( **sub_allocation_quota)[0] return default_quota except TASAPIException: logger.exception('Could not validate user: %s' % user) return None
def validate_user(self, user): """ Validates an account based on the business logic assigned by jetstream. In this example: * Accounts are *ONLY* valid if they have 1+ 'jetstream' allocations. * All other allocations are ignored. """ driver = TASAPIDriver() try: project_allocations = fill_user_allocation_source_for(driver, user) if not project_allocations: return False return True except (NoTaccUserForXsedeException, NoAccountForUsernameException) as e: logger.exception('User is invalid: %s', user) return False except TASAPIException: logger.exception('Some other error happened while trying to validate user: %s', user) active_allocation_count = core.models.UserAllocationSource.objects.filter(user=user).count() # TODO: Also check that: # - the start date of the allocation source is in the past, and # - the end date of the allocation source is not set, or is in the future. logger.debug('user: %s, active_allocation_count: %d', active_allocation_count, user) return active_allocation_count > 0
def __init__(self): self.tas_driver = TASAPIDriver() if not self.tas_driver.tacc_api: raise Exception("Attempting to use the XsedeGroup CreationPlugin without TAS driver. Fix your configuration to continue")