Skip to content

Commit

Permalink
Initial commit. Still need to work on the requirement that there be N…
Browse files Browse the repository at this point in the history
…O race conditions where one process is trying to populate Redis from db while another process is trying to persist samples from Redis to db.
  • Loading branch information
iamigo committed Mar 3, 2017
1 parent e570d43 commit 15f7113
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 1 deletion.
12 changes: 11 additions & 1 deletion api/v1/apiErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ apiErrors.create({
});

apiErrors.create({
code: 11101,
code: 11104,
status: 400,
name: 'InvalidFilterParameterError',
parent: apiErrors.ValidationError,
Expand All @@ -81,6 +81,16 @@ apiErrors.create({
'an include filter or an exclude filter, but not the combination of both.',
});

apiErrors.create({
code: 11105,
status: 400,
name: 'InvalidSampleStoreState',
parent: apiErrors.ValidationError,
fields: [],
defaultMessage: 'You cannot rebuild the sample store if the ' +
'ENABLE_REDIS_SAMPLE_STORE feature is not enabled.',
});

// ----------------------------------------------------------------------------
// Not Found
// ----------------------------------------------------------------------------
Expand Down
62 changes: 62 additions & 0 deletions api/v1/controllers/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@


/**
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* api/v1/controllers/admin.js
*/
'use strict'; // eslint-disable-line strict

const featureToggles = require('feature-toggles');
const sampleStore = require('../../../cache/sampleStore');
const sampleStoreInit = require('../../../cache/sampleStoreInit');
const apiErrors = require('../apiErrors');
const httpStatus = require('../constants').httpStatus;
const authUtils = require('../helpers/authUtils');
const u = require('../helpers/verbs/utils');

module.exports = {

/**
* POST /admin/sampleStore/rebuild
*
* Rebuild the redis sampleStore from the samples in the database. Admin
* only.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
rebuildSampleStore(req, res, next) {
authUtils.isAdmin(req)
.then((ok) => {
if (ok) {
const enabled = featureToggles
.isFeatureEnabled(sampleStore.constants.featureName);
if (enabled) {
sampleStoreInit.eradicate()
.then(() => sampleStoreInit.populate())
.then(() => {
res.status(httpStatus.NO_CONTENT).json();
});
} else {
const err = new apiErrors.InvalidSampleStoreState({
explanation: 'You cannot rebuild the sample store if the ' +
'ENABLE_REDIS_SAMPLE_STORE feature is not enabled.',
});
next(err);
}
} else {
u.forbidden(next);
}
})
.catch(() => u.forbidden(next));
},

}; // exports
25 changes: 25 additions & 0 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ produces:
# =============================================================================
paths:

# ---------------------------------------------------------------------------
/admin/sampleStore/rebuild:
x-swagger-router-controller: admin
post:
security:
- jwt: []
summary: >-
Rebuild the redis sampleStore from the samples in the database (admin only)
tags: [ admin ]
description: >-
Rebuild the redis sampleStore from the samples in the database. Requires user to have an admin profile. If the Refocus configuration parameter `useAccessToken` is set to `true`, you must include an `Authorization` request header with your [JSON Web Token](https://tools.ietf.org/html/rfc7519) (JWT) as the value. You can get a token using `POST /v1/register` or `POST /v1/tokens`.
operationId: rebuildSampleStore
responses:
204:
description: >-
Success.
400:
$ref: "#/responses/400"
401:
$ref: "#/responses/401"
404:
$ref: "#/responses/404"
default:
$ref: "#/responses/genericError"

# ---------------------------------------------------------------------------
/aspects:
x-swagger-router-controller: aspects
Expand Down

0 comments on commit 15f7113

Please sign in to comment.