blob: 704d0cb5fd906f02ae302b8fe169440b55ab8fff [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2/**
3 *
4 * HTTP functions
5 *
6 */
7
8/**
9 * Make generic HTTP call and return parsed JSON
10 *
11 * @param url URL to make the request against
12 * @param method HTTP method to use (default GET)
13 * @param data JSON data to POST or PUT
14 * @param headers Map of additional request headers
15 */
Jakub Josef79ecec32017-02-17 14:36:28 +010016def sendHttpRequest(url, method = 'GET', data = null, headers = [:]) {
Jakub Josef9a836ac2017-04-24 12:26:02 +020017 def common = new com.mk.mirantis.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +010018 def connection = new URL(url).openConnection()
19 if (method != 'GET') {
20 connection.setRequestMethod(method)
21 }
22
23 if (data) {
24 headers['Content-Type'] = 'application/json'
25 }
26
27 headers['User-Agent'] = 'jenkins-groovy'
28 headers['Accept'] = 'application/json'
29
30 for (header in headers) {
31 connection.setRequestProperty(header.key, header.value)
32 }
33
34 if (data) {
35 connection.setDoOutput(true)
36 if (data instanceof String) {
37 dataStr = data
38 } else {
39 dataStr = new groovy.json.JsonBuilder(data).toString()
40 }
41 def output = new OutputStreamWriter(connection.outputStream)
Jakub Josef9a836ac2017-04-24 12:26:02 +020042 common.debugMsg("[HTTP] Request URL: ${url}, method: ${method}, headers: ${headers}, content: ${dataStr}")
Jakub Josef79ecec32017-02-17 14:36:28 +010043 output.write(dataStr)
44 output.close()
45 }
46
47 if ( connection.responseCode == 200 ) {
48 response = connection.inputStream.text
49 try {
50 response_content = new groovy.json.JsonSlurperClassic().parseText(response)
51 } catch (groovy.json.JsonException e) {
52 response_content = response
53 }
Jakub Josef9a836ac2017-04-24 12:26:02 +020054 common.debugMsg("[HTTP] Response: code ${connection.responseCode}")
Jakub Josef79ecec32017-02-17 14:36:28 +010055 return response_content
56 } else {
Jakub Josef9a836ac2017-04-24 12:26:02 +020057 common.debugMsg("[HTTP] Response: code ${connection.responseCode}")
Jakub Josef79ecec32017-02-17 14:36:28 +010058 throw new Exception(connection.responseCode + ": " + connection.inputStream.text)
59 }
60
61}
62
63/**
64 * Make HTTP GET request
65 *
66 * @param url URL which will requested
67 * @param data JSON data to PUT
68 */
69def sendHttpGetRequest(url, data = null, headers = [:]) {
70 return sendHttpRequest(url, 'GET', data, headers)
71}
72
73/**
74 * Make HTTP POST request
75 *
76 * @param url URL which will requested
77 * @param data JSON data to PUT
78 */
79def sendHttpPostRequest(url, data = null, headers = [:]) {
80 return sendHttpRequest(url, 'POST', data, headers)
81}
82
83/**
84 * Make HTTP PUT request
85 *
86 * @param url URL which will requested
87 * @param data JSON data to PUT
88 */
89def sendHttpPutRequest(url, data = null, headers = [:]) {
90 return sendHttpRequest(url, 'PUT', data, headers)
91}
92
93/**
94 * Make HTTP DELETE request
95 *
96 * @param url URL which will requested
97 * @param data JSON data to PUT
98 */
99def sendHttpDeleteRequest(url, data = null, headers = [:]) {
100 return sendHttpRequest(url, 'DELETE', data, headers)
101}
102
103/**
104 * Make generic call using Salt REST API and return parsed JSON
105 *
106 * @param master Salt connection object
107 * @param uri URI which will be appended to Salt server base URL
108 * @param method HTTP method to use (default GET)
109 * @param data JSON data to POST or PUT
110 * @param headers Map of additional request headers
111 */
112def restCall(master, uri, method = 'GET', data = null, headers = [:]) {
113 def connection = new URL("${master.url}${uri}").openConnection()
114 if (method != 'GET') {
115 connection.setRequestMethod(method)
116 }
117
118 connection.setRequestProperty('User-Agent', 'jenkins-groovy')
119 connection.setRequestProperty('Accept', 'application/json')
120 if (master.authToken) {
121 // XXX: removeme
122 connection.setRequestProperty('X-Auth-Token', master.authToken)
123 }
124
125 for (header in headers) {
126 connection.setRequestProperty(header.key, header.value)
127 }
128
129 if (data) {
130 connection.setDoOutput(true)
131 if (data instanceof String) {
132 dataStr = data
133 } else {
134 connection.setRequestProperty('Content-Type', 'application/json')
135 dataStr = new groovy.json.JsonBuilder(data).toString()
136 }
137 def out = new OutputStreamWriter(connection.outputStream)
138 out.write(dataStr)
139 out.close()
140 }
141
142 if ( connection.responseCode >= 200 && connection.responseCode < 300 ) {
143 res = connection.inputStream.text
144 try {
145 return new groovy.json.JsonSlurperClassic().parseText(res)
146 } catch (Exception e) {
147 return res
148 }
149 } else {
150 throw new Exception(connection.responseCode + ": " + connection.inputStream.text)
151 }
152}
153
154/**
155 * Make GET request using Salt REST API and return parsed JSON
156 *
157 * @param master Salt connection object
158 * @param uri URI which will be appended to Salt server base URL
159 */
160def restGet(master, uri, data = null) {
161 return restCall(master, uri, 'GET', data)
162}
163
164/**
165 * Make POST request using Salt REST API and return parsed JSON
166 *
167 * @param master Salt connection object
168 * @param uri URI which will be appended to Docker server base URL
169 * @param data JSON Data to PUT
170 */
171def restPost(master, uri, data = null) {
172 return restCall(master, uri, 'POST', data, ['Accept': '*/*'])
173}
Jakub Josefab6bf1a2017-03-17 15:46:06 +0100174
175/**
176 * Set HTTP and HTTPS proxy for running JVM
177 * @param host HTTP proxy host
178 * @param port HTTP proxy port
179 * @param nonProxyHosts proxy excluded hosts, optional, default *.local
180 */
181def enableHttpProxy(host, port, nonProxyHosts="*.local"){
182 System.getProperties().put("proxySet", "true")
183 System.getProperties().put("http.proxyHost", host)
184 System.getProperties().put("http.proxyPort", port)
185 System.getProperties().put("https.proxyHost", host)
186 System.getProperties().put("https.proxyPort", port)
187 System.getProperties().put("http.nonProxyHosts", nonProxyHosts)
188 System.getProperties().put("https.nonProxyHosts", nonProxyHosts)
189}
190/**
191 * Disable HTTP and HTTPS proxy for running JVM
192 */
193def disableHttpProxy(){
194 System.getProperties().put("proxySet", "false")
195 System.getProperties().remove("http.proxyHost")
196 System.getProperties().remove("http.proxyPort")
197 System.getProperties().remove("https.proxyHost")
198 System.getProperties().remove("https.proxyPort")
199 System.getProperties().remove("http.nonProxyHosts")
200 System.getProperties().remove("https.nonProxyHosts")
201}