OptionsSchemaPlugin.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import type { Field } from '@src/@types/Field'
  15. import type { OptionValues } from '@src/@types/Endpoint'
  16. import type { SchemaProperties, SchemaDefinitions } from '@src/@types/Schema'
  17. import OptionsSchemaPluginBase, {
  18. defaultFillMigrationImageMapValues,
  19. defaultFillFieldValues,
  20. defaultGetDestinationEnv,
  21. defaultGetMigrationImageMap,
  22. } from '../default/OptionsSchemaPlugin'
  23. export default class OptionsSchemaParser extends OptionsSchemaPluginBase {
  24. override migrationImageMapFieldName = 'migr_template_map'
  25. override parseSchemaToFields(opts: {
  26. schema: SchemaProperties,
  27. schemaDefinitions?: SchemaDefinitions | null | undefined,
  28. dictionaryKey?: string,
  29. requiresWindowsImage?: boolean,
  30. }) {
  31. const fields: Field[] = super.parseSchemaToFields(opts)
  32. const makeRequired = (fieldName: string) => {
  33. const field = fields.find(f => f.name === fieldName)
  34. if (field) {
  35. field.required = true
  36. }
  37. }
  38. makeRequired('export_template')
  39. makeRequired('export_template_username')
  40. makeRequired('export_template_password')
  41. const useCoriolisExporterField = fields.find(f => f.name === 'use_coriolis_exporter')
  42. if (useCoriolisExporterField) {
  43. const usableFields: Field[] = [
  44. {
  45. ...useCoriolisExporterField,
  46. nullableBoolean: false,
  47. default: false,
  48. subFields: [
  49. {
  50. name: 'generic_exporter_options',
  51. type: 'object',
  52. properties: fields.filter(f => f.name !== 'use_coriolis_exporter')
  53. .map(f => ({ ...f, groupName: 'generic_exporter_options' })),
  54. },
  55. {
  56. name: 'ovm_exporter_options',
  57. type: 'object',
  58. properties: [],
  59. },
  60. ],
  61. },
  62. ]
  63. return usableFields
  64. }
  65. fields.forEach(f => {
  66. if (
  67. f.name !== 'migr_template_username_map'
  68. && f.name !== 'migr_template_password_map'
  69. && f.name !== 'migr_template_name_map'
  70. ) {
  71. return
  72. }
  73. const password = f.name === 'migr_template_password_map'
  74. f.properties = [
  75. {
  76. type: 'string',
  77. name: 'windows',
  78. required: opts.requiresWindowsImage,
  79. password,
  80. },
  81. {
  82. type: 'string',
  83. name: 'linux',
  84. required: true,
  85. password,
  86. },
  87. ]
  88. })
  89. return fields
  90. }
  91. override fillFieldValues(opts: { field: Field, options: OptionValues[], requiresWindowsImage: boolean }) {
  92. const { field, options, requiresWindowsImage } = opts
  93. if (field.name === 'use_coriolis_exporter') {
  94. field.subFields?.forEach(sf => {
  95. if (sf.properties) {
  96. sf.properties.forEach(f => {
  97. super.fillFieldValues({
  98. field: f, options, customFieldName: f.name.split('/')[1], requiresWindowsImage,
  99. })
  100. if (f.name === 'export_template' && f.enum) {
  101. f.enum = f.enum.map(newF => (typeof newF !== 'string' ? {
  102. ...newF,
  103. // @ts-ignore
  104. disabled: newF.os_type !== 'linux' && newF.os_type !== 'unknown',
  105. // @ts-ignore
  106. subtitleLabel: newF.os_type !== 'linux' && newF.os_type !== 'unknown' ? `Source plugins rely on a Linux-based temporary virtual machine to perform data exports, but the platform reports this image to be of OS type '${newF.os_type}'.` : '',
  107. } : newF))
  108. }
  109. })
  110. }
  111. })
  112. } else {
  113. const option = options.find(f => f.name === field.name)
  114. if (!option) {
  115. return
  116. }
  117. if (!defaultFillMigrationImageMapValues({
  118. field,
  119. option,
  120. migrationImageMapFieldName: this.migrationImageMapFieldName,
  121. requiresWindowsImage,
  122. })) {
  123. defaultFillFieldValues(field, option)
  124. }
  125. }
  126. }
  127. override getDestinationEnv(options: { [prop: string]: any } | null, oldOptions?: any) {
  128. let newOptions: any = { ...options }
  129. if (newOptions.use_coriolis_exporter != null) {
  130. newOptions = { use_coriolis_exporter: newOptions.use_coriolis_exporter }
  131. }
  132. if (options?.generic_exporter_options) {
  133. newOptions = { ...options.generic_exporter_options, use_coriolis_exporter: false }
  134. }
  135. const env = {
  136. ...defaultGetDestinationEnv(newOptions, oldOptions),
  137. ...defaultGetMigrationImageMap(
  138. newOptions,
  139. oldOptions,
  140. this.migrationImageMapFieldName,
  141. ),
  142. }
  143. return env
  144. }
  145. }