This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,6 @@ coverage.* | |
|
||
#e2e | ||
/test-results/ | ||
|
||
# localstack | ||
/volume |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
|
||
import botocore.client | ||
import botocore.exceptions | ||
|
||
import src.logging | ||
from src.adapters.aws import S3Config, get_s3_client | ||
from src.util.local import error_if_not_local | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def does_s3_bucket_exist(s3_client: botocore.client.BaseClient, bucket_name: str) -> bool: | ||
try: | ||
s3_client.head_bucket(Bucket=bucket_name) | ||
return True | ||
except botocore.exceptions.ClientError as e: | ||
# We'll assume that if the error code is a 404 that means | ||
# it could not find the bucket and thus it needs to be created | ||
# as there are not more specific errors than this available | ||
error_code = e.response.get("Error", {}).get("Code") | ||
if error_code != "404": | ||
raise e | ||
|
||
return False | ||
|
||
|
||
def setup_s3() -> None: | ||
s3_config = S3Config() | ||
s3_client = get_s3_client(s3_config) | ||
|
||
if s3_config.s3_opportunity_bucket is None: | ||
raise Exception("S3_OPPORTUNITY_BUCKET env var must be set") | ||
|
||
if not does_s3_bucket_exist(s3_client, s3_config.s3_opportunity_bucket): | ||
logger.info("Creating S3 bucket %s", s3_config.s3_opportunity_bucket) | ||
s3_client.create_bucket(Bucket=s3_config.s3_opportunity_bucket) | ||
else: | ||
logger.info("S3 bucket %s already exists - skipping", s3_config.s3_opportunity_bucket) | ||
|
||
|
||
def main() -> None: | ||
with src.logging.init("setup_localstack"): | ||
error_if_not_local() | ||
setup_s3() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .s3_adapter import S3Config, get_s3_client | ||
|
||
__all__ = ["get_s3_client", "S3Config"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import boto3 | ||
import botocore.client | ||
|
||
from src.util.env_config import PydanticBaseEnvConfig | ||
|
||
|
||
class S3Config(PydanticBaseEnvConfig): | ||
# We should generally not need to set this except | ||
# locally to use localstack | ||
s3_endpoint_url: str | None = None | ||
|
||
### S3 Buckets | ||
# note that we default these to None | ||
# so that we don't need to set all of these for every | ||
# process that uses S3 | ||
|
||
# TODO - I'm not sure how we want to organize our | ||
# s3 buckets so this will likely change in the future | ||
s3_opportunity_bucket: str | None = None | ||
|
||
|
||
def get_s3_client(s3_config: S3Config | None = None) -> botocore.client.BaseClient: | ||
if s3_config is None: | ||
s3_config = S3Config() | ||
|
||
params = {} | ||
if s3_config.s3_endpoint_url is not None: | ||
params["endpoint_url"] = s3_config.s3_endpoint_url | ||
|
||
return boto3.client("s3", **params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters