Skip to content

Commit

Permalink
Adds exception handling and docblocks for the EC2 Service.
Browse files Browse the repository at this point in the history
  • Loading branch information
beqqrry-aws authored and ford-at-aws committed Oct 24, 2024
1 parent 417118d commit d2a1c71
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 35 deletions.
108 changes: 78 additions & 30 deletions php/example_code/ec2/EC2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[php.example_code.ec2.service.Ec2Service]

namespace Ec2;

use Aws\Ec2\Ec2Client;
Expand Down Expand Up @@ -46,54 +48,91 @@ public function __construct($client = null, $region = 'us-west-2', $version = 'l
*/
public function createVpcEndpoint(string $serviceName, string $vpcId, array $routeTableIds): array
{
$result = $this->ec2Client->createVpcEndpoint([
'ServiceName' => $serviceName,
'VpcId' => $vpcId,
'RouteTableIds' => $routeTableIds,
]);

return $result["VpcEndpoint"];
try {
$result = $this->ec2Client->createVpcEndpoint([
'ServiceName' => $serviceName,
'VpcId' => $vpcId,
'RouteTableIds' => $routeTableIds,
]);

return $result["VpcEndpoint"];
} catch(Ec2Exception $caught){
echo "There was a problem creating the VPC Endpoint: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}

// snippet-end:[php.example_code.ec2.service.createVpcEndpoint]

// snippet-start:[php.example_code.ec2.service.createVpc]

public function createVpc($cidr)
/**
* @param string $cidr
* @return array
*/
public function createVpc(string $cidr): array
{
$result = $this->ec2Client->createVpc([
"CidrBlock" => $cidr,
]);
return $result['Vpc'];
try {
$result = $this->ec2Client->createVpc([
"CidrBlock" => $cidr,
]);
return $result['Vpc'];
}catch(Ec2Exception $caught){
echo "There was a problem creating the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}

// snippet-end:[php.example_code.ec2.service.createVpc]

// snippet-start:[php.example_code.ec2.service.deleteVpc]

public function deleteVpc(mixed $vpcId)
/**
* @param string $vpcId
* @return void
*/
public function deleteVpc(string $vpcId)
{
$result = $this->ec2Client->deleteVpc([
"VpcId" => $vpcId,
]);
try {
$this->ec2Client->deleteVpc([
"VpcId" => $vpcId,
]);
}catch(Ec2Exception $caught){
echo "There was a problem deleting the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}

// snippet-end:[php.example_code.ec2.service.deleteVpc]

// snippet-start:[php.example_code.ec2.service.deleteVpcEndpoint]

public function deleteVpcEndpoint(mixed $vpcEndpointId)
/**
* @param string $vpcEndpointId
* @return void
*/
public function deleteVpcEndpoint(string $vpcEndpointId)
{
$result = $this->ec2Client->deleteVpcEndpoints([
"VpcEndpointIds" => [$vpcEndpointId],
]);
try {
$this->ec2Client->deleteVpcEndpoints([
"VpcEndpointIds" => [$vpcEndpointId],
]);
}catch (Ec2Exception $caught){
echo "There was a problem deleting the VPC Endpoint: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}

// snippet-end:[php.example_code.ec2.service.deleteVpcEndpoint]

// snippet-start:[php.example_code.ec2.service.describeRouteTables]

public function describeRouteTables(array $routeTableIds = [], array $filters = [])
/**
* @param array $routeTableIds
* @param array $filters
* @return array
*/
public function describeRouteTables(array $routeTableIds = [], array $filters = []): array
{
$parameters = [];
if($routeTableIds){
Expand Down Expand Up @@ -121,17 +160,26 @@ public function describeRouteTables(array $routeTableIds = [], array $filters =

// snippet-start:[php.example_code.ec2.service.waitForVpcAvailable]

public function waitForVpcAvailable(string $VpcId)
/**
* @param string $VpcId
* @return void
*/
public function waitForVpcAvailable(string $VpcId): void
{

$waiter = $this->ec2Client->getWaiter("VpcAvailable", [
'VpcIds' => [$VpcId],
]);

$promise = $waiter->promise();
$promise->wait();

try {
$waiter = $this->ec2Client->getWaiter("VpcAvailable", [
'VpcIds' => [$VpcId],
]);

$promise = $waiter->promise();
$promise->wait();
}catch(Ec2Exception $caught){
echo "There was a problem waiting for the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}

// snippet-end:[php.example_code.ec2.service.waitForVpcAvailable]
}

// snippet-end:[php.example_code.ec2.service.Ec2Service]
10 changes: 5 additions & 5 deletions php/example_code/ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `php`

Code excerpts that show you how to call individual service functions.

- [CreateVpc](EC2Service.php#L60)
- [CreateVpcEndpoint](EC2Service.php#L39)
- [DeleteVpc](EC2Service.php#L72)
- [DeleteVpcEndpoint](EC2Service.php#L83)
- [DescribeRouteTables](EC2Service.php#L94)
- [CreateVpc](EC2Service.php#L67)
- [CreateVpcEndpoint](EC2Service.php#L41)
- [DeleteVpc](EC2Service.php#L88)
- [DeleteVpcEndpoint](EC2Service.php#L108)
- [DescribeRouteTables](EC2Service.php#L128)


<!--custom.examples.start-->
Expand Down

0 comments on commit d2a1c71

Please sign in to comment.