Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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):
self.tower.create_credential_in_organization_with_type_id('workflow',
'CredName2',
'CredDescription',
'workflow_admin',
'workflow_team',
'Source Control',
'garbage')
credential_with_type_id = self.tower.create_credential_in_organization_with_type_id('workflow',
'CredName2',
'CredDescription',
'workflow_admin',
'workflow_team',
'2',
'{}')
self.assertIsInstance(credential_with_type_id, GenericCredential)
self.assertIsNone(self.tower.create_credential_in_organization_with_type_id('workflow',
inventory = self.tower.create_organization_inventory('Default',
'Inventory_name',
'description',
'{}')
self.assertIsInstance(inventory, Inventory)
duplicate_inventory = self.tower.create_organization_inventory('Default',
'Inventory_name',
'description2',
'{}')
self.assertFalse(duplicate_inventory)
with self.assertRaises(InvalidOrganization):
self.tower.create_organization_inventory('DefaultBroken',
'Inventory_name',
'description2',
'{}')
with self.assertRaises(InvalidVariables):
self.tower.create_organization_inventory('Default',
'Inventory_name',
'description2',
'broken')
self.assertTrue(self.tower.delete_organization_inventory('Default', 'Inventory_name'))
with self.assertRaises(InvalidOrganization):
self.tower.delete_organization_inventory('DefaultBroken', 'Inventory_name')
with self.assertRaises(InvalidInventory):
self.tower.delete_organization_inventory('Default', 'Inventory_nameBroken')
Returns:
CredentialType on success, None otherwise.
Raises:
InvalidCredentialTypeKind: The credential type kind provided as argument does not exist.
InvalidVariables: The inputs or injectors provided as argument is not valid json.
"""
if type_.lower() not in VALID_CREDENTIAL_TYPES:
raise InvalidCredentialType(type_)
payload = {'name': name,
'description': description,
'kind': type_.lower()}
if not validate_json(inputs_):
raise InvalidVariables(inputs_)
if not validate_json(injectors):
raise InvalidVariables(injectors)
payload['inputs'] = json.loads(inputs_)
payload['injectors'] = json.loads(injectors)
url = '{api}/credential_types/'.format(api=self.api)
response = self.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error creating credential type "%s", response was: "%s"', type_, response.text)
return CredentialType(self, response.json()) if response.ok else None
CredentialType on success, None otherwise.
Raises:
InvalidCredentialTypeKind: The credential type kind provided as argument does not exist.
InvalidVariables: The inputs or injectors provided as argument is not valid json.
"""
if type_.lower() not in VALID_CREDENTIAL_TYPES:
raise InvalidCredentialType(type_)
payload = {'name': name,
'description': description,
'kind': type_.lower()}
if not validate_json(inputs_):
raise InvalidVariables(inputs_)
if not validate_json(injectors):
raise InvalidVariables(injectors)
payload['inputs'] = json.loads(inputs_)
payload['injectors'] = json.loads(injectors)
url = '{api}/credential_types/'.format(api=self.api)
response = self.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error creating credential type "%s", response was: "%s"', type_, response.text)
return CredentialType(self, response.json()) if response.ok else None
"""Creates a host.
Args:
name: The name of the host to create.
description: The description of the host.
variables: A json with the variables that will be set on the created host.
Returns:
Host: The created host is successful, None otherwise.
Raises:
InvalidVariables: The variables provided as argument is not valid json.
"""
if not validate_json(variables):
raise InvalidVariables(variables)
url = '{api}/hosts/'.format(api=self._tower.api)
payload = {'name': name,
'description': description,
'inventory': self.id,
'enabled': True,
'instance_id': '',
'variables': variables}
response = self._tower.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error creating host "%s", response was "%s"', name, response.text)
return Host(self._tower, response.json()) if response.ok else None
"""Creates a group.
Args:
name: The name of the group to create.
description: The description of the group.
variables: A json with the variables that will be set on the created group.
Returns:
Group: The created group is successful, None otherwise.
Raises:
InvalidVariables: The variables provided as argument is not valid json.
"""
if not validate_json(variables):
raise InvalidVariables(variables)
url = '{api}/groups/'.format(api=self._tower.api)
payload = {'name': name,
'description': description,
'inventory': self.id,
'variables': variables}
response = self._tower.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error creating group "%s", response was "%s"', name, response.text)
return Group(self._tower, response.json()) if response.ok else None
"""
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_
)
"""Creates an inventory.
Args:
name: The name of the inventory to create.
description: The description of the inventory.
variables: A json with the initial variables set on the inventory.
Returns:
Inventory: The created Inventory object on success, None otherwise.
Raises:
InvalidVariables: The variables provided as argument is not valid json.
"""
if not validate_json(variables):
raise InvalidVariables(variables)
payload = {'name': name,
'description': description,
'organization': self.id,
'variables': variables}
url = '{api}/inventories/'.format(api=self._tower.api)
response = self._tower.session.post(url, json=payload)
if not response.ok:
self._logger.error('Error creating inventory "%s", response was "%s"', name, response.text)
return Inventory(self._tower, response.json()) if response.ok else None
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)
return Credential(self, response.json()) if response.ok else None
__docformat__ = '''google'''
__date__ = '''2018-01-02'''
__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