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