How to use the towerlib.towerlibexceptions.InvalidUser function in towerlib

To help you get started, we’ve selected a few towerlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github schubergphilis / towerlib / tests / integration / test_towerlib.py View on Github external
def test_user_lifecycle(self):
        with self.recorder:
            user = self.tower.create_user('new_user',
                                          'password',
                                          'first_name',
                                          'last_name',
                                          'new@user.com')
            self.assertIsInstance(user, User)
            duplicate_user = self.tower.create_user('new_user',
                                                    'password2',
                                                    'first_name2',
                                                    'last_name2',
                                                    'new2s@user.com')
            self.assertIsNone(duplicate_user)
            self.assertTrue(self.tower.delete_user('new_user'))
            with self.assertRaises(InvalidUser):
                self.tower.delete_user('new_user')
github schubergphilis / towerlib / tests / integration / test_towerlib.py View on Github external
'CredName',
                                                                      'CredDescription',
                                                                      'workflow_admin',
                                                                      'workflow_team',
                                                                      'Source Control',
                                                                      '{}')
            self.assertIsInstance(credential, GenericCredential)
            with self.assertRaises(InvalidOrganization):
                self.tower.create_credential_in_organization_with_type_id('BrokenOrg',
                                                                          'CredName2',
                                                                          'CredDescription',
                                                                          'workflow_admin',
                                                                          'workflow_team',
                                                                          'Source Control',
                                                                          '{}')
            with self.assertRaises(InvalidUser):
                self.tower.create_credential_in_organization_with_type_id('workflow',
                                                                          'CredName2',
                                                                          'CredDescription',
                                                                          'workflow_adminBroken',
                                                                          'workflow_team',
                                                                          'Source Control',
                                                                          '{}')
            with self.assertRaises(InvalidTeam):
                self.tower.create_credential_in_organization_with_type_id('workflow',
                                                                          'CredName2',
                                                                          'CredDescription',
                                                                          'workflow_admin',
                                                                          'workflow_teamBroken',
                                                                          'Source Control',
                                                                          '{}')
            with self.assertRaises(InvalidVariables):
github schubergphilis / towerlib / tests / integration / test_team.py View on Github external
def test_mutating_users(self):
        with self.recorder:
            username = 'workflow_normal'
            username_broken = 'workflow_normalBroken'
            self.assertFalse(bool(list(self.team.users)))
            with self.assertRaises(InvalidUser):
                self.team.add_user_as_member(username_broken)
            self.assertTrue(self.team.add_user_as_member(username))
            user = self.team.get_user_by_username(username)
            self.assertTrue(user.username == username)
            self.assertTrue(self.team.remove_user_as_member(username))
            self.assertTrue(self.team.add_user_as_admin(username))
            self.assertTrue(self.team.remove_user_as_admin(username))
github schubergphilis / towerlib / towerlib / entities / team.py View on Github external
def _post_user_with_permission(self, username, role_name, remove=False):
        permission = self._get_permission(role_name, self.object_roles)
        user = self._tower.get_user_by_username(username)
        if not user:
            raise InvalidUser(username)
        url = '{api}/users/{id}/roles/'.format(api=self._tower.api,
                                               id=user.id)
        payload = {'id': permission.id}
        if remove:
            roles_ids = [role.id for role in user.roles]
            if permission.id not in roles_ids:
                self._logger.warning('"%s" is not part of the team', username)
                return False
            payload['disassociate'] = True
        response = self._tower.session.post(url, json=payload)
        if not response.ok:
            self._logger.error('Error posting to url "%s", response was: "%s"', url, response.text)
        return response.ok
github schubergphilis / towerlib / towerlib / towerlib.py View on Github external
Returns:
            Credential: The created credential upon success, None otherwise.

        Raises:
            InvalidOrganization: The organization provided as argument does not exist.
            InvalidUser: The user provided as argument does not exist.
            InvalidTeam: The team provided as argument does not exist.
            InvalidVariables: The inputs provided as argument is not valid json.

        """
        organization_ = self.get_organization_by_name(organization)
        if not organization_:
            raise InvalidOrganization(organization)
        user_ = self.get_user_by_username(user)
        if not user_:
            raise InvalidUser(user)
        team_ = organization_.get_team_by_name(team)
        if not team_:
            raise InvalidTeam(team)
        payload = {'name': name,
                   'description': description,
                   'organization': organization_.id,
                   'user': user_.id,
                   'team': team_.id,
                   'credential_type': credential_type_id}
        if not validate_json(inputs_):
            raise InvalidVariables(inputs_)
        payload['inputs'] = json.loads(inputs_)
        url = '{api}/credentials/'.format(api=self.api)
        response = self.session.post(url, json=payload)
        if not response.ok:
            self._logger.error('Error creating credential "%s", response was: "%s"', name, response.text)
github schubergphilis / towerlib / towerlib / towerlib.py View on Github external
Credential: The created credential upon success, None otherwise.

        Raises:
            InvalidOrganization: The organization provided as argument does not exist.
            InvalidUser: The user provided as argument does not exist.
            InvalidTeam: The team provided as argument does not exist.
            InvalidCredentialType: The credential type provided as argument does not exist.
            InvalidVariables: The inputs provided as argument is not valid json.

        """
        organization_ = self.get_organization_by_name(organization)
        if not organization_:
            raise InvalidOrganization(organization)
        user_ = self.get_user_by_username(user)
        if not user_:
            raise InvalidUser(user)
        team_ = organization_.get_team_by_name(team)
        if not team_:
            raise InvalidTeam(team)
        credential_type_ = self.get_credential_type_by_name(credential_type)
        if not credential_type_:
            raise InvalidCredentialType(credential_type)
        if not validate_json(inputs_):
            raise InvalidVariables(inputs_)
        return self.create_credential_with_credential_type_id(name,
                                                              credential_type_.id,
                                                              description=description,
                                                              user_id=user_.id,
                                                              team_id=team_.id,
                                                              organization_id=organization_.id,
                                                              inputs=inputs_
                                                              )
github schubergphilis / towerlib / towerlib / __init__.py View on Github external
__copyright__ = '''Copyright 2018, Costas Tyfoxylos'''
__license__ = '''MIT'''
__maintainer__ = '''Costas Tyfoxylos'''
__email__ = ''''''
__status__ = '''Development'''  # "Prototype", "Development", "Production".

# This is to 'use' the module(s), so lint doesn't complain
assert __version__

# assert exceptions
assert AuthFailed
assert InvalidUserLevel
assert InvalidOrganization
assert InvalidVariables
assert InvalidInventory
assert InvalidUser
assert InvalidTeam
assert InvalidCredential
assert InvalidGroup
assert InvalidHost
assert InvalidProject
assert InvalidCredentialType
assert InvalidPlaybook
assert InvalidInstanceGroup
assert InvalidJobType
assert InvalidVerbosity
assert InvalidJobTemplate
assert PermissionNotFound
assert InvalidValue
assert InvalidRole

# assert objects