Skip to content

Commit 71f2628

Browse files
committed
small refactor, create registry credentials example
1 parent d74e0c2 commit 71f2628

File tree

2 files changed

+134
-11
lines changed

2 files changed

+134
-11
lines changed

datacrunch/containers/containers.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -489,29 +489,74 @@ def get_registry_credentials(self) -> List[RegistryCredential]:
489489
response = self.client.get(CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT)
490490
return [RegistryCredential.from_dict(credential) for credential in response.json()]
491491

492-
def add_registry_credentials(self, name: str, registry_type: ContainerRegistryType, username: str, access_token: str) -> RegistryCredential:
492+
def add_registry_credentials(
493+
self,
494+
name: str,
495+
registry_type: ContainerRegistryType,
496+
username: str = None,
497+
access_token: str = None,
498+
service_account_key: str = None,
499+
docker_config_json: str = None,
500+
access_key_id: str = None,
501+
secret_access_key: str = None,
502+
region: str = None,
503+
ecr_repo: str = None
504+
) -> None:
493505
"""Add registry credentials
494506
495507
:param name: name of the credentials
496508
:type name: str
497509
:param registry_type: type of registry (e.g. ContainerRegistryType.DOCKERHUB)
498510
:type registry_type: ContainerRegistryType
499-
:param username: registry username
511+
:param username: registry username (required for DOCKERHUB and GITHUB)
500512
:type username: str
501-
:param access_token: registry access token
513+
:param access_token: registry access token (required for DOCKERHUB and GITHUB)
502514
:type access_token: str
503-
:return: created registry credential
504-
:rtype: RegistryCredential
515+
:param service_account_key: service account key JSON string (required for GCR)
516+
:type service_account_key: str
517+
:param docker_config_json: docker config JSON string (required for CUSTOM)
518+
:type docker_config_json: str
519+
:param access_key_id: AWS access key ID (required for AWS_ECR)
520+
:type access_key_id: str
521+
:param secret_access_key: AWS secret access key (required for AWS_ECR)
522+
:type secret_access_key: str
523+
:param region: AWS region (required for AWS_ECR)
524+
:type region: str
525+
:param ecr_repo: ECR repository URL (required for AWS_ECR)
526+
:type ecr_repo: str
505527
"""
506528
data = {
507529
"name": name,
508-
"registry_type": registry_type.value,
509-
"username": username,
510-
"access_token": access_token
530+
"type": registry_type.value
511531
}
512-
response = self.client.post(
513-
CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT, data)
514-
return RegistryCredential.from_dict(response.json())
532+
533+
# Add specific parameters based on registry type
534+
if registry_type == ContainerRegistryType.DOCKERHUB or registry_type == ContainerRegistryType.GITHUB:
535+
if not username or not access_token:
536+
raise ValueError(
537+
f"Username and access_token are required for {registry_type.value} registry type")
538+
data["username"] = username
539+
data["access_token"] = access_token
540+
elif registry_type == ContainerRegistryType.GCR:
541+
if not service_account_key:
542+
raise ValueError(
543+
"service_account_key is required for GCR registry type")
544+
data["service_account_key"] = service_account_key
545+
elif registry_type == ContainerRegistryType.AWS_ECR:
546+
if not all([access_key_id, secret_access_key, region, ecr_repo]):
547+
raise ValueError(
548+
"access_key_id, secret_access_key, region, and ecr_repo are required for AWS_ECR registry type")
549+
data["access_key_id"] = access_key_id
550+
data["secret_access_key"] = secret_access_key
551+
data["region"] = region
552+
data["ecr_repo"] = ecr_repo
553+
elif registry_type == ContainerRegistryType.CUSTOM:
554+
if not docker_config_json:
555+
raise ValueError(
556+
"docker_config_json is required for CUSTOM registry type")
557+
data["docker_config_json"] = docker_config_json
558+
559+
self.client.post(CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT, data)
515560

516561
def delete_registry_credentials(self, credentials_name: str) -> None:
517562
"""Delete registry credentials
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
from datacrunch import DataCrunchClient
3+
from datacrunch.containers.containers import ContainerRegistryType
4+
5+
# Environment variables
6+
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
7+
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
8+
9+
# Initialize DataCrunch client
10+
client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
11+
client_secret=DATACRUNCH_CLIENT_SECRET)
12+
13+
# Example 1: DockerHub Credentials
14+
client.containers.add_registry_credentials(
15+
name="my-dockerhub-creds",
16+
registry_type=ContainerRegistryType.DOCKERHUB,
17+
username="your-dockerhub-username",
18+
access_token="your-dockerhub-access-token"
19+
)
20+
print("Created DockerHub credentials")
21+
22+
# Example 2: GitHub Container Registry Credentials
23+
client.containers.add_registry_credentials(
24+
name="my-github-creds",
25+
registry_type=ContainerRegistryType.GITHUB,
26+
username="your-github-username",
27+
access_token="your-github-token"
28+
)
29+
print("Created GitHub credentials")
30+
31+
# Example 3: Google Container Registry (GCR) Credentials
32+
# For GCR, you need to provide a service account key JSON string
33+
gcr_service_account_key = """{
34+
"type": "service_account",
35+
"project_id": "your-project-id",
36+
"private_key_id": "private-key-id",
37+
"private_key": "-----BEGIN PRIVATE KEY-----\\nYOUR_PRIVATE_KEY_HERE\\n-----END PRIVATE KEY-----\\n",
38+
"client_email": "your-service-account@your-project.iam.gserviceaccount.com",
39+
"client_id": "client-id",
40+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
41+
"token_uri": "https://oauth2.googleapis.com/token",
42+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
43+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com"
44+
}"""
45+
46+
client.containers.add_registry_credentials(
47+
name="my-gcr-creds",
48+
registry_type=ContainerRegistryType.GCR,
49+
service_account_key=gcr_service_account_key
50+
)
51+
print("Created GCR credentials")
52+
53+
# Example 4: AWS ECR Credentials
54+
client.containers.add_registry_credentials(
55+
name="my-aws-ecr-creds",
56+
registry_type=ContainerRegistryType.AWS_ECR,
57+
access_key_id="your-aws-access-key-id",
58+
secret_access_key="your-aws-secret-access-key",
59+
region="us-west-2",
60+
ecr_repo="123456789012.dkr.ecr.us-west-2.amazonaws.com"
61+
)
62+
print("Created AWS ECR credentials")
63+
64+
# Example 5: Custom Registry Credentials
65+
custom_docker_config = """{
66+
"auths": {
67+
"your-custom-registry.com": {
68+
"auth": "base64-encoded-username-password"
69+
}
70+
}
71+
}"""
72+
73+
client.containers.add_registry_credentials(
74+
name="my-custom-registry-creds",
75+
registry_type=ContainerRegistryType.CUSTOM,
76+
docker_config_json=custom_docker_config
77+
)
78+
print("Created Custom registry credentials")

0 commit comments

Comments
 (0)