|
| 1 | +const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest |
| 2 | +const {log, error} = require('./utils.js') |
| 3 | + |
| 4 | +const baseUrl = randomly_pick_baseurl(process.env.RABBITMQ_URL || 'http://localhost:15672/') |
| 5 | +const otherBaseUrl = randomly_pick_baseurl(process.env.OTHER_RABBITMQ_URL || 'http://localhost:15675/') |
| 6 | +const hostname = process.env.RABBITMQ_HOSTNAME || 'localhost' |
| 7 | +const otherHostname = process.env.OTHER_RABBITMQ_HOSTNAME || 'localhost' |
| 8 | + |
| 9 | +function randomly_pick_baseurl (baseUrl) { |
| 10 | + urls = baseUrl.split(",") |
| 11 | + return urls[getRandomInt(urls.length)] |
| 12 | +} |
| 13 | +function getRandomInt(max) { |
| 14 | + return Math.floor(Math.random() * max) |
| 15 | +} |
| 16 | + |
| 17 | +module.exports = { |
| 18 | + |
| 19 | + getManagementUrl: () => { |
| 20 | + return baseUrl |
| 21 | + }, |
| 22 | + |
| 23 | + geOtherManagementUrl: () => { |
| 24 | + return otherBaseUrl |
| 25 | + }, |
| 26 | + |
| 27 | + setPolicy: (url, vhost, name, pattern, definition, appliedTo = "queues") => { |
| 28 | + let policy = { |
| 29 | + "pattern": pattern, |
| 30 | + "apply-to": appliedTo, |
| 31 | + "definition": definition |
| 32 | + } |
| 33 | + log("Setting policy " + JSON.stringify(policy) |
| 34 | + + " with name " + name + " for vhost " + vhost + " on "+ url) |
| 35 | + const req = new XMLHttpRequest() |
| 36 | + let base64Credentials = btoa('administrator-only' + ":" + 'guest') |
| 37 | + let finalUrl = url + "/api/policies/" + encodeURIComponent(vhost) + "/" + |
| 38 | + encodeURIComponent(name) |
| 39 | + req.open('PUT', finalUrl, false) |
| 40 | + req.setRequestHeader("Authorization", "Basic " + base64Credentials) |
| 41 | + req.setRequestHeader('Content-Type', 'application/json') |
| 42 | + |
| 43 | + req.send(JSON.stringify(policy)) |
| 44 | + if (req.status == 200 || req.status == 204 || req.status == 201) { |
| 45 | + log("Succesfully set policy " + name) |
| 46 | + return |
| 47 | + }else { |
| 48 | + error("status:" + req.status + " : " + req.responseText) |
| 49 | + throw new Error(req.responseText) |
| 50 | + } |
| 51 | + }, |
| 52 | + deletePolicy: (url, vhost, name) => { |
| 53 | + log("Deleting policy " + name + " on vhost " + vhost) |
| 54 | + const req = new XMLHttpRequest() |
| 55 | + let base64Credentials = btoa('administrator-only' + ":" + 'guest') |
| 56 | + let finalUrl = url + "/api/policies/" + encodeURIComponent(vhost) + "/" + |
| 57 | + encodeURIComponent(name) |
| 58 | + req.open('DELETE', finalUrl, false) |
| 59 | + req.setRequestHeader("Authorization", "Basic " + base64Credentials) |
| 60 | + |
| 61 | + req.send() |
| 62 | + if (req.status == 200 || req.status == 204) { |
| 63 | + log("Succesfully deleted policy " + name) |
| 64 | + return |
| 65 | + }else { |
| 66 | + error("status:" + req.status + " : " + req.responseText) |
| 67 | + throw new Error(req.responseText) |
| 68 | + } |
| 69 | + }, |
| 70 | + createVhost: (url, name, description = "", tags = []) => { |
| 71 | + let vhost = { |
| 72 | + "description": description, |
| 73 | + "tags": tags |
| 74 | + } |
| 75 | + log("Create vhost " + JSON.stringify(vhost) |
| 76 | + + " with name " + name + " on " + url) |
| 77 | + const req = new XMLHttpRequest() |
| 78 | + let base64Credentials = btoa('administrator-only' + ":" + 'guest') |
| 79 | + let finalUrl = url + "/api/vhosts/" + encodeURIComponent(name) |
| 80 | + req.open('PUT', finalUrl, false) |
| 81 | + req.setRequestHeader("Authorization", "Basic " + base64Credentials) |
| 82 | + req.setRequestHeader('Content-Type', 'application/json') |
| 83 | + |
| 84 | + req.send(JSON.stringify(vhost)) |
| 85 | + if (req.status == 200 || req.status == 204 || req.status == 201) { |
| 86 | + log("Succesfully created vhost " + name) |
| 87 | + return |
| 88 | + }else { |
| 89 | + error("status:" + req.status + " : " + req.responseText) |
| 90 | + throw new Error(req.responseText) |
| 91 | + } |
| 92 | + }, |
| 93 | + deleteVhost: (url, vhost) => { |
| 94 | + log("Deleting vhost " + vhost) |
| 95 | + const req = new XMLHttpRequest() |
| 96 | + let base64Credentials = btoa('administrator-only' + ":" + 'guest') |
| 97 | + let finalUrl = url + "/api/vhosts/" + encodeURIComponent(vhost) |
| 98 | + req.open('DELETE', finalUrl, false) |
| 99 | + req.setRequestHeader("Authorization", "Basic " + base64Credentials) |
| 100 | + |
| 101 | + req.send() |
| 102 | + if (req.status == 200 || req.status == 204) { |
| 103 | + log("Succesfully deleted vhost " + vhost) |
| 104 | + return |
| 105 | + }else { |
| 106 | + error("status:" + req.status + " : " + req.responseText) |
| 107 | + throw new Error(req.responseText) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | +} |
0 commit comments