Skip to content

Commit

Permalink
Support services
Browse files Browse the repository at this point in the history
  • Loading branch information
solocommand committed Mar 28, 2019
1 parent 1110440 commit 45eb64a
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ All methods require at *minimum* a Rancher 2.x v3 API URL (such as `https://cows
- [Namespace.list()](#namespace.list)
- [Project.create()](#project.create)
- [Project.list()](#project.list)
- [Service.create()](#service.create)
- [Service.list()](#service.list)
- [Workload.create()](#workload.create)
- [Workload.list()](#workload.list)
- [Workload.update()](#workload.update)
Expand Down Expand Up @@ -43,6 +45,19 @@ Parameters: `({ uri, token, clusterId, name })`

Creates and returns a project object ({ id, name }).

## Service
Supported: `List` and `Create`.

#### service.list
Parameters: `({ uri, token, projectId })`

Returns an array of service objects ({ id, name }).

#### service.create
Parameters: `({ uri, token, projectId, namespaceId, name, targetWorkloadIds })`

Creates and returns a service object ({ id, name }).

## Namespace
Supported: `List` and `Create`.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@endeavorb2b/rancher2api",
"version": "1.0.0",
"version": "1.0.2",
"description": "Rancher2 API library",
"main": "src/index.js",
"author": "solocommand <[email protected]>",
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const cluster = require('./cluster');
const ingress = require('./ingress');
const namespace = require('./namespace');
const project = require('./project');
const service = require('./service');
const workload = require('./workload');

module.exports = {
cluster,
ingress,
namespace,
project,
service,
workload,
};
28 changes: 28 additions & 0 deletions src/service/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { validate, post } = require('../common');

module.exports = async ({
uri,
token,
projectId,
namespaceId,
name,
targetWorkloadIds,
}) => {
validate({
uri,
token,
projectId,
namespaceId,
name,
targetWorkloadIds,
});
const url = `${uri}/project/${projectId}/services`;
const payload = {
name,
namespaceId,
projectId,
targetWorkloadIds,
};
const { id, name: Name } = await post(url, token, payload);
return { id, name: Name };
};
7 changes: 7 additions & 0 deletions src/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const create = require('./create');
const list = require('./list');

module.exports = {
create,
list,
};
8 changes: 8 additions & 0 deletions src/service/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { validate, get } = require('../common');

module.exports = async ({ uri, token, projectId }) => {
validate({ uri, token, projectId });
const url = `${uri}/projects/${projectId}/services`;
const { data } = await get(url, token);
return data.map(({ id, name }) => ({ id, name }));
};
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const project = require('./project');
const namespace = require('./namespace');
const workload = require('./workload');
const ingress = require('./ingress');
const service = require('./service');

const { log } = console;
const uri = process.env.RANCHER_URL;
Expand All @@ -28,6 +29,11 @@ const main = async () => {
const workloadIds = workloads.map(({ workload: w }) => w.id);
log('workloads', workloadIds);

// Test service methods
const w = workloads.map(({ workload: wo }) => wo);
const services = await service(uri, token, projectId, namespaceId, w);
log('services', services);

// Test ingress methods
const balancer = await ingress(uri, token, clusterId, projectId, namespaceId, workloadIds);
log('ingress', balancer.id);
Expand Down
40 changes: 40 additions & 0 deletions test/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { list, create } = require('../src/service');

const { log } = console;

module.exports = async (uri, token, projectId, namespaceId, workloads) => {
try {
log('Listing services');
const services = await list({
uri,
token,
projectId,
});
// log(services);
log('found', services.length, 'services!', services.map(n => n.id));

return Promise.all(workloads.map((workload) => {
const filtered = services.filter(svc => svc.name === workload.name);
if (filtered.length) {
log(`Found service ${workload.name}`);
return filtered[0];
}

log(`Creating service ${workload.name} ${workload.id}`);
return create({
uri,
token,
projectId,
namespaceId,
name: workload.name,
targetWorkloadIds: [workload.id],
});
}));

//
} catch (e) {
log(e);
process.exit(1);
}
return false;
};
4 changes: 3 additions & 1 deletion test/workload.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ module.exports = async (uri, token, clusterId, projectId, namespaceId) => {
containers: [containerSpecGraphQL],
};
payload.containers[0].environment.UPDATED = 'true';
created.push({ name: 'graphql-updated', workload: await update(payload) });

log('Updating service');
await update(payload);
return created;

//
Expand Down

0 comments on commit 45eb64a

Please sign in to comment.