APIGateway: Added API for usage plans

This commit is contained in:
Aidan Fewster 2018-07-10 14:58:02 +01:00
commit 43e430560c
No known key found for this signature in database
GPG key ID: 73BB656D17B1F302
5 changed files with 134 additions and 1 deletions

View file

@ -309,6 +309,19 @@ class ApiKey(BaseModel, dict):
self['stageKeys'] = stageKeys
class UsagePlan(BaseModel, dict):
def __init__(self, name=None, description=None, apiStages=[],
throttle=None, quota=None):
super(UsagePlan, self).__init__()
self['id'] = create_id()
self['name'] = name
self['description'] = description
self['apiStages'] = apiStages
self['throttle'] = throttle
self['quota'] = quota
class RestAPI(BaseModel):
def __init__(self, id, region_name, name, description):
@ -408,6 +421,7 @@ class APIGatewayBackend(BaseBackend):
super(APIGatewayBackend, self).__init__()
self.apis = {}
self.keys = {}
self.usage_plans = {}
self.region_name = region_name
def reset(self):
@ -576,6 +590,21 @@ class APIGatewayBackend(BaseBackend):
self.keys.pop(api_key_id)
return {}
def create_usage_plan(self, payload):
plan = UsagePlan(**payload)
self.usage_plans[plan['id']] = plan
return plan
def get_usage_plans(self):
return list(self.usage_plans.values())
def get_usage_plan(self, usage_plan_id):
return self.usage_plans[usage_plan_id]
def delete_usage_plan(self, usage_plan_id):
self.usage_plans.pop(usage_plan_id)
return {}
apigateway_backends = {}
for region_name in Session().get_available_regions('apigateway'):

View file

@ -248,3 +248,25 @@ class APIGatewayResponse(BaseResponse):
elif self.method == 'DELETE':
apikey_response = self.backend.delete_apikey(apikey)
return 200, {}, json.dumps(apikey_response)
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':
usage_plans_response = self.backend.get_usage_plans()
return 200, {}, json.dumps({"item": usage_plans_response})
return 200, {}, json.dumps(usage_plan_response)
def usage_plan_individual(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
url_path_parts = self.path.split("/")
usage_plan = url_path_parts[2]
if self.method == 'GET':
usage_plan_response = self.backend.get_usage_plan(usage_plan)
elif self.method == 'DELETE':
usage_plan_response = self.backend.delete_usage_plan(usage_plan)
return 200, {}, json.dumps(usage_plan_response)

View file

@ -20,4 +20,6 @@ url_paths = {
'{0}/restapis/(?P<function_id>[^/]+)/resources/(?P<resource_id>[^/]+)/methods/(?P<method_name>[^/]+)/integration/responses/(?P<status_code>\d+)/?$': APIGatewayResponse().integration_responses,
'{0}/apikeys$': APIGatewayResponse().apikeys,
'{0}/apikeys/(?P<apikey>[^/]+)': APIGatewayResponse().apikey_individual,
'{0}/usageplans$': APIGatewayResponse().usage_plans,
'{0}/usageplans/(?P<usage_plan_id>[^/]+)': APIGatewayResponse().usage_plan_individual,
}