Skip to content

Commit

Permalink
Merge pull request #127 from splunk/v2-endpoint
Browse files Browse the repository at this point in the history
Add automatic support for V2 search endpoints
  • Loading branch information
JasonConger authored Oct 2, 2024
2 parents 204d8cd + ae7ef12 commit 0ae5673
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
36 changes: 33 additions & 3 deletions out/searchProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
const vscode = require("vscode");
const axios = require("axios");
const { getClient } = require('./notebooks/splunk');

class SearchProvider {
constructor() {
Expand Down Expand Up @@ -32,10 +33,15 @@ class SearchProvider {
}

let searchResults = "No results";

const exportUrl = await shouldUseDeprecatedSearchAPIs()
? `${this.splunkUrl}/services/search/jobs/export?output_mode=${this.outputMode}`
: `${this.splunkUrl}/services/search/v2/jobs/export?output_mode=${this.outputMode}`;

await axios(
{
method: "POST",
url: `${this.splunkUrl}/services/search/jobs/export?output_mode=${this.outputMode}`,
url: exportUrl,
data: "search=" + encodeURIComponent(`search ${search}`)
})
.then(response => {
Expand Down Expand Up @@ -117,10 +123,15 @@ class SavedSearchProvider {
}

let searchResults = "No results";

const exportUrl = await shouldUseDeprecatedSearchAPIs()
? `${this.splunkUrl}/servicesNS/${savedSearchItem.owner}/${savedSearchItem.app}/search/jobs/export?output_mode=${this.outputMode}`
: `${this.splunkUrl}/servicesNS/${savedSearchItem.owner}/${savedSearchItem.app}/search/v2/jobs/export?output_mode=${this.outputMode}`;

await axios(
{
method: "POST",
url: `${this.splunkUrl}/servicesNS/${savedSearchItem.owner}/${savedSearchItem.app}/search/jobs/export?output_mode=${this.outputMode}`,
url: exportUrl,
data: "search=" + encodeURIComponent(`| savedsearch "${savedSearchItem.label}"`)
})
.then(response => {
Expand All @@ -147,4 +158,23 @@ class SavedSearch extends vscode.TreeItem {
this.contextValue = 'savedSearch';
}
}
exports.SavedSearch = SavedSearch;
exports.SavedSearch = SavedSearch;

let cachedDisableV2SearchApi = null; // avoid multiple calls
async function shouldUseDeprecatedSearchAPIs() {
if (cachedDisableV2SearchApi !== null) {
console.log(`shouldUseDeprecatedSearchAPIs found cachedDisableV2SearchApi=${cachedDisableV2SearchApi}`);
return cachedDisableV2SearchApi;
}
// retrieve Splunk version and deployment info for disableV2SearchApi()
const service = getClient();
try {
await service.getInfo();
} catch (error) {
console.warn("error retrieving Splunk version with service.getInfo():");
console.warn(error);
};
cachedDisableV2SearchApi = service.disableV2SearchApi();
console.log(`shouldUseDeprecatedSearchAPIs service.disableV2SearchApi()=${cachedDisableV2SearchApi}`);
return cachedDisableV2SearchApi;
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@
"querystring-es3": "^0.2.1",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"splunk-sdk": "^1.12.1",
"splunk-sdk": "^2.0.0",
"styled-components": "^5.1.1",
"tar-fs": "^2.1.1",
"ts-loader": "^9.4.2",
Expand Down

0 comments on commit 0ae5673

Please sign in to comment.