LabelDictionary.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 type { Field } from "@src/@types/Field";
  15. // The word will be uppercased
  16. const acronyms = [
  17. "id",
  18. "api",
  19. "url",
  20. "vm",
  21. "os",
  22. "dhcp",
  23. "sql",
  24. "oci",
  25. "aws",
  26. "vcn",
  27. "ca",
  28. ];
  29. // The word will be replaced
  30. const abbreviations = {
  31. migr: "Migration",
  32. auth: "Authentication",
  33. fip: "Floating IP",
  34. };
  35. const dictionary = {
  36. migr_image: "Migration Image Name or Id",
  37. migr_network: "Migration Network Name or ID",
  38. migr_worker_boot_from_volume: "Boot Migration Workers from Volume",
  39. volumes_are_zeroed: "Volumes on destination are created zeroed",
  40. keep_mac: "Keep MAC address",
  41. reuse_ports: "Reuse Existing Ports",
  42. replace_mac: "Replace MAC Address",
  43. migr_worker_use_config_drive: "Migration Worker use ConfigDrive",
  44. migr_worker_use_fip: "Migration Worker use FIP",
  45. separate_vm: "Separate Migration/VM?",
  46. windows_migr_image: {
  47. description:
  48. "The Windows Migration Image information found on the Azure page",
  49. },
  50. linux_migr_image: {
  51. description:
  52. "The Linux Migration Image information found on the Azure page",
  53. },
  54. duplicate_to_project: {
  55. label: "Project",
  56. description: "Duplicate endpoint to selected project",
  57. },
  58. // AzureStack suffixes
  59. azure_datalake_analytics_catalog_and_job_endpoint:
  60. "Azure Datalake Analytics Catalog And Job Endpoint Suffix",
  61. azure_datalake_store_file_system_endpoint:
  62. "Azure Datalake Store File System Endpoint Suffix",
  63. keyvault_dns: "Keyvault DNS Suffix",
  64. sql_server_hostname: "SQL Server Hostname Suffix",
  65. storage_endpoint: "Storage Endpoint Suffix",
  66. preserve_nic_ips: "Preserve NIC IPs",
  67. openstack_use_current_user:
  68. "Use Current User/Project/Domain for Authentification",
  69. windows_os_image: "Windows OS",
  70. linux_os_image: "Linux OS",
  71. skip_os_morphing: {
  72. description: `Whether or not to skip the OSMorphing process.
  73. This process is recommended when migrating VMs between platforms with different underlying virtualization technologies or initialization agents, as Coriolis will adapt the offline OS installation by automatically installing any additional drivers/configurations required by the destination platform.
  74. This can be safely skipped if the source and destination platforms are identical (e.g: migrating between two separate regions of the same Public Cloud), or if the two platforms share the same virtualization technology.
  75. (e.g: migrating between two KVM-based OpenStacks from different vendors)`,
  76. },
  77. force: {
  78. description: `Whether or not Coriolis should forcibly attempt the Replica Deployment process despite the Replica not having any successful Executions.
  79. This is only recommended if it is known that the Replica disks were successfully synced but some latter cleanup steps failed (e.g: deleting source-side temporary resources).
  80. This will not help if the Replica disks were never successfully synced.`,
  81. },
  82. clone_disks: {
  83. description: `Whether or not Coriolis should clone the Replica disks on the destination platforms before optionally performing the OSMorphing process and booting the final VM.
  84. Skipping disk cloning leads to a shorter deployment time, but means that the Replica disks will be allocated to the new VM, and thus the next Replica Execution will have to sync the disks from scratch.`,
  85. },
  86. };
  87. const cache: {
  88. name: string;
  89. label: string | null | undefined;
  90. description: string | null | undefined;
  91. key: string;
  92. }[] = [];
  93. class LabelDictionary {
  94. // Fields which have enums for which dictionary labels should be used.
  95. // If a field has enums and is not in this array, their values will be used as labels
  96. static enumFields = [
  97. "port_reuse_policy",
  98. "replica_export_mechanism",
  99. "virtual_disk_clone_type",
  100. ];
  101. /**
  102. *
  103. * @param {string} fieldName The name of the field
  104. * @param {string} dictionaryKey Optional key to more uniquely
  105. * identify the field added to schema cache.
  106. * The `dictionaryKey` is composed by `${provider}-${direction}.
  107. * Direction is 'destination' or 'source'.
  108. */
  109. static get(
  110. fieldName: string | null | undefined,
  111. dictionaryKey?: string
  112. ): string {
  113. if (!fieldName) {
  114. return "";
  115. }
  116. const cachItem = cache.find(
  117. i => i.key === dictionaryKey && i.name === fieldName
  118. );
  119. if (cachItem && cachItem.label) {
  120. return cachItem.label;
  121. }
  122. const dict: any = dictionary;
  123. const labelInfo = dict[fieldName];
  124. if (labelInfo) {
  125. if (typeof labelInfo === "string") {
  126. return labelInfo;
  127. }
  128. if (labelInfo.label) {
  129. return labelInfo.label;
  130. }
  131. }
  132. let words = fieldName.split("_");
  133. words = words.map(word => {
  134. const acronym = acronyms.find(a => a === word);
  135. const abb: any = abbreviations;
  136. const newWord = acronym
  137. ? acronym.toUpperCase()
  138. : abb[word] || word.toLowerCase();
  139. return newWord.charAt(0).toUpperCase() + newWord.substr(1);
  140. });
  141. return words.join(" ");
  142. }
  143. static getDescription(fieldName: string, dictionaryKey?: string): string {
  144. const cachItem = cache.find(
  145. i => i.key === dictionaryKey && i.name === fieldName
  146. );
  147. if (cachItem && cachItem.description) {
  148. return cachItem.description;
  149. }
  150. const dict: any = dictionary;
  151. const labelInfo = dict[fieldName];
  152. if (labelInfo && typeof labelInfo === "object") {
  153. return labelInfo.description || "";
  154. }
  155. return "";
  156. }
  157. static pushToCache(field: Field, dictionaryKey: string) {
  158. if (
  159. (field.title || field.description) &&
  160. !cache.find(i => i.key === dictionaryKey && i.name === field.name)
  161. ) {
  162. cache.push({
  163. label: field.title,
  164. description: field.description,
  165. name: field.name,
  166. key: dictionaryKey,
  167. });
  168. }
  169. }
  170. }
  171. export default LabelDictionary;