1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-24 02:40:05 +02:00

feat(portal-data): include instance/package methods

This commit is contained in:
geek 2017-05-31 14:27:53 -05:00 committed by Sérgio Ramos
parent 0b1c5830d9
commit e879b7be97
4 changed files with 320 additions and 3 deletions

View File

@ -88,7 +88,8 @@
currentMetrics: [CurrentMetric]!
connections: [String!] # list of serviceIds
parent: ID # parent service id
package: Package! # we don't have this in current mock data
package: Package! # we don't have this in current mock data,
environment: [Environment]
}
# for metrics max / min (I guess)
@ -105,6 +106,12 @@
group: String!
}
# environment variables
type Environment {
name: String!
value: String!
}
enum InstanceStatus {
CREATED
RESTARTING

View File

@ -274,4 +274,104 @@ module.exports = class Data {
cb(null, manifests.map(Transform.fromManifest));
});
}
// services
createService (clientService, cb) {
this._db.services.insert(Transform.toService(clientService), (err, key) => {
if (err) {
return cb(err);
}
this.getService({ id: key }, cb);
});
}
getService ({ id, hash }, cb) {
const query = id ? { id } : { version_hash: hash };
this._db.services.single(query, (err, service) => {
if (err) {
return cb(err);
}
VAsync.parallel({
funcs: [
(next) => {
this._db.instances.get(service.instance_ids, next);
},
(next) => {
this._db.packages.single({ id: service.package_id }, next);
}
]
}, (err, results) => {
if (err) {
return cb(err);
}
cb(null, Transform.fromService({ service, instances: results.successes[0], package: results.successes[1] }));
});
});
}
// TODO: get services with join/merge
// instances
createInstance (clientInstance, cb) {
this._db.instances.insert(Transform.toInstance(clientInstance), (err, key) => {
if (err) {
return cb(err);
}
clientInstance.id = key;
cb(null, clientInstance);
});
}
getInstance ({ id }, cb) {
this._db.instances.single({ id }, (err, instance) => {
if (err) {
return cb(err);
}
cb(null, instance ? Transform.fromInstance(instance) : {});
});
}
// packages
createPackage (clientPackage, cb) {
this._db.packages.insert(Transform.toPackage(clientPackage), (err, key) => {
if (err) {
return cb(err);
}
clientPackage.id = key;
cb(null, clientPackage);
});
}
getPackage ({ id }, cb) {
this._db.packages.single({ id }, (err, dbPackage) => {
if (err) {
return cb(err);
}
cb(null, dbPackage ? Transform.fromPackage(dbPackage) : {});
});
}
getPackages ({ name, type }, cb) {
const query = name ? { name } : { type };
this._db.packages.query(query, (err, dbPackages) => {
if (err) {
return cb(err);
}
cb(null, dbPackages ? dbPackages.map(Transform.fromPackage) : []);
});
}
};

View File

@ -31,7 +31,7 @@ exports.fromDeploymentGroup = function (deploymentGroup, services) {
id: deploymentGroup.id,
name: deploymentGroup.name,
slug: deploymentGroup.slug,
services: services.map(exports.fromService),
services: services ? services.map((service) => { return exports.fromService({ service }); }) : [],
version: deploymentGroup.version_id,
history: deploymentGroup.history_version_ids || []
};
@ -48,8 +48,32 @@ exports.toDeploymentGroup = function ({ name }) {
};
exports.fromService = function (service) {
exports.fromService = function ({ service, instances, packages }) {
return {
id: service.id,
hash: service.version_hash,
name: service.name,
slug: service.slug,
environment: service.environment || [],
instances: instances ? instances.map(exports.fromInstance) : [],
currentMetrics: [],
connections: service.service_dependency_ids,
package: packages ? exports.fromPackage(packages) : {},
parent: service.parent_id || ''
};
};
exports.toService = function (clientService) {
return {
version_hash: clientService.hash || '',
name: clientService.name,
slug: clientService.slub || '',
environment: clientService.environment || {},
instance_ids: clientService.intances ? clientService.instances.map((instance) => { return instance.id; }) : [],
service_dependency_ids: clientService.connections || [],
package_id: clientService.package ? clientService.package.id : '',
parent_id: clientService.parent || ''
};
};
@ -127,3 +151,31 @@ exports.fromManifest = function (manifest) {
json: manifest.json
};
};
exports.fromPackage = function (packages) {
return packages;
};
exports.toPackage = function (packages) {
return packages;
};
exports.fromInstance = function (instance) {
return {
id: instance.id,
name: instance.name,
machineId: instance.machine_id,
status: instance.status || ''
};
};
exports.toInstance = function (clientInstance) {
return {
id: clientInstance.id,
name: clientInstance.name,
machine_id: clientInstance.machineId,
status: clientInstance.status || ''
};
};

