From 10806e85faed3db2c99b7d39f672a521c17af7b5 Mon Sep 17 00:00:00 2001 From: Kia Lam Date: Thu, 20 Jul 2023 14:33:18 -0700 Subject: [PATCH] POST to job_templates/{id}/labels endpoint rather than /labels endpoint. --- .../components/LaunchPrompt/LaunchPrompt.js | 10 ++---- .../Schedule/ScheduleEdit/ScheduleEdit.js | 2 +- awx/ui/src/util/labels.js | 34 +++++++++++++++++-- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js b/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js index dba9741bf3e3..731df681c31e 100644 --- a/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js +++ b/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js @@ -5,7 +5,7 @@ import { Formik, useFormikContext } from 'formik'; import { useDismissableError } from 'hooks/useRequest'; import mergeExtraVars from 'util/prompt/mergeExtraVars'; import getSurveyValues from 'util/prompt/getSurveyValues'; -import createNewLabels from 'util/labels'; +import { createNewLabelsOnLaunch } from 'util/labels'; import ContentLoading from '../ContentLoading'; import ContentError from '../ContentError'; import useLaunchSteps from './useLaunchSteps'; @@ -67,7 +67,6 @@ function PromptModalForm({ setValue('forks', values.forks); setValue('job_slice_count', values.job_slice_count); setValue('execution_environment', values.execution_environment?.id); - if (launchConfig.ask_instance_groups_on_launch) { const instanceGroupIds = []; values.instance_groups.forEach((instance_group) => { @@ -77,14 +76,9 @@ function PromptModalForm({ } if (launchConfig.ask_labels_on_launch) { - const { labelIds } = createNewLabels( - values.labels, - resource.organization - ); - + const { labelIds } = createNewLabelsOnLaunch(values.labels, resource); setValue('labels', labelIds); } - onSubmit(postValues); }; const { error, dismissError } = useDismissableError(contentError); diff --git a/awx/ui/src/components/Schedule/ScheduleEdit/ScheduleEdit.js b/awx/ui/src/components/Schedule/ScheduleEdit/ScheduleEdit.js index 0ce554a297e1..5888a596865d 100644 --- a/awx/ui/src/components/Schedule/ScheduleEdit/ScheduleEdit.js +++ b/awx/ui/src/components/Schedule/ScheduleEdit/ScheduleEdit.js @@ -8,7 +8,7 @@ import { getAddedAndRemoved } from 'util/lists'; import { parseVariableField } from 'util/yaml'; import mergeExtraVars from 'util/prompt/mergeExtraVars'; import getSurveyValues from 'util/prompt/getSurveyValues'; -import createNewLabels from 'util/labels'; +import { createNewLabels } from 'util/labels'; import ScheduleForm from '../shared/ScheduleForm'; import buildRuleSet from '../shared/buildRuleSet'; import { CardBody } from '../../Card'; diff --git a/awx/ui/src/util/labels.js b/awx/ui/src/util/labels.js index 8e973d583634..9da840a2bdad 100644 --- a/awx/ui/src/util/labels.js +++ b/awx/ui/src/util/labels.js @@ -1,6 +1,6 @@ -import { LabelsAPI, OrganizationsAPI } from '../api'; +import { LabelsAPI, OrganizationsAPI, JobTemplatesAPI } from '../api'; -async function createNewLabels(labels = [], organization = null) { +export async function createNewLabels(labels = [], organization = null) { let error = null; const labelIds = []; @@ -54,4 +54,32 @@ async function createNewLabels(labels = [], organization = null) { }; } -export default createNewLabels; +export async function createNewLabelsOnLaunch(labels = [], resource = null) { + let error = null; + const labelIds = []; + + try { + const labelRequests = []; + const jobTemplateId = resource.id; + const orgId = resource.organization; + + labels.forEach((label) => { + labelRequests.push( + JobTemplatesAPI.associateLabel(jobTemplateId, label, orgId).then( + ({ data }) => { + labelIds.push(data.id); + } + ) + ); + }); + + await Promise.all(labelRequests); + } catch (err) { + error = err; + } + + return { + labelIds, + error, + }; +}