Skip to content

Commit

Permalink
Update some python examples to use dictionary literals (#1669)
Browse files Browse the repository at this point in the history
Updates the following examples to use typed dictionaries

* aws-py-s3-folder
* kubernetes-py-exposed-deployment
* kubernetes-py-guestbook/simple
  • Loading branch information
julienp authored Jul 18, 2024
2 parents 093166f + 5b96646 commit e65e141
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 225 deletions.
62 changes: 34 additions & 28 deletions aws-py-s3-folder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,52 @@
from pulumi import FileAsset, Output, export, ResourceOptions
from pulumi_aws import s3

web_bucket = s3.Bucket('s3-website-bucket',
website=s3.BucketWebsiteArgs(
index_document="index.html",
))
web_bucket = s3.Bucket(
"s3-website-bucket",
website={
"index_document": "index.html",
},
)

public_access_block = s3.BucketPublicAccessBlock(
'public-access-block',
bucket=web_bucket.id,
block_public_acls=False)
"public-access-block", bucket=web_bucket.id, block_public_acls=False
)

content_dir = "www"
for file in os.listdir(content_dir):
filepath = os.path.join(content_dir, file)
mime_type, _ = mimetypes.guess_type(filepath)
obj = s3.BucketObject(file,
bucket=web_bucket.id,
source=FileAsset(filepath),
content_type=mime_type)
obj = s3.BucketObject(
file, bucket=web_bucket.id, source=FileAsset(filepath), content_type=mime_type
)


def public_read_policy_for_bucket(bucket_name):
return Output.json_dumps({
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
return Output.json_dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": [
Output.format("arn:aws:s3:::{0}/*", bucket_name),
],
}
],
"Resource": [
Output.format("arn:aws:s3:::{0}/*", bucket_name),
]
}]
})
}
)


bucket_name = web_bucket.id
bucket_policy = s3.BucketPolicy("bucket-policy",
bucket_policy = s3.BucketPolicy(
"bucket-policy",
bucket=bucket_name,
policy=public_read_policy_for_bucket(bucket_name),
opts=ResourceOptions(depends_on=[public_access_block]))
policy=public_read_policy_for_bucket(bucket_name),
opts=ResourceOptions(depends_on=[public_access_block]),
)

# Export the name of the bucket
export('bucket_name', web_bucket.id)
export('website_url', web_bucket.website_endpoint)
export("bucket_name", web_bucket.id)
export("website_url", web_bucket.website_endpoint)
14 changes: 7 additions & 7 deletions aws-py-webserver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
ami = aws.ec2.get_ami(
most_recent=True,
owners=["amazon"],
filters=[aws.ec2.GetAmiFilterArgs(name="name", values=["amzn2-ami-hvm-*"])],
filters=[{"name": "name", "values": ["amzn2-ami-hvm-*"]}],
)

group = aws.ec2.SecurityGroup(
"web-secgrp",
description="Enable HTTP access",
ingress=[
aws.ec2.SecurityGroupIngressArgs(
protocol="tcp",
from_port=80,
to_port=80,
cidr_blocks=["0.0.0.0/0"],
)
{
"protocol": "tcp",
"from_port": 80,
"to_port": 80,
"cidr_blocks": ["0.0.0.0/0"],
}
],
)

Expand Down
2 changes: 1 addition & 1 deletion kubernetes-py-exposed-deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ case `35.226.79.225`. It is exported with a stack output variable, `frontend_ip`
and `grep` to retrieve the `<title>` of the site the proxy points at.

> _Note_: minikube does not support type `LoadBalancer`; if you are deploying to minikube, make sure
> to run `kubectl port-forward service/frontend 8080:80` to forward the cluster port to the local
> to run `kubectl port-forward $(kubectl get service -l app=nginx -o name) 8080:80` to forward the cluster port to the local
> machine and access the service via `localhost:8080`.
```sh
Expand Down
73 changes: 37 additions & 36 deletions kubernetes-py-exposed-deployment/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Copyright 2016-2019, Pulumi Corporation. All rights reserved."""
"""Copyright 2016-2024, Pulumi Corporation. All rights reserved."""

import pulumi
import pulumi_kubernetes as k8s
Expand All @@ -9,48 +9,49 @@

"""nginx container, replicated 1 time."""
app_name = "nginx"
app_labels = { "app": app_name }
app_labels = {"app": app_name}

nginx = k8s.apps.v1.Deployment(
app_name,
spec=k8s.apps.v1.DeploymentSpecArgs(
replicas=1,
selector=k8s.meta.v1.LabelSelectorArgs(match_labels=app_labels),
template=k8s.core.v1.PodTemplateSpecArgs(
metadata=k8s.meta.v1.ObjectMetaArgs(labels=app_labels),
spec=k8s.core.v1.PodSpecArgs(
containers=[
k8s.core.v1.ContainerArgs(
name=app_name,
image="nginx:1.15-alpine",
)
]
),
),
)
)
app_name,
spec={
"replicas": 1,
"selector": {"match_labels": app_labels},
"template": {
"metadata": {"labels": app_labels},
"spec": {
"containers": [
{
"name": app_name,
"image": "nginx:1.16-alpine",
}
]
},
},
},
)


"""Allocate an IP to the nginx Deployment."""
frontend = k8s.core.v1.Service(
app_name,
metadata=k8s.meta.v1.ObjectMetaArgs(
labels=app_labels),
spec=k8s.core.v1.ServiceSpecArgs(
selector=app_labels,
ports=[
k8s.core.v1.ServicePortArgs(
port=80,
target_port=80,
protocol="TCP"
)
],
type="ClusterIP" if is_minikube == "true" else "LoadBalancer",
),
)
app_name,
metadata={"labels": app_labels},
spec={
"selector": app_labels,
"ports": [{"port": 80, "target_port": 80, "protocol": "TCP"}],
"type": "ClusterIP" if is_minikube == "true" else "LoadBalancer",
},
)

# """When "done", this will print the public IP."""
if is_minikube == "true":
pulumi.export("frontend_IP", frontend.spec.apply(lambda s: s.cluster_ip))
pulumi.export("frontend_IP", frontend.spec.apply(lambda s: s.cluster_ip))
else:
pulumi.export("frontend_IP", frontend.status.apply(lambda s: s.load_balancer.ingress[0].ip))
pulumi.export(
"frontend_IP",
frontend.status.apply(
lambda s: s
and s.load_balancer
and s.load_balancer.ingress
and s.load_balancer.ingress[0].ip
),
)
4 changes: 2 additions & 2 deletions kubernetes-py-exposed-deployment/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pulumi>=3.5.1,<4.0.0
pulumi-kubernetes>=3.4.0,<4.0.0
pulumi>=3.124.0,<4.0.0
pulumi-kubernetes>=4.15.0,<5.0.0
Loading

0 comments on commit e65e141

Please sign in to comment.