OptionsSchemaPlugin.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 DefaultOptionsSchemaPlugin, {
  15. defaultFillMigrationImageMapValues,
  16. defaultFillFieldValues,
  17. defaultGetDestinationEnv,
  18. defaultGetMigrationImageMap,
  19. } from '../default/OptionsSchemaPlugin'
  20. import type { InstanceScript } from '../../../@types/Instance'
  21. import type { Field } from '../../../@types/Field'
  22. import type { OptionValues, StorageMap } from '../../../@types/Endpoint'
  23. import type { SchemaProperties, SchemaDefinitions } from '../../../@types/Schema'
  24. import type { NetworkMap } from '../../../@types/Network'
  25. import { UserScriptData } from '../../../@types/MainItem'
  26. export default class OptionsSchemaParser {
  27. static migrationImageMapFieldName = 'migr_template_map'
  28. static parseSchemaToFields(
  29. schema: SchemaProperties,
  30. schemaDefinitions: SchemaDefinitions | null | undefined,
  31. dictionaryKey: string,
  32. ) {
  33. const fields = DefaultOptionsSchemaPlugin
  34. .parseSchemaToFields(schema, schemaDefinitions, dictionaryKey)
  35. fields.forEach(f => {
  36. if (
  37. f.name !== 'migr_template_username_map'
  38. && f.name !== 'migr_template_password_map'
  39. && f.name !== 'migr_template_name_map'
  40. ) {
  41. return
  42. }
  43. const password = f.name === 'migr_template_password_map'
  44. f.properties = [
  45. {
  46. type: 'string',
  47. name: 'windows',
  48. password,
  49. },
  50. {
  51. type: 'string',
  52. name: 'linux',
  53. password,
  54. },
  55. ]
  56. })
  57. return fields
  58. }
  59. static fillFieldValues(field: Field, options: OptionValues[]) {
  60. const option = options.find(f => f.name === field.name)
  61. if (!option) {
  62. return
  63. }
  64. if (!defaultFillMigrationImageMapValues(
  65. field,
  66. option,
  67. this.migrationImageMapFieldName,
  68. )) {
  69. defaultFillFieldValues(field, option)
  70. }
  71. }
  72. static getDestinationEnv(options: { [prop: string]: any } | null, oldOptions?: any) {
  73. const env = {
  74. ...defaultGetDestinationEnv(options, oldOptions),
  75. ...defaultGetMigrationImageMap(
  76. options,
  77. oldOptions,
  78. this.migrationImageMapFieldName,
  79. ),
  80. }
  81. return env
  82. }
  83. static getNetworkMap(networkMappings: NetworkMap[] | null | undefined) {
  84. return DefaultOptionsSchemaPlugin.getNetworkMap(networkMappings)
  85. }
  86. static getStorageMap(
  87. defaultStorage: { value: string | null, busType?: string | null },
  88. storageMap: StorageMap[] | null,
  89. configDefault?: string | null,
  90. ) {
  91. return DefaultOptionsSchemaPlugin.getStorageMap(defaultStorage, storageMap, configDefault)
  92. }
  93. static getUserScripts(
  94. uploadedUserScripts: InstanceScript[],
  95. removedUserScripts: InstanceScript[],
  96. userScriptData: UserScriptData | null | undefined,
  97. ) {
  98. return DefaultOptionsSchemaPlugin
  99. .getUserScripts(uploadedUserScripts, removedUserScripts, userScriptData)
  100. }
  101. }