APIGateway: update_usage_plan() now supports some remove-operations (#7457)

This commit is contained in:
Bert Blommers 2024-03-11 19:29:20 +00:00 committed by GitHub
parent 599446fee2
commit 3d3f1c969e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 10 deletions

View File

@ -859,32 +859,37 @@ class UsagePlan(BaseModel):
self.tags = tags self.tags = tags
def to_json(self) -> Dict[str, Any]: def to_json(self) -> Dict[str, Any]:
return { resp = {
"id": self.id, "id": self.id,
"name": self.name, "name": self.name,
"description": self.description, "description": self.description,
"apiStages": self.api_stages, "apiStages": self.api_stages,
"throttle": self.throttle,
"quota": self.quota,
"productCode": self.product_code, "productCode": self.product_code,
"tags": self.tags, "tags": self.tags,
} }
if self.throttle:
resp["throttle"] = self.throttle
if self.quota:
resp["quota"] = self.quota
return resp
def apply_patch_operations(self, patch_operations: List[Dict[str, Any]]) -> None: def apply_patch_operations(self, patch_operations: List[Dict[str, Any]]) -> None:
for op in patch_operations: for op in patch_operations:
path = op["path"] path = op["path"]
value = op["value"]
if op["op"] == "add": if op["op"] == "add":
value = op["value"]
if path == "/apiStages": if path == "/apiStages":
self.api_stages.append( self.api_stages.append(
{"apiId": value.split(":")[0], "stage": value.split(":")[1]} {"apiId": value.split(":")[0], "stage": value.split(":")[1]}
) )
if op["op"] == "replace": if op["op"] == "replace":
value = op["value"]
if "/name" in path: if "/name" in path:
self.name = value self.name = value
if "/description" in path: if "/description" in path:
self.description = value self.description = value
if op["op"] in ["add", "replace"]: if op["op"] in ["add", "replace"]:
value = op["value"]
if "/productCode" in path: if "/productCode" in path:
self.product_code = value self.product_code = value
if "/quota/limit" in path: if "/quota/limit" in path:
@ -897,6 +902,18 @@ class UsagePlan(BaseModel):
self.throttle["rateLimit"] = int(value) self.throttle["rateLimit"] = int(value)
if "/throttle/burstLimit" in path: if "/throttle/burstLimit" in path:
self.throttle["burstLimit"] = int(value) self.throttle["burstLimit"] = int(value)
if op["op"] == "remove":
if path == "/apiStages":
value = op["value"]
self.api_stages.remove(
{"apiId": value.split(":")[0], "stage": value.split(":")[1]}
)
if path == "/productCode":
self.product_code = None
if path == "/quota":
self.quota.clear()
if path == "/throttle":
self.throttle.clear()
class RequestValidator(BaseModel): class RequestValidator(BaseModel):
@ -2115,7 +2132,7 @@ class APIGatewayBackend(BaseBackend):
The following PatchOperations are currently supported: The following PatchOperations are currently supported:
add : Everything except /apiStages/{apidId:stageName}/throttle/ and children add : Everything except /apiStages/{apidId:stageName}/throttle/ and children
replace: Everything except /apiStages/{apidId:stageName}/throttle/ and children replace: Everything except /apiStages/{apidId:stageName}/throttle/ and children
remove : Nothing yet remove : Everything except /apiStages/{apidId:stageName}/throttle/ and children
copy : Nothing yet copy : Nothing yet
""" """
if usage_plan_id not in self.usage_plans: if usage_plan_id not in self.usage_plans:

View File

@ -1698,11 +1698,7 @@ def test_update_usage_plan():
region_name = "us-west-2" region_name = "us-west-2"
client = boto3.client("apigateway", region_name=region_name) client = boto3.client("apigateway", region_name=region_name)
payload = { payload = {"name": "TEST-PLAN-2", "description": "Description"}
"name": "TEST-PLAN-2",
"description": "Description",
"tags": {"tag_key": "tag_value"},
}
response = client.create_usage_plan(**payload) response = client.create_usage_plan(**payload)
usage_plan_id = response["id"] usage_plan_id = response["id"]
@ -1741,6 +1737,21 @@ def test_update_usage_plan():
assert response["description"] == "new-description" assert response["description"] == "new-description"
assert response["productCode"] == "new-productionCode" assert response["productCode"] == "new-productionCode"
remove = client.update_usage_plan(
usagePlanId=usage_plan_id,
patchOperations=[
{"op": "remove", "path": "/apiStages", "value": "foo:bar"},
{"op": "remove", "path": "/productCode"},
{"op": "remove", "path": "/quota"},
{"op": "remove", "path": "/throttle"},
],
)
assert remove["apiStages"] == []
assert "productCode" not in remove
assert remove["description"] == "new-description"
assert "quota" not in remove
assert "throttle" not in remove
@mock_aws @mock_aws
def test_usage_plan_keys(): def test_usage_plan_keys():