Skip to content

Commit 2c87a68

Browse files
committed
consistent load of credentials and naming in examples
1 parent bf62238 commit 2c87a68

15 files changed

+107
-118
lines changed

datacrunch/datacrunch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ def __init__(self, client_id: str, client_secret: str, base_url: str = "https://
2828
:type base_url: str, optional
2929
"""
3030

31+
# Validate that client_id and client_secret are not empty
32+
if not client_id or not client_secret:
33+
raise ValueError(
34+
"client_id and client_secret must be provided")
35+
3136
# Constants
3237
self.constants: Constants = Constants(base_url, VERSION)
3338
"""Constants"""

examples/advanced_create_instance.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
# Arbitrary duration for the example
2121
DURATION = 24 * 7 # one week
2222

23-
# Get client secret from environment variable
24-
CLIENT_SECRET = os.environ['DATACRUNCH_CLIENT_SECRET']
25-
CLIENT_ID = 'Ibk5bdxV64lKAWOqYnvSi' # Replace with your client ID
23+
# Get client secret and id from environment variables
24+
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
25+
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
2626

2727
try:
2828
# Create datcrunch client
29-
datacrunch = DataCrunchClient(CLIENT_ID, CLIENT_SECRET)
29+
datacrunch = DataCrunchClient(
30+
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
3031

3132
# Create new SSH key
3233
public_key = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI0qq2Qjt5GPi7DKdcnBHOkvk8xNsG9dA607tnWagOkHC test_key'

examples/containers/compute_resources_example.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import os
12
from datacrunch import DataCrunchClient
23
from typing import List
34
from datacrunch.containers.containers import ComputeResource
45

6+
# Get client secret and id from environment variables
7+
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
8+
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
9+
510

611
def list_all_compute_resources(client: DataCrunchClient) -> List[ComputeResource]:
712
"""List all available compute resources.
@@ -44,27 +49,25 @@ def list_compute_resources_by_size(client: DataCrunchClient, size: int) -> List[
4449

4550
def main():
4651
# Initialize the client with your credentials
47-
client = DataCrunchClient(
48-
client_id="your_client_id",
49-
client_secret="your_client_secret"
50-
)
52+
datacrunch = DataCrunchClient(
53+
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
5154

5255
# Example 1: List all compute resources
53-
print("\nAll compute resources:")
54-
all_resources = list_all_compute_resources(client)
56+
print("All compute resources:")
57+
all_resources = list_all_compute_resources(datacrunch)
5558
for resource in all_resources:
5659
print(
5760
f"Name: {resource.name}, Size: {resource.size}, Available: {resource.is_available}")
5861

5962
# Example 2: List available compute resources
60-
print("\nAvailable compute resources:")
61-
available_resources = list_available_compute_resources(client)
63+
print("Available compute resources:")
64+
available_resources = list_available_compute_resources(datacrunch)
6265
for resource in available_resources:
6366
print(f"Name: {resource.name}, Size: {resource.size}")
6467

6568
# Example 3: List compute resources of size 8
66-
print("\nCompute resources with size 8:")
67-
size_8_resources = list_compute_resources_by_size(client, 8)
69+
print("Compute resources with size 8:")
70+
size_8_resources = list_compute_resources_by_size(datacrunch, 8)
6871
for resource in size_8_resources:
6972
print(f"Name: {resource.name}, Available: {resource.is_available}")
7073

examples/containers/container_deployments_example.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
DEPLOYMENT_NAME = "my-deployment"
3232
IMAGE_NAME = "your-image-name:version"
3333

34-
# Environment variables
34+
# Get client secret and id from environment variables
3535
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
3636
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
3737

3838
# DataCrunch client instance
39-
datacrunch_client = None
39+
datacrunch = None
4040

4141

4242
def wait_for_deployment_health(client: DataCrunchClient, deployment_name: str, max_attempts: int = 10, delay: int = 30) -> bool:
@@ -81,15 +81,9 @@ def cleanup_resources(client: DataCrunchClient) -> None:
8181
def main() -> None:
8282
"""Main function demonstrating deployment lifecycle management."""
8383
try:
84-
# Check required environment variables
85-
if not DATACRUNCH_CLIENT_ID or not DATACRUNCH_CLIENT_SECRET:
86-
print(
87-
"Please set DATACRUNCH_CLIENT_ID and DATACRUNCH_CLIENT_SECRET environment variables")
88-
return
89-
9084
# Initialize client
91-
global datacrunch_client
92-
datacrunch_client = DataCrunchClient(
85+
global datacrunch
86+
datacrunch = DataCrunchClient(
9387
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
9488

9589
# Create container configuration
@@ -160,19 +154,19 @@ def main() -> None:
160154
)
161155

162156
# Create the deployment
163-
created_deployment = datacrunch_client.containers.create_deployment(
157+
created_deployment = datacrunch.containers.create_deployment(
164158
deployment)
165159
print(f"Created deployment: {created_deployment.name}")
166160

167161
# Wait for deployment to be healthy
168-
if not wait_for_deployment_health(datacrunch_client, DEPLOYMENT_NAME):
162+
if not wait_for_deployment_health(datacrunch, DEPLOYMENT_NAME):
169163
print("Deployment health check failed")
170-
cleanup_resources(datacrunch_client)
164+
cleanup_resources(datacrunch)
171165
return
172166

173167
# Update scaling configuration
174168
try:
175-
deployment = datacrunch_client.containers.get_deployment_by_name(
169+
deployment = datacrunch.containers.get_deployment_by_name(
176170
DEPLOYMENT_NAME)
177171
# Create new scaling options with increased replica counts
178172
deployment.scaling = ScalingOptions(
@@ -194,7 +188,7 @@ def main() -> None:
194188
)
195189
)
196190
)
197-
updated_deployment = datacrunch_client.containers.update_deployment(
191+
updated_deployment = datacrunch.containers.update_deployment(
198192
DEPLOYMENT_NAME, deployment)
199193
print(f"Updated deployment scaling: {updated_deployment.name}")
200194
except APIException as e:
@@ -203,33 +197,33 @@ def main() -> None:
203197
# Demonstrate deployment operations
204198
try:
205199
# Pause deployment
206-
datacrunch_client.containers.pause_deployment(DEPLOYMENT_NAME)
200+
datacrunch.containers.pause_deployment(DEPLOYMENT_NAME)
207201
print("Deployment paused")
208202
time.sleep(60)
209203

210204
# Resume deployment
211-
datacrunch_client.containers.resume_deployment(DEPLOYMENT_NAME)
205+
datacrunch.containers.resume_deployment(DEPLOYMENT_NAME)
212206
print("Deployment resumed")
213207

214208
# Restart deployment
215-
datacrunch_client.containers.restart_deployment(DEPLOYMENT_NAME)
209+
datacrunch.containers.restart_deployment(DEPLOYMENT_NAME)
216210
print("Deployment restarted")
217211

218212
# Purge queue
219-
datacrunch_client.containers.purge_deployment_queue(
213+
datacrunch.containers.purge_deployment_queue(
220214
DEPLOYMENT_NAME)
221215
print("Queue purged")
222216
except APIException as e:
223217
print(f"Error in deployment operations: {e}")
224218

225219
# Clean up
226-
cleanup_resources(datacrunch_client)
220+
cleanup_resources(datacrunch)
227221

228222
except Exception as e:
229223
print(f"Unexpected error: {e}")
230224
# Attempt cleanup even if there was an error
231225
try:
232-
cleanup_resources(datacrunch_client)
226+
cleanup_resources(datacrunch)
233227
except Exception as cleanup_error:
234228
print(f"Error during cleanup after failure: {cleanup_error}")
235229

examples/containers/environment_variables_example.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
from datacrunch import DataCrunchClient
1313
from typing import Dict, List
1414

15+
# Get client secret and id from environment variables
1516
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
1617
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
1718

1819
# Initialize DataCrunch client
19-
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
20-
client_secret=DATACRUNCH_CLIENT_SECRET)
20+
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
2121

2222
# Example deployment and container names
2323
DEPLOYMENT_NAME = "my-deployment"
@@ -36,13 +36,13 @@ def print_env_vars(env_vars: Dict[str, List[EnvVar]]) -> None:
3636
def main():
3737
# First, let's get the current environment variables
3838
print("Getting current environment variables...")
39-
env_vars = datacrunch_client.containers.get_deployment_environment_variables(
39+
env_vars = datacrunch.containers.get_deployment_environment_variables(
4040
DEPLOYMENT_NAME)
4141
print_env_vars(env_vars)
4242

4343
# Create a new secret
4444
secret_name = "my-secret-key"
45-
datacrunch_client.containers.create_secret(
45+
datacrunch.containers.create_secret(
4646
secret_name,
4747
"my-secret-value"
4848
)
@@ -62,7 +62,7 @@ def main():
6262
)
6363
]
6464

65-
env_vars = datacrunch_client.containers.add_deployment_environment_variables(
65+
env_vars = datacrunch.containers.add_deployment_environment_variables(
6666
deployment_name=DEPLOYMENT_NAME,
6767
container_name=CONTAINER_NAME,
6868
env_vars=new_env_vars
@@ -79,7 +79,7 @@ def main():
7979
),
8080
]
8181

82-
env_vars = datacrunch_client.containers.update_deployment_environment_variables(
82+
env_vars = datacrunch.containers.update_deployment_environment_variables(
8383
deployment_name=DEPLOYMENT_NAME,
8484
container_name=CONTAINER_NAME,
8585
env_vars=updated_env_vars
@@ -88,7 +88,7 @@ def main():
8888

8989
# Delete environment variables
9090
print("\nDeleting environment variables...")
91-
env_vars = datacrunch_client.containers.delete_deployment_environment_variables(
91+
env_vars = datacrunch.containers.delete_deployment_environment_variables(
9292
deployment_name=DEPLOYMENT_NAME,
9393
container_name=CONTAINER_NAME,
9494
env_var_names=["DEBUG"]

examples/containers/registry_credentials_example.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
CustomRegistryCredentials
99
)
1010

11-
# Environment variables
11+
# Get client secret and id from environment variables
1212
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
1313
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
1414

1515
# Initialize DataCrunch client
16-
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
17-
client_secret=DATACRUNCH_CLIENT_SECRET)
16+
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
1817

1918
# Example 1: DockerHub Credentials
2019
dockerhub_creds = DockerHubCredentials(
2120
name="my-dockerhub-creds",
2221
username="your-dockerhub-username",
2322
access_token="your-dockerhub-access-token"
2423
)
25-
datacrunch_client.containers.add_registry_credentials(dockerhub_creds)
24+
datacrunch.containers.add_registry_credentials(dockerhub_creds)
2625
print("Created DockerHub credentials")
2726

2827
# Example 2: GitHub Container Registry Credentials
@@ -31,7 +30,7 @@
3130
username="your-github-username",
3231
access_token="your-github-token"
3332
)
34-
datacrunch_client.containers.add_registry_credentials(github_creds)
33+
datacrunch.containers.add_registry_credentials(github_creds)
3534
print("Created GitHub credentials")
3635

3736
# Example 3: Google Container Registry (GCR) Credentials
@@ -53,7 +52,7 @@
5352
name="my-gcr-creds",
5453
service_account_key=gcr_service_account_key
5554
)
56-
datacrunch_client.containers.add_registry_credentials(gcr_creds)
55+
datacrunch.containers.add_registry_credentials(gcr_creds)
5756
print("Created GCR credentials")
5857

5958
# Example 4: AWS ECR Credentials
@@ -64,7 +63,7 @@
6463
region="eu-north-1",
6564
ecr_repo="887841266746.dkr.ecr.eu-north-1.amazonaws.com"
6665
)
67-
datacrunch_client.containers.add_registry_credentials(aws_creds)
66+
datacrunch.containers.add_registry_credentials(aws_creds)
6867
print("Created AWS ECR credentials")
6968

7069
# Example 5: Custom Registry Credentials
@@ -80,13 +79,13 @@
8079
name="my-custom-registry-creds",
8180
docker_config_json=custom_docker_config
8281
)
83-
datacrunch_client.containers.add_registry_credentials(custom_creds)
82+
datacrunch.containers.add_registry_credentials(custom_creds)
8483
print("Created Custom registry credentials")
8584

8685
# Delete all registry credentials
87-
datacrunch_client.containers.delete_registry_credentials('my-dockerhub-creds')
88-
datacrunch_client.containers.delete_registry_credentials('my-github-creds')
89-
datacrunch_client.containers.delete_registry_credentials('my-gcr-creds')
90-
datacrunch_client.containers.delete_registry_credentials('my-aws-ecr-creds')
91-
datacrunch_client.containers.delete_registry_credentials(
86+
datacrunch.containers.delete_registry_credentials('my-dockerhub-creds')
87+
datacrunch.containers.delete_registry_credentials('my-github-creds')
88+
datacrunch.containers.delete_registry_credentials('my-gcr-creds')
89+
datacrunch.containers.delete_registry_credentials('my-aws-ecr-creds')
90+
datacrunch.containers.delete_registry_credentials(
9291
'my-custom-registry-creds')
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
import os
22
from datacrunch import DataCrunchClient
33

4-
# Environment variables
4+
# Get client secret and id from environment variables
55
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
66
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
77

88
# Initialize DataCrunch client
9-
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
10-
client_secret=DATACRUNCH_CLIENT_SECRET)
9+
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
1110

1211
# List all secrets
13-
secrets = datacrunch_client.containers.get_secrets()
12+
secrets = datacrunch.containers.get_secrets()
1413
print("Available secrets:")
1514
for secret in secrets:
1615
print(f"- {secret.name} (created at: {secret.created_at})")
1716

1817
# Create a new secret
1918
secret_name = "my-api-key"
2019
secret_value = "super-secret-value"
21-
datacrunch_client.containers.create_secret(
20+
datacrunch.containers.create_secret(
2221
name=secret_name,
2322
value=secret_value
2423
)
2524
print(f"\nCreated new secret: {secret_name}")
2625

2726
# Delete a secret (with force=False by default)
28-
datacrunch_client.containers.delete_secret(secret_name)
27+
datacrunch.containers.delete_secret(secret_name)
2928
print(f"\nDeleted secret: {secret_name}")
3029

3130
# Delete a secret with force=True (will delete even if secret is in use)
3231
secret_name = "another-secret"
33-
datacrunch_client.containers.create_secret(
32+
datacrunch.containers.create_secret(
3433
name=secret_name,
3534
value=secret_value
3635
)
37-
datacrunch_client.containers.delete_secret(secret_name, force=True)
36+
datacrunch.containers.delete_secret(secret_name, force=True)
3837
print(f"\nForce deleted secret: {secret_name}")

0 commit comments

Comments
 (0)