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