Skip to content

Commit

Permalink
Merge branch 'space-aware-saved-objects' into space-aware-privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Jun 26, 2018
2 parents 1bc36c7 + c6e8925 commit 0544eb3
Show file tree
Hide file tree
Showing 60 changed files with 2,921 additions and 132 deletions.
52 changes: 26 additions & 26 deletions src/core_plugins/kibana/ui_setting_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function getUiSettingDefaults() {
name: 'Scaled date format',
type: 'json',
value:
`[
`[
["", "HH:mm:ss.SSS"],
["PT1S", "HH:mm:ss"],
["PT1M", "HH:mm"],
Expand Down Expand Up @@ -304,7 +304,7 @@ export function getUiSettingDefaults() {
'format:defaultTypeMap': {
name: 'Field type format name',
value:
`{
`{
"ip": { "id": "ip", "params": {} },
"date": { "id": "date", "params": {} },
"number": { "id": "number", "params": {} },
Expand Down Expand Up @@ -362,7 +362,7 @@ export function getUiSettingDefaults() {
'timepicker:timeDefaults': {
name: 'Time picker defaults',
value:
`{
`{
"from": "now-15m",
"to": "now",
"mode": "quick"
Expand All @@ -373,7 +373,7 @@ export function getUiSettingDefaults() {
'timepicker:refreshIntervalDefaults': {
name: 'Time picker refresh interval',
value:
`{
`{
"display": "Off",
"pause": false,
"value": 0
Expand All @@ -384,30 +384,30 @@ export function getUiSettingDefaults() {
'timepicker:quickRanges': {
name: 'Time picker quick ranges',
value: JSON.stringify([
{ from: 'now/d', to: 'now/d', display: 'Today', section: 0 },
{ from: 'now/w', to: 'now/w', display: 'This week', section: 0 },
{ from: 'now/M', to: 'now/M', display: 'This month', section: 0 },
{ from: 'now/y', to: 'now/y', display: 'This year', section: 0 },
{ from: 'now/d', to: 'now', display: 'Today so far', section: 0 },
{ from: 'now/w', to: 'now', display: 'Week to date', section: 0 },
{ from: 'now/M', to: 'now', display: 'Month to date', section: 0 },
{ from: 'now/y', to: 'now', display: 'Year to date', section: 0 },
{ from: 'now/d', to: 'now/d', display: 'Today', section: 0 },
{ from: 'now/w', to: 'now/w', display: 'This week', section: 0 },
{ from: 'now/M', to: 'now/M', display: 'This month', section: 0 },
{ from: 'now/y', to: 'now/y', display: 'This year', section: 0 },
{ from: 'now/d', to: 'now', display: 'Today so far', section: 0 },
{ from: 'now/w', to: 'now', display: 'Week to date', section: 0 },
{ from: 'now/M', to: 'now', display: 'Month to date', section: 0 },
{ from: 'now/y', to: 'now', display: 'Year to date', section: 0 },

{ from: 'now-15m', to: 'now', display: 'Last 15 minutes', section: 1 },
{ from: 'now-30m', to: 'now', display: 'Last 30 minutes', section: 1 },
{ from: 'now-1h', to: 'now', display: 'Last 1 hour', section: 1 },
{ from: 'now-4h', to: 'now', display: 'Last 4 hours', section: 1 },
{ from: 'now-12h', to: 'now', display: 'Last 12 hours', section: 1 },
{ from: 'now-24h', to: 'now', display: 'Last 24 hours', section: 1 },
{ from: 'now-7d', to: 'now', display: 'Last 7 days', section: 1 },
{ from: 'now-15m', to: 'now', display: 'Last 15 minutes', section: 1 },
{ from: 'now-30m', to: 'now', display: 'Last 30 minutes', section: 1 },
{ from: 'now-1h', to: 'now', display: 'Last 1 hour', section: 1 },
{ from: 'now-4h', to: 'now', display: 'Last 4 hours', section: 1 },
{ from: 'now-12h', to: 'now', display: 'Last 12 hours', section: 1 },
{ from: 'now-24h', to: 'now', display: 'Last 24 hours', section: 1 },
{ from: 'now-7d', to: 'now', display: 'Last 7 days', section: 1 },

{ from: 'now-30d', to: 'now', display: 'Last 30 days', section: 2 },
{ from: 'now-60d', to: 'now', display: 'Last 60 days', section: 2 },
{ from: 'now-90d', to: 'now', display: 'Last 90 days', section: 2 },
{ from: 'now-6M', to: 'now', display: 'Last 6 months', section: 2 },
{ from: 'now-1y', to: 'now', display: 'Last 1 year', section: 2 },
{ from: 'now-2y', to: 'now', display: 'Last 2 years', section: 2 },
{ from: 'now-5y', to: 'now', display: 'Last 5 years', section: 2 },
{ from: 'now-30d', to: 'now', display: 'Last 30 days', section: 2 },
{ from: 'now-60d', to: 'now', display: 'Last 60 days', section: 2 },
{ from: 'now-90d', to: 'now', display: 'Last 90 days', section: 2 },
{ from: 'now-6M', to: 'now', display: 'Last 6 months', section: 2 },
{ from: 'now-1y', to: 'now', display: 'Last 1 year', section: 2 },
{ from: 'now-2y', to: 'now', display: 'Last 2 years', section: 2 },
{ from: 'now-5y', to: 'now', display: 'Last 5 years', section: 2 },

], null, 2),
type: 'json',
Expand Down
59 changes: 47 additions & 12 deletions src/server/saved_objects/service/lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SavedObjectsRepository {
index,
mappings,
callCluster,
onBeforeWrite = () => {},
onBeforeWrite = () => { },
} = options;

this._index = index;
Expand All @@ -54,11 +54,13 @@ export class SavedObjectsRepository {
* @param {object} [options={}]
* @property {string} [options.id] - force id on creation, not recommended
* @property {boolean} [options.overwrite=false]
* @property {object} [options.extraBodyProperties={}] - extra properties to append to the document body, outside of the object's type property
* @returns {promise} - { id, type, version, attributes }
*/
async create(type, attributes = {}, options = {}) {
const {
id,
extraBodyProperties = {},
overwrite = false
} = options;

Expand All @@ -72,9 +74,10 @@ export class SavedObjectsRepository {
index: this._index,
refresh: 'wait_for',
body: {
...extraBodyProperties,
type,
updated_at: time,
[type]: attributes
[type]: attributes,
},
});

Expand All @@ -98,7 +101,7 @@ export class SavedObjectsRepository {
/**
* Creates multiple documents at once
*
* @param {array} objects - [{ type, id, attributes }]
* @param {array} objects - [{ type, id, attributes, extraBodyProperties }]
* @param {object} [options={}]
* @property {boolean} [options.overwrite=false] - overwrites existing documents
* @returns {promise} - [{ id, type, version, attributes, error: { message } }]
Expand All @@ -119,9 +122,10 @@ export class SavedObjectsRepository {
}
},
{
...object.extraBodyProperties,
type: object.type,
updated_at: time,
[object.type]: object.attributes
[object.type]: object.attributes,
}
];
};
Expand Down Expand Up @@ -207,6 +211,7 @@ export class SavedObjectsRepository {
* @property {string} [options.search]
* @property {Array<string>} [options.searchFields] - see Elasticsearch Simple Query String
* Query field argument for more information
* @property {object} [options.extraQueryParams] - ES Query parameters to merge/append into the generated query
* @property {integer} [options.page=1]
* @property {integer} [options.perPage=20]
* @property {string} [options.sortField]
Expand All @@ -224,6 +229,7 @@ export class SavedObjectsRepository {
sortField,
sortOrder,
fields,
extraQueryParams,
} = options;

if (searchFields && !Array.isArray(searchFields)) {
Expand All @@ -234,6 +240,10 @@ export class SavedObjectsRepository {
throw new TypeError('options.searchFields must be an array');
}

if (extraQueryParams && typeof extraQueryParams !== 'object') {
throw new TypeError('options.extraQueryParams must be an object');
}

const esOptions = {
index: this._index,
size: perPage,
Expand All @@ -247,7 +257,8 @@ export class SavedObjectsRepository {
searchFields,
type,
sortField,
sortOrder
sortOrder,
extraQueryParams
})
}
};
Expand Down Expand Up @@ -286,6 +297,8 @@ export class SavedObjectsRepository {
* Returns an array of objects by id
*
* @param {array} objects - an array ids, or an array of objects containing id and optionally type
* @param {object} [options = {}]
* @param {array} [options.extraSourceProperties = []] - an array of extra properties to return from the underlying document
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }] }
* @example
*
Expand All @@ -294,7 +307,7 @@ export class SavedObjectsRepository {
* { id: 'foo', type: 'index-pattern' }
* ])
*/
async bulkGet(objects = []) {
async bulkGet(objects = [], options = {}) {
if (objects.length === 0) {
return { saved_objects: [] };
}
Expand All @@ -309,8 +322,12 @@ export class SavedObjectsRepository {
}
});

const { docs } = response;

const { extraSourceProperties = [] } = options;

return {
saved_objects: response.docs.map((doc, i) => {
saved_objects: docs.map((doc, i) => {
const { id, type } = objects[i];

if (!doc.found) {
Expand All @@ -322,13 +339,20 @@ export class SavedObjectsRepository {
}

const time = doc._source.updated_at;
return {
const savedObject = {
id,
type,
...time && { updated_at: time },
version: doc._version,
attributes: doc._source[type]
...extraSourceProperties
.map(s => ({ [s]: doc._source[s] }))
.reduce((acc, prop) => ({ ...acc, ...prop }), {}),
attributes: {
...doc._source[type],
}
};

return savedObject;
})
};
}
Expand All @@ -338,9 +362,11 @@ export class SavedObjectsRepository {
*
* @param {string} type
* @param {string} id
* @param {object} [options = {}]
* @param {array} [options.extraSourceProperties = []] - an array of extra properties to return from the underlying document
* @returns {promise} - { id, type, version, attributes }
*/
async get(type, id) {
async get(type, id, options = {}) {
const response = await this._callCluster('get', {
id: this._generateEsId(type, id),
type: this._type,
Expand All @@ -355,14 +381,21 @@ export class SavedObjectsRepository {
throw errors.createGenericNotFoundError();
}

const { extraSourceProperties = [] } = options;

const { updated_at: updatedAt } = response._source;

return {
id,
type,
...updatedAt && { updated_at: updatedAt },
version: response._version,
attributes: response._source[type]
...extraSourceProperties
.map(s => ({ [s]: response._source[s] }))
.reduce((acc, prop) => ({ ...acc, ...prop }), {}),
attributes: {
...response._source[type],
}
};
}

Expand All @@ -373,6 +406,7 @@ export class SavedObjectsRepository {
* @param {string} id
* @param {object} [options={}]
* @property {integer} options.version - ensures version matches that of persisted object
* @param {array} [options.extraBodyProperties = []] - an array of extra properties to write into the underlying document
* @returns {promise}
*/
async update(type, id, attributes, options = {}) {
Expand All @@ -386,8 +420,9 @@ export class SavedObjectsRepository {
ignore: [404],
body: {
doc: {
...options.extraBodyProperties,
updated_at: time,
[type]: attributes
[type]: attributes,
}
},
});
Expand Down
Loading

0 comments on commit 0544eb3

Please sign in to comment.