OptionsSchemaPlugin.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright (C) 2022 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 { 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. defaultFillFieldValues,
  19. defaultFillMigrationImageMapValues,
  20. removeExportImageFieldValues,
  21. } from '../default/OptionsSchemaPlugin'
  22. export default class OptionsSchemaParser extends OptionsSchemaPluginBase {
  23. override parseSchemaToFields(opts: {
  24. schema: SchemaProperties,
  25. schemaDefinitions?: SchemaDefinitions | null | undefined,
  26. dictionaryKey?: string,
  27. requiresWindowsImage?: boolean,
  28. }) {
  29. const fields: Field[] = super.parseSchemaToFields(opts)
  30. const exportImage = fields.find(f => f.name === 'export_image')
  31. if (exportImage) {
  32. exportImage.required = true
  33. }
  34. return fields
  35. }
  36. override sortFields(fields: Field[]) {
  37. super.sortFields(fields)
  38. fields.sort((f1, f2) => {
  39. // sort region first
  40. if (f1.name === 'region') {
  41. return -1
  42. }
  43. if (f2.name === 'region') {
  44. return 1
  45. }
  46. return 0
  47. })
  48. }
  49. override fillFieldValues(opts: { field: Field, options: OptionValues[], requiresWindowsImage: boolean }) {
  50. const { field, options, requiresWindowsImage } = opts
  51. const option = options.find(f => f.name === field.name)
  52. if (!option) {
  53. return
  54. }
  55. if (!defaultFillMigrationImageMapValues({
  56. field,
  57. option,
  58. migrationImageMapFieldName: this.migrationImageMapFieldName,
  59. requiresWindowsImage,
  60. })) {
  61. defaultFillFieldValues(field, option)
  62. removeExportImageFieldValues(field)
  63. }
  64. }
  65. }