-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.4] [ftr/savedObjects] add simple saved object api client to… (#45941)
* [ftr/savedObjects] add simple saved object api client to ftr services * fix typo * use consistent spacing * fix types and TS-ify the rest of kibanaServer service * expose server urls with better API * tweak status api response types * fix http body param name * second arg to axios post is the post data * use standardized error handling behavior * Revert "use standardized error handling behavior" This reverts commit 7e9a7f8. * revert unnecessary changes
- Loading branch information
Spencer
authored
Sep 18, 2019
1 parent
3c92f98
commit d5af3d5
Showing
5 changed files
with
185 additions
and
13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import Url from 'url'; | ||
|
||
import Axios, { AxiosRequestConfig } from 'axios'; | ||
import { ToolingLog } from '@kbn/dev-utils'; | ||
|
||
const joinPath = (...components: Array<string | undefined>) => | ||
`/${components | ||
.filter((s): s is string => !!s) | ||
.map(c => encodeURIComponent(c)) | ||
.join('/')}`; | ||
|
||
type MigrationVersion = Record<string, string>; | ||
|
||
interface Reference { | ||
id: string; | ||
name: string; | ||
type: string; | ||
} | ||
|
||
interface SavedObjectResponse<Attributes extends Record<string, any>> { | ||
attributes: Attributes; | ||
id: string; | ||
migrationVersion?: MigrationVersion; | ||
references: Reference[]; | ||
type: string; | ||
updated_at?: string; | ||
version?: string; | ||
} | ||
|
||
interface GetOptions { | ||
type: string; | ||
id: string; | ||
} | ||
|
||
interface IndexOptions<Attributes> { | ||
type: string; | ||
attributes: Attributes; | ||
id?: string; | ||
overwrite?: boolean; | ||
migrationVersion?: MigrationVersion; | ||
references?: Reference[]; | ||
} | ||
|
||
interface UpdateOptions<Attributes> extends IndexOptions<Attributes> { | ||
id: string; | ||
} | ||
|
||
export class KibanaServerSavedObjects { | ||
private readonly x = Axios.create({ | ||
baseURL: Url.resolve(this.url, '/api/saved_objects/'), | ||
headers: { | ||
'kbn-xsrf': 'KibanaServerSavedObjects', | ||
}, | ||
}); | ||
|
||
constructor(private readonly url: string, private readonly log: ToolingLog) {} | ||
|
||
/** | ||
* Get an object | ||
*/ | ||
public async get<Attributes extends Record<string, any>>(options: GetOptions) { | ||
this.log.debug('Gettings saved object: %j', options); | ||
|
||
return await this.request<SavedObjectResponse<Attributes>>('get saved object', { | ||
url: joinPath(options.type, options.id), | ||
method: 'GET', | ||
}); | ||
} | ||
|
||
/** | ||
* Create a saved object | ||
*/ | ||
public async create<Attributes extends Record<string, any>>(options: IndexOptions<Attributes>) { | ||
this.log.debug('Creating saved object: %j', options); | ||
|
||
return await this.request<SavedObjectResponse<Attributes>>('update saved object', { | ||
url: joinPath(options.type, options.id), | ||
params: { | ||
overwrite: options.overwrite, | ||
}, | ||
method: 'POST', | ||
data: { | ||
attributes: options.attributes, | ||
migrationVersion: options.migrationVersion, | ||
references: options.references, | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Update a saved object | ||
*/ | ||
public async update<Attributes extends Record<string, any>>(options: UpdateOptions<Attributes>) { | ||
this.log.debug('Updating saved object: %j', options); | ||
|
||
return await this.request<SavedObjectResponse<Attributes>>('update saved object', { | ||
url: joinPath(options.type, options.id), | ||
params: { | ||
overwrite: options.overwrite, | ||
}, | ||
method: 'PUT', | ||
data: { | ||
attributes: options.attributes, | ||
migrationVersion: options.migrationVersion, | ||
references: options.references, | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Delete an object | ||
*/ | ||
public async delete(options: GetOptions) { | ||
this.log.debug('Deleting saved object %s/%s', options); | ||
|
||
return await this.request('delete saved object', { | ||
url: joinPath(options.type, options.id), | ||
method: 'DELETE', | ||
}); | ||
} | ||
|
||
private async request<T>(desc: string, options: AxiosRequestConfig) { | ||
try { | ||
const resp = await this.x.request<T>(options); | ||
return resp.data; | ||
} catch (error) { | ||
if (error.response) { | ||
throw new Error(`Failed to ${desc}:\n${JSON.stringify(error.response.data, null, 2)}`); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
} |