Skip to content

Commit 10b6d3a

Browse files
committed
add example in python
1 parent 6ee3df7 commit 10b6d3a

File tree

8 files changed

+1101
-0
lines changed

8 files changed

+1101
-0
lines changed

examples/python/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/sdk/** linguist-generated

examples/python/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/sdk
2+
/.venv
3+
/**/__pycache__

examples/python/dagger.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "example",
3+
"engineVersion": "v0.18.2",
4+
"sdk": {
5+
"source": "python"
6+
},
7+
"dependencies": [
8+
{
9+
"name": "localstack-dagger-module",
10+
"source": "../.."
11+
}
12+
]
13+
}

examples/python/pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "example"
3+
version = "0.1.0"
4+
requires-python = ">=3.12"
5+
dependencies = ["dagger-io", "boto3"]
6+
7+
[tool.uv.sources]
8+
dagger-io = { path = "sdk", editable = true }
9+
10+
[build-system]
11+
requires = ["hatchling==1.25.0"]
12+
build-backend = "hatchling.build"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""A generated module for Example functions
2+
3+
This module has been generated via dagger init and serves as a reference to
4+
basic module structure as you get started with Dagger.
5+
6+
Two functions have been pre-created. You can modify, delete, or add to them,
7+
as needed. They demonstrate usage of arguments and return types using simple
8+
echo and grep commands. The functions can be called from the dagger CLI or
9+
from one of the SDKs.
10+
11+
The first line in this comment block is a short description line and the
12+
rest is a long description with more detail on the module's purpose or usage,
13+
if appropriate. All modules should have a short description.
14+
"""
15+
16+
from .main import Example as Example
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import dagger
2+
from dagger import dag, function, object_type
3+
import boto3
4+
import time
5+
6+
@object_type
7+
class Example:
8+
@function
9+
async def localstack_dagger_module__quickstart(self) -> str:
10+
"""Example showing how to start LocalStack Community edition."""
11+
service = dag.localstack_dagger_module().start()
12+
13+
await service.start()
14+
endpoint = await service.endpoint()
15+
print(f"LocalStack is running at {endpoint}")
16+
17+
# Create a test S3 bucket
18+
s3 = boto3.client(
19+
's3',
20+
endpoint_url=f"http://{endpoint}",
21+
aws_access_key_id='test',
22+
aws_secret_access_key='test',
23+
region_name='us-east-1'
24+
)
25+
s3.create_bucket(Bucket='test-bucket')
26+
27+
print("S3 bucket created")
28+
29+
# Create a test object
30+
s3.put_object(
31+
Bucket='test-bucket',
32+
Key='test-object',
33+
Body='Hello, LocalStack!'
34+
)
35+
print("S3 object created")
36+
37+
# Verify the object was created
38+
response = s3.get_object(Bucket='test-bucket', Key='test-object')
39+
content = response['Body'].read().decode('utf-8')
40+
print(f"S3 object content: {content}")
41+
42+
@function
43+
async def localstack_dagger_module__pro(self, auth_token: dagger.Secret) -> str:
44+
"""Example showing how to start LocalStack Pro with custom configuration."""
45+
# Start LocalStack Pro using the module
46+
service = dag.localstack_dagger_module().start(
47+
auth_token=auth_token,
48+
configuration="DEBUG=1,SERVICES=ecr"
49+
)
50+
51+
await service.start()
52+
endpoint = await service.endpoint()
53+
print(f"LocalStack Pro is running at {endpoint}")
54+
55+
# Create a test ECR repository
56+
ecr = boto3.client(
57+
'ecr',
58+
endpoint_url=f"http://{endpoint}",
59+
aws_access_key_id='test',
60+
aws_secret_access_key='test',
61+
region_name='us-east-1'
62+
)
63+
64+
repository_name = "test-ecr-repo"
65+
ecr.create_repository(repositoryName=repository_name)
66+
print(f"ECR repository '{repository_name}' created")
67+
68+
@function
69+
async def localstack_dagger_module__state(self, auth_token: dagger.Secret) -> str:
70+
"""Example showing how to manage LocalStack state using Cloud Pods."""
71+
service = dag.localstack_dagger_module().start(auth_token=auth_token)
72+
await service.start()
73+
endpoint = await service.endpoint()
74+
75+
try:
76+
# Create a test bucket
77+
s3 = boto3.client(
78+
's3',
79+
endpoint_url=f"http://{endpoint}",
80+
aws_access_key_id='test',
81+
aws_secret_access_key='test',
82+
region_name='us-east-1'
83+
)
84+
s3.create_bucket(Bucket='test-bucket')
85+
86+
# Save state to Cloud Pod
87+
await dag.localstack_dagger_module().state(
88+
auth_token=auth_token,
89+
save="test-dagger-example-pod",
90+
endpoint=f"http://{endpoint}"
91+
)
92+
93+
# Reset state
94+
await dag.localstack_dagger_module().state(
95+
reset=True,
96+
endpoint=f"http://{endpoint}"
97+
)
98+
99+
# Load state back
100+
await dag.localstack_dagger_module().state(
101+
auth_token=auth_token,
102+
load="test-dagger-example-pod",
103+
endpoint=f"http://{endpoint}"
104+
)
105+
106+
return "Success: State operations completed"
107+
except Exception as e:
108+
return f"Error: {str(e)}"
109+
110+
@function
111+
async def localstack_dagger_module_ephemeral(self, auth_token: dagger.Secret) -> str:
112+
"""Example showing how to manage LocalStack Ephemeral Instances."""
113+
try:
114+
# Create a new ephemeral instance
115+
await dag.localstack_dagger_module().ephemeral(
116+
auth_token=auth_token,
117+
operation="create",
118+
name="test-dagger-example-instance",
119+
lifetime=60,
120+
)
121+
122+
# Wait for instance to be ready
123+
time.sleep(15)
124+
125+
print("Instance created")
126+
127+
# List instances
128+
list_response = await dag.localstack_dagger_module().ephemeral(
129+
auth_token=auth_token,
130+
operation="list"
131+
)
132+
133+
print(f"Ephemeral instances: {list_response}")
134+
135+
# Get instance logs
136+
instance_logs = await dag.localstack_dagger_module().ephemeral(
137+
auth_token=auth_token,
138+
operation="logs",
139+
name="test-dagger-example-instance"
140+
)
141+
142+
print(f"Instance logs: {instance_logs}")
143+
144+
# Delete instance
145+
await dag.localstack_dagger_module().ephemeral(
146+
auth_token=auth_token,
147+
operation="delete",
148+
name="test-dagger-example-instance"
149+
)
150+
151+
print("Instance deleted")
152+
153+
return "Success: Ephemeral instance operations completed"
154+
except Exception as e:
155+
return f"Error: {str(e)}"

0 commit comments

Comments
 (0)