Skip to content

Commit

Permalink
Fill in the new execution environment collection module
Browse files Browse the repository at this point in the history
as well as changes to other ones that need to be able to attach EEs.
  • Loading branch information
jbradberry authored and shanemcd committed Nov 23, 2020
1 parent 703889a commit 1befb35
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 28 deletions.
2 changes: 1 addition & 1 deletion awx/conf/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
BooleanField, CharField, ChoiceField, DictField, DateTimeField, EmailField,
IntegerField, ListField, NullBooleanField
)
from rest_framework.serializers import PrimaryKeyRelatedField
from rest_framework.serializers import PrimaryKeyRelatedField # noqa

logger = logging.getLogger('awx.conf.fields')

Expand Down
2 changes: 1 addition & 1 deletion awx/main/models/unified_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def resolve_execution_environment(self):
if self.inventory.organization.default_environment is not None:
return self.inventory.organization.default_environment
if settings.DEFAULT_EXECUTION_ENVIRONMENT is not None:
return settings.DEFAULT_EXECUTION_ENVIRONMENT
return settings.DEFAULT_EXECUTION_ENVIRONMENT
return ExecutionEnvironment.objects.filter(organization=None, managed_by_tower=True).first()

def create_unified_job(self, **kwargs):
Expand Down
73 changes: 57 additions & 16 deletions awx_collection/plugins/modules/tower_execution_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,38 @@
- Create, update, or destroy Execution Environments in Ansible Tower. See
U(https://www.ansible.com/tower) for an overview.
options:
name:
description:
- Name to use for the execution environment.
required: True
type: str
image:
description:
- The fully qualified name of the container image
- The fully qualified url of the container image.
required: True
type: str
state:
description:
description:
- Desired state of the resource.
choices: ["present", "absent"]
default: "present"
- Description to use for the execution environment.
type: str
credential:
pull:
description:
- Allow pulling from the image registry when launching a job with this execution environment.
type: bool
default: 'yes'
organization:
description:
- Name of the credential to use for the job template.
- Deprecated, use 'credentials'.
- The organization the execution environment belongs to.
type: str
description:
credential:
description:
- Description to use for the job template.
- Name of the credential to use for the execution environment.
type: str
organization:
state:
description:
- TODO
- Desired state of the resource.
choices: ["present", "absent"]
default: "present"
type: str
extends_documentation_fragment: awx.awx.auth
'''
Expand All @@ -53,6 +62,7 @@
EXAMPLES = '''
- name: Add EE to Tower
tower_execution_environment:
name: "My EE"
image: quay.io/awx/ee
'''

Expand All @@ -64,22 +74,53 @@
def main():
# Any additional arguments that are not fields of the item can be added here
argument_spec = dict(
name=dict(required=True),
image=dict(required=True),
description=dict(default=''),
pull=dict(type='bool', default=True),
organization=dict(),
credential=dict(default=''),
state=dict(choices=['present', 'absent'], default='present'),
)

# Create a module for ourselves
module = TowerAPIModule(argument_spec=argument_spec)

# Extract our parameters
name = module.params.get('name')
image = module.params.get('image')
description = module.params.get('description')
pull = moduel.params.get('pull')
state = module.params.get('state')

existing_item = module.get_one('execution_environments', name_or_id=image)
existing_item = module.get_one('execution_environments', name_or_id=name)

if state == 'absent':
module.delete_if_needed(image)

module.create_or_update_if_needed(existing_item, image, endpoint='execution_environments', item_type='execution_environment')
module.delete_if_needed(existing_item)

new_fields = {
'name': name,
'image': image,
}
if description:
new_fields['description'] = description
if pull is not None:
new_fields['pull'] = pull

# Attempt to look up the related items the user specified (these will fail the module if not found)
organization = module.params.get('organization')
if organization:
new_fields['organization'] = module.resolve_name_to_id('organizations', organization)

credential = module.params.get('credential')
if credential:
new_fields['credential'] = module.resolve_name_to_id('credentials', credential)

module.create_or_update_if_needed(
existing_item, new_fields,
endpoint='execution_environments',
item_type='execution_environment'
)


if __name__ == '__main__':
Expand Down
4 changes: 4 additions & 0 deletions awx_collection/plugins/modules/tower_inventory_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def main():
enabled_value=dict(),
host_filter=dict(),
credential=dict(),
execution_environment=dict(),
organization=dict(),
overwrite=dict(type='bool'),
overwrite_vars=dict(type='bool'),
Expand All @@ -203,6 +204,7 @@ def main():
organization = module.params.get('organization')
source_script = module.params.get('source_script')
credential = module.params.get('credential')
ee = module.params.get('execution_environment')
source_project = module.params.get('source_project')
state = module.params.get('state')

Expand Down Expand Up @@ -254,6 +256,8 @@ def main():
# Attempt to look up the related items the user specified (these will fail the module if not found)
if credential is not None:
inventory_source_fields['credential'] = module.resolve_name_to_id('credentials', credential)
if ee is not None:
inventory_source_fields['execution_environment'] = module.resolve_name_to_id('execution_environments', ee)
if source_project is not None:
inventory_source_fields['source_project'] = module.resolve_name_to_id('projects', source_project)
if source_script is not None:
Expand Down
15 changes: 10 additions & 5 deletions awx_collection/plugins/modules/tower_job_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@
description:
- Path to the playbook to use for the job template within the project provided.
type: str
execution_environment:
description:
- Execution Environment to use for the JT.
type: str
credential:
description:
- Name of the credential to use for the job template.
Expand All @@ -79,6 +75,10 @@
- Name of the vault credential to use for the job template.
- Deprecated, use 'credentials'.
type: str
execution_environment:
description:
- Execution Environment to use for the JT.
type: str
forks:
description:
- The number of parallel or simultaneous processes to use while executing the playbook.
Expand Down Expand Up @@ -354,6 +354,7 @@ def main():
vault_credential=dict(default=''),
custom_virtualenv=dict(),
credentials=dict(type='list', elements='str'),
execution_environment=dict(),
forks=dict(type='int'),
limit=dict(default=''),
verbosity=dict(type='int', choices=[0, 1, 2, 3, 4], default=0),
Expand Down Expand Up @@ -420,7 +421,11 @@ def main():
organization = module.params.get('organization')
if organization:
organization_id = module.resolve_name_to_id('organizations', organization)
search_fields['organization'] = new_fields['organization'] = organization_id
search_fields['organization'] = new_fields['organization'] = organization_id

ee = module.params.get('execution_environment')
if ee:
new_fields['execution_environment'] = module.resolve_name_to_id('execution_environments', ee)

# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('job_templates', name_or_id=name, **{'data': search_fields})
Expand Down
6 changes: 5 additions & 1 deletion awx_collection/plugins/modules/tower_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
default: ''
default_environment:
description:
- Default Execution Environment to use for the Organization.
- Default Execution Environment to use for jobs owned by the Organization.
type: str
max_hosts:
description:
Expand Down Expand Up @@ -114,6 +114,7 @@ def main():
name=dict(required=True),
description=dict(),
custom_virtualenv=dict(),
default_environment=dict(),
max_hosts=dict(type='int', default="0"),
notification_templates_started=dict(type="list", elements='str'),
notification_templates_success=dict(type="list", elements='str'),
Expand All @@ -130,6 +131,7 @@ def main():
name = module.params.get('name')
description = module.params.get('description')
custom_virtualenv = module.params.get('custom_virtualenv')
default_ee = module.params.get('default_environment')
max_hosts = module.params.get('max_hosts')
# instance_group_names = module.params.get('instance_groups')
state = module.params.get('state')
Expand Down Expand Up @@ -179,6 +181,8 @@ def main():
org_fields['description'] = description
if custom_virtualenv is not None:
org_fields['custom_virtualenv'] = custom_virtualenv
if default_ee is not None:
org_fields['default_environment'] = module.resolve_name_to_id('execution_environments', default_ee)
if max_hosts is not None:
org_fields['max_hosts'] = max_hosts

Expand Down
20 changes: 16 additions & 4 deletions awx_collection/plugins/modules/tower_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
description:
- Description to use for the project.
type: str
execution_environment:
description:
- Execution Environment to use for the project.
type: str
scm_type:
description:
- Type of SCM resource.
Expand Down Expand Up @@ -104,6 +100,14 @@
- Local absolute file path containing a custom Python virtualenv to use
type: str
default: ''
default_environment:
description:
- Default Execution Environment to use for jobs relating to the project.
type: str
execution_environment:
description:
- Execution Environment to use for project updates.
type: str
organization:
description:
- Name of organization for project.
Expand Down Expand Up @@ -203,6 +207,8 @@ def main():
allow_override=dict(type='bool', aliases=['scm_allow_override']),
timeout=dict(type='int', default=0, aliases=['job_timeout']),
custom_virtualenv=dict(),
default_environment=dict(),
execution_environment=dict(),
organization=dict(),
notification_templates_started=dict(type="list", elements='str'),
notification_templates_success=dict(type="list", elements='str'),
Expand Down Expand Up @@ -232,6 +238,8 @@ def main():
allow_override = module.params.get('allow_override')
timeout = module.params.get('timeout')
custom_virtualenv = module.params.get('custom_virtualenv')
default_ee = module.params.get('default_environment')
ee = module.params.get('execution_environment')
organization = module.params.get('organization')
state = module.params.get('state')
wait = module.params.get('wait')
Expand Down Expand Up @@ -293,6 +301,10 @@ def main():
project_fields['description'] = description
if credential is not None:
project_fields['credential'] = credential
if default_ee is not None:
project_fields['default_environment'] = module.resolve_name_to_id('execution_environments', default_ee)
if ee is not None:
project_fields['execution_environment'] = module.resolve_name_to_id('execution_environments', ee)
if allow_override is not None:
project_fields['allow_override'] = allow_override
if scm_type == '':
Expand Down
5 changes: 5 additions & 0 deletions awx_collection/plugins/modules/tower_workflow_job_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def main():
description=dict(),
extra_vars=dict(type='dict'),
organization=dict(),
execution_environment=dict(),
survey=dict(type='dict'), # special handling
survey_enabled=dict(type='bool'),
allow_simultaneous=dict(type='bool'),
Expand Down Expand Up @@ -202,6 +203,10 @@ def main():
organization_id = module.resolve_name_to_id('organizations', organization)
search_fields['organization'] = new_fields['organization'] = organization_id

ee = module.params.get('execution_environment')
if ee:
new_fields['execution_environment'] = module.resolve_name_to_id('execution_environments', ee)

# Attempt to look up an existing item based on the provided data
existing_item = module.get_one('workflow_job_templates', name_or_id=name, **{'data': search_fields})

Expand Down

0 comments on commit 1befb35

Please sign in to comment.