Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package 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 Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 15 | * @param read_timeout http session read timeout |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 16 | */ |
Jakub Josef | db779dd | 2017-04-24 12:58:33 +0200 | [diff] [blame] | 17 | @NonCPS |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 18 | def sendHttpRequest(url, method = 'GET', data = null, headers = [:], read_timeout=-1) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 19 | def connection = new URL(url).openConnection() |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 20 | |
| 21 | if (read_timeout != -1){ |
| 22 | connection.setReadTimeout(read_timeout*1000) |
| 23 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 24 | 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 Josef | 66976f6 | 2017-04-24 16:32:23 +0200 | [diff] [blame] | 46 | if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){ |
| 47 | println("[HTTP] Request URL: ${url}, method: ${method}, headers: ${headers}, content: ${dataStr}") |
| 48 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 49 | def output = new OutputStreamWriter(connection.outputStream) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 50 | 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 Josef | 66976f6 | 2017-04-24 16:32:23 +0200 | [diff] [blame] | 61 | if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){ |
| 62 | println("[HTTP] Response: code ${connection.responseCode}") |
| 63 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 64 | return response_content |
| 65 | } else { |
Jakub Josef | 66976f6 | 2017-04-24 16:32:23 +0200 | [diff] [blame] | 66 | if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){ |
| 67 | println("[HTTP] Response: code ${connection.responseCode}") |
| 68 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 69 | throw new Exception(connection.responseCode + ": " + connection.inputStream.text) |
| 70 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Make HTTP GET request |
| 75 | * |
| 76 | * @param url URL which will requested |
| 77 | * @param data JSON data to PUT |
| 78 | */ |
| 79 | def 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 Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 88 | * @param read_timeout http session read timeout |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 89 | */ |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 90 | def sendHttpPostRequest(url, data = null, headers = [:], read_timeout=-1) { |
| 91 | return sendHttpRequest(url, 'POST', data, headers, read_timeout) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Make HTTP PUT request |
| 96 | * |
| 97 | * @param url URL which will requested |
| 98 | * @param data JSON data to PUT |
| 99 | */ |
| 100 | def 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 | */ |
| 110 | def 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 | */ |
| 123 | def 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 | */ |
| 171 | def 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 | */ |
| 182 | def restPost(master, uri, data = null) { |
| 183 | return restCall(master, uri, 'POST', data, ['Accept': '*/*']) |
| 184 | } |
Jakub Josef | ab6bf1a | 2017-03-17 15:46:06 +0100 | [diff] [blame] | 185 | |
| 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 | */ |
| 192 | def 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 | */ |
| 204 | def 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 | } |