|
| 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