forked from opensearch-project/reporting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reporting osd server configurations (opensearch-project#222)
Signed-off-by: Zhongnan Su <[email protected]>
- Loading branch information
1 parent
dc68aec
commit 8778efb
Showing
12 changed files
with
254 additions
and
71 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
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,69 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { | ||
CoreSetup, | ||
Logger, | ||
PluginInitializerContext, | ||
} from '../../../../src/core/server'; | ||
import { ReportingConfigType } from './schema'; | ||
import { get } from 'lodash'; | ||
import { first, map } from 'rxjs/operators'; | ||
import { createConfig$ } from './createConfig'; | ||
|
||
interface Config<BaseType> { | ||
get<Key1 extends keyof BaseType>(key1: Key1): BaseType[Key1]; | ||
get<Key1 extends keyof BaseType, Key2 extends keyof BaseType[Key1]>( | ||
key1: Key1, | ||
key2: Key2 | ||
): BaseType[Key1][Key2]; | ||
} | ||
|
||
interface OsdServerConfigType { | ||
server: { | ||
basePath: string; | ||
host: string; | ||
name: string; | ||
port: number; | ||
protocol: string; | ||
}; | ||
} | ||
|
||
export interface ReportingConfig extends Config<ReportingConfigType> { | ||
osdConfig: Config<OsdServerConfigType>; | ||
} | ||
|
||
export const buildConfig = async ( | ||
initContext: PluginInitializerContext<ReportingConfigType>, | ||
core: CoreSetup, | ||
logger: Logger | ||
): Promise<ReportingConfig> => { | ||
const config$ = initContext.config.create<ReportingConfigType>(); | ||
const serverInfo = core.http.getServerInfo(); | ||
const osdConfig = { | ||
server: { | ||
basePath: core.http.basePath.serverBasePath, | ||
host: serverInfo.hostname, | ||
name: serverInfo.name, | ||
port: serverInfo.port, | ||
protocol: serverInfo.protocol, | ||
}, | ||
}; | ||
|
||
const reportingConfig$ = createConfig$(core, config$, logger); | ||
const reportingConfig = await reportingConfig$.pipe(first()).toPromise(); | ||
return { | ||
get: (...keys: string[]) => get(reportingConfig, keys.join('.'), null), | ||
osdConfig: { | ||
get: (...keys: string[]) => get(osdConfig, keys.join('.'), null), | ||
}, | ||
}; | ||
}; |
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,53 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { CoreSetup, Logger } from '../../../../src/core/server'; | ||
|
||
import { ReportingConfigType } from './schema'; | ||
|
||
/* | ||
* Set up dynamic config defaults | ||
*/ | ||
export function createConfig$( | ||
core: CoreSetup, | ||
config$: Observable<ReportingConfigType>, | ||
logger: Logger | ||
) { | ||
return config$.pipe( | ||
map((config) => { | ||
const { osd_server: reportingServer } = config; | ||
const serverInfo = core.http.getServerInfo(); | ||
// osd_server.hostname, default to server.host | ||
const osdServerHostname = reportingServer.hostname | ||
? reportingServer.hostname | ||
: serverInfo.hostname; | ||
|
||
// osd_server.port, default to server.port | ||
const osdServerPort = reportingServer.port | ||
? reportingServer.port | ||
: serverInfo.port; | ||
// osd_server.protocol, default to server.protocol | ||
const osdServerProtocol = reportingServer.protocol | ||
? reportingServer.protocol | ||
: serverInfo.protocol; | ||
return { | ||
...config, | ||
osd_server: { | ||
hostname: osdServerHostname, | ||
port: osdServerPort, | ||
protocol: osdServerProtocol, | ||
}, | ||
}; | ||
}) | ||
); | ||
} |
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,19 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { PluginConfigDescriptor } from '../../../../src/core/server'; | ||
import { ConfigSchema, ReportingConfigType } from './schema'; | ||
export { buildConfig } from './config'; | ||
export { ConfigSchema, ReportingConfigType }; | ||
|
||
export const config: PluginConfigDescriptor<ReportingConfigType> = { | ||
schema: ConfigSchema, | ||
}; |
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,41 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { schema, TypeOf } from '@osd/config-schema'; | ||
|
||
const OsdServerSchema = schema.object({ | ||
hostname: schema.maybe( | ||
schema.string({ | ||
validate(value) { | ||
if (value === '0') { | ||
return 'must not be "0" for the headless browser to correctly resolve the host'; | ||
} | ||
}, | ||
hostname: true, | ||
}) | ||
), | ||
port: schema.maybe(schema.number()), | ||
protocol: schema.maybe( | ||
schema.string({ | ||
validate(value) { | ||
if (!/^https?$/.test(value)) { | ||
return 'must be "http" or "https"'; | ||
} | ||
}, | ||
}) | ||
), | ||
}); // default values are all dynamic in createConfig$ | ||
|
||
export const ConfigSchema = schema.object({ | ||
osd_server: OsdServerSchema, | ||
}); | ||
|
||
export type ReportingConfigType = TypeOf<typeof ConfigSchema>; |
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
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
Oops, something went wrong.