-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added automated Athena workgroup creation if missing (#618)
- Loading branch information
1 parent
ad78189
commit e218ebc
Showing
4 changed files
with
154 additions
and
6 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
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,75 @@ | ||
import json | ||
import logging | ||
import botocore | ||
from typing import Optional, List | ||
|
||
from cid.base import CidBase | ||
from cid.exceptions import CidError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class S3(CidBase): | ||
|
||
def __init__(self, session): | ||
super().__init__(session) | ||
self.client = self.session.client('s3', region_name=self.region) | ||
|
||
def ensure_bucket(self, name: str) -> str: | ||
try: | ||
response = self.client.head_bucket(Bucket=name) | ||
return name | ||
except botocore.exceptions.ClientError as ex: | ||
if int(ex.response['Error']['Code']) != 404: | ||
raise CidError(f"Cannot check bucket {ex}!") | ||
|
||
response = self.client.create_bucket( | ||
ACL='private', | ||
Bucket=name | ||
) | ||
response = self.client.put_bucket_encryption( | ||
Bucket=name, | ||
ServerSideEncryptionConfiguration={ | ||
'Rules': [ | ||
{ | ||
'ApplyServerSideEncryptionByDefault': { | ||
'SSEAlgorithm': 'AES256', | ||
}, | ||
}, | ||
] | ||
} | ||
) | ||
|
||
response = self.client.put_bucket_lifecycle_configuration( | ||
Bucket=name, | ||
LifecycleConfiguration={ | ||
'Rules': [ | ||
{ | ||
'Expiration': { | ||
'Days': 14, | ||
}, | ||
'Filter': { | ||
'Prefix': '/', | ||
}, | ||
'ID': 'ExpireAfter14Days', | ||
'Status': 'Enabled', | ||
}, | ||
], | ||
}, | ||
) | ||
return name | ||
|
||
def list_buckets(self, region_name: Optional[str] = None) -> List[str]: | ||
buckets = self.client.list_buckets() | ||
bucket_regions = { | ||
x['Name']: self.client.get_bucket_location(Bucket=x['Name']).get('LocationConstraint', None) for x in buckets['Buckets'] | ||
} | ||
for bucket in bucket_regions: | ||
if bucket_regions[bucket] is None: | ||
bucket_regions[bucket] = 'us-east-1' | ||
|
||
if region_name: | ||
bucket_names = [x['Name'] for x in buckets['Buckets'] if bucket_regions.get(x['Name']) == region_name] | ||
else: | ||
bucket_names = [x['Name'] for x in buckets['Buckets']] | ||
return bucket_names |