Skip to content

Commit

Permalink
add credentials to execution-prompts (fix #47)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Jul 13, 2024
1 parent b9a2ab7 commit eb4bc91
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ansibleguy-webui/aw/static/js/jobs/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ELEM_ID_TMPL_PROMPT = 'aw-job-form-prompt-tmpl';
const ELEM_ID_PROMPTS = 'aw-job-form-prompts';
const ELEM_ID_PROMPT_PREFIX = 'aw-job-form-prompt-'
const API_FIELD_PROMPTS = 'execution_prompts';
const PROMPT_SIMPLE_TYPES = ['tags', 'skip_tags', 'mode_check', 'mode_diff', 'limit', 'env_vars', 'cmd_args', 'verbosity'];
const PROMPT_SIMPLE_TYPES = ['tags', 'skip_tags', 'mode_check', 'mode_diff', 'limit', 'env_vars', 'cmd_args', 'verbosity', 'credentials'];
const PROMPT_SEPARATOR = ';';
const PROMPT_ARG_SEPARATOR = '#';
var PROMPT_ID = 0;
Expand Down
12 changes: 11 additions & 1 deletion src/ansibleguy-webui/aw/static/js/jobs/manage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
ELEM_ID_TMPL_ROW2 = 'aw-api-data-tmpl-row2';
ELEM_ID_TMPL_FIELD_TEXT = 'aw-api-data-tmpl-exec-text';
ELEM_ID_TMPL_FIELD_CHOICES = 'aw-api-data-tmpl-exec-choices';
ELEM_ID_TMPL_FIELD_CREDS = 'aw-api-data-tmpl-exec-creds';
ELEM_ID_TMPL_FIELD_BOOL = 'aw-api-data-tmpl-exec-bool';
ELEM_ID_TMPL_FIELD_VERB = 'aw-api-data-tmpl-exec-verbosity';
EXEC_BOOL_FIELDS = ['mode_check', 'mode_diff'];
const PROMPT_SIMPLE_TYPES = ['tags', 'skip_tags', 'mode_check', 'mode_diff', 'limit', 'env_vars', 'cmd_args', 'verbosity'];
const PROMPT_SIMPLE_TYPES = ['tags', 'skip_tags', 'mode_check', 'mode_diff', 'limit', 'env_vars', 'cmd_args', 'verbosity', 'credentials'];
const PROMPT_SEPARATOR = ';';
const PROMPT_ARG_SEPARATOR = '#';
const PROMPT_CHOICE_SEPARATOR = ',';
Expand All @@ -17,6 +18,7 @@ PROMPT_SIMPLE_NAMES['limit'] = 'Limit';
PROMPT_SIMPLE_NAMES['env_vars'] = 'Environmental Variables';
PROMPT_SIMPLE_NAMES['cmd_args'] = 'CLI Arguments';
PROMPT_SIMPLE_NAMES['verbosity'] = 'Verbosity';
PROMPT_SIMPLE_NAMES['credentials'] = 'Credentials';


function buildExecutionFields(promptsSerialized) {
Expand All @@ -33,6 +35,8 @@ function buildExecutionFields(promptsSerialized) {
tmplElem = ELEM_ID_TMPL_FIELD_BOOL;
} else if (field == 'verbosity') {
tmplElem = ELEM_ID_TMPL_FIELD_VERB;
} else if (field == 'credentials') {
tmplElem = ELEM_ID_TMPL_FIELD_CREDS;
}
let fieldHtml = document.getElementById(tmplElem).innerHTML;
fieldHtml = fieldHtml.replaceAll('${PRETTY}', name);
Expand Down Expand Up @@ -160,6 +164,12 @@ function customExecution(formElements) {
} else if (elem.name.startsWith('var')) {
let varName = elem.name.split('=')[1];
cmdArgs += ' -e "' + varName + '=' + elem.value + '"';
} else if (elem.name == 'credentials') {
if (elem.value.startsWith('global_')) {
data['credential_global'] = elem.value.split('_')[1];
} else {
data['credential_user'] = elem.value.split('_')[1];
}
} else {
data[elem.name] = elem.value;
}
Expand Down
9 changes: 9 additions & 0 deletions src/ansibleguy-webui/aw/templates/forms/job.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
</select>
</div>
</div>
<div class="mb-3 row">
<label for="id_prompt_credentials" class="form-label col-sm-2 col-form-label form-label-sub">Prompt Credentials</label>
<div class="col-sm-10">
<select class="form-control" id="id_prompt_credentials" name="prompt_credentials">
<option value="True" {% if existing|check_job_prompt_flag:"credentials" %}selected{% endif %}>Yes</option>
<option value="False" {% if not existing|check_job_prompt_flag:"credentials" %}selected{% endif %}>No</option>
</select>
</div>
</div>
<br>
<div id="aw-job-form-prompts"></div>
</div>
Expand Down
16 changes: 16 additions & 0 deletions src/ansibleguy-webui/aw/templates/jobs/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@
</div>
</div>
</div>
<div id="aw-api-data-tmpl-exec-creds" hidden="hidden">
<div class="mb-3 row">
<label for="id_credentials" class="form-label col-sm-2 col-form-label">Credentials</label>
<div class="col-sm-10">
<select class="form-control" id="id_credentials" name="credentials">
<option value>None</option>
{% for creds in credentials_user %}
<option value="user_{{ creds.id }}">User - {{ creds.name }}</option>
{% endfor %}
{% for creds in credentials_global %}
<option value="global_{{ creds.id }}">Global - {{ creds.name }}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div id="aw-api-data-tmpl-actions" hidden="hidden">
<button class="btn btn-primary aw-btn-action aw-api-click" title="Quick Execution" aw-api-endpoint="job" aw-api-item="${ID}" aw-api-method="post">
{% include "../button/icon/run.html" %}
Expand Down
8 changes: 8 additions & 0 deletions src/ansibleguy-webui/aw/views/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

from aw.utils.http import ui_endpoint_wrapper, ui_endpoint_wrapper_kwargs
from aw.model.job import JobExecution, JobExecutionResultHost
from aw.model.job_credential import JobGlobalCredentials, JobUserCredentials
from aw.api_endpoints.job_util import get_viewable_jobs
from aw.utils.util import get_next_cron_execution_str
from aw.utils.permission import has_credentials_permission, CHOICE_PERMISSION_READ
from aw.views.forms.job import job_edit, job_clone, job_credentials_edit, job_repository_static_edit, \
job_repository_git_edit

Expand All @@ -21,6 +23,11 @@ def manage(request) -> HttpResponse:
executions = {}
next_executions = {}
execution_results_hosts = {}
credentials_user = [creds for creds in JobUserCredentials.objects.filter(user=request.user)]
credentials_global = [
creds for creds in JobGlobalCredentials.objects.all()
if has_credentials_permission(user=request.user, credentials=creds, permission_needed=CHOICE_PERMISSION_READ)
]

for job in jobs_viewable:
executions[job.id] = JobExecution.objects.filter(job=job).order_by('-updated')[:LIMIT_JOB_RESULTS]
Expand All @@ -46,6 +53,7 @@ def manage(request) -> HttpResponse:
context={
'jobs': jobs_viewable, 'executions': executions, 'next_executions': next_executions,
'show_update_time': True, 'execution_results_hosts': execution_results_hosts,
'credentials_user': credentials_user, 'credentials_global': credentials_global,
}
)

Expand Down

0 comments on commit eb4bc91

Please sign in to comment.