OptionsSchemaPlugin.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (C) 2019 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. defaultGetDestinationEnv,
  16. defaultGetMigrationImageMap,
  17. defaultFillFieldValues,
  18. defaultFillMigrationImageMapValues,
  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 = DefaultOptionsSchemaPlugin.migrationImageMapFieldName
  28. static parseSchemaToFields(
  29. schema: SchemaProperties,
  30. schemaDefinitions: SchemaDefinitions | null | undefined,
  31. dictionaryKey: string,
  32. ) {
  33. const fields = DefaultOptionsSchemaPlugin.parseSchemaToFields(schema, schemaDefinitions, dictionaryKey)
  34. const exportMechField = fields.find(f => f.name === 'replica_export_mechanism')
  35. if (!exportMechField) {
  36. return fields
  37. }
  38. exportMechField.subFields = []
  39. exportMechField.enum.forEach((exportType: any) => {
  40. const exportTypeFieldIdx = fields.findIndex(f => f.name === `${exportType}_options`)
  41. if (exportTypeFieldIdx === -1) {
  42. return
  43. }
  44. const subField = fields[exportTypeFieldIdx]
  45. if (subField.properties?.length) {
  46. subField.properties = subField.properties.map((p: Field) => ({ ...p, groupName: subField.name }))
  47. }
  48. exportMechField.subFields.push(subField)
  49. fields.splice(exportTypeFieldIdx, 1)
  50. })
  51. return fields
  52. }
  53. static sortFields(fields: Field[]) {
  54. DefaultOptionsSchemaPlugin.sortFields(fields)
  55. }
  56. static fillFieldValues(field: Field, options: OptionValues[]) {
  57. if (field.name === 'replica_export_mechanism' && field.subFields) {
  58. field.subFields.forEach(sf => {
  59. if (sf.properties) {
  60. sf.properties.forEach(f => {
  61. DefaultOptionsSchemaPlugin.fillFieldValues(f, options, f.name.split('/')[1])
  62. })
  63. }
  64. })
  65. } else {
  66. const option = options.find(f => f.name === field.name)
  67. if (!option) {
  68. return
  69. }
  70. if (!defaultFillMigrationImageMapValues(
  71. field,
  72. option,
  73. this.migrationImageMapFieldName,
  74. )) {
  75. defaultFillFieldValues(field, option)
  76. }
  77. }
  78. }
  79. static getDestinationEnv(options: { [prop: string]: any } | null, oldOptions?: any) {
  80. const env = {
  81. ...defaultGetDestinationEnv(options, oldOptions),
  82. ...defaultGetMigrationImageMap(
  83. options,
  84. oldOptions,
  85. this.migrationImageMapFieldName,
  86. ),
  87. }
  88. return env
  89. }
  90. static getNetworkMap(networkMappings: NetworkMap[] | null) {
  91. return DefaultOptionsSchemaPlugin.getNetworkMap(networkMappings)
  92. }
  93. static getStorageMap(
  94. defaultStorage: string | null,
  95. storageMap: StorageMap[] | null,
  96. configDefault?: string | null,
  97. ) {
  98. return DefaultOptionsSchemaPlugin.getStorageMap(defaultStorage, storageMap, configDefault)
  99. }
  100. static getUserScripts(
  101. uploadedUserScripts: InstanceScript[],
  102. removedUserScripts: InstanceScript[],
  103. userScriptData: UserScriptData | null | undefined,
  104. ) {
  105. return DefaultOptionsSchemaPlugin
  106. .getUserScripts(uploadedUserScripts, removedUserScripts, userScriptData)
  107. }
  108. }