blob: 7e22ca6c6d140ef42a2b2c77f4eb87d8d288dd3b [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
Vasyl Saienkoe36ab7c2017-07-17 14:35:48 +030015 * @param read_timeout http session read timeout
Jakub Josef79ecec32017-02-17 14:36:28 +010016 */
Jakub Josefdb779dd2017-04-24 12:58:33 +020017@NonCPS
Vasyl Saienkoe36ab7c2017-07-17 14:35:48 +030018def sendHttpRequest(url, method = 'GET', data = null, headers = [:], read_timeout=-1) {
Jakub Josef79ecec32017-02-17 14:36:28 +010019 def connection = new URL(url).openConnection()
Vasyl Saienkoe36ab7c2017-07-17 14:35:48 +030020
21 if (read_timeout != -1){
22 connection.setReadTimeout(read_timeout*1000)
23 }
Jakub Josef79ecec32017-02-17 14:36:28 +010024 if (method != 'GET') {
25 connection.setRequestMethod(method)
26 }
27
28 if (data) {
29 headers['Content-Type'] = 'application/json'
30 }
31
32 headers['User-Agent'] = 'jenkins-groovy'
33 headers['Accept'] = 'application/json'
34
35 for (header in headers) {
36 connection.setRequestProperty(header.key, header.value)
37 }
38
39 if (data) {
40 connection.setDoOutput(true)
41 if (data instanceof String) {
42 dataStr = data
43 } else {
44 dataStr = new groovy.json.JsonBuilder(data).toString()
45 }
Jakub Josef66976f62017-04-24 16:32:23 +020046 if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){
47 println("[HTTP] Request URL: ${url}, method: ${method}, headers: ${headers}, content: ${dataStr}")
48 }
Jakub Josef79ecec32017-02-17 14:36:28 +010049 def output = new OutputStreamWriter(connection.outputStream)
Jakub Josef79ecec32017-02-17 14:36:28 +010050 output.write(dataStr)
51 output.close()
52 }
53
54 if ( connection.responseCode == 200 ) {
55 response = connection.inputStream.text
56 try {
57 response_content = new groovy.json.JsonSlurperClassic().parseText(response)
58 } catch (groovy.json.JsonException e) {
59 response_content = response
60 }
Jakub Josef66976f62017-04-24 16:32:23 +020061 if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){
62 println("[HTTP] Response: code ${connection.responseCode}")
63 }
Jakub Josef79ecec32017-02-17 14:36:28 +010064 return response_content
65 } else {
Jakub Josef66976f62017-04-24 16:32:23 +020066 if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){
67 println("[HTTP] Response: code ${connection.responseCode}")
68 }
Jakub Josef79ecec32017-02-17 14:36:28 +010069 throw new Exception(connection.responseCode + ": " + connection.inputStream.text)
70 }
Jakub Josef79ecec32017-02-17 14:36:28 +010071}
72
73/**
74 * Make HTTP GET request
75 *
76 * @param url URL which will requested
77 * @param data JSON data to PUT
78 */
79def sendHttpGetRequest(url, data = null, headers = [:]) {
80 return sendHttpRequest(url, 'GET', data, headers)
81}
82
83/**
84 * Make HTTP POST request
85 *
86 * @param url URL which will requested
87 * @param data JSON data to PUT
Vasyl Saienkoe36ab7c2017-07-17 14:35:48 +030088 * @param read_timeout http session read timeout
Jakub Josef79ecec32017-02-17 14:36:28 +010089 */
Vasyl Saienkoe36ab7c2017-07-17 14:35:48 +030090def sendHttpPostRequest(url, data = null, headers = [:], read_timeout=-1) {
91 return sendHttpRequest(url, 'POST', data, headers, read_timeout)
Jakub Josef79ecec32017-02-17 14:36:28 +010092}
93
94/**
95 * Make HTTP PUT request
96 *
97 * @param url URL which will requested
98 * @param data JSON data to PUT
99 */
100def sendHttpPutRequest(url, data = null, headers = [:]) {
101 return sendHttpRequest(url, 'PUT', data, headers)
102}
103
104/**
105 * Make HTTP DELETE request
106 *
107 * @param url URL which will requested
108 * @param data JSON data to PUT
109 */
110def sendHttpDeleteRequest(url, data = null, headers = [:]) {
111 return sendHttpRequest(url, 'DELETE', data, headers)
112}
113
114/**
115 * Make generic call using Salt REST API and return parsed JSON
116 *
117 * @param master Salt connection object
118 * @param uri URI which will be appended to Salt server base URL
119 * @param method HTTP method to use (default GET)
120 * @param data JSON data to POST or PUT
121 * @param headers Map of additional request headers
122 */
123def restCall(master, uri, method = 'GET', data = null, headers = [:]) {
124 def connection = new URL("${master.url}${uri}").openConnection()
125 if (method != 'GET') {
126 connection.setRequestMethod(method)
127 }
128
129 connection.setRequestProperty('User-Agent', 'jenkins-groovy')
130 connection.setRequestProperty('Accept', 'application/json')
131 if (master.authToken) {
132 // XXX: removeme
133 connection.setRequestProperty('X-Auth-Token', master.authToken)
134 }
135
136 for (header in headers) {
137 connection.setRequestProperty(header.key, header.value)
138 }
139
140 if (data) {
141 connection.setDoOutput(true)
142 if (data instanceof String) {
143 dataStr = data
144 } else {
145 connection.setRequestProperty('Content-Type', 'application/json')
146 dataStr = new groovy.json.JsonBuilder(data).toString()
147 }
148 def out = new OutputStreamWriter(connection.outputStream)
149 out.write(dataStr)
150 out.close()
151 }
152
153 if ( connection.responseCode >= 200 && connection.responseCode < 300 ) {
154 res = connection.inputStream.text
155 try {
156 return new groovy.json.JsonSlurperClassic().parseText(res)
157 } catch (Exception e) {
158 return res
159 }
160 } else {
161 throw new Exception(connection.responseCode + ": " + connection.inputStream.text)
162 }
163}
164
165/**
166 * Make GET request using Salt REST API and return parsed JSON
167 *
168 * @param master Salt connection object
169 * @param uri URI which will be appended to Salt server base URL
170 */
171def restGet(master, uri, data = null) {
172 return restCall(master, uri, 'GET', data)
173}
174
175/**
176 * Make POST request using Salt REST API and return parsed JSON
177 *
178 * @param master Salt connection object
179 * @param uri URI which will be appended to Docker server base URL
180 * @param data JSON Data to PUT
181 */
182def restPost(master, uri, data = null) {
183 return restCall(master, uri, 'POST', data, ['Accept': '*/*'])
184}
Jakub Josefab6bf1a2017-03-17 15:46:06 +0100185
186/**
187 * Set HTTP and HTTPS proxy for running JVM
188 * @param host HTTP proxy host
189 * @param port HTTP proxy port
190 * @param nonProxyHosts proxy excluded hosts, optional, default *.local
191 */
192def enableHttpProxy(host, port, nonProxyHosts="*.local"){
193 System.getProperties().put("proxySet", "true")
194 System.getProperties().put("http.proxyHost", host)
195 System.getProperties().put("http.proxyPort", port)
196 System.getProperties().put("https.proxyHost", host)
197 System.getProperties().put("https.proxyPort", port)
198 System.getProperties().put("http.nonProxyHosts", nonProxyHosts)
199 System.getProperties().put("https.nonProxyHosts", nonProxyHosts)
200}
201/**
202 * Disable HTTP and HTTPS proxy for running JVM
203 */
204def disableHttpProxy(){
205 System.getProperties().put("proxySet", "false")
206 System.getProperties().remove("http.proxyHost")
207 System.getProperties().remove("http.proxyPort")
208 System.getProperties().remove("https.proxyHost")
209 System.getProperties().remove("https.proxyPort")
210 System.getProperties().remove("http.nonProxyHosts")
211 System.getProperties().remove("https.nonProxyHosts")
212}