11import { componentTypes , validatorTypes } from '@@ddf' ;
2+ import { parseCondition } from '@data-driven-forms/react-form-renderer' ;
23import validateName from '../../helpers/storage_manager/validate-names' ;
3- import { loadProviderCapabilities } from '../../helpers/storage_manager/load-provider-capabilities' ;
44import filterResourcesByCapabilities from '../../helpers/storage_manager/filter-resources-by-capabilities' ;
55
6- const loadProviders = ( ) =>
7- API . get (
8- '/api/providers?expand=resources&attributes=id,name,supports_block_storage'
9- + '&filter[]=supports_block_storage=true&filter[]=supports_add_storage=true' ,
10- ) . then ( ( { resources } ) =>
11- resources . map ( ( { id, name } ) => ( { value : id , label : name } ) ) ) ;
6+ const changeValue = ( value , loadSchema , emptySchema ) => {
7+ if ( value === '-1' ) {
8+ emptySchema ( ) ;
9+ } else {
10+ miqSparkleOn ( ) ;
11+ API . options ( `/api/storage_services?ems_id=${ value } ` ) . then ( loadSchema ( ) ) . then ( miqSparkleOff ) ;
12+ }
13+ } ;
14+
15+ const storageManagers = ( supports ) =>
16+ API . get ( `/api/providers?expand=resources&attributes=id,name,${ supports } &filter[]=${ supports } =true` )
17+ . then ( ( { resources } ) => {
18+ const storageManagersOptions = resources . map ( ( { id, name } ) => ( { label : name , value : id } ) ) ;
19+ storageManagersOptions . unshift ( { label : `<${ __ ( 'Choose' ) } >` , value : '-1' } ) ;
20+ return storageManagersOptions ;
21+ } ) ;
1222
1323const getProviderCapabilities = async ( providerId ) => API . get ( `/api/providers/${ providerId } ?attributes=capabilities` )
1424 . then ( ( result ) => result . capabilities ) ;
1525
16- const createSchema = ( edit , ems , initialValues , state , setState ) => {
26+ const createSchema = ( fields , edit , ems , loadSchema , emptySchema ) => {
27+ const idx = fields . findIndex ( ( field ) => field . name === 'required_capabilities' ) ;
28+ const supports = edit ? 'supports_storage_service' : 'supports_storage_service_create' ;
1729 let providerCapabilities ;
18- let emsId = state . ems_id ;
19- if ( initialValues && initialValues . ems_id ) {
20- emsId = initialValues . ems_id ;
21- }
2230
2331 return ( {
2432 fields : [
2533 {
2634 component : componentTypes . SELECT ,
2735 name : 'ems_id' ,
28- key : `${ emsId } ` ,
2936 id : 'ems_id' ,
3037 label : __ ( 'Storage Manager' ) ,
31- placeholder : __ ( '<Choose>' ) ,
32- onChange : ( value ) => {
33- emsId = value ;
34- return setState ( { ...state , ems_id : value } ) ;
35- } ,
36- loadOptions : loadProviders ,
38+ onChange : ( value ) => changeValue ( value , loadSchema , emptySchema ) ,
39+ loadOptions : ( ) => storageManagers ( supports ) ,
3740 isDisabled : edit || ems ,
3841 isRequired : true ,
39- includeEmpty : true ,
4042 validate : [ { type : validatorTypes . REQUIRED } ] ,
4143 } ,
4244 {
4345 component : componentTypes . TEXT_FIELD ,
4446 name : 'name' ,
4547 id : 'name' ,
46- label : __ ( 'Name: ' ) ,
48+ label : __ ( 'Service Name' ) ,
4749 isRequired : true ,
4850 validate : [
49- { type : validatorTypes . REQUIRED } ,
51+ {
52+ type : validatorTypes . REQUIRED ,
53+ } ,
54+ {
55+ type : 'pattern' ,
56+ pattern : '^[a-zA-Z0-9-_. ]*$' ,
57+ message : __ ( 'The name can contain letters, numbers, spaces, periods, dashes and underscores' ) ,
58+ } ,
59+ {
60+ type : 'pattern' ,
61+ pattern : '^[^ ]+( +[^ ]+)*$' ,
62+ message : __ ( 'The name must not begin or end with a space' ) ,
63+ } ,
64+ {
65+ type : 'pattern' ,
66+ pattern : '^[a-zA-Z_]' ,
67+ message : __ ( 'The name must begin with a letter or an underscore' ) ,
68+ } ,
5069 async ( value ) => validateName ( 'storage_services' , value , edit ) ,
5170 ] ,
5271 } ,
@@ -57,25 +76,22 @@ const createSchema = (edit, ems, initialValues, state, setState) => {
5776 label : __ ( 'Description:' ) ,
5877 isRequired : false ,
5978 } ,
60- {
61- component : componentTypes . SELECT ,
62- name : 'required_capabilities' ,
63- id : 'required_capabilities' ,
64- label : __ ( 'Required Capabilities' ) ,
65- placeholder : __ ( '<Choose>' ) ,
66- loadOptions : ( ) => ( emsId ? loadProviderCapabilities ( emsId ) : Promise . resolve ( [ ] ) ) ,
67- isDisabled : edit ,
68- isRequired : true ,
69- isMulti : true ,
70- validate : [ { type : validatorTypes . REQUIRED } ] ,
71- condition : { when : 'ems_id' , isNotEmpty : true } ,
72- } ,
79+ ...( idx === - 1 ? fields : [
80+ ...fields . slice ( 0 , idx ) ,
81+ {
82+ ...fields [ idx ] ,
83+ resolveProps : ( { options } , _input , { getState } ) => ( {
84+ options : options . filter ( ( { condition } ) => ! condition || parseCondition ( condition , getState ( ) . values ) . result ) ,
85+ } ) ,
86+ } ,
87+ ...fields . slice ( idx + 1 ) ,
88+ ] ) ,
7389 {
7490 component : 'enhanced-select' ,
7591 name : 'storage_resource_id' ,
7692 id : 'storage_resource_id' ,
7793 label : __ ( 'Storage Resources' ) ,
78- condition : { when : 'required_capabilities ' , isNotEmpty : true } ,
94+ condition : { when : 'compression ' , isNotEmpty : true } ,
7995 onInputChange : ( ) => null ,
8096 isRequired : true ,
8197 helperText : __ ( 'Select storage resources to attach to the new service. Volumes for this service will be created on these resources.' ) ,
@@ -85,8 +101,14 @@ const createSchema = (edit, ems, initialValues, state, setState) => {
85101 ] ,
86102 isMulti : true ,
87103 resolveProps : ( _props , _field , { getState } ) => {
88- const capabilityValues = getState ( ) . values . required_capabilities . map ( ( { value } ) => value ) ;
89- const emsId = getState ( ) . values . ems_id ;
104+ const stateValues = getState ( ) . values ;
105+ const emsId = stateValues . ems_id ;
106+ const capabilityValues = [ ] ;
107+
108+ const capabilityNames = fields . find ( ( object ) => object . id === 'required_capabilities' )
109+ . fields . map ( ( capability ) => capability . id ) ;
110+ capabilityNames . forEach ( ( capabilityName ) => capabilityValues . push ( stateValues [ capabilityName ] ) ) ;
111+
90112 return {
91113 key : JSON . stringify ( capabilityValues ) ,
92114 loadOptions : async ( ) => {
0 commit comments