How to use the towerlib.towerlibexceptions.InvalidCredential 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_team.py View on Github external
def test_mutating_credential_permission(self):
        with self.recorder:
            credential = 'Test Credential'
            credential_type = 'Source Control'
            credential_broken = 'Test CredentialBroken'
            with self.assertRaises(InvalidCredential):
                self.team.add_credential_permission_admin(credential_broken, credential_type)
            self.assertTrue(self.team.add_credential_permission_admin(credential, credential_type))
            with self.assertRaises(InvalidCredential):
                self.team.remove_credential_permission_admin(credential_broken, credential_type)
            self.assertTrue(self.team.remove_credential_permission_admin(credential, credential_type))
            with self.assertRaises(InvalidCredential):
                self.team.add_credential_permission_use(credential_broken, credential_type)
            self.assertTrue(self.team.add_credential_permission_use(credential, credential_type))
            with self.assertRaises(InvalidCredential):
                self.team.remove_credential_permission_use(credential_broken, credential_type)
            self.assertTrue(self.team.remove_credential_permission_use(credential, credential_type))
github schubergphilis / towerlib / tests / integration / test_towerlib.py View on Github external
'https://github.com/ansible/ansible-tower-samples')
            self.assertIsInstance(project, Project)
            url = 'https://github.com/ansible/ansible-tower-samples'
            duplicate_project = self.tower.create_project_in_organization('Default',
                                                                          'Project_name',
                                                                          'description',
                                                                          'Test Credential',
                                                                          url)
            self.assertFalse(duplicate_project)
            with self.assertRaises(InvalidOrganization):
                self.tower.create_project_in_organization('NoOrg',
                                                          'name',
                                                          'description',
                                                          'Test Credential',
                                                          'https://github.com/ansible/ansible-tower-samples')
            with self.assertRaises(InvalidCredential):
                self.tower.create_project_in_organization('Default',
                                                          'name',
                                                          'description',
                                                          'No Credential',
                                                          'https://github.com/ansible/ansible-tower-samples')
            with Timeout(TIMEOUT_IN_SECONDS) as timeout:
                while project.status != 'successful':
                    if timeout.reached:
                        raise TimeoutError
                    time.sleep(1)
            self.assertTrue(self.tower.delete_organization_project('Default', 'Project_name'))
            with self.assertRaises(InvalidOrganization):
                self.tower.delete_organization_project('DefaultBroken', 'Project_name')
            with self.assertRaises(InvalidProject):
                self.tower.delete_organization_project('Default', 'Project_name')
