Skip to content

Commit bd3f129

Browse files
authored
Merge pull request #325 from akkeris/apps-simple-param
Apps "simple mode" endpoint
2 parents f3742cf + 133ef36 commit bd3f129

5 files changed

Lines changed: 127 additions & 7 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ ready.catch((e) => {
116116
});
117117

118118
// -- apps
119-
routes.add.get('/apps$')
119+
routes.add.get('/apps(\\?[A-z0-9\\=\\?\\-\\_\\.\\&\\:]*)*$')
120120
.run(alamo.apps.http.list.bind(alamo.apps.http.list, pg_pool))
121121
.and.authorization([simple_key, jwt_key]);
122122
routes.add.post('/apps$')

lib/apps.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const assert = require('assert');
22
const crypto = require('crypto');
33
const fs = require('fs');
44
const uuid = require('uuid');
5+
const url = require('url');
56
const httph = require('./http_helper.js');
67
const addons = require('./addons.js');
78
const builds = require('./builds.js');
@@ -70,6 +71,37 @@ function app_payload_to_response(payload) {
7071
};
7172
}
7273

74+
// private
75+
function app_simple_payload_to_response(payload) {
76+
return {
77+
created_at: payload.created.toISOString(),
78+
description: payload.description,
79+
id: payload.id,
80+
labels: payload.labels,
81+
maintenance: !!payload.disabled,
82+
name: payload.app_key,
83+
simple_name: payload.simple_name,
84+
key: payload.app_key,
85+
organization: {
86+
id: payload.org_uuid,
87+
name: payload.org_name,
88+
},
89+
preview: payload.preview ? {
90+
id: payload.preview,
91+
} : null,
92+
region: {
93+
id: payload.region_uuid,
94+
name: payload.region_name,
95+
},
96+
space: {
97+
id: payload.space_uuid,
98+
name: payload.space_name,
99+
},
100+
updated_at: payload.modified.toISOString(),
101+
web_url: payload.url,
102+
};
103+
}
104+
73105
// private
74106
function app_postgres_to_response(result) {
75107
result.app_key = `${result.app_name}-${result.space_name}`;
@@ -79,6 +111,15 @@ function app_postgres_to_response(result) {
79111
return app_payload_to_response(result);
80112
}
81113

114+
// private
115+
function app_simple_postgres_to_response(result) {
116+
result.app_key = `${result.app_name}-${result.space_name}`;
117+
result.simple_name = result.app_name;
118+
result.id = result.app_uuid;
119+
result.modified = result.updated;
120+
return app_simple_payload_to_response(result);
121+
}
122+
82123
// private
83124
function check_payload(payload) {
84125
assert.ok(
@@ -102,7 +143,9 @@ const insert_app = query.bind(query, fs.readFileSync('./sql/insert_app.sql').toS
102143
// private
103144
const select_app_setup_by_app = query.bind(query, fs.readFileSync('./sql/select_app_setup_by_app.sql').toString('utf8'), null);
104145
const select_apps_query = fs.readFileSync('./sql/select_apps.sql').toString('utf8');
146+
const select_apps_simple_query = fs.readFileSync('./sql/select_apps_simple.sql').toString('utf8');
105147
const select_apps = query.bind(query, select_apps_query, app_postgres_to_response);
148+
const select_apps_simple = query.bind(query, select_apps_simple_query, app_simple_postgres_to_response);
106149

107150
// private
108151
// const select_app_query = fs.readFileSync('./sql/select_app.sql').toString('utf8');
@@ -169,7 +212,6 @@ async function update(pg_pool, app_key, name, maintenance, description, labels,
169212
hook_updated.app.maintenance = app.disabled = app.maintenance = maintenance;
170213
setTimeout(() => common.notify_hooks(pg_pool, app.app_uuid, 'updated', JSON.stringify(hook_updated), user || 'System'), 10);
171214

172-
173215
let errors = false;
174216
await Promise.all((await common.formations_exists(pg_pool, app.app_uuid)).map(async (dyno_type) => {
175217
try {
@@ -224,7 +266,7 @@ async function create(pg_pool, org_key, space_key, app_name, description, labels
224266
// determine the url for the application depending on its compliance.
225267
const id = uuid.v4();
226268
const created = new Date();
227-
const url = await common.determine_app_url(pg_pool, space.tags, app_name, space.name, organization.name);
269+
const uri = await common.determine_app_url(pg_pool, space.tags, app_name, space.name, organization.name);
228270
// create config set in alamo
229271
await common.alamo.config.set.create(pg_pool, app_name, space.name);
230272
// add the default port.
@@ -237,7 +279,7 @@ async function create(pg_pool, org_key, space_key, app_name, description, labels
237279
name: app_name,
238280
space_uuid: space.space,
239281
org_uuid: organization.org,
240-
url,
282+
url: uri,
241283
description,
242284
labels,
243285
}));
@@ -270,7 +312,7 @@ async function create(pg_pool, org_key, space_key, app_name, description, labels
270312
space_uuid: space.space,
271313
space_name: space.name,
272314
modified: created,
273-
url,
315+
url: uri,
274316
space_tags: space.tags,
275317
};
276318

@@ -427,7 +469,18 @@ async function del(pg_pool, app_key, elevated_access, user) {
427469

428470
// public
429471
async function http_list(pg_pool, req, res /* regex */) {
430-
const apps = await select_apps(pg_pool, []);
472+
let apps;
473+
try {
474+
const queryObject = url.parse(req.url, true).query;
475+
if (queryObject && queryObject.simple) {
476+
apps = await select_apps_simple(pg_pool, []);
477+
} else {
478+
apps = await select_apps(pg_pool, []);
479+
}
480+
} catch (err) {
481+
apps = await select_apps(pg_pool, []);
482+
}
483+
431484
return httph.ok_response(res, JSON.stringify(apps));
432485
}
433486

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "akkeris-controller-api",
3-
"version": "4.1.1",
3+
"version": "4.2.0",
44
"description": "Central API for controlling apps in akkeris",
55
"main": "index.js",
66
"scripts": {

sql/select_apps_simple.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
select
2+
apps.app app_uuid,
3+
apps.created,
4+
apps.updated,
5+
apps.name app_name,
6+
apps.disabled,
7+
apps.description description,
8+
apps.labels labels,
9+
spaces.name space_name,
10+
spaces.space space_uuid,
11+
stacks.stack stack_uuid,
12+
stacks.name stack_name,
13+
regions.region region_uuid,
14+
regions.name region_name,
15+
organizations.name org_name,
16+
organizations.org org_uuid,
17+
apps.url,
18+
(select preview from previews where apps.app = previews.target and previews.deleted = false limit 1) preview
19+
from
20+
apps
21+
join spaces on apps.space = spaces.space
22+
join organizations on apps.org = organizations.org
23+
join stacks on spaces.stack = stacks.stack and stacks.deleted = false
24+
join regions on regions.region = stacks.region and regions.deleted = false
25+
where
26+
apps.deleted = false and
27+
spaces.deleted = false
28+
order by apps.name, spaces.name desc

test/apps.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,45 @@ describe('apps: ensure we can create an app, list apps, view app info and delete
257257
});
258258
});
259259

260+
it('Ensures we can pull all applications in simple mode.', (done) => {
261+
httph.request('get', 'http://localhost:5000/apps?simple=true', alamo_headers, null, (err, data) => {
262+
if (err) {
263+
console.error(err);
264+
}
265+
expect(err).to.be.null;
266+
expect(data).to.be.a('string');
267+
const appsobj = JSON.parse(data);
268+
expect(appsobj).to.be.an('array');
269+
appsobj.forEach((appobj) => {
270+
if (appobj.name === 'alamotestapp-default') {
271+
expect(appobj).to.be.an('object');
272+
expect(appobj.archived_at).to.be.undefined;
273+
expect(appobj.buildpack_provided_description).to.be.undefined;
274+
expect(appobj.build_stack).to.be.undefined;
275+
expect(appobj.created_at).to.be.a('string');
276+
expect(appobj.id).to.be.a('string');
277+
expect(appobj.maintenance).to.equal(false);
278+
expect(appobj.name).to.equal('alamotestapp-default');
279+
expect(appobj.key).to.equal('alamotestapp-default');
280+
expect(appobj.owner).to.be.undefined;
281+
expect(appobj.organization).to.be.an('object');
282+
expect(appobj.organization.name).to.equal('test');
283+
expect(appobj.region).to.be.an('object');
284+
expect(appobj.region.name).to.be.a('string');
285+
expect(appobj.released_at).to.be.undefined;
286+
expect(appobj.repo_size).to.be.undefined;
287+
expect(appobj.slug_size).to.be.undefined;
288+
expect(appobj.space).to.be.an('object');
289+
expect(appobj.space.name).to.equal('default');
290+
expect(appobj.stack).to.be.undefined;
291+
expect(appobj.updated_at).to.be.a('string');
292+
expect(appobj.web_url).to.contain(`https://alamotestapp${process.env.ALAMO_BASE_DOMAIN}`);
293+
done();
294+
}
295+
});
296+
});
297+
});
298+
260299
it('Ensures we cannot delete an app in a socs/prod space.', (done) => {
261300
const new_headers = JSON.parse(JSON.stringify(alamo_headers));
262301
delete new_headers['x-elevated-access'];

0 commit comments

Comments
 (0)