diff --git a/README.md b/README.md index 58f0e90..8322ecc 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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`. diff --git a/package.json b/package.json index 316af8a..7365f8e 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/index.js b/src/index.js index 2370225..0cd1216 100755 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ 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 = { @@ -9,5 +10,6 @@ module.exports = { ingress, namespace, project, + service, workload, }; diff --git a/src/service/create.js b/src/service/create.js new file mode 100644 index 0000000..3f9edb2 --- /dev/null +++ b/src/service/create.js @@ -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 }; +}; diff --git a/src/service/index.js b/src/service/index.js new file mode 100644 index 0000000..0d4a1bc --- /dev/null +++ b/src/service/index.js @@ -0,0 +1,7 @@ +const create = require('./create'); +const list = require('./list'); + +module.exports = { + create, + list, +}; diff --git a/src/service/list.js b/src/service/list.js new file mode 100644 index 0000000..4f7b005 --- /dev/null +++ b/src/service/list.js @@ -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 })); +}; diff --git a/test/index.js b/test/index.js index f0c7a5b..7908fb0 100644 --- a/test/index.js +++ b/test/index.js @@ -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; @@ -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); diff --git a/test/service.js b/test/service.js new file mode 100644 index 0000000..8832104 --- /dev/null +++ b/test/service.js @@ -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; +}; diff --git a/test/workload.js b/test/workload.js index 5d7d210..d02be82 100644 --- a/test/workload.js +++ b/test/workload.js @@ -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; //