1 | 'use strict'
|
2 |
|
3 | let cli = require('heroku-cli-util')
|
4 |
|
5 | module.exports = function (heroku) {
|
6 | function getOutboundRules (space) {
|
7 | return heroku.request({
|
8 | path: `/spaces/${space}/outbound-ruleset`,
|
9 | headers: {Accept: 'application/vnd.heroku+json; version=3.dogwood'}
|
10 | })
|
11 | }
|
12 |
|
13 | function putOutboundRules (space, ruleset) {
|
14 | return heroku.request({
|
15 | method: 'PUT',
|
16 | path: `/spaces/${space}/outbound-ruleset`,
|
17 | body: ruleset,
|
18 | headers: {Accept: 'application/vnd.heroku+json; version=3.dogwood'}
|
19 | })
|
20 | }
|
21 |
|
22 | function displayRules (space, ruleset) {
|
23 | if (ruleset.rules.length > 0) {
|
24 | cli.styledHeader('Outbound Rules')
|
25 | display(ruleset.rules)
|
26 | } else {
|
27 | cli.styledHeader(`${space} has no Outbound Rules. Your Dynos cannot communicate with hosts outside of ${space}.`)
|
28 | }
|
29 | }
|
30 |
|
31 | function lined (rules) {
|
32 | var lined = []
|
33 | for (var i = 0, len = rules.length; i < len; i++) {
|
34 | lined.push({
|
35 | line: i + 1,
|
36 | target: rules[i].target,
|
37 | from_port: rules[i].from_port,
|
38 | to_port: rules[i].to_port,
|
39 | protocol: rules[i].protocol
|
40 | })
|
41 | }
|
42 |
|
43 | return lined
|
44 | }
|
45 |
|
46 | function display (rules) {
|
47 | var f = function (p) {
|
48 | var n = p
|
49 | return n.toString()
|
50 | }
|
51 |
|
52 | cli.table(lined(rules), {
|
53 | columns: [
|
54 | {key: 'line', label: 'Rule Number'},
|
55 | {key: 'target', label: 'Destination'},
|
56 | {key: 'from_port', label: 'From Port', format: fromPort => f(fromPort)},
|
57 | {key: 'to_port', label: 'To Port', format: toPort => f(toPort)},
|
58 | {key: 'protocol', label: 'Protocol'}
|
59 | ]
|
60 | })
|
61 | }
|
62 |
|
63 | function parsePorts (proto, p) {
|
64 | if (p === '-1' || p === 'any') {
|
65 | if (proto === 'icmp') {
|
66 | return [0, 255]
|
67 | } else {
|
68 | return [0, 65535]
|
69 | }
|
70 | }
|
71 |
|
72 | var actual = []
|
73 | if (p != null) {
|
74 | var ports = p.split('-')
|
75 | if (ports.length === 2) {
|
76 | actual = [ports[0] | 0, ports[1] | 0]
|
77 | } else if (ports.length === 1) {
|
78 | actual = [ports[0] | 0, ports[0] | 0]
|
79 | } else {
|
80 | throw new Error('Specified --port range seems incorrect.')
|
81 | }
|
82 | }
|
83 |
|
84 | if (actual.length !== 2) {
|
85 | throw new Error('Specified --port range seems incorrect.')
|
86 | }
|
87 |
|
88 | return actual
|
89 | }
|
90 |
|
91 | return {
|
92 | getOutboundRules,
|
93 | putOutboundRules,
|
94 | displayRules,
|
95 | parsePorts
|
96 | }
|
97 | }
|