Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/auth/authorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ class Authorizer:
IAMユーザーのアクセスキーを用いたBasic認証により、APIへのアクセス可否を判定する。
環境変数 IS_SKIPPED を true と設定している場合、認証スキップができる(Lambda自体の実行はする)。

Args:
event (dict): API Gatewayから受取ったイベント。

"""

def __init__(self, event):
"""初期化する。

Args:
event (dict): API Gatewayから受取ったイベント。

"""
self._event = event
self._is_skipped = os.getenv('IS_SKIPPED') == 'true'

Expand Down
41 changes: 15 additions & 26 deletions src/aws_resources/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ class EC2Instance:

boto3を使用してEC2インスタンスの状態確認・起動を行う。

Attributes:
state (State): インスタンスの現在の状態。
Args:
instance_id (str): 対象のEC2インスタンスID。
dry_run (bool): ドライランモードの場合は真を表す真偽値
dry_run (bool, optional): Trueの場合は実際の起動を行わない、デフォルトはFalse

"""

def __init__(self, instance_id, dry_run=False):
"""初期化する。

Args:
instance_id (str): 対象のEC2インスタンスID。
dry_run (bool, optional): Trueの場合は実際の起動を行わない、デフォルトはFalse。

"""
self._state = State.UNDEFINED
self._instance_id = instance_id
self._dry_run = dry_run
Expand All @@ -40,7 +32,7 @@ def __init__(self, instance_id, dry_run=False):

@property
def state(self):
"""インスタンスの現在の状態。
"""State: インスタンスの現在の状態。

インスタンスの現在の状態を表す。
本クラスの各種メソッドを呼出すタイミングによって自動的に変化する。
Expand All @@ -50,12 +42,12 @@ def state(self):

@property
def instance_id(self):
"""対象のEC2インスタンスID。"""
"""str: 対象のEC2インスタンスID。"""
return self._instance_id

@property
def dry_run(self):
"""ドライランモードの場合は真を表す真偽値。"""
"""bool: ドライランモードの場合は真を表す真偽値。"""
return self._dry_run

def _describe_instance(self):
Expand Down Expand Up @@ -112,7 +104,7 @@ def start(self):
"""EC2インスタンスを起動する。

Returns:
dict: boto3のstart_instancesレスポンス。ドライランの場合はNone。
dict | None: boto3のstart_instancesレスポンス。ドライランの場合はNone。

Raises:
ClientError: インスタンスの起動に失敗した場合。
Expand Down Expand Up @@ -144,24 +136,21 @@ def _start_instance(self, instance_id, dry_run):


class State(Enum):
"""EC2インスタンスの状態を表す列挙子。

Attributes:
UNDEFINED: 初期状態。
DRY_RUN: ドライランモードで実行した状態。
INSTANCE_ID_IS_NOT_STRING: インスタンスIDが文字列でない状態。
INSTANCE_ID_IS_NOT_FOUND: インスタンスIDが見つからない状態。
INSTANCE_IS_NOT_RUNNING: インスタンスが起動中でない状態。
INSTANCE_IS_RUNNING: インスタンスが起動中の状態。
INSUFFICIENT_CAPACITY: キャパシティ不足の状態。
INSTANCE_STARTING_IS_FAILED: インスタンスの起動に失敗した状態。
"""EC2インスタンスの状態を表す列挙子。"""

"""
UNDEFINED = auto()
"""int: 初期状態。"""
DRY_RUN = auto()
"""int: ドライランモードで実行した状態。"""
INSTANCE_ID_IS_NOT_STRING = auto()
"""int: インスタンスIDが文字列でない状態。"""
INSTANCE_ID_IS_NOT_FOUND = auto()
"""int: インスタンスIDが見つからない状態。"""
INSTANCE_IS_NOT_RUNNING = auto()
"""int: インスタンスが起動中でない状態。"""
INSTANCE_IS_RUNNING = auto()
"""int: インスタンスが起動中の状態。"""
INSUFFICIENT_CAPACITY = auto()
"""int: キャパシティ不足の状態。"""
INSTANCE_STARTING_IS_FAILED = auto()
"""int: インスタンスの起動に失敗した状態。"""
11 changes: 4 additions & 7 deletions src/aws_resources/lambda_function/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ class LambdaFunction:

API Gatewayから受取ったイベントを解析し、実行結果をレスポンスとして返す。

Args:
event (dict): API Gatewayから受取ったイベント。
context (dict): Lambda実行コンテキスト。

"""

def __init__(self, event, context):
"""初期化する。

Args:
event (dict): API Gatewayから受取ったイベント。
context (dict): Lambda実行コンテキスト。

"""
self._event = event
self._context = context

