Skip to content

Commit

Permalink
Changes to get execution environments factories working (#8126)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding authored and shanemcd committed Feb 3, 2021
1 parent 5d1cffb commit 31f934b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
26 changes: 25 additions & 1 deletion awxkit/awxkit/api/pages/execution_environments.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging

from awxkit.api.mixins import HasCreate
from awxkit.api.mixins import DSAdapter, HasCreate
from awxkit.api.pages import (
Credential,
Organization,
)
from awxkit.api.resources import resources
from awxkit.utils import random_title, PseudoNamespace

from . import base
from . import page
Expand All @@ -19,6 +20,29 @@ class ExecutionEnvironment(HasCreate, base.Base):
dependencies = [Organization, Credential]
NATURAL_KEY = ('organization', 'image')

# fields are image, organization, managed_by_tower, credential
def create(self, image='quay.io/ansible/ansible-runner:devel', credential=None, **kwargs):
# we do not want to make a credential by default
payload = self.create_payload(image=image, credential=credential, **kwargs)
ret = self.update_identity(ExecutionEnvironments(self.connection).post(payload))
return ret

def create_payload(self, organization=Organization, **kwargs):
self.create_and_update_dependencies(organization)
payload = self.payload(organization=self.ds.organization, **kwargs)
payload.ds = DSAdapter(self.__class__.__name__, self._dependency_store)
return payload

def payload(self, image=None, organization=None, credential=None, **kwargs):
payload = PseudoNamespace(
image=image or random_title(10),
organization=organization.id if organization else None,
credential=credential.id if credential else None,
**kwargs
)

return payload


page.register_page([resources.execution_environment,
(resources.execution_environments, 'post'),
Expand Down
21 changes: 10 additions & 11 deletions awxkit/awxkit/api/pages/job_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,17 @@ def payload(self, job_type='run', playbook='ping.yml', **kwargs):

if kwargs.get('project'):
payload.update(project=kwargs.get('project').id, playbook=playbook)
if kwargs.get('inventory'):
payload.update(inventory=kwargs.get('inventory').id)
if kwargs.get('credential'):
payload.update(credential=kwargs.get('credential').id)
if kwargs.get('webhook_credential'):
webhook_cred = kwargs.get('webhook_credential')
if isinstance(webhook_cred, int):
payload.update(webhook_credential=int(webhook_cred))
elif hasattr(webhook_cred, 'id'):
payload.update(webhook_credential=webhook_cred.id)

for fk_field in ('inventory', 'credential', 'webhook_credential', 'execution_environment'):
rel_obj = kwargs.get(fk_field)
if rel_obj is None:
continue
elif isinstance(rel_obj, int):
payload.update(**{fk_field: int(rel_obj)})
elif hasattr(rel_obj, 'id'):
payload.update(**{fk_field: rel_obj.id})
else:
raise AttributeError("Webhook credential must either be integer of pkid or Credential object")
raise AttributeError(f'Related field {fk_field} must be either integer of pkid or object')

return payload

Expand Down

0 comments on commit 31f934b

Please sign in to comment.