Skip to content

Commit

Permalink
Refactor and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Jul 6, 2024
1 parent 764d251 commit e17b19d
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,27 @@
load_dotenv()

PROJECT_NAME = f"{pulumi.get_project()}-{pulumi.get_stack()}"

# Generate a timestamp for versioning
TIMESTAMP = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

config = pulumi.Config()
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")


def create_zip(files_to_zip):
temp_dir = tempfile.TemporaryDirectory().name
os.makedirs(temp_dir)
zip_file = os.path.join(temp_dir, "function.zip")
with zipfile.ZipFile(zip_file, "w") as zipf:
for file in files_to_zip:
zipf.write(file, os.path.basename(file))
return zip_file
with tempfile.TemporaryDirectory() as temp_dir:
zip_file = os.path.join(temp_dir, "function.zip")
with zipfile.ZipFile(zip_file, "w") as zipf:
for file in files_to_zip:
zipf.write(file, os.path.basename(file))
return zip_file


zip_path = create_zip(["main.py", "calculator.py", "requirements.txt"])

bucket = gcp.storage.Bucket("bucket", name=f"{PROJECT_NAME}-bucket", location="ASIA")

# Create a more descriptive archive name
archive_name = f"{PROJECT_NAME}-function-{TIMESTAMP}.zip"

bucket = gcp.storage.Bucket("bucket", name=f"{PROJECT_NAME}-bucket", location="ASIA")
archive = gcp.storage.BucketObject(
"archive", bucket=bucket.name, name=archive_name, source=pulumi.FileAsset(zip_path)
)


function = gcp.cloudfunctionsv2.Function(
"function",
name=f"{PROJECT_NAME}-function",
Expand Down Expand Up @@ -67,7 +58,7 @@ def create_zip(files_to_zip):
),
)

function_invoker = gcp.cloudfunctionsv2.FunctionIamMember(
gcp.cloudfunctionsv2.FunctionIamMember(
"function-invoker",
project=function.project,
location=function.location,
Expand All @@ -76,7 +67,7 @@ def create_zip(files_to_zip):
member="allUsers",
)

cloudrun_invoker = gcp.cloudrun.IamMember(
gcp.cloudrun.IamMember(
"cloudrun-invoker",
location=function.location,
project=function.project,
Expand Down

0 comments on commit e17b19d

Please sign in to comment.