mirror of
https://github.com/status-im/spiffworkflow-connector.git
synced 2025-02-22 11:38:17 +00:00
Merge pull request #9 from sartography/feature/dynamo_2
using locally configured access keys for AWS
This commit is contained in:
commit
1b9ecca6aa
@ -6,20 +6,24 @@ from botocore.config import Config # type: ignore
|
|||||||
class SimpleAuth:
|
class SimpleAuth:
|
||||||
"""Established a simple Boto 3 Client based on an access key and a secret key."""
|
"""Established a simple Boto 3 Client based on an access key and a secret key."""
|
||||||
|
|
||||||
def __init__(self, resource_type: str, access_key: str, secret_key: str):
|
def __init__(self, resource_type: str, config: dict):
|
||||||
"""
|
"""
|
||||||
:param access_key: AWS Access Key
|
:param access_key: AWS Access Key
|
||||||
:param secret_key: AWS Secret Key
|
:param secret_key: AWS Secret Key
|
||||||
"""
|
"""
|
||||||
|
aws_access_key_id = config["AWS_ACCESS_KEY_ID"]
|
||||||
|
aws_secret_access_key = config["AWS_SECRET_ACCESS_KEY"]
|
||||||
|
aws_region = config["AWS_REGION"]
|
||||||
|
|
||||||
my_config = Config(
|
my_config = Config(
|
||||||
region_name="us-east-1", retries={"max_attempts": 10, "mode": "standard"}
|
region_name=aws_region, retries={"max_attempts": 10, "mode": "standard"}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get the service resource.
|
# Get the service resource.
|
||||||
self.resource = boto3.resource(
|
self.resource = boto3.resource(
|
||||||
resource_type,
|
resource_type,
|
||||||
aws_access_key_id=access_key,
|
aws_access_key_id=aws_access_key_id,
|
||||||
aws_secret_access_key=secret_key,
|
aws_secret_access_key=aws_secret_access_key,
|
||||||
config=my_config,
|
config=my_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -8,29 +8,25 @@ class AddDynamoItem:
|
|||||||
"""Add a new record to a dynamo db table."""
|
"""Add a new record to a dynamo db table."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, access_key: str, secret_key: str, table_name: str, item_data: str
|
self, table_name: str, item_data: str
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
:param access_key: AWS Access Key
|
|
||||||
:param secret_key: AWS Secret Key
|
|
||||||
:param table_name: The name of hte Dynamo DB table to add information to.
|
:param table_name: The name of hte Dynamo DB table to add information to.
|
||||||
:param item_data: The data to add
|
:param item_data: The data to add, should be in json format.
|
||||||
:return: Json Data structure containing a http status code (hopefully '200' for success..)
|
:return: Json Data structure containing a http status code (hopefully '200' for success..)
|
||||||
and a response string.
|
and a response string.
|
||||||
"""
|
"""
|
||||||
# Get the service resource.
|
self.table_name = table_name
|
||||||
self.dynamodb = SimpleAuth("dynamodb", access_key, secret_key).get_resource()
|
self.item_data = item_data
|
||||||
|
|
||||||
# Instantiate a table resource object without actually
|
|
||||||
# creating a DynamoDB table. Note that the attributes of this table
|
|
||||||
# are lazy-loaded: a request is not made nor are the attribute
|
|
||||||
# values populated until the attributes
|
|
||||||
# on the table resource are accessed or its load() method is called.
|
|
||||||
self.table = self.dynamodb.Table(table_name)
|
|
||||||
self.item_data = json.loads(item_data)
|
|
||||||
|
|
||||||
def execute(self, config, task_data):
|
def execute(self, config, task_data):
|
||||||
"""Execute."""
|
"""Execute."""
|
||||||
|
# Get the service resource.
|
||||||
|
self.dynamodb = SimpleAuth("dynamodb", config).get_resource()
|
||||||
|
self.table = self.dynamodb.Table(self.table_name)
|
||||||
|
self.item_data = json.loads(self.item_data)
|
||||||
|
|
||||||
result = self.table.put_item(Item=self.item_data)
|
result = self.table.put_item(Item=self.item_data)
|
||||||
if "ResponseMetadata" in result:
|
if "ResponseMetadata" in result:
|
||||||
del result["ResponseMetadata"]
|
del result["ResponseMetadata"]
|
||||||
|
@ -7,21 +7,20 @@ from connector_aws.auths.simpleAuth import SimpleAuth # type: ignore
|
|||||||
class QueryDynamoTable:
|
class QueryDynamoTable:
|
||||||
"""Return all records for a given partition key."""
|
"""Return all records for a given partition key."""
|
||||||
|
|
||||||
def __init__(self, access_key: str, secret_key: str, table_name: str, key: str):
|
def __init__(self, table_name: str, key: str):
|
||||||
"""
|
"""
|
||||||
:param access_key: AWS Access Key
|
|
||||||
:param secret_key: AWS Secret Key
|
|
||||||
:param table_name: The name of hte Dynamo DB table to add information to.
|
:param table_name: The name of hte Dynamo DB table to add information to.
|
||||||
:param key: The partition key for what to return.
|
:param key: The partition key for what to return.
|
||||||
:return: Json Data structure containing the requested data.
|
:return: Json Data structure containing the requested data.
|
||||||
"""
|
"""
|
||||||
self.dynamodb = SimpleAuth("dynamodb", access_key, secret_key).get_resource()
|
self.table_name = table_name
|
||||||
self.table = self.dynamodb.Table(table_name)
|
|
||||||
self.key = key
|
self.key = key
|
||||||
|
|
||||||
def execute(self, config, task_data):
|
def execute(self, config, task_data):
|
||||||
"""Execute."""
|
"""Execute."""
|
||||||
result = self.table.get_item(Key={"primaryKeyName": self.key})
|
dynamodb = SimpleAuth("dynamodb", config).get_resource()
|
||||||
|
table = dynamodb.Table(self.table_name)
|
||||||
|
result = table.get_item(Key={"primaryKeyName": self.key})
|
||||||
if "ResponseMetadata" in result:
|
if "ResponseMetadata" in result:
|
||||||
del result["ResponseMetadata"]
|
del result["ResponseMetadata"]
|
||||||
result_str = json.dumps(result)
|
result_str = json.dumps(result)
|
||||||
|
@ -7,19 +7,18 @@ from connector_aws.auths.simpleAuth import SimpleAuth # type: ignore
|
|||||||
class ScanDynamoTable:
|
class ScanDynamoTable:
|
||||||
"""Return all records in a given table. Potentially very expensive."""
|
"""Return all records in a given table. Potentially very expensive."""
|
||||||
|
|
||||||
def __init__(self, access_key: str, secret_key: str, table_name: str):
|
def __init__(self, table_name: str):
|
||||||
"""
|
"""
|
||||||
:param access_key: AWS Access Key
|
|
||||||
:param secret_key: AWS Secret Key
|
|
||||||
:param table_name: The name of hte Dynamo DB table to scan
|
:param table_name: The name of hte Dynamo DB table to scan
|
||||||
:return: Json Data structure containing the requested data.
|
:return: Json Data structure containing the requested data.
|
||||||
"""
|
"""
|
||||||
self.dynamodb = SimpleAuth("dynamodb", access_key, secret_key).get_resource()
|
self.table_name = table_name
|
||||||
self.table = self.dynamodb.Table(table_name)
|
|
||||||
|
|
||||||
def execute(self, config, task_data):
|
def execute(self, config, task_data):
|
||||||
"""Execute."""
|
"""Execute."""
|
||||||
result = self.table.scan()
|
dynamodb = SimpleAuth("dynamodb", config).get_resource()
|
||||||
|
table = dynamodb.Table(self.table_name)
|
||||||
|
result = table.scan()
|
||||||
if "ResponseMetadata" in result:
|
if "ResponseMetadata" in result:
|
||||||
del result["ResponseMetadata"]
|
del result["ResponseMetadata"]
|
||||||
result_str = json.dumps(result)
|
result_str = json.dumps(result)
|
||||||
|
@ -8,22 +8,17 @@ class UploadFileData:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
access_key: str,
|
|
||||||
secret_key: str,
|
|
||||||
file_data: bytes,
|
file_data: bytes,
|
||||||
bucket: str,
|
bucket: str,
|
||||||
object_name: str,
|
object_name: str,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
:param access_key: AWS Access Key
|
|
||||||
:param secret_key: AWS Secret Key
|
|
||||||
:param file_data: Contents of file to be uploaded
|
:param file_data: Contents of file to be uploaded
|
||||||
:param bucket: Bucket to upload to
|
:param bucket: Bucket to upload to
|
||||||
:param object_name: S3 object name.
|
:param object_name: S3 object name.
|
||||||
:return: Json Data structure containing a http status code (hopefully '200' for success..)
|
:return: Json Data structure containing a http status code (hopefully '200' for success..)
|
||||||
and a response string.
|
and a response string.
|
||||||
"""
|
"""
|
||||||
self.client = SimpleAuth("s3", access_key, secret_key).get_resource()
|
|
||||||
self.file_data = file_data
|
self.file_data = file_data
|
||||||
self.bucket = bucket
|
self.bucket = bucket
|
||||||
self.object_name = object_name
|
self.object_name = object_name
|
||||||
@ -31,8 +26,9 @@ class UploadFileData:
|
|||||||
def execute(self, config, task_data):
|
def execute(self, config, task_data):
|
||||||
"""Execute."""
|
"""Execute."""
|
||||||
# Upload the file
|
# Upload the file
|
||||||
|
client = SimpleAuth("s3", config).get_resource()
|
||||||
try:
|
try:
|
||||||
result = self.client.Object(self.bucket, self.object_name).put(
|
result = client.Object(self.bucket, self.object_name).put(
|
||||||
Body=self.file_data
|
Body=self.file_data
|
||||||
)
|
)
|
||||||
status = str(result["ResponseMetadata"]["HTTPStatusCode"])
|
status = str(result["ResponseMetadata"]["HTTPStatusCode"])
|
||||||
|
@ -65,8 +65,6 @@ class CreatePDFAndUploadToS3:
|
|||||||
}
|
}
|
||||||
|
|
||||||
aws_result = UploadFileData(
|
aws_result = UploadFileData(
|
||||||
aws_access_key_id,
|
|
||||||
aws_secret_access_key,
|
|
||||||
pdf_result["response"],
|
pdf_result["response"],
|
||||||
aws_bucket,
|
aws_bucket,
|
||||||
self.aws_object_name,
|
self.aws_object_name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user