convert_endpoints.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env node
  2. 'use strict';
  3. const fs = require('fs');
  4. const json = require('./endpoints.json');
  5. const products = Object.keys(json.regional_endpoints);
  6. const regions = json.regions;
  7. const mapping = {};
  8. const global_endpoints = json.global_endpoints;
  9. const regional_endpoints = json.regional_endpoints;
  10. const document_id = json.document_id;
  11. const pattern = json.regional_endpoint_pattern;
  12. const location_code_mapping = json.location_code_mapping;
  13. const p_in_mapping = Object.keys(location_code_mapping);
  14. const tocpp = {};
  15. // assign global EP, as the lowest priority, can be overlopped by pattern and regional configuration
  16. const products_with_global_endpoint = Object.keys(global_endpoints);
  17. products_with_global_endpoint.forEach(function(p) {
  18. if (!products.includes(p)) {
  19. products.push(p);
  20. }
  21. if (!tocpp[p]) {
  22. tocpp[p] = {};
  23. tocpp[p].regional = {};
  24. }
  25. let ep = global_endpoints[p];
  26. regions.forEach(function(r) {
  27. tocpp[p].regional[r] = ep;
  28. });
  29. });
  30. // pattern
  31. const products_with_pattern = Object.keys(pattern);
  32. products_with_pattern.forEach(function (p) {
  33. if (!products.includes(p)) {
  34. products.push(p);
  35. }
  36. if (!tocpp[p]) {
  37. tocpp[p] = {};
  38. tocpp[p].regional = {};
  39. }
  40. let p_pattern = pattern[p];
  41. regions.forEach(function(r) {
  42. let pattern_ep = p_pattern.replace('[RegionId]', r);
  43. tocpp[p].regional[r] = pattern_ep;
  44. });
  45. });
  46. // regional has the highest priority
  47. const products_with_regional = Object.keys(regional_endpoints);
  48. products_with_regional.forEach(function(p) {
  49. if (!tocpp[p]) {
  50. tocpp[p] = {};
  51. tocpp[p].regional = {};
  52. }
  53. let ep_regional = regional_endpoints[p];
  54. Object.assign(tocpp[p].regional, ep_regional);
  55. });
  56. const aliyun_products = Object.keys(tocpp);
  57. const mapped_products = Object.keys(location_code_mapping);
  58. const mapped_tocpp = {};
  59. aliyun_products.forEach(function(ap) {
  60. let found = false;
  61. for (var i = 0; i < mapped_products.length; i++) {
  62. let mp = mapped_products[i];
  63. if (ap === location_code_mapping[mp]) {
  64. mapped_tocpp[mp] = tocpp[ap];
  65. found = true;
  66. break
  67. }
  68. }
  69. if (!found) {
  70. mapped_tocpp[ap] = tocpp[ap];
  71. }
  72. });
  73. const total_products = Object.keys(mapped_tocpp);
  74. const cpp_header = {};
  75. cpp_header.products = total_products;
  76. cpp_header.regions = regions;
  77. cpp_header.endpoints = {};
  78. total_products.forEach(function(p) {
  79. let regional = mapped_tocpp[p].regional;
  80. cpp_header.endpoints[p] = {}
  81. cpp_header.endpoints[p].regions = Object.keys(regional);
  82. cpp_header.endpoints[p].regional = regional;
  83. });
  84. var sorted_string = JSON.stringify(cpp_header, null, 2);
  85. sorted_string = sorted_string.replace(/\"/g, "\\\"");
  86. sorted_string = sorted_string.replace(/\n/g,"\"\n\"");
  87. console.log("#include <string.h>");
  88. console.log();
  89. console.log("const std::string LOCAL_ENDPOINTS_CONFIG = \"" + sorted_string + '\";');