OptionsSchemaPlugin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 DefaultOptionsSchemaPlugin, {
  16. defaultFillMigrationImageMapValues,
  17. defaultFillFieldValues,
  18. defaultGetDestinationEnv,
  19. defaultGetMigrationImageMap,
  20. } from '../default/OptionsSchemaPlugin'
  21. import type { InstanceScript } from '../../../types/Instance'
  22. import type { Field } from '../../../types/Field'
  23. import type { OptionValues, StorageMap } from '../../../types/Endpoint'
  24. import type { SchemaProperties, SchemaDefinitions } from '../../../types/Schema'
  25. import type { NetworkMap } from '../../../types/Network'
  26. export default class OptionsSchemaParser {
  27. static migrationImageMapFieldName = 'migr_template_map'
  28. static parseSchemaToFields(schema: SchemaProperties, schemaDefinitions?: ?SchemaDefinitions, dictionaryKey: string) {
  29. let fields = DefaultOptionsSchemaPlugin.parseSchemaToFields(schema, schemaDefinitions, dictionaryKey)
  30. fields.forEach(f => {
  31. if (
  32. f.name !== 'migr_template_username_map'
  33. && f.name !== 'migr_template_password_map'
  34. && f.name !== 'migr_template_name_map'
  35. ) {
  36. return
  37. }
  38. f.properties = [
  39. {
  40. type: 'string',
  41. name: `${f.name}/windows`,
  42. password: f.name === 'migr_template_password_map',
  43. },
  44. {
  45. type: 'string',
  46. name: `${f.name}/linux`,
  47. password: f.name === 'migr_template_password_map',
  48. },
  49. ]
  50. })
  51. return fields
  52. }
  53. static fillFieldValues(field: Field, options: OptionValues[]) {
  54. let option = options.find(f => f.name === field.name)
  55. if (!option) {
  56. return
  57. }
  58. if (!defaultFillMigrationImageMapValues(field, option, this.migrationImageMapFieldName)) {
  59. defaultFillFieldValues(field, option)
  60. }
  61. }
  62. static getDestinationEnv(options: ?{ [string]: mixed }, oldOptions?: any) {
  63. let env = {
  64. ...defaultGetDestinationEnv(options, oldOptions),
  65. ...defaultGetMigrationImageMap(options, oldOptions, this.migrationImageMapFieldName),
  66. }
  67. return env
  68. }
  69. static getNetworkMap(networkMappings: ?NetworkMap[]) {
  70. return DefaultOptionsSchemaPlugin.getNetworkMap(networkMappings)
  71. }
  72. static getStorageMap(defaultStorage: ?string, storageMap: ?StorageMap[], configDefault?: ?string) {
  73. return DefaultOptionsSchemaPlugin.getStorageMap(defaultStorage, storageMap, configDefault)
  74. }
  75. static getUserScripts(uploadedUserScripts: InstanceScript[]) {
  76. return DefaultOptionsSchemaPlugin.getUserScripts(uploadedUserScripts)
  77. }
  78. }