View File

@ -515,3 +515,161 @@ describe('manifests', () => {
});
});
});
describe('instances', () => {
describe('createInstance()', () => {
it('creates a new instance record in the instances table', (done) => {
const data = new PortalData(internals.options);
data.connect((err) => {
expect(err).to.not.exist();
const clientInstance = {
name: 'my test',
machineId: 'something',
status: 'CREATED'
};
data.createInstance(clientInstance, (err, result) => {
expect(err).to.not.exist();
expect(result.id).to.exist();
expect(result.name).to.equal(clientInstance.name);
done();
});
});
});
});
describe('geInstance()', () => {
it('retrieves an instance record from the instances table', (done) => {
const data = new PortalData(internals.options);
data.connect((err) => {
expect(err).to.not.exist();
const clientInstance = {
name: 'my test',
machineId: 'something',
status: 'CREATED'
};
data.createInstance(clientInstance, (err, result) => {
expect(err).to.not.exist();
expect(result.id).to.exist();
data.getInstance({ id: result.id }, (err, instance) => {
expect(err).to.not.exist();
expect(instance).to.equal(result);
done();
});
});
});
});
});
});
describe('packages', () => {
describe('createPackage()', () => {
it('creates a new package record in the packages table', (done) => {
const data = new PortalData(internals.options);
data.connect((err) => {
expect(err).to.not.exist();
const clientPackage = {
name: 'small',
type: 'mem-small',
memory: 12123413,
disk: 4224243,
swap: 1224324,
lwps: 12,
vcpus: 1,
version: '',
group: ''
};
data.createPackage(clientPackage, (err, result) => {
expect(err).to.not.exist();
expect(result.id).to.exist();
expect(result.name).to.equal(clientPackage.name);
done();
});
});
});
});
describe('getPackage()', () => {
it('retrieves a package record from the packages table', (done) => {
const data = new PortalData(internals.options);
data.connect((err) => {
expect(err).to.not.exist();
const clientPackage = {
name: 'small',
type: 'mem-small',
memory: 12123413,
disk: 4224243,
swap: 1224324,
lwps: 12,
vcpus: 1,
version: '',
group: ''
};
data.createPackage(clientPackage, (err, createdPackage) => {
expect(err).to.not.exist();
expect(createdPackage.id).to.exist();
data.getPackage({ id: createdPackage.id }, (err, retrievedPackage) => {
expect(err).to.not.exist();
expect(retrievedPackage).to.equal(createdPackage);
done();
});
});
});
});
});
describe('getPackages()', () => {
it('retrieves packages using with the same type', (done) => {
const data = new PortalData(internals.options);
data.connect((err) => {
expect(err).to.not.exist();
const clientPackage1 = {
name: 'small',
type: 'mem-small',
memory: 12123413,
disk: 4224243,
swap: 1224324,
lwps: 12,
vcpus: 1,
version: '',
group: ''
};
const clientPackage2 = {
name: 'smaller',
type: 'mem-small',
memory: 1213,
disk: 243,
swap: 1324,
lwps: 1,
vcpus: 1,
version: '',
group: ''
};
data.createPackage(clientPackage1, (err, createdPackage1) => {
expect(err).to.not.exist();
data.createPackage(clientPackage2, (err, createdPackage2) => {
expect(err).to.not.exist();
data.getPackages({ type: clientPackage1.type }, (err, packages) => {
expect(err).to.not.exist();
expect(packages.length).to.equal(2);
done();
});
});
});
});
});
});
});