Skip to content

Commit

Permalink
Fix API links when generating API key snippet (#155435)
Browse files Browse the repository at this point in the history
Fixes the Search Applications API page to set an URL to the ES plugin
rather than Enterprise Search URL.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
kderusso and kibanamachine authored Apr 24, 2023
1 parent 2c14b58 commit 3d78370
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ import { useValues } from 'kea';
import { EuiCodeBlock, EuiSpacer, EuiText, EuiTabs, EuiTab } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { getEnterpriseSearchUrl } from '../../../../shared/enterprise_search_url';

import { useCloudDetails } from '../../../../shared/cloud_details/cloud_details';
import { EngineViewLogic } from '../engine_view_logic';

import { EngineApiLogic } from './engine_api_logic';

const SearchUISnippet = (enterpriseSearchUrl: string, engineName: string, apiKey: string) => `
import { elasticsearchUrl } from './search_application_api';

const SearchUISnippet = (esUrl: string, engineName: string, apiKey: string) => `6
import EnginesAPIConnector from "@elastic/search-ui-engines-connector";
const connector = new EnginesAPIConnector({
host: "${enterpriseSearchUrl}",
host: "${esUrl}",
engineName: "${engineName}",
apiKey: "${apiKey || '<YOUR_API_KEY>'}"
});`;

const cURLSnippet = (enterpriseSearchUrl: string, engineName: string, apiKey: string) => `
curl --location --request GET '${enterpriseSearchUrl}/api/engines/${engineName}/_search' \\
const cURLSnippet = (esUrl: string, engineName: string, apiKey: string) => `
curl --location --request GET '${esUrl}/${engineName}/_search' \\
--header 'Authorization: apiKey ${apiKey || '<YOUR_API_KEY>'}' \\
--header 'Content-Type: application/json' \\
--data-raw '{
Expand All @@ -47,19 +48,19 @@ interface Tab {
export const EngineApiIntegrationStage: React.FC = () => {
const [selectedTab, setSelectedTab] = React.useState<TabId>('curl');
const { engineName } = useValues(EngineViewLogic);
const enterpriseSearchUrl = getEnterpriseSearchUrl();
const { apiKey } = useValues(EngineApiLogic);
const cloudContext = useCloudDetails();

const Tabs: Record<TabId, Tab> = {
curl: {
code: cURLSnippet(enterpriseSearchUrl, engineName, apiKey),
code: cURLSnippet(elasticsearchUrl(cloudContext), engineName, apiKey),
language: 'bash',
title: i18n.translate('xpack.enterpriseSearch.content.engine.api.step3.curlTitle', {
defaultMessage: 'cURL',
}),
},
searchui: {
code: SearchUISnippet(enterpriseSearchUrl, engineName, apiKey),
code: SearchUISnippet(elasticsearchUrl(cloudContext), engineName, apiKey),
language: 'javascript',
title: i18n.translate('xpack.enterpriseSearch.content.engine.api.step3.searchUITitle', {
defaultMessage: 'Search UI',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import { i18n } from '@kbn/i18n';

import { ANALYTICS_PLUGIN } from '../../../../../../common/constants';
import { COLLECTION_INTEGRATE_PATH } from '../../../../analytics/routes';
import { CloudDetails, useCloudDetails } from '../../../../shared/cloud_details/cloud_details';
import { decodeCloudId } from '../../../../shared/decode_cloud_id/decode_cloud_id';
import { docLinks } from '../../../../shared/doc_links';
import { generateEncodedPath } from '../../../../shared/encode_path_params';
import { getEnterpriseSearchUrl } from '../../../../shared/enterprise_search_url';
import { KibanaLogic } from '../../../../shared/kibana';

import { EngineViewLogic } from '../engine_view_logic';
Expand All @@ -34,12 +35,19 @@ import { EngineApiIntegrationStage } from './engine_api_integration';
import { EngineApiLogic } from './engine_api_logic';
import { GenerateEngineApiKeyModal } from './generate_engine_api_key_modal/generate_engine_api_key_modal';

export const elasticsearchUrl = (cloudContext: CloudDetails): string => {
const defaultUrl = 'https://localhost:9200';
const url =
(cloudContext.cloudId && decodeCloudId(cloudContext.cloudId)?.elasticsearchUrl) || defaultUrl;
return url;
};

export const SearchApplicationAPI = () => {
const { engineName } = useValues(EngineViewLogic);
const { isGenerateModalOpen } = useValues(EngineApiLogic);
const { openGenerateModal, closeGenerateModal } = useActions(EngineApiLogic);
const enterpriseSearchUrl = getEnterpriseSearchUrl();
const { navigateToUrl } = useValues(KibanaLogic);
const cloudContext = useCloudDetails();

const steps = [
{
Expand Down Expand Up @@ -132,7 +140,7 @@ export const SearchApplicationAPI = () => {
<EuiFlexGroup>
<EuiFlexItem grow>
<EuiCodeBlock language="markup" fontSize="m" paddingSize="m" isCopyable>
{enterpriseSearchUrl}
{elasticsearchUrl(cloudContext)}
</EuiCodeBlock>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down

0 comments on commit 3d78370

Please sign in to comment.