-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored boto client module and update boto clients
- Loading branch information
alpegon
committed
Jun 18, 2019
1 parent
e93f867
commit d7889f9
Showing
11 changed files
with
183 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (C) GRyCAP - I3M - UPV | ||
# | ||
# 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. | ||
"""Module with the generic classes and methods used in the boto client modules.""" | ||
|
||
from typing import Dict | ||
import boto3 | ||
import botocore | ||
from scar.utils import lazy_property | ||
|
||
|
||
class BotoClient(): | ||
"""Generic class in charge of creating a boto client based on the | ||
'boto_client_name' property of the class that inherits it.""" | ||
|
||
_READ_TIMEOUT = 360 | ||
_BOTO_CLIENT_NAME = '' | ||
|
||
def __init__(self, client: Dict = None, session: Dict = None): | ||
""" | ||
Default client args: | ||
'client' : {'region_name' : self.aws.region} | ||
Default session args: | ||
'session' : {'profile_name' : self.aws.boto_profile} | ||
""" | ||
self.client_args = {} | ||
if client: | ||
self.client_args = client | ||
self.session_args = {} | ||
if session: | ||
self.session_args = session | ||
|
||
@lazy_property | ||
def client(self): | ||
"""Returns a boto client based on the 'boto_client_name' property, | ||
the region specified on the client args and | ||
the profile specified on the session args.""" | ||
# 'default' profile if nothing set | ||
session = boto3.Session(**self.session_args) | ||
self.client_args['config'] = botocore.config.Config(read_timeout=self._READ_TIMEOUT) | ||
return session.client(self._BOTO_CLIENT_NAME, **self.client_args) | ||
|
||
def get_access_key(self) -> str: | ||
"""Returns the access key belonging to the boto_profile used.""" | ||
session = boto3.Session(**self.session_args) | ||
credentials = session.get_credentials() | ||
return credentials.access_key |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.