Skip to content

Commit b4eb3ed

Browse files
committed
Add Python SDK examples for first 10 tutorials
- Wrapper classes with one method per API action - Scenario orchestrators (setup/interact/teardown) - pytest test scaffolds with Stubber fixtures - 4 tutorials with full implementations, 6 with scaffolds
1 parent 637ebf8 commit b4eb3ed

40 files changed

Lines changed: 730 additions & 0 deletions
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import logging
5+
import boto3
6+
from botocore.exceptions import ClientError
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class LightsailWrapper:
12+
def __init__(self, client):
13+
self.client = client
14+
15+
def create_instances(self, instance_name, blueprint_id='amazon_linux_2023', bundle_id='nano_3_2'):
16+
"""Calls lightsail:CreateInstances."""
17+
try:
18+
self.client.create_instances(instanceNames=[instance_name], blueprintId=blueprint_id, bundleId=bundle_id, availabilityZone=f'{self.client.meta.region_name}a')
19+
logger.info("CreateInstances succeeded.")
20+
except ClientError:
21+
logger.exception("CreateInstances failed.")
22+
raise
23+
24+
def create_disk(self, disk_name, size_in_gb=8):
25+
"""Calls lightsail:CreateDisk."""
26+
try:
27+
self.client.create_disk(diskName=disk_name, sizeInGb=size_in_gb, availabilityZone=f'{self.client.meta.region_name}a')
28+
logger.info("CreateDisk succeeded.")
29+
except ClientError:
30+
logger.exception("CreateDisk failed.")
31+
raise
32+
33+
def get_instance(self, instance_name):
34+
"""Calls lightsail:GetInstance."""
35+
try:
36+
return self.client.get_instance(instanceName=instance_name)['instance']
37+
logger.info("GetInstance succeeded.")
38+
except ClientError:
39+
logger.exception("GetInstance failed.")
40+
raise
41+
42+
def delete_instance(self, instance_name):
43+
"""Calls lightsail:DeleteInstance."""
44+
try:
45+
self.client.delete_instance(instanceName=instance_name)
46+
logger.info("DeleteInstance succeeded.")
47+
except ClientError:
48+
logger.exception("DeleteInstance failed.")
49+
raise
50+
51+
def delete_disk(self, disk_name):
52+
"""Calls lightsail:DeleteDisk."""
53+
try:
54+
self.client.delete_disk(diskName=disk_name)
55+
logger.info("DeleteDisk succeeded.")
56+
except ClientError:
57+
logger.exception("DeleteDisk failed.")
58+
raise
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3>=1.26
2+
pytest>=7.0
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import uuid
5+
import boto3
6+
from lightsail_wrapper import LightsailWrapper
7+
8+
9+
def run_scenario():
10+
client = boto3.client("lightsail")
11+
wrapper = LightsailWrapper(client)
12+
suffix = uuid.uuid4().hex[:8]
13+
print(f"Running Lightsail getting started scenario...")
14+
# TODO: implement setup, interact, teardown
15+
print("Scenario complete.")
16+
17+
18+
if __name__ == "__main__":
19+
run_scenario()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import pytest
5+
from botocore.stub import Stubber
6+
import boto3
7+
from lightsail_wrapper import LightsailWrapper
8+
9+
10+
@pytest.fixture
11+
def lightsail_stubber():
12+
client = boto3.client("lightsail", region_name="us-east-1")
13+
with Stubber(client) as stubber:
14+
yield client, stubber
15+
16+
17+
def test_wrapper_creates(capsys, lightsail_stubber):
18+
client, stubber = lightsail_stubber
19+
wrapper = LightsailWrapper(client)
20+
# TODO: add stubbed responses and assertions
21+
assert wrapper is not None
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import logging
5+
import boto3
6+
from botocore.exceptions import ClientError
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class EC2Wrapper:
12+
def __init__(self, client):
13+
self.client = client
14+
15+
# TODO: Add wrapper methods matching the CLI tutorial actions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3>=1.26
2+
pytest>=7.0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import uuid
5+
import boto3
6+
from ec2_wrapper import EC2Wrapper
7+
8+
9+
def run_scenario():
10+
client = boto3.client("ec2")
11+
wrapper = EC2Wrapper(client)
12+
print(f"Running EC2 getting started scenario...")
13+
# TODO: implement setup, interact, teardown
14+
print("Scenario complete.")
15+
16+
17+
if __name__ == "__main__":
18+
run_scenario()

