OptionsSchemaPlugin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 type { Field } from '../../../types/Field'
  16. import type { DestinationOption } from '../../../types/Endpoint'
  17. import type { WizardData } from '../../../types/WizardData'
  18. import { executionOptions } from '../../../config'
  19. const migrationImageOsTypes = ['windows', 'linux']
  20. export const defaultFillFieldValues = (field: Field, option: DestinationOption) => {
  21. if (field.type === 'string') {
  22. field.enum = [...option.values]
  23. if (option.config_default) {
  24. field.default = typeof option.config_default === 'string' ? option.config_default : option.config_default.id
  25. }
  26. }
  27. if (field.type === 'array') {
  28. field.enum = [...option.values]
  29. }
  30. }
  31. export const defaultFillMigrationImageMapValues = (field: Field, option: DestinationOption): boolean => {
  32. if (field.name === 'migr_image_map') {
  33. field.properties = migrationImageOsTypes.map(os => {
  34. let values = option.values
  35. .filter(v => v.os_type === os || v.os_type === 'unknown')
  36. .sort((v1, v2) => {
  37. if (v1.os_type === 'unknown' && v2.os_type !== 'unknown') {
  38. return 1
  39. } else if (v1.os_type !== 'unknown' && v2.os_type === 'unknown') {
  40. return -1
  41. }
  42. return 0
  43. })
  44. let unknownIndex = values.findIndex(v => v.os_type === 'unknown')
  45. if (unknownIndex > -1 && values.filter(v => v.os_type === 'unknown').length < values.length) {
  46. values.splice(unknownIndex, 0, { separator: true })
  47. }
  48. return {
  49. name: `${os}_os_image`,
  50. type: 'string',
  51. enum: values,
  52. }
  53. })
  54. return true
  55. }
  56. return false
  57. }
  58. export const defaultGetDestinationEnv = (data: WizardData): any => {
  59. let env = {}
  60. let specialOptions = ['execute_now', 'separate_vm', 'skip_os_morphing']
  61. .concat(executionOptions.map(o => o.name))
  62. .concat(migrationImageOsTypes.map(o => `${o}_os_image`))
  63. if (data.options) {
  64. Object.keys(data.options).forEach(optionName => {
  65. if (specialOptions.find(o => o === optionName) || !data.options || data.options[optionName] == null) {
  66. return
  67. }
  68. if (optionName.indexOf('/') > 0) {
  69. let parentName = optionName.substr(0, optionName.lastIndexOf('/'))
  70. if (!env[parentName]) {
  71. env[parentName] = {}
  72. }
  73. env[parentName][optionName.substr(optionName.lastIndexOf('/') + 1)] = data.options ? data.options[optionName] : null
  74. } else {
  75. env[optionName] = data.options ? data.options[optionName] : null
  76. }
  77. })
  78. }
  79. return env
  80. }
  81. export const defaultGetNetworkMap = (data: WizardData) => {
  82. let env = {}
  83. env.network_map = {}
  84. if (data.networks && data.networks.length) {
  85. data.networks.forEach(mapping => {
  86. env.network_map[mapping.sourceNic.network_name] = mapping.targetNetwork.id
  87. })
  88. }
  89. return env
  90. }
  91. export const defaultGetMigrationImageMap = (data: WizardData) => {
  92. let env = {}
  93. if (data.options) {
  94. migrationImageOsTypes.forEach(os => {
  95. if (data.options && data.options[`${os}_os_image`]) {
  96. if (!env.migr_image_map) {
  97. env.migr_image_map = {}
  98. }
  99. env.migr_image_map[os] = data.options[`${os}_os_image`]
  100. }
  101. })
  102. }
  103. return env
  104. }
  105. export default class OptionsSchemaParser {
  106. static fillFieldValues(field: Field, options: DestinationOption[]) {
  107. let option = options.find(f => f.name === field.name)
  108. if (!option) {
  109. return
  110. }
  111. if (!defaultFillMigrationImageMapValues(field, option)) {
  112. defaultFillFieldValues(field, option)
  113. }
  114. }
  115. static getDestinationEnv(data: WizardData) {
  116. let env = {
  117. ...defaultGetDestinationEnv(data),
  118. ...defaultGetNetworkMap(data),
  119. ...defaultGetMigrationImageMap(data),
  120. }
  121. return env
  122. }
  123. }