mirror of
https://github.com/ansible/awx.git
synced 2026-01-10 15:32:07 -03:30
Changes to get execution environments factories working (#8126)
This commit is contained in:
parent
b7209d1694
commit
9530c6ca50
@ -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
|
||||
@ -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'),
|
||||
|
||||
@ -101,18 +101,17 @@ class JobTemplate(
|
||||
|
||||
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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user