1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Helper;
/**
* Helper class to access RabbitMQ server configuration
*/
class Amqp
{
const CONFIG_PATH_HOST = 'queue/amqp/host';
const CONFIG_PATH_USER = 'queue/amqp/user';
const CONFIG_PATH_PASSWORD = 'queue/amqp/password';
const DEFAULT_MANAGEMENT_PORT = '15672';
/**
* @var Curl
*/
private $curl;
/**
* @var \Magento\Framework\App\DeploymentConfig
*/
private $deploymentConfig;
/**
* RabbitMQ API host
*
* @var string
*/
private $host;
/**
* Initialize dependencies.
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
*/
public function __construct(
\Magento\Framework\App\DeploymentConfig $deploymentConfig = null
) {
$this->deploymentConfig = $deploymentConfig ?? \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\App\DeploymentConfig::class);
$this->curl = new Curl();
$this->curl->setCredentials(
$this->deploymentConfig->get(self::CONFIG_PATH_USER),
$this->deploymentConfig->get(self::CONFIG_PATH_PASSWORD)
);
$this->curl->addHeader('content-type', 'application/json');
$this->host = sprintf(
'http://%s:%s/api/',
$this->deploymentConfig->get(self::CONFIG_PATH_HOST),
defined('RABBITMQ_MANAGEMENT_PORT') ? RABBITMQ_MANAGEMENT_PORT : self::DEFAULT_MANAGEMENT_PORT
);
}
/**
* Check that the RabbitMQ instance has the management plugin installed and the api is available.
*
* @return bool
*/
public function isAvailable(): bool
{
$this->curl->get($this->host . 'overview');
$data = $this->curl->getBody();
$data = json_decode($data, true);
return isset($data['management_version']);
}
/**
* Get declared exchanges.
*
* @return array
*/
public function getExchanges()
{
$this->curl->get($this->host . 'exchanges');
$data = $this->curl->getBody();
$data = json_decode($data, true);
$output = [];
foreach ($data as $value) {
$output[$value['name']] = $value;
}
return $output;
}
/**
* Get declared exchange bindings.
*
* @param string $name
* @return array
*/
public function getExchangeBindings($name)
{
$this->curl->get($this->host . 'exchanges/%2f/' . $name . '/bindings/source');
$data = $this->curl->getBody();
return json_decode($data, true);
}
/**
* Get All available connections
*
* @return array
*/
public function getConnections()
{
$this->curl->get($this->host . 'connections');
$data = $this->curl->getBody();
$data = json_decode($data, true);
$output = [];
foreach ($data as $value) {
$output[$value['name']] = $value;
}
return $output;
}
/**
* Clear Queue
*
* @param string $name
* @param int $numMessages
* @return string
*/
public function clearQueue(string $name, int $numMessages = 50)
{
$body = [
"count" => $numMessages,
"ackmode" => "ack_requeue_false",
"encoding" => "auto",
"truncate" => 50000
];
$this->curl->post($this->host . 'queue/%2f/' . $name . '/get', json_encode($body));
return $this->curl->getBody();
}
/**
* Delete connection
*
* @param string $name
* @return string $data
*/
public function deleteConnection($name)
{
$this->curl->delete($this->host . 'conections/' . urlencode($name));
$data = $this->curl->getBody();
return $data;
}
}