Skip to content

Commit

Permalink
feat(mirage): improve discovery route response and fix FormData handler
Browse files Browse the repository at this point in the history
Signed-off-by: Thuan Vo <thvo@redhat.com>
  • Loading branch information
Thuan Vo committed Aug 22, 2023
1 parent 6db0bed commit 27765b0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/mirage/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const targetFactory: FactoryDefinition<any> = Factory.extend({
jvmId: '1234',
annotations: {
platform: { 'io.cryostat.demo': 'this-is-not-real' },
cryostat: { hello: 'world' },
cryostat: { hello: 'world', REALM: 'KubernetesApi' },
},
});

Expand Down
79 changes: 51 additions & 28 deletions src/mirage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ export const startMirage = ({ environment = 'development' } = {}) => {
() => new Response(400, {}, 'Resource downloads are not supported in this demo')
);
this.post('api/v2/targets', (schema, request) => {
const attrs = JSON.parse(request.requestBody);
const attrs = request.requestBody as any;
const target = schema.create(Resource.TARGET, {
jvmId: `${Math.floor(1000 * Math.random())}`,
jvmId: `${Date.now().toString(16)}`,
alias: attrs.get('alias'),
connectUrl: attrs.get('connectUrl'),
annotations: {
platform: {},
cryostat: {},
cryostat: {
REALM: 'Custom Targets',
},
},
});
websocket.send(
Expand All @@ -126,31 +128,50 @@ export const startMirage = ({ environment = 'development' } = {}) => {
};
});
this.get('api/v1/targets', (schema) => schema.all(Resource.TARGET).models);
this.get('api/v2.1/discovery', (schema) => ({
meta: {
status: 'OK',
type: 'application/json',
},
data: {
result: {
name: 'Universe',
nodeType: 'Universe',
labels: {},
children: [
{
name: 'KubernetesApi',
nodeType: 'Realm',
labels: {},
children: schema.all(Resource.TARGET).models.map((t) => ({
name: t.alias,
nodeType: 'JVM',
target: t,
})),
},
],
this.get('api/v2.1/discovery', (schema) => {
const models = schema.all(Resource.TARGET).models;
const ct = models.filter((t) => t.annotations.cryostat['REALM'] === 'Custom Targets');
const k8s = models.filter((t) => t.annotations.cryostat['REALM'] === 'KubernetesApi');
return {
meta: {
status: 'OK',
type: 'application/json',
},
},
}));
data: {
result: {
name: 'Universe',
nodeType: 'Universe',
labels: {},
children: [
{
name: 'KubernetesApi',
nodeType: 'Realm',
labels: {},
id: 'KubernetesApi',
children: k8s.map((t) => ({
id: t.alias,
name: t.alias,
nodeType: 'JVM',
target: t,
})),
},
{
name: 'Custom Targets',
nodeType: 'Realm',
labels: {},
id: 'Custom Targets',
children: ct.map((t) => ({
id: t.alias,
name: t.alias,
nodeType: 'CustomTarget',
target: t,
})),
},
],
},
},
};
});
this.get('api/v1/recordings', (schema) => schema.all(Resource.ARCHIVE).models);
this.get('api/beta/fs/recordings', (schema) => {
const target = schema.first(Resource.TARGET);
Expand Down Expand Up @@ -186,7 +207,9 @@ export const startMirage = ({ environment = 'development' } = {}) => {
return new Response(200);
});
this.post('api/v1/targets/:targetId/recordings', (schema, request) => {
const attrs = JSON.parse(request.requestBody);
// Note: MirageJS will fake serialize FormData (i.e. FormData object is returned when accessing request.requestBody)
const attrs = request.requestBody as any;

const recording = schema.create(Resource.RECORDING, {
// id will generated by Mirage (i.e. increment intergers)
downloadUrl: '',
Expand Down

0 comments on commit 27765b0

Please sign in to comment.