mirror of
https://github.com/ansible/awx.git
synced 2026-05-19 14:57:39 -02:30
fixup objects creation helpers
This commit is contained in:
@@ -27,6 +27,17 @@ from .fixtures import (
|
|||||||
from .exc import NotUnique
|
from .exc import NotUnique
|
||||||
|
|
||||||
|
|
||||||
|
def generate_objects(artifacts, kwargs):
|
||||||
|
'''generate_objects takes a list of artifacts that are supported by
|
||||||
|
a create function and compares it to the kwargs passed in to the create
|
||||||
|
function. If a kwarg is found that is not in the artifacts list a RuntimeError
|
||||||
|
is raised.
|
||||||
|
'''
|
||||||
|
for k in kwargs.keys():
|
||||||
|
if k not in artifacts:
|
||||||
|
raise RuntimeError('{} is not a valid argument'.format(k))
|
||||||
|
return namedtuple("Objects", ",".join(artifacts))
|
||||||
|
|
||||||
def generate_role_objects(objects):
|
def generate_role_objects(objects):
|
||||||
'''generate_role_objects assembles a dictionary of all possible objects by name.
|
'''generate_role_objects assembles a dictionary of all possible objects by name.
|
||||||
It will raise an exception if any of the objects share a name due to the fact that
|
It will raise an exception if any of the objects share a name due to the fact that
|
||||||
@@ -167,15 +178,19 @@ class _Mapped(object):
|
|||||||
# or encapsulated by specific factory fixtures in a conftest
|
# or encapsulated by specific factory fixtures in a conftest
|
||||||
#
|
#
|
||||||
|
|
||||||
def create_job_template(name, **kwargs):
|
def create_job_template(name, roles=None, persisted=True, **kwargs):
|
||||||
Objects = namedtuple("Objects", "job_template, inventory, project, credential, job_type")
|
Objects = generate_objects(["job_template",
|
||||||
|
"organization",
|
||||||
|
"inventory",
|
||||||
|
"project",
|
||||||
|
"credential",
|
||||||
|
"job_type",], kwargs)
|
||||||
|
|
||||||
org = None
|
org = None
|
||||||
proj = None
|
proj = None
|
||||||
inv = None
|
inv = None
|
||||||
cred = None
|
cred = None
|
||||||
job_type = kwargs.get('job_type', 'run')
|
job_type = kwargs.get('job_type', 'run')
|
||||||
persisted = kwargs.get('persisted', True)
|
|
||||||
|
|
||||||
if 'organization' in kwargs:
|
if 'organization' in kwargs:
|
||||||
org = kwargs['organization']
|
org = kwargs['organization']
|
||||||
@@ -202,37 +217,28 @@ def create_job_template(name, **kwargs):
|
|||||||
job_type=job_type, persisted=persisted)
|
job_type=job_type, persisted=persisted)
|
||||||
|
|
||||||
role_objects = generate_role_objects([org, proj, inv, cred])
|
role_objects = generate_role_objects([org, proj, inv, cred])
|
||||||
apply_roles(kwargs.get('roles'), role_objects, persisted)
|
apply_roles(roles, role_objects, persisted)
|
||||||
|
|
||||||
return Objects(job_template=jt,
|
return Objects(job_template=jt,
|
||||||
project=proj,
|
project=proj,
|
||||||
inventory=inv,
|
inventory=inv,
|
||||||
credential=cred,
|
credential=cred,
|
||||||
job_type=job_type)
|
job_type=job_type,
|
||||||
|
organization=org,)
|
||||||
|
|
||||||
def create_organization(name, **kwargs):
|
def create_organization(name, roles=None, persisted=True, **kwargs):
|
||||||
artifacts = [
|
Objects = generate_objects(["organization",
|
||||||
"organization",
|
"teams", "users",
|
||||||
"teams",
|
"superusers",
|
||||||
"users",
|
"projects",
|
||||||
"superusers",
|
"labels",
|
||||||
"projects",
|
"notification_templates",
|
||||||
"labels",
|
"inventories",], kwargs)
|
||||||
"notification_templates",
|
|
||||||
"inventories",
|
|
||||||
]
|
|
||||||
|
|
||||||
for k in kwargs.keys():
|
|
||||||
if k not in artifacts:
|
|
||||||
raise RuntimeError('{} is not a valid argument'.format(k))
|
|
||||||
|
|
||||||
Objects = namedtuple("Objects", ",".join(artifacts))
|
|
||||||
|
|
||||||
projects = {}
|
projects = {}
|
||||||
inventories = {}
|
inventories = {}
|
||||||
labels = {}
|
labels = {}
|
||||||
notification_templates = {}
|
notification_templates = {}
|
||||||
persisted = kwargs.get('persisted', True)
|
|
||||||
|
|
||||||
org = mk_organization(name, '%s-desc'.format(name), persisted=persisted)
|
org = mk_organization(name, '%s-desc'.format(name), persisted=persisted)
|
||||||
|
|
||||||
@@ -269,7 +275,7 @@ def create_organization(name, **kwargs):
|
|||||||
notification_templates[nt] = mk_notification_template(nt, organization=org, persisted=persisted)
|
notification_templates[nt] = mk_notification_template(nt, organization=org, persisted=persisted)
|
||||||
|
|
||||||
role_objects = generate_role_objects([org, superusers, users, teams, projects, labels, notification_templates])
|
role_objects = generate_role_objects([org, superusers, users, teams, projects, labels, notification_templates])
|
||||||
apply_roles(kwargs.get('roles'), role_objects, persisted)
|
apply_roles(roles, role_objects, persisted)
|
||||||
return Objects(organization=org,
|
return Objects(organization=org,
|
||||||
superusers=_Mapped(superusers),
|
superusers=_Mapped(superusers),
|
||||||
users=_Mapped(users),
|
users=_Mapped(users),
|
||||||
@@ -279,11 +285,14 @@ def create_organization(name, **kwargs):
|
|||||||
notification_templates=_Mapped(notification_templates),
|
notification_templates=_Mapped(notification_templates),
|
||||||
inventories=_Mapped(inventories))
|
inventories=_Mapped(inventories))
|
||||||
|
|
||||||
def create_notification_template(name, **kwargs):
|
def create_notification_template(name, roles=None, persisted=True, **kwargs):
|
||||||
Objects = namedtuple("Objects", "notification_template,organization,users,superusers,teams")
|
Objects = generate_objects(["notification_template",
|
||||||
|
"organization",
|
||||||
|
"users",
|
||||||
|
"superusers",
|
||||||
|
"teams",], kwargs)
|
||||||
|
|
||||||
organization = None
|
organization = None
|
||||||
persisted = kwargs.get('persisted', True)
|
|
||||||
|
|
||||||
if 'organization' in kwargs:
|
if 'organization' in kwargs:
|
||||||
org = kwargs['organization']
|
org = kwargs['organization']
|
||||||
@@ -296,7 +305,7 @@ def create_notification_template(name, **kwargs):
|
|||||||
users = generate_users(organization, teams, False, persisted, users=kwargs.get('users'))
|
users = generate_users(organization, teams, False, persisted, users=kwargs.get('users'))
|
||||||
|
|
||||||
role_objects = generate_role_objects([organization, notification_template])
|
role_objects = generate_role_objects([organization, notification_template])
|
||||||
apply_roles(kwargs.get('roles'), role_objects, persisted)
|
apply_roles(roles, role_objects, persisted)
|
||||||
return Objects(notification_template=notification_template,
|
return Objects(notification_template=notification_template,
|
||||||
organization=organization,
|
organization=organization,
|
||||||
users=_Mapped(users),
|
users=_Mapped(users),
|
||||||
|
|||||||
Reference in New Issue
Block a user