OptionsSchemaPlugin.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 { 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
  31. .find(f => f.name === "coriolis_backups_options")
  32. ?.properties?.find(p => p.name === "export_image");
  33. if (exportImage) {
  34. exportImage.required = true;
  35. }
  36. const exportMechField = fields.find(
  37. f => f.name === "replica_export_mechanism"
  38. );
  39. if (!exportMechField) {
  40. return fields;
  41. }
  42. exportMechField.subFields = [];
  43. exportMechField.enum!.forEach((exportType: any) => {
  44. const exportTypeFieldIdx = fields.findIndex(
  45. f => f.name === `${exportType}_options`
  46. );
  47. if (exportTypeFieldIdx === -1) {
  48. return;
  49. }
  50. const subField = fields[exportTypeFieldIdx];
  51. if (subField.properties?.length) {
  52. subField.properties = subField.properties.map((p: Field) => ({
  53. ...p,
  54. groupName: subField.name,
  55. }));
  56. }
  57. exportMechField.subFields!.push(subField);
  58. fields.splice(exportTypeFieldIdx, 1);
  59. });
  60. return fields;
  61. }
  62. override fillFieldValues(opts: {
  63. field: Field;
  64. options: OptionValues[];
  65. requiresWindowsImage: boolean;
  66. }) {
  67. const { field, options, requiresWindowsImage } = opts;
  68. if (field.name === "replica_export_mechanism" && field.subFields) {
  69. field.subFields.forEach(sf => {
  70. if (sf.properties) {
  71. sf.properties.forEach(f => {
  72. super.fillFieldValues({
  73. field: f,
  74. options,
  75. customFieldName: f.name.split("/")[1],
  76. requiresWindowsImage,
  77. });
  78. removeExportImageFieldValues(f);
  79. });
  80. }
  81. });
  82. }
  83. const option = options.find(f => f.name === field.name);
  84. if (!option) {
  85. return;
  86. }
  87. if (
  88. !defaultFillMigrationImageMapValues({
  89. field,
  90. option,
  91. migrationImageMapFieldName: this.migrationImageMapFieldName,
  92. requiresWindowsImage,
  93. })
  94. ) {
  95. defaultFillFieldValues(field, option);
  96. }
  97. }
  98. }