github schubergphilis / towerlib / tests / integration / test_towerlib.py View on Github external
self.assertIsNone(self.tower.create_credential_in_organization_with_type_id('workflow',
                                                                                        'CredName2',
                                                                                        'CredDescription',
                                                                                        'workflow_admin',
                                                                                        'workflow_team',
                                                                                        '2',
                                                                                        '{}'))
            with self.assertRaises(InvalidOrganization):
                self.tower.delete_organization_credential_by_name('workflowBroken',
                                                                  'CredName',
                                                                  'Source Control')
            with self.assertRaises(InvalidCredentialType):
                self.tower.delete_organization_credential_by_name('workflow',
                                                                  'CredName',
                                                                  'Source ControlBroken')
            with self.assertRaises(InvalidCredential):
                self.tower.delete_organization_credential_by_name('workflow',
                                                                  'CredNameBroken',
                                                                  'Source Control')
            self.assertTrue(self.tower.delete_organization_credential_by_name('workflow',
                                                                              'CredName',
                                                                              'Source Control'))
            with self.assertRaises(InvalidOrganization):
                self.tower.delete_organization_credential_by_name_with_type_id('workflowBroken',
                                                                               'CredName2',
                                                                               '2')
            with self.assertRaises(InvalidCredential):
                self.tower.delete_organization_credential_by_name_with_type_id('workflow',
                                                                               'CredNameBroken',
                                                                               '2')
            self.assertTrue(self.tower.delete_organization_credential_by_name_with_type_id('workflow',
                                                                                           'CredName2',
github schubergphilis / towerlib / tests / integration / test_team.py View on Github external
def test_mutating_credential_permission(self):
        with self.recorder:
            credential = 'Test Credential'
            credential_type = 'Source Control'
            credential_broken = 'Test CredentialBroken'
            with self.assertRaises(InvalidCredential):
                self.team.add_credential_permission_admin(credential_broken, credential_type)
            self.assertTrue(self.team.add_credential_permission_admin(credential, credential_type))
            with self.assertRaises(InvalidCredential):
                self.team.remove_credential_permission_admin(credential_broken, credential_type)
            self.assertTrue(self.team.remove_credential_permission_admin(credential, credential_type))
            with self.assertRaises(InvalidCredential):
                self.team.add_credential_permission_use(credential_broken, credential_type)
            self.assertTrue(self.team.add_credential_permission_use(credential, credential_type))
            with self.assertRaises(InvalidCredential):
                self.team.remove_credential_permission_use(credential_broken, credential_type)
            self.assertTrue(self.team.remove_credential_permission_use(credential, credential_type))
github schubergphilis / towerlib / towerlib / entities / team.py View on Github external
def _post_credential_permission(self, credential_name, credential_type, permission_name, remove=False):
        credential = self.organization.get_credential_by_name(credential_name, credential_type)
        if not credential:
            raise InvalidCredential(credential_name)
        return self._post_permission(credential.object_roles, permission_name, remove)
github schubergphilis / towerlib / towerlib / towerlib.py View on Github external
InvalidVerbosity: The verbosity provided is not in valid range of 0-4.
            InvalidCredentialType: The credential type is invalid.

        """
        inventory_ = self.get_organization_inventory_by_name(organization, inventory)
        if not inventory_:
            raise InvalidInventory(inventory)
        project_ = self.get_organization_project_by_name(organization, project)
        if not project_:
            raise InvalidProject(project)
        if playbook not in project_.playbooks:
            raise InvalidPlaybook(playbook)
        if all([credential, credential_type]):
            credential_ = inventory_.organization.get_credential_by_name(credential, credential_type)
            if not credential_:
                raise InvalidCredential(credential)
            credential = credential_.id
        elif any([credential, credential_type]):
            self._logger.error('Both credential and credential type should be provided.')
            raise InvalidCredential(credential)
        instance_group_ids = []
        if instance_groups:
            if not isinstance(instance_groups, (list, tuple)):
                instance_groups = [instance_groups]
            tower_instance_groups = [group_ for group_ in self.instance_groups]
            tower_instance_groups_names = [group.name for group in tower_instance_groups]
            invalid = set(instance_groups) - set(tower_instance_groups_names)
            if invalid:
                raise InvalidInstanceGroup(invalid)
            for instance_group in set(instance_groups):
                group = next((group for group in tower_instance_groups
                              if group.name == instance_group), None)
github schubergphilis / towerlib / towerlib / towerlib.py View on Github external
Returns:
            bool: True on success, False otherwise.

        Raises:
            InvalidOrganization: The Organization given was not found.
            InvalidCredential: The credential was not found.

        """
        organization_ = self.get_organization_by_name(organization)
        if not organization_:
            raise InvalidOrganization(organization)
        credential = next(self.credentials.filter({'organization': organization_.id,
                                                   'name__iexact': name,
                                                   'credential_type': credential_type_id}), None)
        if not credential:
            raise InvalidCredential(name)
        return credential.delete()
github schubergphilis / towerlib / towerlib / towerlib.py View on Github external
InvalidCredentialType: The CredentialType given was not found.
            InvalidOrganization: The Organization given was not found.
            InvalidCredential: The credential was not found.

        """
        credential_type_ = self.get_credential_type_by_name(credential_type)
        if not credential_type_:
            raise InvalidCredentialType(credential_type)
        organization_ = self.get_organization_by_name(organization)
        if not organization_:
            raise InvalidOrganization(organization)
        credential = next(self.credentials.filter({'organization': organization_.id,
                                                   'name__iexact': name,
                                                   'credential_type': credential_type_.id}), None)
        if not credential:
            raise InvalidCredential(name)
        return credential.delete()
github schubergphilis / towerlib / towerlib / entities / inventory.py View on Github external
overwrite ():
            overwrite_vars ():
            timeout ():
            verbosity ():
            update_on_launch ():
            update_cache_timeout ():
            source_project ():
            update_on_project_update ():

        Returns:
            bool

        """
        credential_ = self.organization.get_credential_by_name(credential, credential_type)
        if not credential_:
            raise InvalidCredential(credential)
        project = self.organization.get_project_by_name(source_project)
        if not project:
            raise InvalidProject(source_project)
        url = '{api}/inventory_sources/'.format(api=self._tower.api)
        payload = {'name': name,
                   'description': description,
                   'source': source,
                   'source_path': source_path,
                   'source_script': source_script,
                   'source_vars': source_vars,
                   'credential': credential_.id,
                   'source_regions': source_regions,
                   'instance_filters': instance_filters,
                   'group_by': group_by,
                   'overwrite': overwrite,
                   'overwrite_vars': overwrite_vars,