Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(elasticsearch): update extractNameFromEndpoint method to remove #14052

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/@aws-cdk/aws-elasticsearch/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1759,19 +1759,20 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {
*
* Domain endpoints look like this:
*
* https://example-domain-jcjotrt6f7otem4sqcwbch3c4u.us-east-1.es.amazonaws.com
* https://<domain-name>-<suffix>.<region>.es.amazonaws.com
* https://search-example-domain-jcjotrt6f7otem4sqcwbch3c4u.us-east-1.es.amazonaws.com
* https://search-<domain-name>-<suffix>.<region>.es.amazonaws.com
*
* ..which means that in order to extract the domain name from the endpoint, we can
* split the endpoint using "-<suffix>" and select the component in index 0.
* split the endpoint using "-<suffix>" and select the component in index 0, and then replace
* the "search-" prefix.
*
* @param domainEndpoint The Elasticsearch domain endpoint
*/
function extractNameFromEndpoint(domainEndpoint: string) {
const { hostname } = new URL(domainEndpoint);
const domain = hostname.split('.')[0];
const suffix = '-' + domain.split('-').slice(-1)[0];
return domain.split(suffix)[0];
return domain.split(suffix)[0].replace('search-', '');
saudkhanzada marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-elasticsearch/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ describe('grants', () => {
});

test('"grant" for an imported domain', () => {
const domainEndpoint = 'https://test-domain-2w2x2u3tifly-jcjotrt6f7otem4sqcwbch3c4u.testregion.es.amazonaws.com';
const domainEndpoint = 'https://search-test-domain-2w2x2u3tifly-jcjotrt6f7otem4sqcwbch3c4u.testregion.es.amazonaws.com';
const domain = Domain.fromDomainEndpoint(stack, 'Domain', domainEndpoint);
const user = new iam.User(stack, 'user');

Expand Down Expand Up @@ -889,7 +889,7 @@ describe('import', () => {

test('static fromDomainEndpoint(endpoint) allows importing an external/existing domain', () => {
const domainName = 'test-domain-2w2x2u3tifly';
const domainEndpoint = `https://${domainName}-jcjotrt6f7otem4sqcwbch3c4u.testregion.es.amazonaws.com`;
const domainEndpoint = `https://search-${domainName}-jcjotrt6f7otem4sqcwbch3c4u.testregion.es.amazonaws.com`;
const imported = Domain.fromDomainEndpoint(stack, 'Domain', domainEndpoint);

expect(imported.domainName).toEqual(domainName);
Expand Down