1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | const request = require('request');
|
20 | const extend = require('extend');
|
21 |
|
22 | module.exports = function Service(options, serviceName, invokeUri) {
|
23 | this.options = options;
|
24 | this.serviceName = serviceName;
|
25 | this.invokeUri = invokeUri;
|
26 |
|
27 | this.invoke = (data) => {
|
28 | let opOptions = {};
|
29 | opOptions = extend(opOptions, this.options);
|
30 | opOptions.method = 'PUT';
|
31 | opOptions.uri = this.invokeUri;
|
32 | opOptions.json = true;
|
33 | opOptions.body = data;
|
34 | return new Promise(((resolve, reject) => {
|
35 | request(opOptions, (error, response) => {
|
36 | if (error) {
|
37 | reject(error);
|
38 | } else {
|
39 | resolve(response);
|
40 | }
|
41 | });
|
42 | }));
|
43 | };
|
44 |
|
45 | this.getRequestSchema = () => {
|
46 | let opOptions = {};
|
47 | opOptions = extend(opOptions, this.options);
|
48 | opOptions.uri += '/schemas/request';
|
49 | return new Promise(((resolve, reject) => {
|
50 | request.get(opOptions, (error, response, body) => {
|
51 | if (error) {
|
52 | reject(error);
|
53 | } else if (response.statusCode !== 200) {
|
54 | reject(new Error(`Failed to get schema (${response.statusCode})`));
|
55 | } else {
|
56 | resolve(body);
|
57 | }
|
58 | });
|
59 | }));
|
60 | };
|
61 |
|
62 | this.getResponseSchema = () => {
|
63 | let opOptions = {};
|
64 | opOptions = extend(opOptions, this.options);
|
65 | opOptions.uri += '/schemas/response';
|
66 | return new Promise(((resolve, reject) => {
|
67 | request.get(opOptions, (error, response, body) => {
|
68 | if (error) {
|
69 | reject(error);
|
70 | } else if (response.statusCode !== 200) {
|
71 | reject(new Error(`Failed to get schema (${response.statusCode})`));
|
72 | } else {
|
73 | resolve(body);
|
74 | }
|
75 | });
|
76 | }));
|
77 | };
|
78 |
|
79 | this.start = () => {
|
80 | let opOptions = {};
|
81 | opOptions = extend(opOptions, this.options);
|
82 | opOptions.uri += '?status=started';
|
83 | return new Promise((resolve, reject) => {
|
84 | request.put(opOptions, (error, response, body) => {
|
85 | if (error) {
|
86 | reject(error);
|
87 | } else if (response.statusCode !== 200) {
|
88 | reject(new Error(`Failed to start service (${response.statusCode})`));
|
89 | } else {
|
90 | const json = JSON.parse(body);
|
91 | const status = json.zosConnect.serviceStatus;
|
92 | resolve(status);
|
93 | }
|
94 | });
|
95 | });
|
96 | };
|
97 |
|
98 | this.stop = () => {
|
99 | let opOptions = {};
|
100 | opOptions = extend(opOptions, this.options);
|
101 | opOptions.uri += '?status=stopped';
|
102 | return new Promise((resolve, reject) => {
|
103 | request.put(opOptions, (error, response, body) => {
|
104 | if (error) {
|
105 | reject(error);
|
106 | } else if (response.statusCode !== 200) {
|
107 | reject(new Error(`Failed to start service (${response.statusCode})`));
|
108 | } else {
|
109 | const json = JSON.parse(body);
|
110 | const status = json.zosConnect.serviceStatus;
|
111 | resolve(status);
|
112 | }
|
113 | });
|
114 | });
|
115 | };
|
116 |
|
117 | this.update = (sarFile) => {
|
118 | let opOptions = {};
|
119 | opOptions = extend(opOptions, this.options);
|
120 | return this.stop().then(() => new Promise(((resolve, reject) => {
|
121 | opOptions.method = 'PUT';
|
122 | opOptions.uri += '?status=started';
|
123 | opOptions.body = sarFile;
|
124 | opOptions.headers = {
|
125 | 'Content-Type': 'application/zip',
|
126 | };
|
127 | request(opOptions, (error, response) => {
|
128 | if (error) {
|
129 | reject(error);
|
130 | } else if (response.statusCode !== 200) {
|
131 | reject(new Error(`Unable to update service (${response.statusCode})`));
|
132 | } else {
|
133 | resolve();
|
134 | }
|
135 | });
|
136 | })));
|
137 | };
|
138 |
|
139 | this.delete = () => {
|
140 | let opOptions = {};
|
141 | opOptions = extend(opOptions, this.options);
|
142 | opOptions.method = 'DELETE';
|
143 | return new Promise((resolve, reject) => {
|
144 | request(opOptions, (error, response) => {
|
145 | if (error) {
|
146 | reject(error);
|
147 | } else if (response.statusCode !== 200) {
|
148 | reject(new Error(`Unable to delete service (${response.statusCode})`));
|
149 | } else {
|
150 | resolve();
|
151 | }
|
152 | });
|
153 | });
|
154 | };
|
155 | };
|