-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-elasticsearch): add locale to index names (#2323)
- Loading branch information
1 parent
e33bc4a
commit 1b480ea
Showing
28 changed files
with
591 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/api-file-manager-ddb-es/__tests__/elasticsearchIndex.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { configurations } from "~/configurations"; | ||
|
||
describe("Elasticsearch index", () => { | ||
const withLocaleItems = [ | ||
["root", "en-US"], | ||
["admin", "en-EN"], | ||
["root", "de-DE"], | ||
["admin", "en-GB"], | ||
["root,", "de"] | ||
]; | ||
|
||
beforeEach(() => { | ||
process.env.ELASTICSEARCH_SHARED_INDEXES = undefined; | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = undefined; | ||
}); | ||
|
||
it.each(withLocaleItems)( | ||
"should create index with locale code as part of the name", | ||
async (tenant, locale) => { | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true"; | ||
|
||
const { index } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
|
||
expect(index).toEqual(`${tenant}-${locale}-file-manager`.toLowerCase()); | ||
} | ||
); | ||
|
||
it.each(withLocaleItems)( | ||
"should create index without locale code as part of the name", | ||
async (tenant, locale) => { | ||
const { index } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
|
||
expect(index).toEqual(`${tenant}-file-manager`.toLowerCase()); | ||
} | ||
); | ||
|
||
it("should throw error when missing tenant but it is required", async () => { | ||
expect(() => { | ||
configurations.es({ | ||
tenant: null as any, | ||
locale: "en-US" | ||
}); | ||
}).toThrowError( | ||
`Missing "tenant" parameter when trying to create Elasticsearch index name.` | ||
); | ||
}); | ||
|
||
it("should throw error when missing locale but it is required", async () => { | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true"; | ||
|
||
expect(() => { | ||
configurations.es({ | ||
tenant: "root", | ||
locale: null as any | ||
}); | ||
}).toThrowError( | ||
`Missing "locale" parameter when trying to create Elasticsearch index name.` | ||
); | ||
}); | ||
|
||
it.each(withLocaleItems)( | ||
"should be root tenant in the index, no matter which one is sent", | ||
async (tenant, locale) => { | ||
process.env.ELASTICSEARCH_SHARED_INDEXES = "true"; | ||
|
||
const { index: noLocaleIndex } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
expect(noLocaleIndex).toEqual("root-file-manager"); | ||
} | ||
); | ||
|
||
it.each(withLocaleItems)( | ||
"should be root tenant in the index, no matter which one is sent", | ||
async (tenant, locale) => { | ||
process.env.ELASTICSEARCH_SHARED_INDEXES = "true"; | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true"; | ||
|
||
const { index: noLocaleIndex } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
expect(noLocaleIndex).toEqual(`root-${locale}-file-manager`.toLowerCase()); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import WebinyError from "@webiny/error"; | ||
|
||
export interface ElasticsearchConfigurationParams { | ||
tenant: string; | ||
locale: string; | ||
} | ||
export const configurations = { | ||
es(params: ElasticsearchConfigurationParams) { | ||
const { tenant, locale } = params; | ||
|
||
if (!tenant) { | ||
throw new WebinyError( | ||
`Missing "tenant" parameter when trying to create Elasticsearch index name.`, | ||
"TENANT_ERROR" | ||
); | ||
} | ||
|
||
const sharedIndex = process.env.ELASTICSEARCH_SHARED_INDEXES === "true"; | ||
|
||
const tenantId = sharedIndex ? "root" : tenant; | ||
let localeCode: string | null = null; | ||
if (process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE === "true") { | ||
if (!locale) { | ||
throw new WebinyError( | ||
`Missing "locale" parameter when trying to create Elasticsearch index name.`, | ||
"LOCALE_ERROR" | ||
); | ||
} | ||
localeCode = locale; | ||
} | ||
|
||
const index = [tenantId, localeCode, "file-manager"] | ||
.filter(Boolean) | ||
.join("-") | ||
.toLowerCase(); | ||
|
||
const prefix = process.env.ELASTIC_SEARCH_INDEX_PREFIX; | ||
if (!prefix) { | ||
return { | ||
index | ||
}; | ||
} | ||
return { | ||
index: prefix + index | ||
}; | ||
} | ||
}; |
16 changes: 0 additions & 16 deletions
16
packages/api-file-manager-ddb-es/src/operations/configurations.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
packages/api-file-manager-ddb/src/operations/configurations.ts
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
packages/api-form-builder-so-ddb-es/__tests__/elasticsearchIndex.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { configurations } from "~/configurations"; | ||
|
||
describe("Elasticsearch index", () => { | ||
const withLocaleItems = [ | ||
["root", "en-US"], | ||
["admin", "en-EN"], | ||
["root", "de-DE"], | ||
["admin", "en-GB"], | ||
["root,", "de"] | ||
]; | ||
|
||
beforeEach(() => { | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = undefined; | ||
}); | ||
|
||
it.each(withLocaleItems)( | ||
"should create index with locale code as part of the name", | ||
async (tenant, locale) => { | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true"; | ||
|
||
const { index } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
|
||
expect(index).toEqual(`${tenant}-${locale}-form-builder`.toLowerCase()); | ||
} | ||
); | ||
|
||
it.each(withLocaleItems)( | ||
"should create index without locale code as part of the name", | ||
async (tenant, locale) => { | ||
const { index } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
|
||
expect(index).toEqual(`${tenant}-form-builder`.toLowerCase()); | ||
} | ||
); | ||
|
||
it("should throw error when missing tenant but it is required", async () => { | ||
expect(() => { | ||
configurations.es({ | ||
tenant: null as any, | ||
locale: "en-US" | ||
}); | ||
}).toThrowError( | ||
`Missing "tenant" parameter when trying to create Elasticsearch index name.` | ||
); | ||
}); | ||
|
||
it("should throw error when missing locale but it is required", async () => { | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true"; | ||
|
||
expect(() => { | ||
configurations.es({ | ||
tenant: "root", | ||
locale: null as any | ||
}); | ||
}).toThrowError( | ||
`Missing "locale" parameter when trying to create Elasticsearch index name.` | ||
); | ||
}); | ||
|
||
it.each(withLocaleItems)( | ||
"should be root tenant in the index, no matter which one is sent", | ||
async (tenant, locale) => { | ||
process.env.ELASTICSEARCH_SHARED_INDEXES = "true"; | ||
|
||
const { index: noLocaleIndex } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
expect(noLocaleIndex).toEqual("root-form-builder"); | ||
} | ||
); | ||
|
||
it.each(withLocaleItems)( | ||
"should be root tenant in the index, no matter which one is sent", | ||
async (tenant, locale) => { | ||
process.env.ELASTICSEARCH_SHARED_INDEXES = "true"; | ||
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true"; | ||
|
||
const { index: noLocaleIndex } = configurations.es({ | ||
tenant, | ||
locale | ||
}); | ||
expect(noLocaleIndex).toEqual(`root-${locale}-form-builder`.toLowerCase()); | ||
} | ||
); | ||
}); |
Oops, something went wrong.