Skip to content

Commit

Permalink
Merge pull request #825 from pulumi/julienp/dict-literals
Browse files Browse the repository at this point in the history
Update more python templates to use dictionary literals
  • Loading branch information
julienp authored Aug 23, 2024
2 parents 060a12f + 61868e8 commit 3faa2b5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 90 deletions.
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

0 comments on commit 3faa2b5

Please sign in to comment.