UNPKG

4.62 kBJavaScriptView Raw
1'use strict';
2
3const ForceArray = require('force-array');
4const Hasha = require('hasha');
5const FWRule = require('fwrule');
6const Uniq = require('lodash.uniq');
7
8const Handlers = require('./handlers');
9const Utils = require('./utils');
10
11
12module.exports = {
13 User: {
14 keys: (root, args, request) => {
15 return Handlers.keys(request.plugins.cloudapi.fetch, args);
16 }
17 },
18 Machine: {
19 brand: ({ brand }) => { return (brand ? brand.toUpperCase() : brand); },
20
21 state: ({ state }) => { return (state ? state.toUpperCase() : state); },
22
23 image: (root, args, request) => {
24 return Handlers.image(request.plugins.cloudapi.fetch, { id: root.image });
25 },
26
27 // eslint-disable-next-line camelcase
28 primary_ip: ({ primaryIp }) => { return primaryIp; },
29
30 tags: (root, { name }, request) => {
31 const { id: machine } = root;
32
33 return Handlers.tags(request.plugins.cloudapi.fetch, { machine, name });
34 },
35
36 metadata: (root, { name }, request) => {
37 const { id: machine } = root;
38 return Handlers.metadata(request.plugins.cloudapi.fetch, { machine, name });
39 },
40
41 networks: async (root, args, request) => {
42 const networks = [];
43 for (const id of root.networks) {
44 const network = await Handlers.network(request.plugins.cloudapi.fetch, { id });
45 networks.push(network);
46 }
47
48 return networks;
49 },
50
51 // eslint-disable-next-line camelcase
52 package: (root, args, request) => {
53 return Handlers.package(request.plugins.cloudapi.fetch, { name: root.package });
54 },
55
56 snapshots: (root, { name }, request) => {
57 const { id: machine } = root;
58 return Handlers.snapshots(request.plugins.cloudapi.fetch, { machine, name });
59 },
60
61 // eslint-disable-next-line camelcase
62 firewall_rules: (root, { id }, request) => {
63 const { id: machine } = root;
64 return Handlers.firewall_rules(request.plugins.cloudapi.fetch, { machine, id });
65 },
66
67 actions: (root, args, request) => {
68 const { id: machine } = root;
69 return Handlers.actions(request.plugins.cloudapi.fetch, { machine });
70 }
71 },
72 Image: {
73 os: ({ os }) => { return (os ? os.toUpperCase() : os); },
74
75 state: ({ state }) => { return (state ? state.toUpperCase() : state); },
76
77 type: ({ type }) => { return (type ? type.replace('-', '_').toUpperCase() : type); }
78 },
79 Action: {
80 name: ({ action }) => { return action; },
81
82 parameters: ({ parameters }) => { return Utils.toNameValues(parameters); }
83 },
84 Caller: {
85 type: ({ type }) => { return (type ? type.toUpperCase() : type); },
86
87 // eslint-disable-next-line camelcase
88 key_id: ({ keyId }) => { return keyId; }
89 },
90 FirewallRule: {
91 machines: ({ id }, args, request) => {
92 return request.plugins.cloudapi.fetch(`/fwrules/${id}/machines`);
93 },
94 rule_str: ({ rule }) => { return rule; },
95 rule_obj: ({ rule }) => {
96 const parsed = FWRule.parse(rule);
97 const _from = ForceArray(parsed.from);
98 const _to = ForceArray(parsed.to);
99
100 const getTags = (partial) => {
101 return partial
102 .map((partial) => { return ForceArray(partial); })
103 .filter((partial) => { return partial[0] === 'tag'; })
104 .map((partial) => { return [partial[0], ForceArray(partial[1])]; })
105 .filter((partial) => { return partial[1]; })
106 .filter((partial) => { return partial[1][0]; })
107 .map((partial) => {
108 return {
109 name: partial[1][0],
110 value: partial[1][1]
111 };
112 });
113 };
114
115 const isWildcard = (
116 _from.some((frm) => { return frm[0] === 'wildcard'; }) &&
117 _to.some((to) => { return to[0] === 'wildcard'; })
118 );
119
120 const tags = Uniq(getTags(_from).concat(getTags(_to)));
121
122 return {
123 ...parsed,
124 isWildcard,
125 tags
126 };
127 }
128 },
129 Snapshot: {
130 state: ({ state }) => { return (state ? state.toUpperCase() : state); },
131 id: ({ name }) => { return Hasha(name); }
132 },
133 ImageError: {
134 code: ({ code }) => { return (code ? code.toUpperCase() : code); }
135 },
136 ImageFile: {
137 compression: ({ compression }) => {
138 return (compression ? compression.toUpperCase() : compression);
139 }
140 },
141 Network: {
142 machines: async ({ id, fabric }, args, request) => {
143 if (!fabric) {
144 return [];
145 }
146
147 const machines = await request.plugins.cloudapi.fetch('/machines');
148
149 return ForceArray(machines).filter(({ networks }) => {
150 return ForceArray(networks).some((network) => { return network === id; });
151 });
152 }
153 }
154};