cloudapi-gql
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -60,3 +60,3 @@ 'use strict'; | ||
const method = options.method && options.method.toLowerCase() || 'get'; | ||
try { | ||
@@ -63,0 +63,0 @@ const { payload } = await this._wreck[method](path, wreckOptions); |
@@ -10,3 +10,3 @@ 'use strict'; | ||
const Package = require('../package.json'); | ||
const { CloudApi } = require('./cloudapi'); | ||
const { CloudApi } = require('./cloudapi'); | ||
const Resolvers = require('./resolvers'); | ||
@@ -32,5 +32,9 @@ | ||
const register = async (server, { authStrategy, cloudapiUrl }) => { | ||
const schema = makeExecutableSchema({ typeDefs: Schema.toString(), resolvers: Resolvers }); | ||
const schema = makeExecutableSchema({ | ||
typeDefs: Schema.toString(), | ||
resolvers: Resolvers | ||
}); | ||
const graphiOptions = { | ||
graphiqlPath: (NODE_ENV === 'development') ? '/graphiql' : false, | ||
graphiqlPath: NODE_ENV === 'development' ? '/graphiql' : false, | ||
schema, | ||
@@ -37,0 +41,0 @@ resolvers: Resolvers, |
'use strict'; | ||
const ForceArray = require('force-array'); | ||
const constantCase = require('constant-case'); | ||
const Hasha = require('hasha'); | ||
const map = require('apr-map'); | ||
const Map = require('apr-map'); | ||
const FWRule = require('fwrule'); | ||
const CloudApi = require('./cloudapi'); | ||
const internals = {}; | ||
@@ -14,3 +16,3 @@ | ||
account: (root, args = {}, request) => { | ||
return CloudApi('/', args, request); | ||
return CloudApi('', {}, request); | ||
}, | ||
@@ -172,3 +174,3 @@ | ||
return map(machinesList, ({ id }) => internals.resolvers.Query.machine(root, { id }, request)); | ||
return Map(machinesList, ({ id }) => internals.resolvers.Query.machine(root, { id }, request)); | ||
}, | ||
@@ -246,2 +248,59 @@ | ||
// TEMPORARY | ||
// TODO: run in a worker | ||
firewall_rules_create_machine: async (root, args, request) => { | ||
const tags = internals.fromNameValues(args.tags); | ||
const res = await internals.resolvers.Query.firewall_rules(root, {}, request); | ||
const rules = res.map(({ rule, ...rest }) => { | ||
return Object.assign(rest, { | ||
rule_str: rule, | ||
rule_obj: FWRule.parse(rule) | ||
}); | ||
}); | ||
const defaultRules = rules.filter(({ enabled, rule_obj = {} }) => { | ||
return ( | ||
ForceArray(rule_obj.from).some(frm => frm[0] === 'wildcard') && | ||
ForceArray(rule_obj.to).some(to => to[0] === 'wildcard') | ||
); | ||
}); | ||
const filterTagRulePartial = partial => { | ||
return partial | ||
.map(partial => ForceArray(partial)) | ||
.filter(partial => partial[0] === 'tag') | ||
.filter(partial => { | ||
const tag = ForceArray(partial[1]); | ||
const foundTagValue = tags[tag[0]]; | ||
if (!foundTagValue) { | ||
return false; | ||
} | ||
if (tag.length === 1) { | ||
return true; | ||
} | ||
return foundTagValue === tag[1]; | ||
}); | ||
}; | ||
const tagRules = rules | ||
.filter(({ enabled, rule_obj = {} }) => { | ||
const _from = ForceArray(rule_obj.from); | ||
const _to = ForceArray(rule_obj.to); | ||
const fromHas = filterTagRulePartial(_from).length; | ||
const toHas = filterTagRulePartial(_to).length; | ||
return Boolean(fromHas) || Boolean(toHas); | ||
}) | ||
.map(rule => { | ||
return Object.assign(rule, { tag: true }); | ||
}); | ||
return defaultRules.concat(tagRules); | ||
}, | ||
vlans: async (root, { id }, request) => { | ||
@@ -396,2 +455,25 @@ if (id) { | ||
createMachine: async (root, { name, image, networks, affinity, metadata, tags, firewall_enabled, ...args }, request) => { | ||
const AffinityRuleTypes = { | ||
MUST_EQUAL: '==', | ||
MUST_NOT_EQUAL: '==~', | ||
SHOULD_EQUAL: '!=', | ||
SHOULD_NOT_EQUAL: '!=~' | ||
}; | ||
const payload = { | ||
name, | ||
'package': args.package, | ||
image, | ||
networks, | ||
affinity: affinity.map(({ key, value, type }) => `${key}${AffinityRuleTypes[type]}${value}`), | ||
...internals.fromNameValues(tags, 'tag.'), | ||
...internals.fromNameValues(metadata, 'tag.'), | ||
firewall_enabled | ||
}; | ||
const { id } = await CloudApi(`/machines`, { method: 'post', payload }, request); | ||
return internals.resolvers.Query.machine(root, { id }, request); | ||
}, | ||
updateMachineMetadata: async (root, { id, metadata }, request) => { | ||
@@ -501,3 +583,3 @@ const payload = internals.fromNameValues(metadata) | ||
type: ({ type }) => { return (type ? type.toUpperCase() : type); } | ||
type: ({ type }) => { return (type ? constantCase(type) : type); } | ||
}, | ||
@@ -518,3 +600,5 @@ Action: { | ||
return CloudApi(`/fwrules/${id}/machines`, {}, request); | ||
} | ||
}, | ||
rule_str: ({ rule }, args, request) => rule, | ||
rule_obj: ({ rule }, args, request) => FWRule.parse(rule) | ||
}, | ||
@@ -551,5 +635,5 @@ Snapshot: { | ||
return Object.assign(accumulator, { | ||
[prefix + name]: value | ||
[prefix + name]: name === 'triton.cns.disable' ? JSON.parse(value) : value | ||
}); | ||
}, {}); | ||
}; |
{ | ||
"name": "cloudapi-gql", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"license": "MPL-2.0", | ||
@@ -19,3 +19,5 @@ "repository": "github:yldio/joyent-portal", | ||
"bounce": "^1.2.0", | ||
"constant-case": "^2.0.0", | ||
"force-array": "^3.1.0", | ||
"fwrule": "^1.4.1", | ||
"graphi": "^5.2.0", | ||
@@ -22,0 +24,0 @@ "graphql-tools": "^2.6.1", |
Sorry, the diff of this file is not supported yet
223603
729
11
+ Addedconstant-case@^2.0.0
+ Addedfwrule@^1.4.1
+ Addedassert-plus@1.0.0(transitive)
+ Addedconstant-case@2.0.0(transitive)
+ Addedextsprintf@1.0.01.0.1(transitive)
+ Addedfwrule@1.4.1(transitive)
+ Addedip6addr@0.2.2(transitive)
+ Addedlower-case@1.1.4(transitive)
+ Addedno-case@2.3.2(transitive)
+ Addednode-uuid@1.2.0(transitive)
+ Addedsnake-case@2.1.0(transitive)
+ Addedupper-case@1.1.3(transitive)
+ Addedverror@1.3.4(transitive)