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