Expand Down
33 changes: 11 additions & 22 deletions src/aws_resources/lambda_function/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,20 @@ class Request:

API Gatewayから受取ったイベントを解析し、インスタンスIDやドライランフラグを取出す。

Attributes:
state (State): リクエストの現在の状態。
instance_id (str): EC2インスタンスID。
dry_run (bool): ドライランモードの場合は真を表す真偽値。
Args:
event (dict): API Gatewayから受取ったイベント。

"""

def __init__(self, event):
"""Requestを初期化する。

Args:
event (dict): API Gatewayから受取ったイベント。

"""
self._state = State.UNDEFINED
self._instance_id = None
self._dry_run = False
self.extract(event)

@property
def state(self):
"""リクエストの現在の状態。
"""State: リクエストの現在の状態。

リクエストの現在の状態を表す。
本クラスの各種メソッドを呼出すタイミングによって自動的に変化する。
Expand All @@ -43,12 +35,12 @@ def state(self):

@property
def instance_id(self):
"""EC2インスタンスID。"""
"""str: EC2インスタンスID。"""
return self._instance_id

@property
def dry_run(self):
"""ドライランモードの場合は真を表す真偽値。"""
"""bool: ドライランモードの場合は真を表す真偽値。"""
return self._dry_run

def extract(self, event):
Expand Down Expand Up @@ -89,18 +81,15 @@ def _extract_body(self, event):


class State(Enum):
"""リクエストの状態を表す列挙子。

Attributes:
UNDEFINED: 初期状態。
BODY_IS_EMPTY: ボディが空の状態。
BODY_IS_NOT_JSON: ボディがJSON形式でない状態。
BODY_HAS_NOT_INSTANCE_ID: ボディにinstance_idが含まれない状態。
BODY_IS_VALID: ボディが正常に解析できた状態。
"""リクエストの状態を表す列挙子。"""

"""
UNDEFINED = auto()
"""int: 初期状態。"""
BODY_IS_EMPTY = auto()
"""int: ボディが空の状態。"""
BODY_IS_NOT_JSON = auto()
"""int: ボディがJSON形式でない状態。"""
BODY_HAS_NOT_INSTANCE_ID = auto()
"""int: ボディにinstance_idが含まれない状態。"""
BODY_IS_VALID = auto()
"""int: ボディが正常に解析できた状態。"""
35 changes: 18 additions & 17 deletions src/aws_resources/lambda_function/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,41 @@ class Response:

設定された状態に基づき、HTTPステータスコードとボディを含むレスポンスを生成する。

Attributes:
status_code (int): HTTPステータスコード。
body (str): ドライランモードの場合は真を表す真偽値。
state (State): レスポンスの現在の状態。

"""

def __init__(self):
"""初期化する。

stateの初期値はNoneState。

"""
self.state = NoneState()

@property
def status_code(self):
"""HTTPステータスコード。"""
"""int: HTTPステータスコード。"""
return self.state.status_code

@property
def body(self):
"""JSONシリアライズしたレスポンスボディ。"""
"""str: JSONシリアライズしたレスポンスボディ。"""
return json.dumps({
"message": self.state.body_message,
})

@property
def state(self):
"""レスポンスの現在の状態。"""
"""State: レスポンスの現在の状態。初期値はNoneState。"""
return self._state

@state.setter
def state(self, state):
"""レスポンスの状態を設定する。"""
self._state = state

def create_response(self):
"""Lambdaレスポンスを生成する。

Returns:
dict: HTTPステータスコードとボディを含むレスポンス。
{
"statusCode": int,
"body": str
}

"""
return {
Expand All @@ -60,10 +53,10 @@ class State:
"""レスポンスの状態を表す基底クラス。

具象クラスは status_code と body_message を必ずオーバーライドする必要がある。
オブジェクト生成時に上記がオーバーライドされているか確認する。

Attributes:
status_code: HTTPステータスコード。
body_message: レスポンスボディに設定する文字列。
Raises:
NotImplementedError: 実装必須プロパティが実装されていない場合。

"""

Expand All @@ -72,13 +65,21 @@ def __init__(self):

@property
def status_code(self):
"""int: HTTPステータスコード。"""
raise NotImplementedError

@property
def body_message(self):
"""str: レスポンスボディに設定する文字列。"""
raise NotImplementedError

def check(self):
"""実装必須メソッドが正常に実装されているかチェックする。

Raises:
NotImplementedError: 実装必須プロパティが実装されていない場合。

"""
self._check_must_overriding_methods()

def _check_must_overriding_methods(self):
Expand Down
Loading