Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update more python templates to use dictionary literals #825

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 52 additions & 54 deletions container-azure-python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
location=app_path,
),
platforms=[docker_build.Platform.LINUX_AMD64],
registries=[docker_build.RegistryArgs(
address=registry.login_server,
username=registry_username,
password=registry_password,
)],
registries=[{
"address": registry.login_server,
"username": registry_username,
"password": registry_password,
}],
)

# Use a random string to give the service a unique DNS name.
Expand All @@ -63,56 +63,54 @@
# Create a container group for the service that makes it publicly accessible.
container_group = containerinstance.ContainerGroup(
"container-group",
containerinstance.ContainerGroupArgs(
resource_group_name=resource_group.name,
os_type="linux",
restart_policy="always",
image_registry_credentials=[
containerinstance.ImageRegistryCredentialArgs(
server=registry.login_server,
username=registry_username,
password=registry_password,
),
],
containers=[
containerinstance.ContainerArgs(
name=image_name,
image=image.image_name,
ports=[
containerinstance.ContainerPortArgs(
port=container_port,
protocol="tcp",
),
],
environment_variables=[
containerinstance.EnvironmentVariableArgs(
name="FLASK_RUN_PORT",
value=str(container_port),
),
containerinstance.EnvironmentVariableArgs(
name="FLASK_RUN_HOST",
value="0.0.0.0",
),
],
resources=containerinstance.ResourceRequirementsArgs(
requests=containerinstance.ResourceRequestsArgs(
cpu=cpu,
memory_in_gb=memory,
),
),
),
],
ip_address=containerinstance.IpAddressArgs(
type=containerinstance.ContainerGroupIpAddressType.PUBLIC,
dns_name_label=dns_name,
ports=[
containerinstance.PortArgs(
port=container_port,
protocol="tcp",
),
resource_group_name=resource_group.name,
os_type="linux",
restart_policy="always",
image_registry_credentials=[
{
"server": registry.login_server,
"username": registry_username,
"password": registry_password,
},
],
containers=[
{
"name": image_name,
"image": image.image_name,
"ports": [
{
"port": container_port,
"protocol": "tcp",
},
],
),
),
"environment_variables": [
{
"name": "FLASK_RUN_PORT",
"value": str(container_port),
},
{
"name": "FLASK_RUN_HOST",
"value": "0.0.0.0",
},
],
"resources": {
"requests": {
"cpu": cpu,
"memory_in_gb": memory,
},
},
},
],
ip_address={
"type": containerinstance.ContainerGroupIpAddressType.PUBLIC,
"dns_name_label": dns_name,
"ports": [
{
"port": container_port,
"protocol":"tcp",
},
],
}
)

# Export the service's IP address, hostname, and fully-qualified URL.
Expand Down
68 changes: 32 additions & 36 deletions container-gcp-python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,47 +67,43 @@
# Create a Cloud Run service definition.
service = cloudrun.Service(
"service",
cloudrun.ServiceArgs(
location=location,
template=cloudrun.ServiceTemplateArgs(
spec=cloudrun.ServiceTemplateSpecArgs(
containers=[
cloudrun.ServiceTemplateSpecContainerArgs(
image=image.repo_digest,
resources=cloudrun.ServiceTemplateSpecContainerResourcesArgs(
limits=dict(
memory=memory,
cpu=cpu,
),
),
ports=[
cloudrun.ServiceTemplateSpecContainerPortArgs(
container_port=container_port,
),
],
envs=[
cloudrun.ServiceTemplateSpecContainerEnvArgs(
name="FLASK_RUN_PORT",
value=container_port,
),
],
),
],
container_concurrency=concurrency,
),
),
),
location=location,
template={
"spec": {
"containers": [
{
"image": image.repo_digest,
"resources": {
"limits": {
"memory": memory,
"cpu": str(cpu),
},
},
"ports": [
{
"container_port": container_port,
},
],
"envs": [
{
"name": "FLASK_RUN_PORT",
"value": str(container_port),
},
],
},
],
"container_concurrency": concurrency,
},
},
)

# Create an IAM member to make the service publicly accessible.
invoker = cloudrun.IamMember(
"invoker",
cloudrun.IamMemberArgs(
location=location,
service=service.name,
role="roles/run.invoker",
member="allUsers",
),
location=location,
service=service.name,
role="roles/run.invoker",
member="allUsers",
)

# Export the URL of the service.
Expand Down
Loading