Skip to content

Commit

Permalink
Implementation of WFJT limit & prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Jul 23, 2019
1 parent 0fff746 commit f2aa581
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
36 changes: 32 additions & 4 deletions awx/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3275,11 +3275,13 @@ class WorkflowJobTemplateSerializer(JobTemplateMixin, LabelsListMixin, UnifiedJo
'admin', 'execute',
{'copy': 'organization.workflow_admin'}
]
limit = serializers.CharField(allow_blank=True, allow_null=True, required=False, default=None)

class Meta:
model = WorkflowJobTemplate
fields = ('*', 'extra_vars', 'organization', 'survey_enabled', 'allow_simultaneous',
'ask_variables_on_launch', 'inventory', 'ask_inventory_on_launch',)
'ask_variables_on_launch', 'inventory', 'limit', 'ask_inventory_on_launch',
'ask_limit_on_launch',)

def get_related(self, obj):
res = super(WorkflowJobTemplateSerializer, self).get_related(obj)
Expand All @@ -3305,6 +3307,27 @@ def get_related(self, obj):
def validate_extra_vars(self, value):
return vars_validate_or_raise(value)

def validate(self, attrs):
attrs = super(WorkflowJobTemplateSerializer, self).validate(attrs)

# process char_prompts
mock_obj = self.Meta.model()
field_names = set(field.name for field in mock_obj._meta.fields)
if self.instance:
for field in self.instance._meta.fields:
setattr(mock_obj, field.name, getattr(self.instance, field.name))

for field_name, value in list(attrs.items()):
setattr(mock_obj, field_name, value)
if field_name not in field_names:
attrs.pop(field_name)

# Model `.save` needs the container dict, not the psuedo fields
if mock_obj.char_prompts:
attrs['char_prompts'] = mock_obj.char_prompts

return attrs


class WorkflowJobTemplateWithSpecSerializer(WorkflowJobTemplateSerializer):
'''
Expand All @@ -3317,13 +3340,14 @@ class Meta:


class WorkflowJobSerializer(LabelsListMixin, UnifiedJobSerializer):
limit = serializers.CharField(allow_blank=True, allow_null=True, required=False, default=None)

class Meta:
model = WorkflowJob
fields = ('*', 'workflow_job_template', 'extra_vars', 'allow_simultaneous',
'job_template', 'is_sliced_job',
'-execution_node', '-event_processing_finished', '-controller_node',
'inventory',)
'inventory', 'limit',)

def get_related(self, obj):
res = super(WorkflowJobSerializer, self).get_related(obj)
Expand Down Expand Up @@ -4105,12 +4129,14 @@ class WorkflowJobLaunchSerializer(BaseSerializer):
queryset=Inventory.objects.all(),
required=False, write_only=True
)
limit = serializers.CharField(required=False, write_only=True, allow_blank=True)
workflow_job_template_data = serializers.SerializerMethodField()

class Meta:
model = WorkflowJobTemplate
fields = ('ask_inventory_on_launch', 'can_start_without_user_input', 'defaults', 'extra_vars',
'inventory', 'survey_enabled', 'variables_needed_to_start',
fields = ('ask_inventory_on_launch', 'ask_limit_on_launch',
'can_start_without_user_input', 'defaults', 'extra_vars',
'inventory', 'limit', 'survey_enabled', 'variables_needed_to_start',
'node_templates_missing', 'node_prompts_rejected',
'workflow_job_template_data', 'survey_enabled', 'ask_variables_on_launch')
read_only_fields = ('ask_inventory_on_launch', 'ask_variables_on_launch')
Expand Down Expand Up @@ -4150,9 +4176,11 @@ def validate(self, attrs):

WFJT_extra_vars = template.extra_vars
WFJT_inventory = template.inventory
WFJT_limit = template.limit
super(WorkflowJobLaunchSerializer, self).validate(attrs)
template.extra_vars = WFJT_extra_vars
template.inventory = WFJT_inventory
template.limit = WFJT_limit
return accepted


Expand Down
13 changes: 9 additions & 4 deletions awx/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3135,10 +3135,15 @@ def update_raw_data(self, data):
extra_vars.setdefault(v, u'')
if extra_vars:
data['extra_vars'] = extra_vars
if obj.ask_inventory_on_launch:
data['inventory'] = obj.inventory_id
else:
data.pop('inventory', None)
modified_ask_mapping = models.WorkflowJobTemplate.get_ask_mapping()
modified_ask_mapping.pop('extra_vars')
for field_name, ask_field_name in obj.get_ask_mapping().items():
if not getattr(obj, ask_field_name):
data.pop(field_name, None)
elif field == 'inventory':
data[field] = getattrd(obj, "%s.%s" % (field, 'id'), None)
else:
data[field] = getattr(obj, field)
return data

def post(self, request, *args, **kwargs):
Expand Down
24 changes: 24 additions & 0 deletions awx/main/migrations/0082_v360_WFJT_prompts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.2 on 2019-07-23 17:56

import awx.main.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('main', '0081_v360_notify_on_start'),
]

operations = [
migrations.AddField(
model_name='workflowjobtemplate',
name='char_prompts',
field=awx.main.fields.JSONField(blank=True, default=dict),
),
migrations.AddField(
model_name='workflowjobtemplate',
name='ask_limit_on_launch',
field=awx.main.fields.AskForField(blank=True, default=False),
),
]
20 changes: 10 additions & 10 deletions awx/main/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ def display_extra_vars(self):
'''
Hides fields marked as passwords in survey.
'''
if self.survey_passwords:
if hasattr(self, 'survey_passwords') and self.survey_passwords:
extra_vars = parse_yaml_or_json(self.extra_vars).copy()
for key, value in self.survey_passwords.items():
if key in extra_vars:
Expand Down Expand Up @@ -916,6 +916,15 @@ def credential(self):
return None


for field_name in JobTemplate.get_ask_mapping().keys():
if field_name == 'extra_vars':
continue
try:
LaunchTimeConfigBase._meta.get_field(field_name)
except FieldDoesNotExist:
setattr(LaunchTimeConfigBase, field_name, NullablePromptPsuedoField(field_name))


class LaunchTimeConfig(LaunchTimeConfigBase):
'''
Common model for all objects that save details of a saved launch config
Expand Down Expand Up @@ -949,15 +958,6 @@ def extra_vars(self, extra_vars):
self.extra_data = extra_vars


