Skip to content

Commit

Permalink
added ec2 code and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws committed Dec 19, 2024
1 parent 4eedc91 commit 8f43c45
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 42 deletions.
49 changes: 47 additions & 2 deletions .doc_gen/metadata/ec2_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,15 @@ ec2_DescribeRouteTables:
- description:
snippet_tags:
- php.example_code.ec2.service.describeRouteTables
Python:
versions:
- sdk_version: 3
github: python/example_code/ec2
excerpts:
- description:
snippet_tags:
- python.example_code.ec2.VpcWrapper.decl
- python.example_code.ec2.DescribeRouteTables
services:
ec2: {DescribeRouteTables}
ec2_DescribeRegions:
Expand Down Expand Up @@ -2003,6 +2012,15 @@ ec2_CreateVpc:
- description:
snippet_tags:
- ec2.Ruby.createVpc
Python:
versions:
- sdk_version: 3
github: python/example_code/ec2
excerpts:
- description:
snippet_tags:
- python.example_code.ec2.VpcWrapper.decl
- python.example_code.ec2.CreateVpc
services:
ec2: {CreateVpc}
ec2_DeleteVpc:
Expand All @@ -2016,6 +2034,15 @@ ec2_DeleteVpc:
- description:
snippet_tags:
- php.example_code.ec2.service.deleteVpc
Python:
versions:
- sdk_version: 3
github: python/example_code/ec2
excerpts:
- description:
snippet_tags:
- python.example_code.ec2.VpcWrapper.decl
- python.example_code.ec2.DeleteVpc
services:
ec2: {DeleteVpc}
ec2_CreateVpcEndpoint:
Expand All @@ -2029,9 +2056,18 @@ ec2_CreateVpcEndpoint:
- description:
snippet_tags:
- php.example_code.ec2.service.createVpcEndpoint
Python:
versions:
- sdk_version: 3
github: python/example_code/ec2
excerpts:
- description:
snippet_tags:
- python.example_code.ec2.VpcWrapper.decl
- python.example_code.ec2.CreateVpcEndpoint
services:
ec2: {CreateVpcEndpoint}
ec2_DeleteVpcEndpoint:
ec2_DeleteVpcEndpoints:
languages:
PHP:
versions:
Expand All @@ -2042,8 +2078,17 @@ ec2_DeleteVpcEndpoint:
- description:
snippet_tags:
- php.example_code.ec2.service.deleteVpcEndpoint
Python:
versions:
- sdk_version: 3
github: python/example_code/ec2
excerpts:
- description:
snippet_tags:
- python.example_code.ec2.VpcWrapper.decl
- python.example_code.ec2.DeleteVpcEndpoints
services:
ec2: {DeleteVpcEndpoint}
ec2: {DeleteVpcEndpoints}
ec2_DescribeIamInstanceProfileAssociations:
languages:
.NET:
Expand Down
3 changes: 3 additions & 0 deletions .doc_gen/metadata/s3-directory-buckets_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ s3-directory-buckets_Scenario_ExpressBasics:
and S3 Express One Zone.
snippet_tags:
- python.example_code.s3.s3_express_basics
- description: A wrapper class for &S3; Express SDK functions.
snippet_tags:
- python.example_code.s3_directory.S3ExpressWrapper.class
services:
s3-directory-buckets: {CreateBucket, CopyObject, GetObject, PutObject, ListObjects, DeleteObject, DeleteBucket}

