Skip to content

Commit

Permalink
Search use actual es endpoint in guides (#174518)
Browse files Browse the repository at this point in the history
## Summary

Use the actual configured Elasticsearch endpoint in the search plugin,
outside of cloud where we still use the cloud configured endpoint.
  • Loading branch information
sphilipse authored Jan 10, 2024
1 parent 1470a36 commit 664c1a0
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const mockKibanaValues = {
},
config: { host: 'http://localhost:3002' },
data: dataPluginMock.createStartContract(),
esConfig: { elasticsearch_host: 'https://your_deployment_url' },
guidedOnboarding: {},
history: mockHistory,
isCloud: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ import { EuiHorizontalRule, EuiSpacer, EuiText, EuiTitle } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import { LanguageDefinitionSnippetArguments } from '@kbn/search-api-panels';
import { ELASTICSEARCH_URL_PLACEHOLDER } from '@kbn/search-api-panels/constants';

import { FetchApiKeysAPILogic } from '../../../enterprise_search_overview/api/fetch_api_keys_logic';
import { CreateApiKeyFlyout } from '../../../shared/api_key/create_api_key_flyout';
import { useCloudDetails } from '../../../shared/cloud_details/cloud_details';
import { GettingStarted } from '../../../shared/getting_started/getting_started';
import { KibanaLogic } from '../../../shared/kibana';
import { EnterpriseSearchElasticsearchPageTemplate } from '../layout';

export const ElasticsearchGuide = () => {
const cloudContext = useCloudDetails();
const [isFlyoutOpen, setIsFlyoutOpen] = useState(false);
const { esConfig } = useValues(KibanaLogic);

const codeArgs: LanguageDefinitionSnippetArguments = {
apiKey: '',
cloudId: cloudContext.cloudId,
url: cloudContext.elasticsearchUrl || ELASTICSEARCH_URL_PLACEHOLDER,
url: esConfig.elasticsearch_host,
};
const { makeRequest } = useActions(FetchApiKeysAPILogic);
const { data } = useValues(FetchApiKeysAPILogic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Router } from '@kbn/shared-ux-router';

import { DEFAULT_PRODUCT_FEATURES } from '../../common/constants';
import { ClientConfigType, InitialAppData, ProductAccess } from '../../common/types';
import { PluginsStart, ClientData } from '../plugin';
import { PluginsStart, ClientData, ESConfig } from '../plugin';

import { externalUrl } from './shared/enterprise_search_url';
import { mountFlashMessagesLogic, Toasts } from './shared/flash_messages';
Expand Down Expand Up @@ -50,7 +50,7 @@ export const renderApp = (
params: AppMountParameters;
plugins: PluginsStart;
},
{ config, data }: { config: ClientConfigType; data: ClientData }
{ config, data, esConfig }: { config: ClientConfigType; data: ClientData; esConfig: ESConfig }
) => {
const {
access,
Expand Down Expand Up @@ -106,6 +106,7 @@ export const renderApp = (
charts,
cloud,
config,
esConfig,
data: plugins.data,
guidedOnboarding,
history,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {

import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { ELASTICSEARCH_URL_PLACEHOLDER } from '@kbn/search-api-panels/constants';

import { FetchApiKeysAPILogic } from '../../enterprise_search_overview/api/fetch_api_keys_logic';
import { KibanaLogic } from '../kibana';
Expand All @@ -37,16 +36,16 @@ const COPIED_LABEL = i18n.translate('xpack.enterpriseSearch.overview.apiKey.copi
});

export const ApiKeyPanel: React.FC = () => {
const { cloud, navigateToUrl } = useValues(KibanaLogic);
const { cloud, esConfig, navigateToUrl } = useValues(KibanaLogic);
const { makeRequest } = useActions(FetchApiKeysAPILogic);
const { data } = useValues(FetchApiKeysAPILogic);
const [isFlyoutOpen, setIsFlyoutOpen] = useState(false);
const elasticsearchEndpoint = esConfig.elasticsearch_host;

useEffect(() => makeRequest({}), []);

const apiKeys = data?.api_keys || [];
const cloudId = cloud?.cloudId;
const elasticsearchEndpoint = cloud?.elasticsearchUrl || ELASTICSEARCH_URL_PLACEHOLDER;

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
*/

export const API_KEY_PLACEHOLDER = 'your_api_key';
export const ELASTICSEARCH_URL_PLACEHOLDER = 'https://your_deployment_url';
export const INDEX_NAME_PLACEHOLDER = 'index_name';
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import {
IUiSettingsClient,
} from '@kbn/core/public';
import { DataPublicPluginStart } from '@kbn/data-plugin/public';

import { GuidedOnboardingPluginStart } from '@kbn/guided-onboarding-plugin/public';
import { LensPublicStart } from '@kbn/lens-plugin/public';
import { MlPluginStart } from '@kbn/ml-plugin/public';
import { ELASTICSEARCH_URL_PLACEHOLDER } from '@kbn/search-api-panels/constants';
import { AuthenticatedUser, SecurityPluginStart } from '@kbn/security-plugin/public';
import { SharePluginStart } from '@kbn/share-plugin/public';

import { ClientConfigType, ProductAccess, ProductFeatures } from '../../../../common/types';
import { ESConfig } from '../../../plugin';

import { HttpLogic } from '../http';
import { createHref, CreateHrefOptions } from '../react_router_helpers';
Expand All @@ -40,6 +43,7 @@ export interface KibanaLogicProps {
cloud?: CloudSetup;
config: ClientConfigType;
data: DataPublicPluginStart;
esConfig: ESConfig;
guidedOnboarding?: GuidedOnboardingPluginStart;
history: ScopedHistory;
isSidebarEnabled: boolean;
Expand Down Expand Up @@ -75,6 +79,7 @@ export const KibanaLogic = kea<MakeLogicType<KibanaValues>>({
cloud: [props.cloud || {}, {}],
config: [props.config || {}, {}],
data: [props.data, {}],
esConfig: [props.esConfig || { elasticsearch_host: ELASTICSEARCH_URL_PLACEHOLDER }, {}],
guidedOnboarding: [props.guidedOnboarding, {}],
history: [props.history, {}],
isSidebarEnabled: [props.isSidebarEnabled, {}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {

import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { ELASTICSEARCH_URL_PLACEHOLDER } from '@kbn/search-api-panels/constants';

import { FetchApiKeysAPILogic } from '../../enterprise_search_overview/api/fetch_api_keys_logic';
import { CreateApiKeyFlyout } from '../api_key/create_api_key_flyout';
Expand All @@ -43,7 +42,7 @@ import { EndpointIcon } from './endpoint_icon';

export const EndpointsHeaderAction: React.FC = ({ children }) => {
const [isPopoverOpen, setPopoverOpen] = useState(false);
const { cloud, navigateToUrl } = useValues(KibanaLogic);
const { cloud, esConfig, navigateToUrl } = useValues(KibanaLogic);
const { makeRequest } = useActions(FetchApiKeysAPILogic);
const { data } = useValues(FetchApiKeysAPILogic);
const [isFlyoutOpen, setIsFlyoutOpen] = useState(false);
Expand All @@ -57,7 +56,7 @@ export const EndpointsHeaderAction: React.FC = ({ children }) => {

const apiKeys = data?.api_keys || [];
const cloudId = cloud?.cloudId;
const elasticsearchEndpoint = cloud?.elasticsearchUrl || ELASTICSEARCH_URL_PLACEHOLDER;
const elasticsearchEndpoint = esConfig.elasticsearch_host;

const button = (
<EuiButtonEmpty iconType={EndpointIcon} size="s" onClick={() => setPopoverOpen(!isPopoverOpen)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const mockKibanaProps: KibanaLogicProps = {
},
},
data: dataPluginMock.createStartContract(),
esConfig: {
elasticsearch_host: 'https://your_deployment_url',
},
guidedOnboarding: {},
history: mockHistory,
isSidebarEnabled: true,
Expand Down
22 changes: 20 additions & 2 deletions x-pack/plugins/enterprise_search/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { HomePublicPluginSetup } from '@kbn/home-plugin/public';
import { LensPublicStart } from '@kbn/lens-plugin/public';
import { LicensingPluginStart } from '@kbn/licensing-plugin/public';
import { MlPluginStart } from '@kbn/ml-plugin/public';
import { ELASTICSEARCH_URL_PLACEHOLDER } from '@kbn/search-api-panels/constants';
import { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/public';
import { SharePluginStart } from '@kbn/share-plugin/public';

Expand Down Expand Up @@ -69,17 +70,29 @@ export interface PluginsStart {
ml: MlPluginStart;
}

export interface ESConfig {
elasticsearch_host: string;
}

export class EnterpriseSearchPlugin implements Plugin {
private config: ClientConfigType;
private esConfig: ESConfig;

constructor(initializerContext: PluginInitializerContext) {
this.config = initializerContext.config.get<ClientConfigType>();
this.esConfig = { elasticsearch_host: ELASTICSEARCH_URL_PLACEHOLDER };
}

private data: ClientData = {} as ClientData;

private async getInitialData(http: HttpSetup) {
if (!this.config.host && this.config.canDeployEntSearch) return; // No API to call
try {
this.esConfig = await http.get('/internal/enterprise_search/es_config');
} catch {
this.esConfig = { elasticsearch_host: ELASTICSEARCH_URL_PLACEHOLDER };
}

if (!this.config.host) return; // No API to call
if (this.hasInitialized) return; // We've already made an initial call

try {
Expand Down Expand Up @@ -113,7 +126,12 @@ export class EnterpriseSearchPlugin implements Plugin {

private getPluginData() {
// Small helper for grouping plugin data related args together
return { config: this.config, data: this.data, isSidebarEnabled: this.isSidebarEnabled };
return {
config: this.config,
data: this.data,
esConfig: this.esConfig,
isSidebarEnabled: this.isSidebarEnabled,
};
}

private hasInitialized: boolean = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { loggingSystemMock } from '@kbn/core/server/mocks';
import { mlPluginServerMock } from '@kbn/ml-plugin/server/mocks';

import { ConfigType } from '..';
import { GlobalConfigService } from '../services/global_config_service';

export const mockLogger = loggingSystemMock.createLogger().get();

Expand All @@ -36,6 +37,7 @@ export const mockConfig = {
export const mockDependencies = {
// Mock router should be handled on a per-test basis
config: mockConfig,
globalConfigService: new GlobalConfigService(),
log: mockLogger,
enterpriseSearchRequestHandler: mockRequestHandler as any,
ml: mockMl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { spacesMock } from '@kbn/spaces-plugin/server/mocks';

import { GlobalConfigService } from '../services/global_config_service';

import { checkAccess } from './check_access';

jest.mock('./enterprise_search_config_api', () => ({
Expand Down Expand Up @@ -51,6 +53,7 @@ describe('checkAccess', () => {
canDeployEntSearch: true,
host: 'http://localhost:3002',
},
globalConfigService: new GlobalConfigService(),
security: mockSecurity,
spaces: mockSpaces,
} as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jest.mock('@kbn/repo-info', () => ({

import { loggingSystemMock } from '@kbn/core-logging-server-mocks';

import { GlobalConfigService } from '../services/global_config_service';

import {
callEnterpriseSearchConfigAPI,
warnMismatchedVersions,
Expand All @@ -37,6 +39,7 @@ describe('callEnterpriseSearchConfigAPI', () => {
};
const mockDependencies = {
config: mockConfig,
globalConfigService: new GlobalConfigService(),
request: mockRequest,
log: loggingSystemMock.create().get(),
} as any;
Expand Down
34 changes: 29 additions & 5 deletions x-pack/plugins/enterprise_search/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { appSearchTelemetryType } from './saved_objects/app_search/telemetry';
import { enterpriseSearchTelemetryType } from './saved_objects/enterprise_search/telemetry';
import { workplaceSearchTelemetryType } from './saved_objects/workplace_search/telemetry';

import { GlobalConfigService } from './services/global_config_service';
import { uiSettings as enterpriseSearchUISettings } from './ui_settings';

import { getSearchResultProvider } from './utils/search_result_provider';
Expand Down Expand Up @@ -104,9 +105,8 @@ export interface PluginsStart {
export interface RouteDependencies {
config: ConfigType;
enterpriseSearchRequestHandler: IEnterpriseSearchRequestHandler;

getSavedObjectsService?(): SavedObjectsServiceStart;

globalConfigService: GlobalConfigService;
log: Logger;
ml?: MlPluginSetup;
router: IRouter;
Expand All @@ -115,18 +115,27 @@ export interface RouteDependencies {
export class EnterpriseSearchPlugin implements Plugin {
private readonly config: ConfigType;
private readonly logger: Logger;
private readonly globalConfigService: GlobalConfigService;

/**
* Exposed services
*/

constructor(initializerContext: PluginInitializerContext) {
this.config = initializerContext.config.get<ConfigType>();
this.globalConfigService = new GlobalConfigService();
this.logger = initializerContext.logger.get();
}

public setup(
{ capabilities, http, savedObjects, getStartServices, uiSettings }: CoreSetup<PluginsStart>,
{
capabilities,
elasticsearch,
http,
savedObjects,
getStartServices,
uiSettings,
}: CoreSetup<PluginsStart>,
{
usageCollection,
security,
Expand All @@ -139,6 +148,7 @@ export class EnterpriseSearchPlugin implements Plugin {
cloud,
}: PluginsSetup
) {
this.globalConfigService.setup(elasticsearch.legacy.config$, cloud);
const config = this.config;
const log = this.logger;
const PLUGIN_IDS = [
Expand Down Expand Up @@ -185,7 +195,14 @@ export class EnterpriseSearchPlugin implements Plugin {
async (request: KibanaRequest) => {
const [, { spaces }] = await getStartServices();

const dependencies = { config, security, spaces, request, log, ml };
const dependencies = {
config,
security,
spaces,
request,
log,
ml,
};

const { hasAppSearchAccess, hasWorkplaceSearchAccess } = await checkAccess(dependencies);
const showEnterpriseSearch =
Expand Down Expand Up @@ -228,7 +245,14 @@ export class EnterpriseSearchPlugin implements Plugin {
*/
const router = http.createRouter();
const enterpriseSearchRequestHandler = new EnterpriseSearchRequestHandler({ config, log });
const dependencies = { router, config, log, enterpriseSearchRequestHandler, ml };
const dependencies = {
router,
config,
globalConfigService: this.globalConfigService,
log,
enterpriseSearchRequestHandler,
ml,
};

registerConfigDataRoute(dependencies);
if (config.canDeployEntSearch) registerAppSearchRoutes(dependencies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ const errorMessage = i18n.translate(
}
);

export function registerConfigDataRoute({ router, config, log }: RouteDependencies) {
export function registerConfigDataRoute({
router,
config,
log,
globalConfigService,
}: RouteDependencies) {
router.get(
{
path: '/internal/enterprise_search/config_data',
Expand All @@ -45,4 +50,17 @@ export function registerConfigDataRoute({ router, config, log }: RouteDependenci
}
})
);

router.get(
{
path: '/internal/enterprise_search/es_config',
validate: false,
},
elasticsearchErrorHandler(log, async (context, request, response) => {
return response.ok({
body: { elasticsearch_host: globalConfigService.elasticsearchUrl },
headers: { 'content-type': 'application/json' },
});
})
);
}
Loading

0 comments on commit 664c1a0

Please sign in to comment.