tuts/002-vpc-gs/python/test_ec2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import pytest
5+
from botocore.stub import Stubber
6+
import boto3
7+
from ec2_wrapper import EC2Wrapper
8+
9+
10+
@pytest.fixture
11+
def stubber():
12+
client = boto3.client("ec2", region_name="us-east-1")
13+
with Stubber(client) as stubber:
14+
yield client, stubber
15+
16+
17+
def test_wrapper_creates(stubber):
18+
client, stub = stubber
19+
wrapper = EC2Wrapper(client)
20+
assert wrapper is not None
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3>=1.26
2+
pytest>=7.0
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import logging
5+
import boto3
6+
from botocore.exceptions import ClientError
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class S3Wrapper:
12+
def __init__(self, client):
13+
self.client = client
14+
15+
def create_bucket(self, bucket_name):
16+
"""Calls s3:CreateBucket."""
17+
try:
18+
self.client.create_bucket(Bucket=bucket_name)
19+
logger.info("CreateBucket succeeded.")
20+
except ClientError:
21+
logger.exception("CreateBucket failed.")
22+
raise
23+
24+
def put_object(self, bucket_name, key, body):
25+
"""Calls s3:PutObject."""
26+
try:
27+
self.client.put_object(Bucket=bucket_name, Key=key, Body=body)
28+
logger.info("PutObject succeeded.")
29+
except ClientError:
30+
logger.exception("PutObject failed.")
31+
raise
32+
33+
def get_object(self, bucket_name, key):
34+
"""Calls s3:GetObject."""
35+
try:
36+
return self.client.get_object(Bucket=bucket_name, Key=key)['Body'].read()
37+
logger.info("GetObject succeeded.")
38+
except ClientError:
39+
logger.exception("GetObject failed.")
40+
raise
41+
42+
def copy_object(self, src_bucket, src_key, dst_bucket, dst_key):
43+
"""Calls s3:CopyObject."""
44+
try:
45+
self.client.copy_object(CopySource={'Bucket': src_bucket, 'Key': src_key}, Bucket=dst_bucket, Key=dst_key)
46+
logger.info("CopyObject succeeded.")
47+
except ClientError:
48+
logger.exception("CopyObject failed.")
49+
raise
50+
51+
def list_objects(self, bucket_name):
52+
"""Calls s3:ListObjectsV2."""
53+
try:
54+
return self.client.list_objects_v2(Bucket=bucket_name).get('Contents', [])
55+
logger.info("ListObjectsV2 succeeded.")
56+
except ClientError:
57+
logger.exception("ListObjectsV2 failed.")
58+
raise
59+
60+
def delete_objects(self, bucket_name, keys):
61+
"""Calls s3:DeleteObjects."""
62+
try:
63+
self.client.delete_objects(Bucket=bucket_name, Delete={'Objects': [{'Key': k} for k in keys]})
64+
logger.info("DeleteObjects succeeded.")
65+
except ClientError:
66+
logger.exception("DeleteObjects failed.")
67+
raise
68+
69+
def delete_bucket(self, bucket_name):
70+
"""Calls s3:DeleteBucket."""
71+
try:
72+
self.client.delete_bucket(Bucket=bucket_name)
73+
logger.info("DeleteBucket succeeded.")
74+
except ClientError:
75+
logger.exception("DeleteBucket failed.")
76+
raise

0 commit comments

Comments
 (0)