Expand Down
4 changes: 2 additions & 2 deletions php/example_code/ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Code excerpts that show you how to call individual service functions.
- [CreateVpc](EC2Service.php#L67)
- [CreateVpcEndpoint](EC2Service.php#L41)
- [DeleteVpc](EC2Service.php#L88)
- [DeleteVpcEndpoint](EC2Service.php#L108)
- [DeleteVpcEndpoints](EC2Service.php#L108)
- [DescribeRouteTables](EC2Service.php#L128)


Expand Down Expand Up @@ -79,4 +79,4 @@ in the `php` folder.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
7 changes: 6 additions & 1 deletion python/example_code/ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@ Code excerpts that show you how to call individual service functions.
- [CreateKeyPair](key_pair.py#L57)
- [CreateLaunchTemplate](../../cross_service/resilient_service/auto_scaler.py#L383)
- [CreateSecurityGroup](security_group.py#L42)
- [CreateVpc](vpc.py#L39)
- [CreateVpcEndpoint](vpc.py#L86)
- [DeleteKeyPair](key_pair.py#L118)
- [DeleteLaunchTemplate](../../cross_service/resilient_service/auto_scaler.py#L447)
- [DeleteSecurityGroup](security_group.py#L148)
- [DeleteVpc](vpc.py#L136)
- [DeleteVpcEndpoints](vpc.py#L116)
- [DescribeAvailabilityZones](../../cross_service/resilient_service/auto_scaler.py#L473)
- [DescribeIamInstanceProfileAssociations](../../cross_service/resilient_service/auto_scaler.py#L216)
- [DescribeImages](instance.py#L253)
- [DescribeInstanceTypes](instance.py#L274)
- [DescribeInstances](instance.py#L106)
- [DescribeKeyPairs](key_pair.py#L89)
- [DescribeRouteTables](vpc.py#L63)
- [DescribeSecurityGroups](security_group.py#L108)
- [DescribeSubnets](../../cross_service/resilient_service/auto_scaler.py#L821)
- [DescribeVpcs](../../cross_service/resilient_service/auto_scaler.py#L684)
Expand Down Expand Up @@ -180,4 +185,4 @@ in the `python` folder.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
123 changes: 123 additions & 0 deletions python/example_code/ec2/test/test_vpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""
Unit tests for cleanup in vpc.py.
"""
import sys

import boto3
import pytest
import os
from botocore.exceptions import ClientError
from botocore import waiter

script_dir = os.path.dirname(os.path.abspath(__file__))


sys.path.append(script_dir)
# Add relative path to include VpcWrapper.
sys.path.append(os.path.dirname(script_dir))
from vpc import VpcWrapper

# Add relative path to include demo_tools in this code example without need for setup.
sys.path.append(os.path.join(script_dir, "../../.."))

from test_tools.fixtures.common import *


@pytest.mark.parametrize("error_code", [None, "TestException"])
def test_create(monkeypatch, make_stubber, error_code):
ec2_client = boto3.client("ec2")
ec2_stubber = make_stubber(ec2_client)

# Mock the waiters.
monkeypatch.setattr(waiter.Waiter, "wait", lambda arg, **kwargs: None)

wrapper = VpcWrapper(ec2_client)
vpc_id = "vpc-a01106c2"
cidr_block = "10.0.0.0/16"
ec2_stubber.stub_create_vpc(cidr_block, vpc_id, error_code=error_code)
if error_code is None:
created_vpc_id = wrapper.create(cidr_block)
assert created_vpc_id == vpc_id
else:
with pytest.raises(ClientError) as exc_info:
wrapper.create(cidr_block)
assert exc_info.value.response["Error"]["Code"] == error_code


@pytest.mark.parametrize("error_code", [None, "TestException"])
def test_create_vpc_endpoint(make_stubber, error_code):
ec2_client = boto3.client("ec2")
ec2_stubber = make_stubber(ec2_client)

wrapper = VpcWrapper(ec2_client)
vpc_id = "XXXXXXXXXXXX"
service_name = "com.amazonaws.us-west-2.s3"
route_table_id = "XXXXXXXXXXXXX"
vpc_endpoint_id = f"vpce-{vpc_id}"
ec2_stubber.stub_create_vpc_endpoint(
vpc_id, route_table_id, service_name, error_code=error_code
)
if error_code is None:
created_endpoint_id = wrapper.create_vpc_endpoint(
vpc_id, service_name, [route_table_id]
)
assert created_endpoint_id["VpcEndpointId"] == vpc_endpoint_id
else:
with pytest.raises(ClientError) as exc_info:
wrapper.create_vpc_endpoint(vpc_id, service_name, [route_table_id])
assert exc_info.value.response["Error"]["Code"] == error_code


@pytest.mark.parametrize("error_code", [None, "TestException"])
def test_describe_route_tables(make_stubber, error_code):
ec2_client = boto3.client("ec2")
ec2_stubber = make_stubber(ec2_client)

wrapper = VpcWrapper(ec2_client)
vpc_id = "XXXXXXXXXXXX"
route_table_id = "XXXXXXXXXXXXX"
filters = [{"Name": "vpc-id", "Values": [vpc_id]}]
ec2_stubber.stub_describe_route_tables(
filters, vpc_id, route_table_id, error_code=error_code
)
if error_code is None:
wrapper.describe_route_tables([vpc_id])
else:
with pytest.raises(ClientError) as exc_info:
wrapper.describe_route_tables([vpc_id])
assert exc_info.value.response["Error"]["Code"] == error_code


@pytest.mark.parametrize("error_code", [None, "TestException"])
def test_delete_vpc_endpoints(make_stubber, error_code):
ec2_client = boto3.client("ec2")
ec2_stubber = make_stubber(ec2_client)

wrapper = VpcWrapper(ec2_client)
vpc_endpoint_id = "XXXXXXXXXXXX"
ec2_stubber.stub_delete_vpc_endpoints([vpc_endpoint_id], error_code=error_code)
if error_code is None:
wrapper.delete_vpc_endpoints([vpc_endpoint_id])
else:
with pytest.raises(ClientError) as exc_info:
wrapper.delete_vpc_endpoints([vpc_endpoint_id])
assert exc_info.value.response["Error"]["Code"] == error_code


@pytest.mark.parametrize("error_code", [None, "TestException"])
def test_delete(make_stubber, error_code):
ec2_client = boto3.client("ec2")
ec2_stubber = make_stubber(ec2_client)

wrapper = VpcWrapper(ec2_client)
vpc_id = "XXXXXXXXXXXX"
ec2_stubber.stub_delete_vpc(vpc_id, error_code=error_code)
if error_code is None:
wrapper.delete(vpc_id)
else:
with pytest.raises(ClientError) as exc_info:
wrapper.delete(vpc_id)
assert exc_info.value.response["Error"]["Code"] == error_code
Loading

0 comments on commit 8f43c45

Please sign in to comment.