2021-12-25 16:29:10 -08:00
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-01-07 06:17:48 +01:00
|
|
|
from typing import Any, Optional
|
2021-12-25 16:29:10 -08:00
|
|
|
|
|
|
|
from boto3 import Session
|
2022-04-07 13:45:47 +05:30
|
|
|
from pydantic import SecretStr
|
2021-12-25 16:29:10 -08:00
|
|
|
|
|
|
|
from metadata.config.common import ConfigModel
|
|
|
|
|
|
|
|
|
|
|
|
class AWSClientConfigModel(ConfigModel):
|
|
|
|
"""
|
|
|
|
AWSClientConfigModel holds all config parameters required to instantiate an AWSClient.
|
|
|
|
"""
|
|
|
|
|
2022-04-07 13:45:47 +05:30
|
|
|
awsAccessKeyId: Optional[str]
|
|
|
|
awsSecretAccessKey: Optional[SecretStr]
|
|
|
|
awsSessionToken: Optional[str]
|
|
|
|
endPointURL: Optional[str]
|
|
|
|
awsRegion: Optional[str]
|
2021-12-25 16:29:10 -08:00
|
|
|
|
|
|
|
|
|
|
|
class AWSClient:
|
|
|
|
"""
|
|
|
|
AWSClient creates a boto3 Session client based on AWSClientConfigModel.
|
|
|
|
"""
|
|
|
|
|
|
|
|
config: AWSClientConfigModel
|
|
|
|
|
|
|
|
def __init__(self, config: AWSClientConfigModel):
|
2022-04-07 13:45:47 +05:30
|
|
|
|
2021-12-25 16:29:10 -08:00
|
|
|
self.config = config
|
|
|
|
|
|
|
|
def _get_session(self) -> Session:
|
|
|
|
if (
|
2022-04-07 13:45:47 +05:30
|
|
|
self.config.awsAccessKeyId
|
|
|
|
and self.config.awsSecretAccessKey
|
|
|
|
and self.config.awsSessionToken
|
2021-12-25 16:29:10 -08:00
|
|
|
):
|
|
|
|
return Session(
|
2022-04-07 13:45:47 +05:30
|
|
|
aws_access_key_id=self.config.awsAccessKeyId,
|
|
|
|
aws_secret_access_key=self.config.awsSecretAccessKey.get_secret_value(),
|
|
|
|
aws_session_token=self.config.awsSessionToken,
|
|
|
|
region_name=self.config.awsRegion,
|
2021-12-25 16:29:10 -08:00
|
|
|
)
|
2022-04-07 13:45:47 +05:30
|
|
|
if self.config.awsAccessKeyId and self.config.awsSecretAccessKey:
|
2021-12-25 16:29:10 -08:00
|
|
|
return Session(
|
2022-04-07 13:45:47 +05:30
|
|
|
aws_access_key_id=self.config.awsAccessKeyId,
|
|
|
|
aws_secret_access_key=self.config.awsSecretAccessKey.get_secret_value(),
|
|
|
|
region_name=self.config.awsRegion,
|
2021-12-25 16:29:10 -08:00
|
|
|
)
|
2022-04-07 13:45:47 +05:30
|
|
|
if self.config.awsRegion:
|
|
|
|
return Session(region_name=self.config.awsRegion)
|
2021-12-25 16:29:10 -08:00
|
|
|
return Session()
|
|
|
|
|
|
|
|
def get_client(self, service_name: str) -> Any:
|
|
|
|
session = self._get_session()
|
2022-04-07 13:45:47 +05:30
|
|
|
if self.config.endPointURL is not None:
|
2021-12-25 16:29:10 -08:00
|
|
|
return session.client(
|
2022-04-07 13:45:47 +05:30
|
|
|
service_name=service_name, endpoint_url=self.config.endPointURL
|
2021-12-25 16:29:10 -08:00
|
|
|
)
|
|
|
|
return session.client(service_name=service_name)
|
2022-02-08 00:22:11 +05:30
|
|
|
|
|
|
|
def get_resource(self, service_name: str) -> Any:
|
|
|
|
session = self._get_session()
|
2022-04-07 13:45:47 +05:30
|
|
|
if self.config.endPointURL is not None:
|
2022-02-08 00:22:11 +05:30
|
|
|
return session.resource(
|
2022-04-07 13:45:47 +05:30
|
|
|
service_name=service_name, endpoint_url=self.config.endPointURL
|
2022-02-08 00:22:11 +05:30
|
|
|
)
|
|
|
|
return session.resource(service_name=service_name)
|