ContentPlugin.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import * as React from 'react'
  16. import styled from 'styled-components'
  17. import configLoader from '../../../utils/Config'
  18. import ToggleButtonBar from '../../../components/atoms/ToggleButtonBar'
  19. import type { Field } from '../../../types/Field'
  20. import { Wrapper, FieldStyled, Row } from '../default/ContentPlugin'
  21. import StyleProps from '../../../components/styleUtils/StyleProps'
  22. import Palette from '../../../components/styleUtils/Palette'
  23. const ToggleButtonBarStyled = styled(ToggleButtonBar)`
  24. margin-top: 16px;
  25. `
  26. const Fields = styled.div`
  27. margin-top: 32px;
  28. padding: 0 32px;
  29. display: flex;
  30. flex-direction: column;
  31. overflow: auto;
  32. `
  33. const Group = styled.div`
  34. display: flex;
  35. flex-direction: column;
  36. flex-shrink: 0;
  37. `
  38. const GroupName = styled.div`
  39. display: flex;
  40. align-items: center;
  41. margin: 32px 0 24px 0;
  42. `
  43. const GroupNameText = styled.div`
  44. margin: 0 32px;
  45. font-size: 16px;
  46. `
  47. const GroupNameBar = styled.div`
  48. flex-grow: 1;
  49. background: ${Palette.grayscale[3]};
  50. height: 1px;
  51. `
  52. const GroupFields = styled.div`
  53. display: flex;
  54. justify-content: space-between;
  55. flex-direction: column;
  56. `
  57. type Props = {
  58. connectionInfoSchema: Field[],
  59. validation: { valid: boolean, validation: { message: string } },
  60. invalidFields: string[],
  61. getFieldValue: (field: ?Field) => any,
  62. handleFieldChange: (field: ?Field, value: any) => void,
  63. handleFieldsChange: (items: { field: Field, value: any }[]) => void,
  64. disabled: boolean,
  65. cancelButtonText: string,
  66. validating: boolean,
  67. onRef: (contentPlugin: any) => void,
  68. onResizeUpdate: (scrollOfset: number) => void,
  69. scrollableRef: (ref: HTMLElement) => void,
  70. }
  71. type State = {
  72. useAdvancedOptions: boolean,
  73. showCephOptions: boolean,
  74. }
  75. class ContentPlugin extends React.Component<Props, State> {
  76. // This is a temporary hack, should be always true for all plugins, but momentaraly causes issues in Azure plugins
  77. // Fix Azure plugin and remove this line
  78. static REQUIRES_PARENT_OBJECT_PATH = true
  79. state = {
  80. useAdvancedOptions: false,
  81. showCephOptions: false,
  82. }
  83. previouslySelectedChoices: string[] = []
  84. get useCurrentUser(): boolean {
  85. return Boolean(this.getFieldValue(this.props.connectionInfoSchema.find(n => n.name === 'openstack_use_current_user')))
  86. }
  87. get hasCephOptionsSet(): boolean {
  88. console.log('schema', JSON.parse(JSON.stringify(this.props.connectionInfoSchema)))
  89. let cephOptionsField = this.props.connectionInfoSchema.find(n => n.name === 'ceph_options')
  90. if (!cephOptionsField || !cephOptionsField.properties) {
  91. return false
  92. }
  93. let hasValues = cephOptionsField.properties.filter(f => this.getFieldValue(f))
  94. return hasValues.length > 0
  95. }
  96. componentDidMount() {
  97. this.props.onRef(this)
  98. }
  99. componentDidUpdate(prevProps: Props, prevState: State) {
  100. if (prevState.useAdvancedOptions !== this.state.useAdvancedOptions) {
  101. this.props.onResizeUpdate(0)
  102. }
  103. }
  104. componentWillUnmount() {
  105. this.props.onRef(undefined)
  106. }
  107. getApiVersion(): number {
  108. return this.props.getFieldValue(this.props.connectionInfoSchema.find(n => n.name === 'identity_api_version'))
  109. }
  110. getFieldValue(field: ?Field) {
  111. let fieldValue = this.props.getFieldValue(field)
  112. if (fieldValue) {
  113. return fieldValue
  114. }
  115. let getInputChoiceValue = (fieldBaseName: string): string => {
  116. let id = this.props.getFieldValue(this.props.connectionInfoSchema.find(n => n.name === `${fieldBaseName}_id`))
  117. let previouslySelected = this.previouslySelectedChoices.find(f => f === `${fieldBaseName}_id`)
  118. if (id || previouslySelected) {
  119. if (!previouslySelected) this.previouslySelectedChoices.push(`${fieldBaseName}_id`)
  120. return `${fieldBaseName}_id`
  121. }
  122. return `${fieldBaseName}_name`
  123. }
  124. if (field && field.name === 'user_domain') {
  125. return getInputChoiceValue('user_domain')
  126. }
  127. if (field && field.name === 'project_domain') {
  128. return getInputChoiceValue('project_domain')
  129. }
  130. return fieldValue
  131. }
  132. handleAdvancedOptionsToggle(useAdvancedOptions: boolean) {
  133. this.setState({ useAdvancedOptions })
  134. }
  135. handleShowCepthOptionsChange(value: boolean) {
  136. let cephOptions = this.props.connectionInfoSchema.find(f => f.name === 'ceph_options')
  137. if (!cephOptions || !cephOptions.properties) {
  138. return
  139. }
  140. let resetFields = cephOptions.properties.map(field => ({
  141. field,
  142. value: null,
  143. }))
  144. this.props.handleFieldsChange(resetFields)
  145. this.setState({ showCephOptions: value })
  146. }
  147. findInvalidFields = () => {
  148. let inputChoices = ['user_domain', 'project_domain']
  149. let invalidFields = this.props.connectionInfoSchema.filter(field => {
  150. if (this.isFieldRequired(field)) {
  151. let value = this.getFieldValue(field)
  152. return !value
  153. }
  154. let inputChoice = inputChoices.find(c => c === field.name)
  155. if (inputChoice && this.getApiVersion() > 2) {
  156. let selectionValue = this.getFieldValue(this.props.connectionInfoSchema.find(f => f.name === inputChoice))
  157. let itemValue = this.getFieldValue(this.props.connectionInfoSchema.find(f => f.name === selectionValue))
  158. return !itemValue
  159. }
  160. return false
  161. }).map(f => f.name)
  162. let cephOptions = this.props.connectionInfoSchema.find(f => f.name === 'ceph_options')
  163. let cephOptionsProperties = cephOptions && cephOptions.properties
  164. if (cephOptionsProperties && (this.state.showCephOptions || this.hasCephOptionsSet)) {
  165. invalidFields = invalidFields.concat(
  166. cephOptionsProperties.filter(f => f.required && !this.getFieldValue(f)).map(f => f.name)
  167. )
  168. }
  169. return invalidFields
  170. }
  171. filterSimpleAdvanced(): Field[] {
  172. let extraAdvancedFields = ['description', 'glance_api_version', 'identity_api_version', 'openstack_use_current_user']
  173. if (this.getApiVersion() > 2) {
  174. extraAdvancedFields = extraAdvancedFields.concat(['user_domain', 'project_domain'])
  175. }
  176. let ignoreFields = ['user_domain_id', 'project_domain_id', 'user_domain_name', 'project_domain_name']
  177. if (!configLoader.config.showOpenstackCurrentUserSwitch) {
  178. ignoreFields.push('openstack_use_current_user')
  179. }
  180. return this.props.connectionInfoSchema.filter(f => !ignoreFields.find(i => i === f.name)).filter(field => {
  181. if (field.name === 'ceph_options') {
  182. return this.state.useAdvancedOptions && (this.state.showCephOptions || this.hasCephOptionsSet)
  183. }
  184. if (this.state.useAdvancedOptions) {
  185. return true
  186. }
  187. return field.required || extraAdvancedFields.find(fieldName => field.name === fieldName)
  188. })
  189. }
  190. isFieldRequired(field: Field) {
  191. return this.useCurrentUser ? field.name === 'name' : field.required
  192. }
  193. renderSimpleAdvancedToggle() {
  194. return (
  195. <ToggleButtonBarStyled
  196. items={[{ label: 'Simple', value: 'simple' }, { label: 'Advanced', value: 'advanced' }]}
  197. selectedValue={this.state.useAdvancedOptions ? 'advanced' : 'simple'}
  198. onChange={item => { this.handleAdvancedOptionsToggle(item.value === 'advanced') }}
  199. />
  200. )
  201. }
  202. renderFields() {
  203. const rows = []
  204. let fields = this.filterSimpleAdvanced()
  205. if (this.state.useAdvancedOptions) {
  206. let showCepthOptionsField = {
  207. name: 'show_ceph_options',
  208. label: 'Use Ceph for Replication',
  209. type: 'boolean',
  210. description: 'If performing Ceph-based Replicas from a source OpenStack, the Ceph configuration file and credentials for a user with read-only access to the Ceph pool used by Cinder backups/snapshots must be provided. Coriolis must be able to connect to the source OpenStack\'s Ceph RADOS cluster by being able to reach at least one Ceph- monitor host.For the easiest setup possible, simply using the same credentials used by the Cinder service(s) will work.',
  211. }
  212. fields.push(showCepthOptionsField)
  213. }
  214. const renderField = field => {
  215. let disabled = this.props.disabled
  216. || (this.useCurrentUser && field.name !== 'name' && field.name !== 'description' && field.name !== 'openstack_use_current_user')
  217. let required = this.isFieldRequired(field)
  218. || (this.getApiVersion() > 2 ? field.name === 'user_domain' || field.name === 'project_domain' : false)
  219. let isPassword = Boolean(configLoader.config.passwordFields.find(fn => field.name === fn))
  220. || field.name.indexOf('password') > -1
  221. let value = field.name === 'show_ceph_options' ? (this.state.showCephOptions || this.hasCephOptionsSet) : this.getFieldValue(field)
  222. let onChange = value => {
  223. if (field.name === 'show_ceph_options') {
  224. this.handleShowCepthOptionsChange(value)
  225. } else {
  226. this.props.handleFieldChange(field, value)
  227. }
  228. }
  229. return (
  230. <FieldStyled
  231. {...field}
  232. required={required}
  233. password={isPassword}
  234. width={StyleProps.inputSizes.large.width}
  235. disabled={disabled}
  236. highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
  237. value={value}
  238. onChange={onChange}
  239. getFieldValue={fieldName => this.getFieldValue(this.props.connectionInfoSchema.find(n => n.name === fieldName))}
  240. onFieldChange={(fieldName, fieldValue) => { this.props.handleFieldChange(this.props.connectionInfoSchema.find(n => n.name === fieldName), fieldValue) }}
  241. />
  242. )
  243. }
  244. let lastField = null
  245. let nonCephFields = fields.filter(f => f.name !== 'ceph_options')
  246. nonCephFields.forEach((field, i) => {
  247. const currentField = renderField(field)
  248. if (i % 2 !== 0) {
  249. rows.push((
  250. <Row key={field.name}>
  251. {lastField}
  252. {currentField}
  253. </Row>
  254. ))
  255. } else if (i === nonCephFields.length - 1) {
  256. rows.push((
  257. <Row key={field.name}>
  258. {currentField}
  259. </Row>
  260. ))
  261. }
  262. lastField = currentField
  263. })
  264. const cephOptionsRows = []
  265. let cephOptionsField = fields.find(f => f.name === 'ceph_options')
  266. let cephOptions = null
  267. let properties = cephOptionsField && cephOptionsField.properties
  268. if (properties) {
  269. let i = 0
  270. properties.forEach((field, fieldIndex) => {
  271. if (field.name === 'ceph_options/ceph_conf_file' || field.name === 'ceph_options/ceph_keyring_file') {
  272. field.useTextArea = true
  273. }
  274. const currentField = renderField(field)
  275. const pushRow = (field1: React.Node, field2?: React.Node) => {
  276. cephOptionsRows.push((
  277. <Row key={field.name}>
  278. {field1}
  279. {field2}
  280. </Row>
  281. ))
  282. }
  283. if (field.useTextArea) {
  284. pushRow(currentField)
  285. i -= 1
  286. } else if (i % 2 !== 0) {
  287. pushRow(lastField, currentField)
  288. } else if (fieldIndex === properties.length - 1) {
  289. pushRow(currentField)
  290. if (field.useTextArea) {
  291. i -= 1
  292. }
  293. } else {
  294. lastField = currentField
  295. }
  296. i += 1
  297. })
  298. cephOptions = (
  299. <Group>
  300. <GroupName>
  301. <GroupNameBar />
  302. <GroupNameText>Ceph Options</GroupNameText>
  303. <GroupNameBar />
  304. </GroupName>
  305. <GroupFields>{cephOptionsRows}</GroupFields>
  306. </Group>
  307. )
  308. }
  309. return (
  310. <Fields innerRef={ref => { this.props.scrollableRef(ref) }}>
  311. <Group>
  312. <GroupFields>
  313. {rows}
  314. </GroupFields>
  315. </Group>
  316. {cephOptions}
  317. </Fields>
  318. )
  319. }
  320. render() {
  321. return (
  322. <Wrapper>
  323. {this.renderSimpleAdvancedToggle()}
  324. {this.renderFields()}
  325. </Wrapper>
  326. )
  327. }
  328. }
  329. export default ContentPlugin