|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\NewRelic\Business\Model; |
|
9
|
|
|
|
|
10
|
|
|
use GuzzleHttp\Client; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException; |
|
13
|
|
|
|
|
14
|
|
|
class RecordDeployment implements RecordDeploymentInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const STATUS_CODE_SUCCESS = 200; |
|
17
|
|
|
const STATUS_CODE_REDIRECTION = 300; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
* |
|
22
|
|
|
* @example https://api.eu.newrelic.com/v2/applications/12345/deployments.json |
|
23
|
|
|
* @example https://api.eu.newrelic.com/v2/applications/%s/deployments.json |
|
24
|
|
|
* |
|
25
|
|
|
* @see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-deployments |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $newRelicDeploymentApiUrl; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $newRelicApiKey; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $newRelicApplicationIds; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $newRelicDeploymentApiUrl |
|
41
|
|
|
* @param string $newRelicApiKey |
|
42
|
|
|
* @param array $newRelicApplicationIds |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
string $newRelicDeploymentApiUrl, |
|
46
|
|
|
string $newRelicApiKey, |
|
47
|
|
|
array $newRelicApplicationIds = [] |
|
48
|
|
|
) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->newRelicDeploymentApiUrl = $newRelicDeploymentApiUrl; |
|
51
|
|
|
$this->newRelicApiKey = $newRelicApiKey; |
|
52
|
|
|
$this->newRelicApplicationIds = $newRelicApplicationIds; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param array $arguments |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function recordDeployment(array $arguments = []): RecordDeploymentInterface |
|
63
|
|
|
{ |
|
64
|
|
|
if (empty($this->newRelicApplicationIds)) { |
|
65
|
|
|
return $this->recordSingleDeployment($arguments); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($this->newRelicApplicationIds as $singleApplicationId) { |
|
69
|
|
|
$arguments['application_id'] = $singleApplicationId; |
|
70
|
|
|
$this->recordSingleDeployment($arguments); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $params |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function createRecordDeploymentRequest(array $params): ResponseInterface |
|
83
|
|
|
{ |
|
84
|
|
|
$applicationId = $params['application_id'] ?? null; |
|
85
|
|
|
|
|
86
|
|
|
unset($params['app_name']); |
|
87
|
|
|
unset($params['application_id']); |
|
88
|
|
|
|
|
89
|
|
|
$options = [ |
|
90
|
|
|
'headers' => [ |
|
91
|
|
|
'X-Api-Key' => $this->newRelicApiKey, |
|
92
|
|
|
], |
|
93
|
|
|
'json' => [ |
|
94
|
|
|
'deployment' => $params, |
|
95
|
|
|
], |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
$httpClient = new Client(); |
|
99
|
|
|
|
|
100
|
|
|
$deploymentUrl = $this->newRelicDeploymentApiUrl; |
|
101
|
|
|
if ($applicationId) { |
|
102
|
|
|
$deploymentUrl = sprintf($this->newRelicDeploymentApiUrl, $applicationId); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$request = $httpClient->post($deploymentUrl, $options); |
|
106
|
|
|
|
|
107
|
|
|
return $request; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array $arguments |
|
112
|
|
|
* |
|
113
|
|
|
* @throws \SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
private function recordSingleDeployment(array $arguments=[]): RecordDeploymentInterface |
|
118
|
|
|
{ |
|
119
|
|
|
$response = $this->createRecordDeploymentRequest($arguments); |
|
120
|
|
|
$statusCode = $response->getStatusCode(); |
|
121
|
|
|
if ($statusCode < static::STATUS_CODE_SUCCESS || $statusCode >= static::STATUS_CODE_REDIRECTION) { |
|
122
|
|
|
throw new RecordDeploymentException(sprintf( |
|
123
|
|
|
'Record deployment to New Relic request failed with code %d. %s', |
|
124
|
|
|
$response->getStatusCode(), |
|
125
|
|
|
$response->getBody() |
|
126
|
|
|
)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|