Skip to content

Commit

Permalink
Merge pull request #4 from scality/config-runtime
Browse files Browse the repository at this point in the history
ZENKO-2519 Customizable configuration at runtime
  • Loading branch information
nicolas2bert authored Apr 9, 2020
2 parents ec3a362 + 36e273f commit 526949c
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 58 deletions.
3 changes: 2 additions & 1 deletion public/assets/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"api_endpoint": "http://127.0.0.1:5000"
"apiEndpoint": "http://127.0.0.1:5000",
"instanceId": "c4a88541-2a25-4896-a1f4-65dd7e88fab3"
}
14 changes: 9 additions & 5 deletions src/js/pensieveClient.js → src/js/apiClient.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import Swagger from 'swagger-client';

function makePensieveClient(apiEndpoint, instanceId){
function makeApiClient(apiEndpoint, instanceId){
// NOTE: This is not production-ready.
// It implements an authentication call based on a hardcoded OIDC token and an instance ID set in the `config.json` file.
// This call returns a JWT token which allows the user to access "pensieve-api" resources that are permitted with that token.
const request = {
url: `${apiEndpoint}/api/v1/management/${instanceId}/token`,
method: 'GET',
headers: { 'X-Management-Authentication-Token': 'coco' },
headers: { 'X-Management-Authentication-Token': 'oidc.token' },
};

// TODO: use refreshToken API
return Swagger.http(request)
.then((res) => {
return Swagger(apiEndpoint + '/swagger.json',
{ authorizations: { 'public-api': res.body.token } });
})
.then(client => {
client.spec.schemes = [apiEndpoint.split(':')[0]];
const pensieveClient = client.apis['ui-facing'];
return pensieveClient;
const apiClient = client.apis['ui-facing'];
return apiClient;
})
.catch(error => {
throw new Error(
`Unable to fetch OpenAPI descriptor: ${error.message || '(unknown reason)'}`);
});
}

export default makePensieveClient;
export default makeApiClient;
16 changes: 8 additions & 8 deletions src/react/Routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class Routes extends React.Component{
// link: <Link to="/groups">Groups</Link>,
// selected: isSelected(location, '/groups'),
// },
{
link: <Link to="/users">Users</Link>,
selected: isSelected(location, '/users'),
},
{
link: <Link to="/databrowser">Data Browser</Link>,
selected: isSelected(location, '/databrowser'),
},
// {
// link: <Link to="/users">Users</Link>,
// selected: isSelected(location, '/users'),
// },
// {
// link: <Link to="/databrowser">Data Browser</Link>,
// selected: isSelected(location, '/databrowser'),
// },
// {
// link: <Link to="/workflow">Data Workflow</Link>,
// selected: isSelected(location, '/workflow'),
Expand Down
48 changes: 18 additions & 30 deletions src/react/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,42 @@
import { handleApiError, handleClientError, listBuckets, listUsers, loadInstanceLatestStatus, loadInstanceStats} from './';
import IAMClient from '../../js/IAMClient';
import S3Client from '../../js/S3Client';
import creds from '../../../creds';
import makePensieveClient from '../../js/pensieveClient';
import { handleApiError, handleClientError, loadInstanceLatestStatus, loadInstanceStats} from './';
import makeApiClient from '../../js/apiClient';

const apiEndpoint = 'http://127.0.0.1:5000';

export function login(instanceId, clients) {
export function login(instanceId, apiClient) {
return {
type: 'LOG_IN',
instanceId,
clients,
apiClient,
};
}

function getAuth() {
return new Promise((resolve) => {
return resolve({
instanceId: creds.instanceId,
oidcToken: 'oidc',
function getConfig() {
return fetch('/config.json', { credentials: 'same-origin' })
.then(response => response.json())
// TODO: validate configuration file
.then((jsonResp) => {
return {
instanceId: jsonResp.instanceId,
apiEndpoint: jsonResp.apiEndpoint,
oidcToken: 'oidc',
};
});
});
}

export function loadCredentials() {
return dispatch => {
return getAuth()
return getConfig()
.then((resp) => {
return Promise.all([
resp.instanceId,
// TODO: use oidc token
makePensieveClient(apiEndpoint, resp.instanceId),
new IAMClient({
accessKey: creds.accessKey,
secretKey: creds.secretKey,
}),
new S3Client({
// MADEUP KEYS
accessKey: '82XRRF5KN3XBPOSXLVAB',
secretKey: 'PCJukX09Vk2D/LMdxnp4enETgaJuIIc2BC3T6CxV',
}),
makeApiClient(resp.apiEndpoint, resp.instanceId),
]);
})
.then(([instanceId, pensieveClient, iamClient, s3Client]) => {
dispatch(login(instanceId, {pensieveClient, iamClient, s3Client}));
.then(([instanceId, apiClient]) => {
dispatch(login(instanceId, apiClient));
return Promise.all([
dispatch(loadInstanceLatestStatus()),
dispatch(loadInstanceStats()),
dispatch(listBuckets()),
dispatch(listUsers()),
]);
})
.then(() => {})
Expand Down
5 changes: 2 additions & 3 deletions src/react/actions/configuration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @noflow
import { networkEnd, networkStart } from './network';
import type { ConfigurationOverlay } from '../../types/config';
import creds from '../../../creds';
import { getClients } from '../utils/actions';

export function newConfiguration(configuration: ConfigurationOverlay) {
Expand All @@ -13,8 +12,8 @@ export function newConfiguration(configuration: ConfigurationOverlay) {

export function updateConfiguration() {
return async (dispatch, getState) => {
const { pensieveClient, instanceId } = getClients(getState());
return pensieveClient.getConfigurationOverlayView({ uuid: instanceId })
const { apiClient, instanceId } = getClients(getState());
return apiClient.getConfigurationOverlayView({ uuid: instanceId })
.then(res => {
dispatch(newConfiguration(res.body));
})
Expand Down
10 changes: 5 additions & 5 deletions src/react/actions/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function closeLocationDeleteDialog() {

export function saveLocation(location: Location): ThunkStatePromisedAction {
return (dispatch, getState) => {
const { pensieveClient, instanceId } = getClients(getState());
const { apiClient, instanceId } = getClients(getState());
const params = {
uuid: instanceId,
location,
Expand All @@ -42,9 +42,9 @@ export function saveLocation(location: Location): ThunkStatePromisedAction {

dispatch(networkStart('Saving Location'));
const op = location.objectId ?
pensieveClient.updateConfigurationOverlayLocation(params)
apiClient.updateConfigurationOverlayLocation(params)
:
pensieveClient.createConfigurationOverlayLocation(params);
apiClient.createConfigurationOverlayLocation(params);
return op.then(() => {
batch(() => {
dispatch(updateConfiguration());
Expand All @@ -58,15 +58,15 @@ export function saveLocation(location: Location): ThunkStatePromisedAction {

export function deleteLocation(locationName: LocationName): ThunkStatePromisedAction {
return (dispatch, getState) => {
const { pensieveClient, instanceId } = getClients(getState());
const { apiClient, instanceId } = getClients(getState());
const params = {
uuid: instanceId,
locationName,
};

dispatch(resetSelectLocation());
dispatch(networkStart('Deleting Location'));
return pensieveClient.deleteConfigurationOverlayLocation(params)
return apiClient.deleteConfigurationOverlayLocation(params)
.then(() => {
dispatch(updateConfiguration());
dispatch(closeLocationDeleteDialog());
Expand Down
8 changes: 4 additions & 4 deletions src/react/actions/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function receiveInstanceStats(stats) {

export function loadInstanceStats(){
return (dispatch, getState) => {
const { pensieveClient, instanceId } = getClients(getState());
return pensieveClient.getInstanceStats({ uuid: instanceId})
const { apiClient, instanceId } = getClients(getState());
return apiClient.getInstanceStats({ uuid: instanceId})
.then(res => {
dispatch(receiveInstanceStats(res.body));
})
Expand All @@ -31,8 +31,8 @@ export function loadInstanceStats(){

export function loadInstanceLatestStatus(){
return (dispatch, getState) => {
const { pensieveClient, instanceId } = getClients(getState());
return pensieveClient.getLatestInstanceStatus({ uuid: instanceId})
const { apiClient, instanceId } = getClients(getState());
return apiClient.getLatestInstanceStatus({ uuid: instanceId})
.then(res => {
dispatch(instanceStatus(res.body));
})
Expand Down
2 changes: 1 addition & 1 deletion src/react/reducers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function auth(state = {}, action) {
case 'LOG_IN':
return {
...state,
clients: action.clients,
apiClient: action.apiClient,
};
default:
return state;
Expand Down
2 changes: 1 addition & 1 deletion src/react/utils/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Map } from 'immutable';

export function getClients(state) {
return {
...state.auth.clients,
apiClient: state.auth.apiClient,
instanceId: state.instances.selectedId,
};
}
Expand Down

0 comments on commit 526949c

Please sign in to comment.