From d3baebd25fb9d62d84fb2604b6997fa7820ea33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Tue, 3 Oct 2017 16:04:10 +0100 Subject: [PATCH] feat(cloudapi-gql): transform objects on the type resolvers --- packages/cloudapi-gql/src/api/images.js | 9 +++- packages/cloudapi-gql/src/api/key-value.js | 2 +- packages/cloudapi-gql/src/api/machines.js | 2 +- packages/cloudapi-gql/src/api/networks.js | 2 +- packages/cloudapi-gql/src/schema/resolvers.js | 46 ++++++++++--------- .../cloudapi-gql/src/schema/schema.graphql | 2 +- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/packages/cloudapi-gql/src/api/images.js b/packages/cloudapi-gql/src/api/images.js index 41d9113e..8bc85b72 100644 --- a/packages/cloudapi-gql/src/api/images.js +++ b/packages/cloudapi-gql/src/api/images.js @@ -1,6 +1,13 @@ const request = require('./request'); -module.exports.list = ctx => request('listImages', ctx); +const transform = ({ os, type, state, ...rest }) => + Object.assign(rest, { + type: type ? type.toLowerCase() : type, + os: os ? os.toLowerCase() : os, + state: state ? state.toLowerCase() : state + }); + +module.exports.list = ctx => request('listImages', transform(ctx)); module.exports.get = ctx => request('getImage', ctx); module.exports.destroy = uuid => request('deleteImage', uuid); module.exports.export = uuid => request('deleteImage', uuid); diff --git a/packages/cloudapi-gql/src/api/key-value.js b/packages/cloudapi-gql/src/api/key-value.js index b8591790..738b3240 100644 --- a/packages/cloudapi-gql/src/api/key-value.js +++ b/packages/cloudapi-gql/src/api/key-value.js @@ -2,7 +2,7 @@ const forceArray = require('force-array'); const hasha = require('hasha'); module.exports.toKeyValue = r => - Object.keys(r).map(name => ({ + r && Object.keys(r).map(name => ({ id: hasha(JSON.stringify({ name, value: r[name] })), name, value: r[name] diff --git a/packages/cloudapi-gql/src/api/machines.js b/packages/cloudapi-gql/src/api/machines.js index c72ef739..26769a99 100644 --- a/packages/cloudapi-gql/src/api/machines.js +++ b/packages/cloudapi-gql/src/api/machines.js @@ -38,7 +38,7 @@ module.exports.reboot = ctx => request('rebootMachine', ctx); module.exports.resize = ctx => request('', ctx); module.exports.rename = ctx => request('', ctx); module.exports.destroy = ctx => request('deleteMachine', ctx); -module.exports.audit = ctx => request('machineAudit', ctx); +module.exports.audit = ({ id }) => request('machineAudit', id); module.exports.snapshots = snapshots; module.exports.metadata = metadata; diff --git a/packages/cloudapi-gql/src/api/networks.js b/packages/cloudapi-gql/src/api/networks.js index 6a5340fb..68a0a93b 100644 --- a/packages/cloudapi-gql/src/api/networks.js +++ b/packages/cloudapi-gql/src/api/networks.js @@ -2,7 +2,7 @@ const request = require('./request'); // lists all networks, including fabric networks module.exports.list = () => request('listNetworks'); -module.exports.get = ctx => request('getNetwork', ctx); +module.exports.get = ({ id }) => request('getNetwork', id); // create fabric network module.exports.create = () => request(''); // destroy fabric network diff --git a/packages/cloudapi-gql/src/schema/resolvers.js b/packages/cloudapi-gql/src/schema/resolvers.js index 7a6873e5..ff3dd229 100644 --- a/packages/cloudapi-gql/src/schema/resolvers.js +++ b/packages/cloudapi-gql/src/schema/resolvers.js @@ -1,20 +1,6 @@ const { toKeyValue, fromKeyValue } = require('../api/key-value'); const api = require('../api'); -const transform = { - toImage: ({ os, ...rest }) => { - console.log(rest); - return Object.assign(rest, { - os: os ? os.toUpperCase() : os - }); - }, - fromImage: ({ os, state, type, ...rest }) => Object.assign(rest, { - os: os ? os.toLowerCase() : os, - state: state ? state.toLowerCase() : state, - type: type ? type.toLowerCase() : type - }) -}; - const resolvers = { Query: { account: () => api.account.get(), @@ -45,12 +31,10 @@ const resolvers = { })) ), services: () => api.services().then(toKeyValue), - images: (root, { id, ...rest }) => id - ? api.images.get({ id }) - .then(image => [image]) - .then((imgs) => imgs.map(transform.toImage)) - : api.images.list(transform.fromImage(rest)) - .then((imgs) => imgs.map(transform.toImage)), + images: (root, { id, ...rest }) => + id + ? api.images.get({ id }).then(image => [image]) + : api.images.list(rest), image: (root, { id }) => api.images.get({ id }), packages: (root, { id, ...rest }) => id @@ -59,7 +43,7 @@ const resolvers = { package: (root, { id }) => api.packages.get({ id }), machines: (root, { id, brand, state, tags, ...rest }) => id - ? api.machines.get({ id }) + ? api.machines.get({ id }).then(machine => [machine]) : api.machines.list( Object.assign(rest, { brand: brand ? brand.toLowerCase() : brand, @@ -96,6 +80,7 @@ const resolvers = { api.machines.tags .get({ id: machine, tag: name }) .then(value => toKeyValue({ [name]: value }).shift()), + actions: (root, { machine }) => api.machines.audit({ id: machine }), // eslint-disable-next-line camelcase firewall_rules: (root, { machine, id }) => id @@ -118,15 +103,32 @@ const resolvers = { keys: ({ login }, { name }) => resolvers.Query.keys(null, { login, name }) }, Machine: { + state: ({ state }) => (state ? state.toUpperCase() : state), tags: ({ id }, { name }) => resolvers.Query.tags(null, { machine: id, name }), metadata: ({ id }, { name }) => resolvers.Query.metadata(null, { machine: id, name }), snapshots: ({ id }, { name }) => resolvers.Query.snapshots(null, { machine: id, name }), + networks: ({ networks }) => + Promise.all(networks.map(id => resolvers.Query.network(null, { id }))), // eslint-disable-next-line camelcase firewall_rules: ({ id: machine }, { id }) => - resolvers.Query.firewall_rules(null, { machine, id }) + resolvers.Query.firewall_rules(null, { machine, id }), + actions: ({ id }) => resolvers.Query.actions(null, { machine: id }) + }, + Image: { + os: ({ os }) => (os ? os.toUpperCase() : os), + state: ({ state }) => (state ? state.toUpperCase() : state), + type: ({ type }) => (type ? type.toUpperCase() : type) + }, + Action: { + name: ({ action }) => action, + parameters: ({ parameters }) => toKeyValue(parameters) + }, + Caller: { + type: ({ type }) => (type ? type.toUpperCase() : type), + key_id: ({ keyId }) => keyId } }; diff --git a/packages/cloudapi-gql/src/schema/schema.graphql b/packages/cloudapi-gql/src/schema/schema.graphql index d752bdca..fde32026 100644 --- a/packages/cloudapi-gql/src/schema/schema.graphql +++ b/packages/cloudapi-gql/src/schema/schema.graphql @@ -109,7 +109,7 @@ type Caller { # When the authentication type is BASIC, this member will be present and include user login ip: String # When authentication type is either SIGNATURE or TOKEN, SSH key identifier - keyId: String + key_id: String } type Action {