for field_name in JobTemplate.get_ask_mapping().keys():
if field_name == 'extra_vars':
continue
try:
LaunchTimeConfig._meta.get_field(field_name)
except FieldDoesNotExist:
setattr(LaunchTimeConfig, field_name, NullablePromptPsuedoField(field_name))


class JobLaunchConfig(LaunchTimeConfig):
'''
Historical record of user launch-time overrides for a job
Expand Down
9 changes: 7 additions & 2 deletions awx/main/models/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def get_job_kwargs(self):
return data


class WorkflowJobOptions(BaseModel):
class WorkflowJobOptions(LaunchTimeConfigBase):
class Meta:
abstract = True

Expand Down Expand Up @@ -361,6 +361,7 @@ class Meta:
on_delete=models.SET_NULL,
related_name='workflows',
)
# declared here for help_text
inventory = models.ForeignKey(
'Inventory',
related_name='%(class)ss',
Expand All @@ -374,6 +375,10 @@ class Meta:
blank=True,
default=False,
)
ask_limit_on_launch = AskForField(
blank=True,
default=False,
)
admin_role = ImplicitRoleField(parent_role=[
'singleton:' + ROLE_SINGLETON_SYSTEM_ADMINISTRATOR,
'organization.workflow_admin_role'
Expand Down Expand Up @@ -500,7 +505,7 @@ def _get_related_jobs(self):
return WorkflowJob.objects.filter(workflow_job_template=self)


class WorkflowJob(UnifiedJob, WorkflowJobOptions, SurveyJobMixin, JobNotificationMixin, LaunchTimeConfigBase):
class WorkflowJob(UnifiedJob, WorkflowJobOptions, SurveyJobMixin, JobNotificationMixin):
class Meta:
app_label = 'main'
ordering = ('id',)
Expand Down

0 comments on commit f2aa581

Please sign in to comment.