-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b04f88b
commit 3b005ba
Showing
15 changed files
with
375 additions
and
667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import MiqFormRenderer from '@@ddf'; | ||
import createSchema from './subnet-form.schema'; | ||
import miqRedirectBack from '../../helpers/miq-redirect-back'; | ||
import { API } from '../../http_api'; | ||
import { Loading } from 'carbon-components-react'; | ||
|
||
const SubnetForm = ({ recordId }) => { | ||
const [{ initialValues, isLoading, fields }, setState] = useState({ isLoading: !!recordId, fields: [] }); | ||
const submitLabel = !!recordId ? __('Save') : __('Add'); | ||
|
||
const loadSchema = (appendState = {}) => ({ data: { form_schema: { fields } } }) => { | ||
if (!!recordId && appendState.initialValues.type === 'ManageIQ::Providers::Openstack::NetworkManager::CloudSubnet') { | ||
Object.assign(fields[0], {isDisabled: true}); | ||
Object.assign(fields[1], {isDisabled: true}); | ||
Object.assign(fields[4], {isDisabled: true}); | ||
} | ||
setState((state) => ({ | ||
...state, | ||
...appendState, | ||
fields, | ||
})); | ||
}; | ||
|
||
useEffect(() => { | ||
if (recordId) { | ||
API.get(`/api/cloud_subnets/${recordId}`).then((initialValues) => { | ||
if(typeof initialValues.cloud_network_id ==="string") { | ||
initialValues.cloud_network_id=Number(initialValues.cloud_network_id); | ||
} | ||
if(typeof initialValues.cloud_tenant_id ==="string") { | ||
initialValues.cloud_tenant_id=Number(initialValues.cloud_tenant_id); | ||
} | ||
API.options(`/api/cloud_subnets?ems_id=${initialValues.ems_id}`).then(loadSchema({ initialValues, isLoading: false })); | ||
}); | ||
} | ||
}, [recordId]); | ||
|
||
const onSubmit = (values) => { | ||
API.get(`/api/providers/${values.ems_id}`).then(({ type }) => { | ||
if (type === 'ManageIQ::Providers::Openstack::NetworkManager') { | ||
if (values.ip_version === undefined) { | ||
values.ip_version = '4'; | ||
} | ||
if (values.dhcp_enabled === undefined) { | ||
values.dhcp_enabled = false; | ||
} | ||
values.enable_dhcp = values.dhcp_enabled; | ||
delete values.dhcp_enabled; | ||
delete Object.assign(values, values.extra_attributes).extra_attributes; | ||
} | ||
|
||
miqSparkleOn(); | ||
const request = recordId ? API.patch(`/api/cloud_subnets/${recordId}`, values) : API.post('/api/cloud_subnets', values); | ||
|
||
request.then(() => { | ||
const message = sprintf(recordId | ||
? __('Modification of Cloud Subnet %s has been successfully queued') | ||
: __('Add of Cloud Subnet "%s" has been successfully queued.'), | ||
values.name); | ||
|
||
miqRedirectBack(message, 'success', '/cloud_subnet/show_list'); | ||
}).catch(miqSparkleOff); | ||
}); | ||
}; | ||
|
||
const onCancel = () => { | ||
const message = sprintf( | ||
recordId | ||
? __('Edit of Cloud Subnet "%s" was canceled by the user.') | ||
: __('Creation of new Cloud Subnet was canceled by the user.'), | ||
initialValues && initialValues.name, | ||
); | ||
miqRedirectBack(message, 'warning', '/cloud_subnet/show_list'); | ||
}; | ||
|
||
if (isLoading) return <Loading className='export-spinner' withOverlay={false} small />; | ||
return !isLoading && ( | ||
<MiqFormRenderer | ||
schema={createSchema(!!recordId, fields, loadSchema)} | ||
initialValues={initialValues} | ||
canReset={!!recordId} | ||
onSubmit={onSubmit} | ||
onCancel={onCancel} | ||
buttonsLabels={{ submitLabel }} | ||
/> | ||
); | ||
}; | ||
|
||
SubnetForm.propTypes = { | ||
recordId: PropTypes.string, | ||
}; | ||
SubnetForm.defaultProps = { | ||
recordId: undefined, | ||
}; | ||
|
||
export default SubnetForm; |
73 changes: 73 additions & 0 deletions
73
app/javascript/components/subnet-form/subnet-form.schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { componentTypes, validatorTypes } from '@@ddf'; | ||
import { API } from '../../http_api'; | ||
|
||
const emsUrl = '/api/providers?expand=resources&attributes=id,name,supports_cloud_subnet_create&filter[]=supports_cloud_subnet_create=true'; | ||
|
||
const createSchema = (edit, fields = [], loadSchema) => ({ | ||
fields: [ | ||
{ | ||
component: componentTypes.SELECT, | ||
name: 'ems_id', | ||
id: 'ems_id', | ||
label: __('Network Manager'), | ||
onChange: (value) => API.options(`/api/cloud_subnets?ems_id=${value}`).then(loadSchema()), | ||
loadOptions: () => API.get(emsUrl).then(({ resources }) => resources.map(({ id, name }) => ({ label: name, value: id }))), | ||
includeEmpty: true, | ||
isDisabled: edit, | ||
isRequired: true, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'name', | ||
id: 'name', | ||
label: __('Name'), | ||
isRequired: true, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
condition: { | ||
when: 'ems_id', | ||
isNotEmpty: true, | ||
}, | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'cidr', | ||
id: 'cidr', | ||
label: __('CIDR'), | ||
isRequired: true, | ||
isDisabled: edit, | ||
validate: [{ type: validatorTypes.REQUIRED }], // TODO: pattern for validating IPv4/mask or IPv6/mask | ||
condition: { | ||
when: 'ems_id', | ||
isNotEmpty: true, | ||
}, | ||
}, | ||
{ | ||
component: componentTypes.FIELD_ARRAY, | ||
name: 'dns_nameservers', | ||
id: 'dns_nameservers', | ||
label: __('DNS Servers'), | ||
noItemsMessage: __('None'), | ||
buttonLabels: { | ||
add: __('Add'), | ||
remove: __('Remove'), | ||
}, | ||
AddButtonProps: { | ||
size: 'small', | ||
}, | ||
RemoveButtonProps: { | ||
size: 'small', | ||
}, | ||
fields: [{ // TODO: pattern for validating IPv4 or IPv6 | ||
component: componentTypes.TEXT_FIELD, | ||
}], | ||
condition: { | ||
when: 'ems_id', | ||
isNotEmpty: true, | ||
}, | ||
}, | ||
...fields, | ||
], | ||
}); | ||
|
||
export default createSchema; |
Oops, something went wrong.