Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 3x 10x 10x 3x 3x 18x 3x 3x 3x 3x 3x 10x 3x 10x 3x 10x 3x 10x 3x 10x 3x 10x 3x 10x 21x 9x 10x 24x 9x 10x 3x 3x 10x 3x 10x 3x 10x 3x 10x 3x 10x 3x 10x 10x 10x 3x 3x 3x 3x 10x 10x 3x 3x 10x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 10x 10x 3x 3x 3x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x | import React, { useState, useEffect, useMemo } from 'react';
import { JobsView, Scheduler } from '@jupyterlab/scheduler';
import { AppNetworkAccessType } from '../../types';
import { IEnvironmentVariable } from './AdvancedOptions/EnvironmentVariables';
import AdvancedOptions from './AdvancedOptions/AdvancedOptions';
import { getAdditionalOptionsContainerStyles } from './styles';
import {
getInitialEnvironmentVariables,
getInitialInitScriptValue,
getInitialLCCValue,
getInitialRoleArnValues,
getInitialS3Values,
getInitialSubnetOrSecurityGroupValues,
getNetworkAccessType,
ISubnetSecurityGroupValues,
getInitialKMSKeys,
getAdvancedOptionsFromSettingRegistry,
NO_SCRIPT,
getInitialKeyValue,
getInitialKeyValueAsBoolean,
} from './initialValueHelpers';
import * as validationHelpers from './AdvancedOptions/validationHelpers';
import { ContentsManager, ServerConnection } from '@jupyterlab/services';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { JobEnvironmentContainer } from './JobEnvironment/JobEnvironmentContainer';
import { IAutoDetectedConfig } from './JobEnvironment';
interface FormState {
sm_lcc_init_script_arn: string;
sm_init_script: string;
role_arn: string;
vpc_security_group_ids: string[];
vpc_subnets: string[];
sm_kernel: string;
sm_image: string;
s3_input: string;
s3_input_account_id: string;
s3_output: string;
s3_output_account_id: string;
sm_output_kms_key: string;
sm_volume_kms_key: string;
max_retry_attempts: number;
max_run_time_in_seconds: number;
enable_network_isolation: boolean;
}
export type CreateNotebookJobFormProps = Scheduler.IAdvancedOptionsProps & {
requestClient: ServerConnection.ISettings;
settingRegistry: ISettingRegistry;
executionEnvironments: {
environment_configs: IAutoDetectedConfig[] | null;
auto_detected_config: IAutoDetectedConfig[];
};
contentsManager: ContentsManager;
};
const CreateNotebookJobForm: React.FunctionComponent<CreateNotebookJobFormProps> = (props) => {
const {
executionEnvironments,
settingRegistry,
jobsView,
requestClient,
errors: formErrors,
handleErrorsChange: setFormErrors,
model,
handleModelChange,
} = props;
const networkAccessType = useMemo(() => {
return getNetworkAccessType(executionEnvironments?.auto_detected_config, jobsView);
}, []);
const areFieldsDisabled = jobsView === JobsView.JobDefinitionDetail || jobsView === JobsView.JobDetail;
const initialLccValues = useMemo(() => {
const ALL_LCC_OPTIONS: string[] = [];
const PRE_DEFINED_LCC_OPTIONS =
(executionEnvironments.auto_detected_config?.find(
c => c.name === 'lcc_arn'
)?.value as string[]) || [];
ALL_LCC_OPTIONS.push(NO_SCRIPT);
ALL_LCC_OPTIONS.push(...PRE_DEFINED_LCC_OPTIONS);
const selectedLccValue = getInitialLCCValue(
model.runtimeEnvironmentParameters,
jobsView
);
Iif (model.runtimeEnvironmentParameters && selectedLccValue !== NO_SCRIPT) {
ALL_LCC_OPTIONS.push(selectedLccValue);
}
return {
allLCCOptions: ALL_LCC_OPTIONS,
selectedLccValue
};
}, []);
const initialRoleArnValue = useMemo(() => {
return getInitialRoleArnValues(
model.runtimeEnvironmentParameters,
executionEnvironments.auto_detected_config,
jobsView,
);
}, []);
const initialS3OutputValue = useMemo(() => {
return getInitialS3Values(
model.runtimeEnvironmentParameters,
executionEnvironments.auto_detected_config,
jobsView,
's3_output',
);
}, []);
const initialS3InputValue = useMemo(() => {
return getInitialS3Values(
model.runtimeEnvironmentParameters,
executionEnvironments.auto_detected_config,
jobsView,
's3_input',
);
}, []);
const initialMaxRetryAttemtpsValue = useMemo(() => {
return getInitialKeyValue(
model.runtimeEnvironmentParameters,
jobsView,
1,
'max_retry_attempts',
);
}, []);
// get initial value from runtime environment
const initialNetworkIsolationValue = useMemo(() => {
return getInitialKeyValueAsBoolean(
model.runtimeEnvironmentParameters,
jobsView,
'enable_network_isolation'
)
}, []);
const initialMaxRunTimeInSecondsValue = useMemo(() => {
return getInitialKeyValue(
model.runtimeEnvironmentParameters,
jobsView,
172800,
'max_run_time_in_seconds',
);
}, []);
const securityGroupOptions = useMemo(() => {
const ALL_SECURITY_GROUPS =
(executionEnvironments.auto_detected_config?.find((c) => c.name === 'vpc_security_group_ids')
?.value as ISubnetSecurityGroupValues) || [];
return ALL_SECURITY_GROUPS?.map((item: { name: any }) => item.name);
}, []);
const subnetOptions = useMemo(() => {
const ALL_SUBNETS =
(executionEnvironments.auto_detected_config?.find((c) => c.name === 'vpc_subnets')
?.value as ISubnetSecurityGroupValues) || [];
return ALL_SUBNETS?.map((item: { name: any }) => item.name);
}, []);
const initialSecurityGroupAndSubnetValues = useMemo(() => {
if (networkAccessType === AppNetworkAccessType.PublicInternetOnly) {
return {
securityGroups: [],
subnets: [],
};
}
// if no private subnets then users can't create a job with a VPC, so switch to empty list for security groups
const securityGroups =
subnetOptions.length === 0 && jobsView === JobsView.CreateForm
? []
: getInitialSubnetOrSecurityGroupValues(
model.runtimeEnvironmentParameters,
executionEnvironments.auto_detected_config,
jobsView,
'vpc_security_group_ids',
);
const subnets = getInitialSubnetOrSecurityGroupValues(
model.runtimeEnvironmentParameters,
executionEnvironments.auto_detected_config,
jobsView,
'vpc_subnets',
);
return { securityGroups, subnets };
}, []);
const initialInitScriptValue = useMemo(() => {
return getInitialInitScriptValue(model.runtimeEnvironmentParameters, jobsView);
}, []);
const initialEnvironmentVariables = useMemo(() => {
return getInitialEnvironmentVariables(model.runtimeEnvironmentParameters);
}, []);
const initialOutputKMSKey = useMemo(() => {
return getInitialKMSKeys(model.runtimeEnvironmentParameters, jobsView, 'sm_output_kms_key');
}, []);
const initialVolumeKMSKey = useMemo(() => {
return getInitialKMSKeys(model.runtimeEnvironmentParameters, jobsView, 'sm_volume_kms_key');
}, []);
const initialCheckedState = useMemo(() => {
return false;
}, []);
// user default values object is the combination of the auto detected configs and the user default advanced options
const [userDefaultValues, setUserDefaultValues] = useState<FormState>({
sm_lcc_init_script_arn: initialLccValues.selectedLccValue || '',
role_arn: initialRoleArnValue || '',
vpc_security_group_ids: initialSecurityGroupAndSubnetValues.securityGroups || [],
vpc_subnets: initialSecurityGroupAndSubnetValues.subnets || [],
s3_input: initialS3InputValue || '',
s3_input_account_id: '',
s3_output: initialS3OutputValue || '',
s3_output_account_id: '',
sm_kernel: '',
sm_image: '',
sm_init_script: initialInitScriptValue || '',
sm_output_kms_key: initialOutputKMSKey || '',
sm_volume_kms_key: initialVolumeKMSKey || '',
max_retry_attempts: initialMaxRetryAttemtpsValue,
max_run_time_in_seconds: initialMaxRunTimeInSecondsValue,
enable_network_isolation: initialNetworkIsolationValue,
});
// initially output and volume kms keys should be empty even if we have the initial values coming from the user defaults,
// because initially the kms encryption is disabled. When the kms encryption gets enabled the user default values will get picked up.
const [formState, setFormState] = useState<FormState>({
...userDefaultValues,
sm_output_kms_key: '',
sm_volume_kms_key: '',
});
// validate initial form values to be safe
useEffect(() => {
const initialSubnetOptionsError = validationHelpers.validateSubnetOptions(subnetOptions);
const initialNoCompatibleSubnetsError = validationHelpers.validateInitialSubnets(
initialSecurityGroupAndSubnetValues.subnets,
);
const newFormErrors = {
...formErrors,
roleError: validationHelpers.validateRoleArn(initialRoleArnValue),
s3InputFolderError: validationHelpers.validateS3Url(initialS3InputValue),
s3OutputFolderError: validationHelpers.validateS3Url(initialS3OutputValue),
environmentsStillLoading: '',
kernelsStillLoading: '',
subnetError: initialCheckedState ? initialSubnetOptionsError || initialNoCompatibleSubnetsError || '' : '',
}
setFormErrors(newFormErrors);
}, []);
const [userDefaultAdvancedOptions, setUserDefaultAdvancedOptions] = useState<Partial<FormState>>();
useEffect(() => {
getAdvancedOptionsFromSettingRegistry(settingRegistry).then((advancedOptions) => {
setUserDefaultAdvancedOptions(advancedOptions);
});
}, []);
useEffect(() => {
let newFormState = {};
let newFormErrors = {};
let newUserDefaults = {};
// we want to use the value from user settings always
const enableNetworkIsolation = userDefaultAdvancedOptions?.enable_network_isolation ?? false;
newFormState = { ...newFormState, enable_network_isolation: enableNetworkIsolation };
const roleArn = userDefaultAdvancedOptions?.role_arn ?? '';
Iif (roleArn && roleArn !== initialRoleArnValue) {
newFormState = { ...newFormState, role_arn: roleArn };
newFormErrors = {
...newFormErrors,
roleError: validationHelpers.validateRoleArn(roleArn),
};
}
const s3Input = userDefaultAdvancedOptions?.s3_input ?? '';
Iif (s3Input && s3Input !== initialS3InputValue) {
newFormState = { ...newFormState, s3_input: s3Input };
newFormErrors = {
...newFormErrors,
s3InputFolderError: validationHelpers.validateS3Url(s3Input),
};
}
const s3Output = userDefaultAdvancedOptions?.s3_output ?? '';
Iif (s3Output && s3Output !== initialS3OutputValue) {
newFormState = { ...newFormState, s3_output: s3Output };
newFormErrors = {
...newFormErrors,
s3OutputFolderError: validationHelpers.validateS3Url(s3Output),
};
}
const outputKMSKey = userDefaultAdvancedOptions?.sm_output_kms_key ?? '';
Iif (outputKMSKey && outputKMSKey !== initialOutputKMSKey) {
// only update the user default values, the kms key value will get picked up
// from the user default values object whenever the user enables the kms key encryption
newUserDefaults = { ...newUserDefaults, sm_output_kms_key: outputKMSKey };
}
const volumeKMSKey = userDefaultAdvancedOptions?.sm_volume_kms_key ?? '';
Iif (volumeKMSKey && volumeKMSKey !== initialVolumeKMSKey) {
// only update the user default values, the kms key value will get picked up
// from the user default values object whenever the user enables the kms key encryption
newUserDefaults = { ...newUserDefaults, sm_volume_kms_key: volumeKMSKey };
}
newUserDefaults = { ...newFormState, ...newUserDefaults };
// we don't need the defaults if on the detail page
if (Object.keys(newFormState).length > 0 && !areFieldsDisabled) {
setFormState({ ...formState, ...newFormState });
}
if (Object.keys(newUserDefaults).length > 0) {
// update the user default values object which contains the auto detected configs with
// the values coming from the user default advanced options
setUserDefaultValues({ ...userDefaultValues, ...newUserDefaults });
}
Iif (Object.keys(newFormErrors).length > 0) {
setFormErrors({ ...formErrors, ...newFormErrors });
}
}, [userDefaultAdvancedOptions]);
const [environmentVariables, setEnvironmentVariables] = useState<IEnvironmentVariable[]>(initialEnvironmentVariables);
const [enableVPCSetting, setEnableVPCSetting] = useState(initialCheckedState);
const environmentVariablesAsMap = useMemo(() => {
const map: { [key: string]: string } = {};
environmentVariables?.map((environmentVariable) => {
const { key, value } = environmentVariable;
// if key and value are empty, do not add to the map
// TODO: modify this code to handle non string values as well
Iif (key.trim().length !== 0 && value.trim().length !== 0) {
map[key] = value;
}
});
return map;
}, [environmentVariables]);
useEffect(() => {
const commaSeparatedVpcGroupIds = formState.vpc_security_group_ids?.join(',') || '';
const commaSeparatedVpcSubnets = formState.vpc_subnets?.join(',') || '';
handleModelChange({
...(model as any),
runtimeEnvironmentParameters: {
...formState,
vpc_security_group_ids: commaSeparatedVpcGroupIds,
vpc_subnets: commaSeparatedVpcSubnets,
...environmentVariablesAsMap,
},
});
}, [formState, environmentVariablesAsMap]);
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const name = e.target.name;
const value = e.target.value;
setFormState({ ...formState, [name]: value });
};
const handleNumberValueChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const name = e.target.name;
const value = parseInt(e.target.value);
setFormState({ ...formState, [name]: isNaN(value) ? '' : value });
};
const onSelectLCCScript = (startupScript: string) => {
setFormState({
...formState,
sm_lcc_init_script_arn: startupScript
});
};
const setSecurityGroups = (securityGroups: string[]) => {
setFormState({
...formState,
vpc_security_group_ids: securityGroups,
});
};
const setRoleArn = (roleArn: string) => {
setFormState({ ...formState, role_arn: roleArn });
};
const setSubnets = (subnets: string[]) => {
console.log('setSubnets called with:', subnets);
setFormState({ ...formState, vpc_subnets: subnets });
};
return (
<div className={getAdditionalOptionsContainerStyles(areFieldsDisabled)}>
<JobEnvironmentContainer
isDisabled={areFieldsDisabled}
formState={formState}
setFormState={setFormState}
formErrors={formErrors}
setFormErrors={setFormErrors}
{...props} />
<AdvancedOptions
isDisabled={areFieldsDisabled}
formState={formState}
setFormState={setFormState}
handleChange={handleChange}
handleNumberValueChange={handleNumberValueChange}
requestClient={requestClient}
formErrors={formErrors}
setFormValidationErrors={setFormErrors}
environmentVariables={environmentVariables}
userDefaultValues={userDefaultValues}
setEnvironmentVariables={setEnvironmentVariables}
lccOptions={initialLccValues.allLCCOptions}
availableSecurityGroups={securityGroupOptions}
availableSubnets={subnetOptions}
initialSecurityGroups={initialSecurityGroupAndSubnetValues.securityGroups}
initialSubnets={initialSecurityGroupAndSubnetValues.subnets}
setSubnets={setSubnets}
setRoleArn={setRoleArn}
setSecurityGroups={setSecurityGroups}
onSelectLCCScript={onSelectLCCScript}
isVPCDomain={networkAccessType === AppNetworkAccessType.VpcOnly}
enableVPCSetting={enableVPCSetting}
setEnableVPCSetting={setEnableVPCSetting}
/>
</div>
);
};
export { CreateNotebookJobForm, FormState, IAutoDetectedConfig };
|