1+ """LocalStack module for managing LocalStack instances and state.
2+
3+ This module provides functions to start LocalStack containers, manage state through Cloud Pods,
4+ and handle ephemeral LocalStack instances in the cloud.
5+ """
6+
17import os
28import dagger
3- from dagger import dag , function , object_type
4- from typing import Optional
9+ from dagger import dag , function , object_type , Doc
10+ from typing import Optional , Annotated
511import base64
612from datetime import datetime
713import requests
1016
1117@object_type
1218class Localstack :
19+ """LocalStack service management functions."""
20+
1321 @function
1422 def start (
1523 self ,
16- auth_token : Optional [dagger .Secret ] = None ,
17- configuration : Optional [str ] = None ,
18- docker_sock : Optional [dagger .Socket ] = None ,
19- image_name : Optional [str ] = None
24+ auth_token : Annotated [ Optional [dagger .Secret ], Doc ( "LocalStack Pro Auth Token for authentication" ) ] = None ,
25+ configuration : Annotated [ Optional [str ], Doc ( "Configuration variables in format 'KEY1=value1,KEY2=value2'" ) ] = None ,
26+ docker_sock : Annotated [ Optional [dagger .Socket ], Doc ( "Docker socket for container interactions" ) ] = None ,
27+ image_name : Annotated [ Optional [str ], Doc ( "Custom LocalStack image name to use" ) ] = None
2028 ) -> dagger .Service :
21- """Start a LocalStack service with appropriate configuration.
22-
23- If image_name is provided, starts that specific image.
24- If auth_token is provided but no image_name, starts LocalStack Pro edition.
25- Otherwise starts LocalStack Community edition.
26-
27- Args:
28- auth_token: Optional secret containing LocalStack Pro auth token
29- configuration: Optional string of configuration variables in format "KEY1=value1,KEY2=value2"
30- Example: "DEBUG=1,LS_LOG=trace"
31- docker_sock: Optional Docker socket for container interactions
32- image_name: Optional custom LocalStack image name to use
33-
34- Returns:
35- A running LocalStack service container
36- """
29+ """Start a LocalStack service with appropriate configuration."""
3730 # Determine image based on parameters
3831 if image_name :
3932 image = image_name
@@ -47,7 +40,7 @@ def start(
4740 if docker_sock :
4841 container = container .with_unix_socket ("/var/run/docker.sock" , docker_sock )
4942
50- # Add auth token if provided
43+ # Add Auth Token if provided
5144 if auth_token :
5245 container = container .with_secret_variable ("LOCALSTACK_AUTH_TOKEN" , auth_token )
5346
@@ -74,23 +67,13 @@ def start(
7467 @function
7568 async def state (
7669 self ,
77- auth_token : Optional [dagger .Secret ] = None ,
78- load : Optional [str ] = None ,
79- save : Optional [str ] = None ,
80- endpoint : Optional [str ] = None ,
81- reset : bool = False
70+ auth_token : Annotated [ Optional [dagger .Secret ], Doc ( "LocalStack Auth Token (required for save/load)" ) ] = None ,
71+ load : Annotated [ Optional [str ], Doc ( "Name of the Cloud Pod to load" ) ] = None ,
72+ save : Annotated [ Optional [str ], Doc ( "Name of the Cloud Pod to save" ) ] = None ,
73+ endpoint : Annotated [ Optional [str ], Doc ( "LocalStack endpoint (defaults to host.docker.internal:4566)" ) ] = None ,
74+ reset : Annotated [ bool , Doc ( "Reset the LocalStack state" )] = False
8275 ) -> str :
83- """Load, save, or reset LocalStack state.
84-
85- Args:
86- auth_token: Secret containing LocalStack auth token (required for save/load)
87- load: Name of the Cloud Pod to load
88- save: Name of the Cloud Pod to save
89- reset: Reset the LocalStack state
90- endpoint: LocalStack endpoint to use (optional, defaults to host.docker.internal:4566)
91- Returns:
92- Output from the pod operation or error message if LocalStack is not running
93- """
76+ """Load, save, or reset LocalStack state."""
9477 # Base URL for LocalStack API
9578 localstack_url = endpoint or "http://host.docker.internal:4566"
9679
@@ -140,7 +123,7 @@ async def state(
140123 save_response .raise_for_status ()
141124 return save_response .text
142125 except requests .RequestException :
143- return f"Error: Failed to save pod '{ save } '. Please check the pod name and your auth token ."
126+ return f"Error: Failed to save pod '{ save } '. Please check the pod name and your Auth Token ."
144127 elif load :
145128 try :
146129 load_response = requests .put (
@@ -151,40 +134,28 @@ async def state(
151134 load_response .raise_for_status ()
152135 return load_response .text
153136 except requests .RequestException :
154- return f"Error: Failed to load pod '{ load } '. Please check the pod name and your auth token ."
137+ return f"Error: Failed to load pod '{ load } '. Please check the pod name and your Auth Token ."
155138
156139 return "No operation specified. Please provide either --load, --save, or --reset parameter."
157140
158141 @function
159142 async def ephemeral (
160143 self ,
161- auth_token : dagger .Secret ,
162- operation : str ,
163- name : Optional [str ] = None ,
164- lifetime : Optional [int ] = None ,
165- auto_load_pod : Optional [str ] = None ,
166- extension_auto_install : Optional [str ] = None
144+ auth_token : Annotated [ dagger .Secret , Doc ( "LocalStack Auth Token (required)" )] ,
145+ operation : Annotated [ str , Doc ( "Operation to perform (create, list, delete, logs)" )] ,
146+ name : Annotated [ Optional [str ], Doc ( "Name of the ephemeral instance (required for create, delete, logs)" ) ] = None ,
147+ lifetime : Annotated [ Optional [int ], Doc ( "Lifetime of the instance in minutes (default: 60)" ) ] = None ,
148+ auto_load_pod : Annotated [ Optional [str ], Doc ( "Auto load pod configuration" ) ] = None ,
149+ extension_auto_install : Annotated [ Optional [str ], Doc ( "Extension auto install configuration" ) ] = None
167150 ) -> str :
168- """Manage ephemeral LocalStack instances in the cloud.
169-
170- Args:
171- auth_token: LocalStack auth token (required)
172- operation: Operation to perform (create, list, delete, logs)
173- name: Name of the ephemeral instance (required for create, delete, logs)
174- lifetime: Lifetime of the instance in minutes (optional, default: 60)
175- auto_load_pod: Auto load pod configuration (optional)
176- extension_auto_install: Extension auto install configuration (optional)
177-
178- Returns:
179- Response from the API operation
180- """
151+ """Manage ephemeral LocalStack instances in the cloud."""
181152 if not auth_token :
182153 return "Error: auth_token is required for ephemeral instance operations"
183154
184155 # Base API endpoint
185156 api_endpoint = "https://api.localstack.cloud/v1"
186157
187- # Get auth token value from secret
158+ # Get Auth Token value from secret
188159 auth_token_value = await auth_token .plaintext ()
189160
190161 # Common headers
0 commit comments