diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts index bd91995b6efc3..7383a996b32b6 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts @@ -10,6 +10,7 @@ import { isCloud, getNewJobDefaults, getNewJobLimits, + extractDeploymentId, } from './ml_server_info'; import mockMlInfoResponse from './__mocks__/ml_info_response.json'; @@ -59,4 +60,24 @@ describe('ml_server_info', () => { done(); }); }); + + describe('cloud extract deployment ID', () => { + const cloudIdWithDeploymentName = + 'cloud_message_test:ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg=='; + + const cloudIdWithOutDeploymentName = + ':ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg=='; + + it('cloud ID with deployment name', () => { + expect(extractDeploymentId(cloudIdWithDeploymentName)).toBe( + '85d666f3350c469e8c3242d76a7f459c' + ); + }); + + it('cloud ID without deployment name', () => { + expect(extractDeploymentId(cloudIdWithOutDeploymentName)).toBe( + '85d666f3350c469e8c3242d76a7f459c' + ); + }); + }); }); diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.ts index 66fd491b59731..d1f92df01f061 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.ts @@ -53,10 +53,11 @@ export function isCloud(): boolean { } export function getCloudDeploymentId(): string | null { - if (cloudInfo.cloudId === null) { - return null; - } - const tempCloudId = cloudInfo.cloudId.replace(/^(.+)?:/, ''); + return cloudInfo.cloudId === null ? null : extractDeploymentId(cloudInfo.cloudId); +} + +export function extractDeploymentId(cloudId: string) { + const tempCloudId = cloudId.replace(/^(.+)?:/, ''); try { const matches = atob(tempCloudId).match(/^.+\$(.+)(?=\$)/); return matches !== null && matches.length === 2 ? matches[1] : null;