Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70eceb2

Browse files
authored
Added CloudFormation DescribeStackDriftDetectionStatus (async-aws#1147)
* Added CloudFormation DescribeStackDriftDetectionStatus * Update readme * Fix lint issue * Fix cs issue again * Update changelog
1 parent f67f9c2 commit 70eceb2

12 files changed

Lines changed: 379 additions & 3 deletions

docs/clients/cf.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ foreach ($result->getStackEvents() as $event) {
3737
echo $event->getResourceType().'-'.$event->getResourceStatus().PHP_EOL;
3838
}
3939
```
40+
41+
### Detect Stack Drift Status
42+
43+
```php
44+
use AsyncAws\CloudFormation\CloudFormationClient;
45+
use AsyncAws\CloudFormation\Input\DescribeStackEventsInput;
46+
47+
$cloudFormation = new CloudFormationClient();
48+
49+
$driftStatus = $cloudFormation->describeStackDriftDetectionStatus([
50+
'StackDriftDetectionId' => 'b78ac9b0-dec1-11e7-a451-503a3example',
51+
]);
52+
```

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"example": "https://raw.githubusercontent.com/aws/aws-sdk-php/${LATEST}/src/data/cloudformation/2010-05-15/examples-1.json",
3131
"api-reference": "https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference",
3232
"methods": [
33+
"DescribeStackDriftDetectionStatus",
3334
"DescribeStackEvents",
3435
"DescribeStacks"
3536
]

src/Service/CloudFormation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- AWS api-change: Added `us-iso-west-1` region
88
- AWS api-change: Use specific configuration for `us` regions
99
- AWS enhancement: Documentation updates.
10+
- Added `describeStackDriftDetectionStatus` method.
1011

1112
## 1.1.0
1213

src/Service/CloudFormation/src/CloudFormationClient.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace AsyncAws\CloudFormation;
44

5+
use AsyncAws\CloudFormation\Input\DescribeStackDriftDetectionStatusInput;
56
use AsyncAws\CloudFormation\Input\DescribeStackEventsInput;
67
use AsyncAws\CloudFormation\Input\DescribeStacksInput;
8+
use AsyncAws\CloudFormation\Result\DescribeStackDriftDetectionStatusOutput;
79
use AsyncAws\CloudFormation\Result\DescribeStackEventsOutput;
810
use AsyncAws\CloudFormation\Result\DescribeStacksOutput;
911
use AsyncAws\CloudFormation\ValueObject\Stack;
@@ -15,6 +17,30 @@
1517

1618
class CloudFormationClient extends AbstractApi
1719
{
20+
/**
21+
* Returns information about a stack drift detection operation. A stack drift detection operation detects whether a
22+
* stack's actual configuration differs, or has *drifted*, from it's expected configuration, as defined in the stack
23+
* template and any values specified as template parameters. A stack is considered to have drifted if one or more of its
24+
* resources have drifted. For more information on stack and resource drift, see Detecting Unregulated Configuration
25+
* Changes to Stacks and Resources.
26+
*
27+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html
28+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_DescribeStackDriftDetectionStatus.html
29+
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-cloudformation-2010-05-15.html#describestackdriftdetectionstatus
30+
*
31+
* @param array{
32+
* StackDriftDetectionId: string,
33+
* @region?: string,
34+
* }|DescribeStackDriftDetectionStatusInput $input
35+
*/
36+
public function describeStackDriftDetectionStatus($input): DescribeStackDriftDetectionStatusOutput
37+
{
38+
$input = DescribeStackDriftDetectionStatusInput::create($input);
39+
$response = $this->getResponse($input->request(), new RequestContext(['operation' => 'DescribeStackDriftDetectionStatus', 'region' => $input->getRegion()]));
40+
41+
return new DescribeStackDriftDetectionStatusOutput($response);
42+
}
43+
1844
/**
1945
* Returns all stack related events for a specified stack in reverse chronological order. For more information about a
2046
* stack's event history, go to Stacks in the CloudFormation User Guide.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace AsyncAws\CloudFormation\Enum;
4+
5+
/**
6+
* The status of the stack drift detection operation.
7+
*
8+
* - `DETECTION_COMPLETE`: The stack drift detection operation has successfully completed for all resources in the stack
9+
* that support drift detection. (Resources that do not currently support stack detection remain unchecked.)
10+
* If you specified logical resource IDs for CloudFormation to use as a filter for the stack drift detection
11+
* operation, only the resources with those logical IDs are checked for drift.
12+
* - `DETECTION_FAILED`: The stack drift detection operation has failed for at least one resource in the stack. Results
13+
* will be available for resources on which CloudFormation successfully completed drift detection.
14+
* - `DETECTION_IN_PROGRESS`: The stack drift detection operation is currently in progress.
15+
*/
16+
final class StackDriftDetectionStatus
17+
{
18+
public const DETECTION_COMPLETE = 'DETECTION_COMPLETE';
19+
public const DETECTION_FAILED = 'DETECTION_FAILED';
20+
public const DETECTION_IN_PROGRESS = 'DETECTION_IN_PROGRESS';
21+
22+
public static function exists(string $value): bool
23+
{
24+
return isset([
25+
self::DETECTION_COMPLETE => true,
26+
self::DETECTION_FAILED => true,
27+
self::DETECTION_IN_PROGRESS => true,
28+
][$value]);
29+
}
30+
}

src/Service/CloudFormation/src/Enum/StackDriftStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace AsyncAws\CloudFormation\Enum;
44

55
/**
6-
* Status of the stack's actual configuration compared to its expected template configuration.
6+
* Status of the stack's actual configuration compared to its expected configuration.
77
*
88
* - `DRIFTED`: The stack differs from its expected template configuration. A stack is considered to have drifted if one
99
* or more of its resources have drifted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace AsyncAws\CloudFormation\Input;
4+
5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
use AsyncAws\Core\Input;
7+
use AsyncAws\Core\Request;
8+
use AsyncAws\Core\Stream\StreamFactory;
9+
10+
final class DescribeStackDriftDetectionStatusInput extends Input
11+
{
12+
/**
13+
* The ID of the drift detection results of this operation.
14+
*
15+
* @required
16+
*
17+
* @var string|null
18+
*/
19+
private $stackDriftDetectionId;
20+
21+
/**
22+
* @param array{
23+
* StackDriftDetectionId?: string,
24+
* @region?: string,
25+
* } $input
26+
*/
27+
public function __construct(array $input = [])
28+
{
29+
$this->stackDriftDetectionId = $input['StackDriftDetectionId'] ?? null;
30+
parent::__construct($input);
31+
}
32+
33+
public static function create($input): self
34+
{
35+
return $input instanceof self ? $input : new self($input);
36+
}
37+
38+
public function getStackDriftDetectionId(): ?string
39+
{
40+
return $this->stackDriftDetectionId;
41+
}
42+
43+
/**
44+
* @internal
45+
*/
46+
public function request(): Request
47+
{
48+
// Prepare headers
49+
$headers = ['content-type' => 'application/x-www-form-urlencoded'];
50+
51+
// Prepare query
52+
$query = [];
53+
54+
// Prepare URI
55+
$uriString = '/';
56+
57+
// Prepare Body
58+
$body = http_build_query(['Action' => 'DescribeStackDriftDetectionStatus', 'Version' => '2010-05-15'] + $this->requestBody(), '', '&', \PHP_QUERY_RFC1738);
59+
60+
// Return the Request
61+
return new Request('POST', $uriString, $query, $headers, StreamFactory::create($body));
62+
}
63+
64+
public function setStackDriftDetectionId(?string $value): self
65+
{
66+
$this->stackDriftDetectionId = $value;
67+
68+
return $this;
69+
}
70+
71+
private function requestBody(): array
72+
{
73+
$payload = [];
74+
if (null === $v = $this->stackDriftDetectionId) {
75+
throw new InvalidArgument(sprintf('Missing parameter "StackDriftDetectionId" for "%s". The value cannot be null.', __CLASS__));
76+
}
77+
$payload['StackDriftDetectionId'] = $v;
78+
79+
return $payload;
80+
}
81+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace AsyncAws\CloudFormation\Result;
4+
5+
use AsyncAws\CloudFormation\Enum\StackDriftDetectionStatus;
6+
use AsyncAws\CloudFormation\Enum\StackDriftStatus;
7+
use AsyncAws\Core\Response;
8+
use AsyncAws\Core\Result;
9+
10+
class DescribeStackDriftDetectionStatusOutput extends Result
11+
{
12+
/**
13+
* The ID of the stack.
14+
*/
15+
private $stackId;
16+
17+
/**
18+
* The ID of the drift detection results of this operation.
19+
*/
20+
private $stackDriftDetectionId;
21+
22+
/**
23+
* Status of the stack's actual configuration compared to its expected configuration.
24+
*/
25+
private $stackDriftStatus;
26+
27+
/**
28+
* The status of the stack drift detection operation.
29+
*/
30+
private $detectionStatus;
31+
32+
/**
33+
* The reason the stack drift detection operation has its current status.
34+
*/
35+
private $detectionStatusReason;
36+
37+
/**
38+
* Total number of stack resources that have drifted. This is NULL until the drift detection operation reaches a status
39+
* of `DETECTION_COMPLETE`. This value will be 0 for stacks whose drift status is `IN_SYNC`.
40+
*/
41+
private $driftedStackResourceCount;
42+
43+
/**
44+
* Time at which the stack drift detection operation was initiated.
45+
*/
46+
private $timestamp;
47+
48+
/**
49+
* @return StackDriftDetectionStatus::*
50+
*/
51+
public function getDetectionStatus(): string
52+
{
53+
$this->initialize();
54+
55+
return $this->detectionStatus;
56+
}
57+
58+
public function getDetectionStatusReason(): ?string
59+
{
60+
$this->initialize();
61+
62+
return $this->detectionStatusReason;
63+
}
64+
65+
public function getDriftedStackResourceCount(): ?int
66+
{
67+
$this->initialize();
68+
69+
return $this->driftedStackResourceCount;
70+
}
71+
72+
public function getStackDriftDetectionId(): string
73+
{
74+
$this->initialize();
75+
76+
return $this->stackDriftDetectionId;
77+
}
78+
79+
/**
80+
* @return StackDriftStatus::*|null
81+
*/
82+
public function getStackDriftStatus(): ?string
83+
{
84+
$this->initialize();
85+
86+
return $this->stackDriftStatus;
87+
}
88+
89+
public function getStackId(): string
90+
{
91+
$this->initialize();
92+
93+
return $this->stackId;
94+
}
95+
96+
public function getTimestamp(): \DateTimeImmutable
97+
{
98+
$this->initialize();
99+
100+
return $this->timestamp;
101+
}
102+
103+
protected function populateResult(Response $response): void
104+
{
105+
$data = new \SimpleXMLElement($response->getContent());
106+
$data = $data->DescribeStackDriftDetectionStatusResult;
107+
108+
$this->stackId = (string) $data->StackId;
109+
$this->stackDriftDetectionId = (string) $data->StackDriftDetectionId;
110+
$this->stackDriftStatus = ($v = $data->StackDriftStatus) ? (string) $v : null;
111+
$this->detectionStatus = (string) $data->DetectionStatus;
112+
$this->detectionStatusReason = ($v = $data->DetectionStatusReason) ? (string) $v : null;
113+
$this->driftedStackResourceCount = ($v = $data->DriftedStackResourceCount) ? (int) (string) $v : null;
114+
$this->timestamp = new \DateTimeImmutable((string) $data->Timestamp);
115+
}
116+
}

src/Service/CloudFormation/tests/Integration/CloudFormationClientTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,39 @@
33
namespace AsyncAws\CloudFormation\Tests\Integration;
44

55
use AsyncAws\CloudFormation\CloudFormationClient;
6+
use AsyncAws\CloudFormation\Input\DescribeStackDriftDetectionStatusInput;
67
use AsyncAws\CloudFormation\Input\DescribeStackEventsInput;
78
use AsyncAws\CloudFormation\Input\DescribeStacksInput;
89
use AsyncAws\Core\Credentials\Credentials;
910
use AsyncAws\Core\Exception\Http\ClientException;
10-
use PHPUnit\Framework\TestCase;
11+
use AsyncAws\Core\Test\TestCase;
1112

1213
class CloudFormationClientTest extends TestCase
1314
{
15+
public function testDescribeStackDriftDetectionStatus(): void
16+
{
17+
self::markTestSkipped('The CloudFormation Docker image does not implement StackDrifts.');
18+
19+
$client = $this->getClient();
20+
21+
$input = new DescribeStackDriftDetectionStatusInput([
22+
'StackDriftDetectionId' => 'b78ac9b0-dec1-11e7-a451-503a3example',
23+
]);
24+
25+
$result = $client->describeStackDriftDetectionStatus($input);
26+
27+
self::expectException(ClientException::class);
28+
29+
$result->resolve();
30+
31+
self::assertSame('changeIt', $result->getStackId());
32+
self::assertSame('changeIt', $result->getStackDriftDetectionId());
33+
self::assertSame('changeIt', $result->getStackDriftStatus());
34+
self::assertSame('changeIt', $result->getDetectionStatus());
35+
self::assertSame('changeIt', $result->getDetectionStatusReason());
36+
self::assertSame(1337, $result->getDriftedStackResourceCount());
37+
}
38+
1439
public function testDescribeStackEvents(): void
1540
{
1641
$client = $this->getClient();

src/Service/CloudFormation/tests/Unit/CloudFormationClientTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@
33
namespace AsyncAws\CloudFormation\Tests\Unit;
44

55
use AsyncAws\CloudFormation\CloudFormationClient;
6+
use AsyncAws\CloudFormation\Input\DescribeStackDriftDetectionStatusInput;
67
use AsyncAws\CloudFormation\Input\DescribeStackEventsInput;
78
use AsyncAws\CloudFormation\Input\DescribeStacksInput;
9+
use AsyncAws\CloudFormation\Result\DescribeStackDriftDetectionStatusOutput;
810
use AsyncAws\CloudFormation\Result\DescribeStackEventsOutput;
911
use AsyncAws\CloudFormation\Result\DescribeStacksOutput;
1012
use AsyncAws\Core\Credentials\NullProvider;
11-
use PHPUnit\Framework\TestCase;
13+
use AsyncAws\Core\Test\TestCase;
1214
use Symfony\Component\HttpClient\MockHttpClient;
1315

1416
class CloudFormationClientTest extends TestCase
1517
{
18+
public function testDescribeStackDriftDetectionStatus(): void
19+
{
20+
$client = new CloudFormationClient([], new NullProvider(), new MockHttpClient());
21+
22+
$input = new DescribeStackDriftDetectionStatusInput([
23+
'StackDriftDetectionId' => 'b78ac9b0-dec1-11e7-a451-503a3example',
24+
]);
25+
$result = $client->describeStackDriftDetectionStatus($input);
26+
27+
self::assertInstanceOf(DescribeStackDriftDetectionStatusOutput::class, $result);
28+
self::assertFalse($result->info()['resolved']);
29+
}
30+
1631
public function testDescribeStackEvents(): void
1732
{
1833
$client = new CloudFormationClient([], new NullProvider(), new MockHttpClient());

0 commit comments

Comments
 (0)