Skip to content

Commit

Permalink
implement apigw and secretsmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
cloutierMat committed Oct 4, 2024
1 parent 44274da commit 65365fd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
50 changes: 37 additions & 13 deletions moto/apigateway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
ValidationException,
VpcLinkNotFound,
)
from .utils import create_id, to_path
from .utils import create_apigw_id, create_id, to_path

STAGE_URL = "https://{api_id}.execute-api.{region_name}.amazonaws.com/{stage_name}"
PATCH_OPERATIONS = ["add", "remove", "replace", "move", "copy", "test"]
Expand Down Expand Up @@ -789,6 +789,7 @@ def _apply_operation_to_variables(self, op: Dict[str, Any]) -> None:
class ApiKey(BaseModel):
def __init__(
self,
api_key_id: str,
name: Optional[str] = None,
description: Optional[str] = None,
enabled: bool = False,
Expand All @@ -798,7 +799,7 @@ def __init__(
tags: Optional[List[Dict[str, str]]] = None,
customerId: Optional[str] = None,
):
self.id = create_id()
self.id = api_key_id
self.value = value or "".join(
random.sample(string.ascii_letters + string.digits, 40)
)
Expand Down Expand Up @@ -846,6 +847,7 @@ def _str2bool(self, v: str) -> bool:
class UsagePlan(BaseModel):
def __init__(
self,
usage_plan_id: str,
name: Optional[str] = None,
description: Optional[str] = None,
apiStages: Any = None,
Expand All @@ -854,7 +856,7 @@ def __init__(
productCode: Optional[str] = None,
tags: Optional[List[Dict[str, str]]] = None,
):
self.id = create_id()
self.id = usage_plan_id
self.name = name
self.description = description
self.api_stages = apiStages or []
Expand Down Expand Up @@ -985,12 +987,13 @@ def to_json(self) -> Dict[str, Any]:
class VpcLink(BaseModel):
def __init__(
self,
vpc_link_id: str,
name: str,
description: str,
target_arns: List[str],
tags: List[Dict[str, str]],
):
self.id = create_id()
self.id = vpc_link_id
self.name = name
self.description = description
self.target_arns = target_arns
Expand Down Expand Up @@ -1162,7 +1165,9 @@ def create_from_cloudformation_json( # type: ignore[misc]
)

def add_child(self, path: str, parent_id: Optional[str] = None) -> Resource:
child_id = create_id()
child_id = create_apigw_id(
self.account_id, self.region_name, "resource", parent_id + "." + path
)
child = Resource(
resource_id=child_id,
account_id=self.account_id,
Expand All @@ -1181,7 +1186,7 @@ def add_model(
schema: str,
content_type: str,
) -> "Model":
model_id = create_id()
model_id = create_apigw_id(self.account_id, self.region_name, "model", name)
new_model = Model(
model_id=model_id,
name=name,
Expand Down Expand Up @@ -1293,7 +1298,9 @@ def create_deployment(
) -> Deployment:
if stage_variables is None:
stage_variables = {}
deployment_id = create_id()
deployment_id = create_apigw_id(
self.account_id, self.region_name, "deployment", name
)
deployment = Deployment(deployment_id, name, description)
self.deployments[deployment_id] = deployment
if name:
Expand Down Expand Up @@ -1332,7 +1339,9 @@ def create_request_validator(
validateRequestBody: Optional[bool],
validateRequestParameters: Any,
) -> RequestValidator:
validator_id = create_id()
validator_id = create_apigw_id(
self.account_id, self.region_name, "request_validator", name
)
request_validator = RequestValidator(
_id=validator_id,
name=name,
Expand Down Expand Up @@ -1631,7 +1640,7 @@ def create_rest_api(
minimum_compression_size: Optional[int] = None,
disable_execute_api_endpoint: Optional[bool] = None,
) -> RestAPI:
api_id = create_id()
api_id = create_apigw_id(self.account_id, self.region_name, "rest_api", name)
rest_api = RestAPI(
api_id,
self.account_id,
Expand Down Expand Up @@ -1882,7 +1891,9 @@ def create_authorizer(
self, restapi_id: str, name: str, authorizer_type: str, **kwargs: Any
) -> Authorizer:
api = self.get_rest_api(restapi_id)
authorizer_id = create_id()
authorizer_id = create_apigw_id(
self.account_id, self.region_name, "authorizer", name
)
return api.create_authorizer(
authorizer_id,
name,
Expand Down Expand Up @@ -2146,7 +2157,10 @@ def create_api_key(self, payload: Dict[str, Any]) -> ApiKey:
for api_key in self.get_api_keys():
if api_key.value == payload["value"]:
raise ApiKeyAlreadyExists()
key = ApiKey(**payload)
api_key_id = create_apigw_id(
self.account_id, self.region_name, "api_key", payload.get("name")
)
key = ApiKey(api_key_id=api_key_id, **payload)
self.keys[key.id] = key
return key

Expand All @@ -2166,7 +2180,10 @@ def delete_api_key(self, api_key_id: str) -> None:
self.keys.pop(api_key_id)

def create_usage_plan(self, payload: Any) -> UsagePlan:
plan = UsagePlan(**payload)
usage_plan_id = create_apigw_id(
self.account_id, self.region_name, "usage_plan", payload["name"]
)
plan = UsagePlan(usage_plan_id=usage_plan_id, **payload)
self.usage_plans[plan.id] = plan
return plan

Expand Down Expand Up @@ -2484,8 +2501,15 @@ def create_vpc_link(
target_arns: List[str],
tags: List[Dict[str, str]],
) -> VpcLink:
vpc_link_id = create_apigw_id(
self.account_id, self.region_name, "vpc_link", name
)
vpc_link = VpcLink(
name, description=description, target_arns=target_arns, tags=tags
vpc_link_id,
name,
description=description,
target_arns=target_arns,
tags=tags,
)
self.vpc_links[vpc_link.id] = vpc_link
return vpc_link
Expand Down
14 changes: 14 additions & 0 deletions moto/apigateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
import yaml

from moto.moto_api._internal import mock_random as random
from moto.utilities.id_generator import generate_str_id


def create_apigw_id(account_id, region, resource, name) -> str:
return generate_str_id(
account_id,
region,
"apigateway",
resource,
name,
length=10,
include_digits=True,
lower_case=True,
)


def create_id() -> str:
Expand Down
11 changes: 10 additions & 1 deletion moto/secretsmanager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import string

from moto.moto_api._internal import mock_random as random
from moto.utilities.id_generator import generate_str_id
from moto.utilities.utils import ARN_PARTITION_REGEX, get_partition


Expand Down Expand Up @@ -63,7 +64,15 @@ def random_password(


def secret_arn(account_id: str, region: str, secret_id: str) -> str:
id_string = "".join(random.choice(string.ascii_letters) for _ in range(6))
id_string = generate_str_id(
account_id,
region,
"secretsmanager",
"secret",
secret_id,
length=6,
include_digits=False,
)
return f"arn:{get_partition(region)}:secretsmanager:{region}:{account_id}:secret:{secret_id}-{id_string}"


Expand Down
10 changes: 0 additions & 10 deletions moto/utilities/id_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ def _wrapper(account_id, region, service, resource, name, **kwargs):
return _wrapper


@moto_id
def generate_uid(account_id, region, service, resource, name, length=32):
return mock_random.get_random_hex(length)


@moto_id
def generate_short_uid(account_id, region, service, resource, name):
return mock_random.get_random_hex(8)


@moto_id
def generate_str_id(
account_id,
Expand Down

0 comments on commit 65365fd

Please sign in to comment.