Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added feature update_usage_plan and fixed some lint errors #3727

Merged
merged 1 commit into from
Feb 25, 2021
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
19 changes: 19 additions & 0 deletions moto/apigateway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,25 @@ def get_usage_plan(self, usage_plan_id):

return self.usage_plans[usage_plan_id]

def __apply_usage_plan_patch_operations(self, plan, patch_operations):
for op in patch_operations:
if op["op"] == "replace":
if "/quota/limit" in op["path"]:
plan["quota"]["limit"] = op["value"]
if "/quota/period" in op["path"]:
plan["quota"]["period"] = op["value"]
if "/throttle/rateLimit" in op["path"]:
plan["throttle"]["rateLimit"] = op["value"]
if "/throttle/burstLimit" in op["path"]:
plan["throttle"]["burstLimit"] = op["value"]

def update_usage_plan(self, usage_plan_id, patch_operations):
if usage_plan_id not in self.usage_plans:
raise UsagePlanNotFoundException()
plan = self.usage_plans[usage_plan_id]
self.__apply_usage_plan_patch_operations(plan, patch_operations)
return self.usage_plans[usage_plan_id]

def delete_usage_plan(self, usage_plan_id):
self.usage_plans.pop(usage_plan_id)
return {}
Expand Down
6 changes: 5 additions & 1 deletion moto/apigateway/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ def apikey_individual(self, request, full_url, headers):

def usage_plans(self, request, full_url, headers):
self.setup_class(request, full_url, headers)

if self.method == "POST":
usage_plan_response = self.backend.create_usage_plan(json.loads(self.body))
elif self.method == "GET":
Expand Down Expand Up @@ -505,6 +504,11 @@ def usage_plan_individual(self, request, full_url, headers):
)
elif self.method == "DELETE":
usage_plan_response = self.backend.delete_usage_plan(usage_plan)
elif self.method == "PATCH":
patch_operations = self._get_param("patchOperations")
usage_plan_response = self.backend.update_usage_plan(
usage_plan, patch_operations
)
return 200, {}, json.dumps(usage_plan_response)

def usage_plan_keys(self, request, full_url, headers):
Expand Down
2 changes: 1 addition & 1 deletion moto/batch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def create_compute_environment(

if compute_resources is None and _type == "MANAGED":
raise InvalidParameterValueException(
"computeResources must be specified when creating a MANAGED environment".format(
"computeResources must be specified when creating a {0} environment".format(
state
)
)
Expand Down
6 changes: 3 additions & 3 deletions moto/elb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ def set_load_balancer_policies_of_listener(
):
load_balancer = self.get_load_balancer(load_balancer_name)
listener = [
l
for l in load_balancer.listeners
if int(l.load_balancer_port) == load_balancer_port
l_listener
for l_listener in load_balancer.listeners
if int(l_listener.load_balancer_port) == load_balancer_port
][0]
listener_idx = load_balancer.listeners.index(listener)
listener.policy_names = policies
Expand Down
6 changes: 3 additions & 3 deletions moto/elb/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ def set_load_balancer_policies_of_listener(self):
load_balancer_port = int(self._get_param("LoadBalancerPort"))

mb_listener = [
l
for l in load_balancer.listeners
if int(l.load_balancer_port) == load_balancer_port
listner
for listner in load_balancer.listeners
if int(listner.load_balancer_port) == load_balancer_port
]
if mb_listener:
policies = self._get_multi_param("PolicyNames.member")
Expand Down
4 changes: 2 additions & 2 deletions moto/opsworks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,11 @@ def create_layer(self, **kwargs):
stackid = kwargs["stack_id"]
if stackid not in self.stacks:
raise ResourceNotFoundException(stackid)
if name in [l.name for l in self.stacks[stackid].layers]:
if name in [layer.name for layer in self.stacks[stackid].layers]:
raise ValidationException(
'There is already a layer named "{0}" ' "for this stack".format(name)
)
if shortname in [l.shortname for l in self.stacks[stackid].layers]:
if shortname in [layer.shortname for layer in self.stacks[stackid].layers]:
raise ValidationException(
'There is already a layer with shortname "{0}" '
"for this stack".format(shortname)
Expand Down
2 changes: 1 addition & 1 deletion moto/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def infer_service_region_host(self, environ):
if dynamo_api_version > "20111205":
host = "dynamodb2"
elif service == "sagemaker":
host = "api.sagemaker.{region}.amazonaws.com".format(
host = "api.{service}.{region}.amazonaws.com".format(
service=service, region=region
)
else:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_apigateway/test_apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,36 @@ def test_usage_plans():
len(response["items"]).should.equal(1)


@mock_apigateway
def test_update_usage_plan():
region_name = "us-west-2"
client = boto3.client("apigateway", region_name=region_name)

payload = {
"name": "TEST-PLAN-2",
"description": "Description",
"quota": {"limit": 10, "period": "DAY", "offset": 0},
"throttle": {"rateLimit": 2, "burstLimit": 1},
"apiStages": [{"apiId": "foo", "stage": "bar"}],
"tags": {"tag_key": "tag_value"},
}
response = client.create_usage_plan(**payload)
usage_plan_id = response["id"]
response = client.update_usage_plan(
usagePlanId=usage_plan_id,
patchOperations=[
{"op": "replace", "path": "/quota/limit", "value": "1000"},
{"op": "replace", "path": "/quota/period", "value": "MONTH"},
{"op": "replace", "path": "/throttle/rateLimit", "value": "500"},
{"op": "replace", "path": "/throttle/burstLimit", "value": "1500"},
],
)
response["quota"]["limit"].should.equal("1000")
response["quota"]["period"].should.equal("MONTH")
response["quota"]["limit"].should.equal("1000")
response["quota"]["limit"].should.equal("1000")


@mock_apigateway
def test_usage_plan_keys():
region_name = "us-west-2"
Expand Down