ConfigResourceWaiter.php
7.36 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
namespace Aws\Common\Waiter;
use Aws\Common\Exception\InvalidArgumentException;
use Aws\Common\Exception\RuntimeException;
use Aws\Common\Exception\ServiceResponseException;
use Guzzle\Service\Resource\Model;
use Guzzle\Service\Exception\ValidationException;
/**
* Resource waiter driven by configuration options
*/
class ConfigResourceWaiter extends AbstractResourceWaiter
{
/**
* @var WaiterConfig Waiter configuration
*/
protected $waiterConfig;
/**
* @param WaiterConfig $waiterConfig Waiter configuration
*/
public function __construct(WaiterConfig $waiterConfig)
{
$this->waiterConfig = $waiterConfig;
$this->setInterval($waiterConfig->get(WaiterConfig::INTERVAL));
$this->setMaxAttempts($waiterConfig->get(WaiterConfig::MAX_ATTEMPTS));
}
/**
* {@inheritdoc}
*/
public function setConfig(array $config)
{
foreach ($config as $key => $value) {
if (substr($key, 0, 7) == 'waiter.') {
$this->waiterConfig->set(substr($key, 7), $value);
}
}
if (!isset($config[self::INTERVAL])) {
$config[self::INTERVAL] = $this->waiterConfig->get(WaiterConfig::INTERVAL);
}
if (!isset($config[self::MAX_ATTEMPTS])) {
$config[self::MAX_ATTEMPTS] = $this->waiterConfig->get(WaiterConfig::MAX_ATTEMPTS);
}
return parent::setConfig($config);
}
/**
* Get the waiter's configuration data
*
* @return WaiterConfig
*/
public function getWaiterConfig()
{
return $this->waiterConfig;
}
/**
* {@inheritdoc}
*/
protected function doWait()
{
$params = $this->config;
// remove waiter settings from the operation's input
foreach (array_keys($params) as $key) {
if (substr($key, 0, 7) == 'waiter.') {
unset($params[$key]);
}
}
$operation = $this->client->getCommand($this->waiterConfig->get(WaiterConfig::OPERATION), $params);
try {
return $this->checkResult($this->client->execute($operation));
} catch (ValidationException $e) {
throw new InvalidArgumentException(
$this->waiterConfig->get(WaiterConfig::WAITER_NAME) . ' waiter validation failed: ' . $e->getMessage(),
$e->getCode(),
$e
);
} catch (ServiceResponseException $e) {
// Check if this exception satisfies a success or failure acceptor
$transition = $this->checkErrorAcceptor($e);
if (null !== $transition) {
return $transition;
}
// Check if this exception should be ignored
foreach ((array) $this->waiterConfig->get(WaiterConfig::IGNORE_ERRORS) as $ignore) {
if ($e->getExceptionCode() == $ignore) {
// This exception is ignored, so it counts as a failed attempt rather than a fast-fail
return false;
}
}
// Allow non-ignore exceptions to bubble through
throw $e;
}
}
/**
* Check if an exception satisfies a success or failure acceptor
*
* @param ServiceResponseException $e
*
* @return bool|null Returns true for success, false for failure, and null for no transition
*/
protected function checkErrorAcceptor(ServiceResponseException $e)
{
if ($this->waiterConfig->get(WaiterConfig::SUCCESS_TYPE) == 'error') {
if ($e->getExceptionCode() == $this->waiterConfig->get(WaiterConfig::SUCCESS_VALUE)) {
// Mark as a success
return true;
}
}
// Mark as an attempt
return null;
}
/**
* Check to see if the response model satisfies a success or failure state
*
* @param Model $result Result model
*
* @return bool
* @throws RuntimeException
*/
protected function checkResult(Model $result)
{
// Check if the result evaluates to true based on the path and output model
if ($this->waiterConfig->get(WaiterConfig::SUCCESS_TYPE) == 'output' &&
$this->checkPath(
$result,
$this->waiterConfig->get(WaiterConfig::SUCCESS_PATH),
$this->waiterConfig->get(WaiterConfig::SUCCESS_VALUE)
)
) {
return true;
}
// It did not finish waiting yet. Determine if we need to fail-fast based on the failure acceptor.
if ($this->waiterConfig->get(WaiterConfig::FAILURE_TYPE) == 'output') {
$failureValue = $this->waiterConfig->get(WaiterConfig::FAILURE_VALUE);
if ($failureValue) {
$key = $this->waiterConfig->get(WaiterConfig::FAILURE_PATH);
if ($this->checkPath($result, $key, $failureValue, false)) {
// Determine which of the results triggered the failure
$triggered = array_intersect(
(array) $this->waiterConfig->get(WaiterConfig::FAILURE_VALUE),
array_unique((array) $result->getPath($key))
);
// fast fail because the failure case was satisfied
throw new RuntimeException(
'A resource entered into an invalid state of "'
. implode(', ', $triggered) . '" while waiting with the "'
. $this->waiterConfig->get(WaiterConfig::WAITER_NAME) . '" waiter.'
);
}
}
}
return false;
}
/**
* Check to see if the path of the output key is satisfied by the value
*
* @param Model $model Result model
* @param string $key Key to check
* @param string $checkValue Compare the key to the value
* @param bool $all Set to true to ensure all value match or false to only match one
*
* @return bool
*/
protected function checkPath(Model $model, $key = null, $checkValue = array(), $all = true)
{
// If no key is set, then just assume true because the request succeeded
if (!$key) {
return true;
}
if (!($result = $model->getPath($key))) {
return false;
}
$total = $matches = 0;
foreach ((array) $result as $value) {
$total++;
foreach ((array) $checkValue as $check) {
if ($value == $check) {
$matches++;
break;
}
}
}
// When matching all values, ensure that the match count matches the total count
if ($all && $total != $matches) {
return false;
}
return $matches > 0;
}
}