def resource(api_id, path): resource_id = apigateway("get_resources", restApiId=api_id, query="items[?path==`/{}`].id | [0]".format(path)) if resource_id: return resource_id root_resource_id = apigateway("get_resources", restApiId=api_id, query="items[?path==`/`].id | [0]") resource_id = apigateway("create_resource", restApiId=api_id, parentId=root_resource_id, pathPart=path, query="id") return resource_id
def _clear_method(api_id, resource_id, http_method): try: method = apigateway("get_method", restApiId=api_id, resourceId=resource_id, httpMethod=http_method) except ClientError: method = None if method: apigateway("delete_method", restApiId=api_id, resourceId=resource_id, httpMethod=http_method)
def get_create_api(): api_id = apigateway('get_rest_apis', query='items[?name==`gimel`] | [0].id') if not api_id: api_id = apigateway('create_rest_api', name='gimel', description='Gimel API', query='id') logger.debug("api_id={}".format(api_id)) return api_id
def api_key(api_id): key = apigateway('get_api_keys', query='items[?name==`gimel`] | [0].id') if key: apigateway('update_api_key', apiKey=key, patchOperations=[{'op': 'add', 'path': '/stages', 'value': '{}/prod'.format(api_id)}]) else: key = apigateway('create_api_key', name='gimel', enabled=True, stageKeys=[{'restApiId': api_id, 'stageName': 'prod'}]) return key
def resource(api_id, path): resource_id = apigateway('get_resources', restApiId=api_id, query='items[?path==`/{}`].id | [0]'.format(path)) if resource_id: return resource_id root_resource_id = apigateway('get_resources', restApiId=api_id, query='items[?path==`/`].id | [0]') resource_id = apigateway('create_resource', restApiId=api_id, parentId=root_resource_id, pathPart=path, query='id') return resource_id
def _clear_method(api_id, resource_id, http_method): try: method = apigateway('get_method', restApiId=api_id, resourceId=resource_id, httpMethod=http_method) except ClientError: method = None if method: apigateway('delete_method', restApiId=api_id, resourceId=resource_id, httpMethod=http_method)
def api_key(api_id): key = get_api_key() if key: apigateway( "update_api_key", apiKey=key, patchOperations=[{"op": "add", "path": "/stages", "value": "{}/prod".format(api_id)}], ) else: key = apigateway( "create_api_key", name="gimel", enabled=True, stageKeys=[{"restApiId": api_id, "stageName": "prod"}] ) return key
def api_method(api_id, resource_id, role_arn, function_uri, wiring): http_method = wiring['method']['httpMethod'] _clear_method(api_id, resource_id, http_method) apigateway('put_method', restApiId=api_id, resourceId=resource_id, authorizationType='NONE', **wiring['method']) apigateway('put_integration', restApiId=api_id, resourceId=resource_id, httpMethod=http_method, type='AWS', integrationHttpMethod='POST', credentials=role_arn, uri=function_uri, requestTemplates=REQUEST_TEMPLATE) apigateway('put_method_response', restApiId=api_id, resourceId=resource_id, httpMethod=http_method, statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": False, "method.response.header.Pragma": False, "method.response.header.Cache-Control": False}, responseModels={'application/json': 'Empty'}) apigateway('put_integration_response', restApiId=api_id, resourceId=resource_id, httpMethod=http_method, statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": "'*'", "method.response.header.Pragma": "'no-cache'", "method.response.header.Cache-Control": "'no-cache, no-store, must-revalidate'"}, responseTemplates={'application/json': ''})
def deploy_api(api_id): logger.info('deploying API') return apigateway('create_deployment', restApiId=api_id, description='gimel deployment', stageName='prod', stageDescription='gimel production', cacheClusterEnabled=False, query='id')
def api_key(api_id): key = get_api_key() if key: apigateway('update_api_key', apiKey=key, patchOperations=[{ 'op': 'add', 'path': '/stages', 'value': '{}/prod'.format(api_id) }]) else: key = apigateway('create_api_key', name='gimel', enabled=True, stageKeys=[{ 'restApiId': api_id, 'stageName': 'prod' }]) return key
def deploy_api(api_id): logger.info("deploying API") return apigateway( "create_deployment", restApiId=api_id, description="gimel deployment", stageName="prod", stageDescription="gimel production", cacheClusterEnabled=False, query="id", )
def cors(api_id, resource_id): _clear_method(api_id, resource_id, "OPTIONS") apigateway( "put_method", restApiId=api_id, resourceId=resource_id, httpMethod="OPTIONS", authorizationType="NONE", apiKeyRequired=False, ) apigateway( "put_integration", restApiId=api_id, resourceId=resource_id, httpMethod="OPTIONS", type="MOCK", integrationHttpMethod="POST", requestTemplates={"application/json": '{"statusCode": 200}'}, ) apigateway( "put_method_response", restApiId=api_id, resourceId=resource_id, httpMethod="OPTIONS", statusCode="200", responseParameters={ "method.response.header.Access-Control-Allow-Origin": False, "method.response.header.Access-Control-Allow-Methods": False, "method.response.header.Access-Control-Allow-Headers": False, }, responseModels={"application/json": "Empty"}, ) apigateway( "put_integration_response", restApiId=api_id, resourceId=resource_id, httpMethod="OPTIONS", statusCode="200", responseParameters={ "method.response.header.Access-Control-Allow-Origin": "'*'", "method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'", "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'", }, # noqa responseTemplates={"application/json": ""}, )
def api_method(api_id, resource_id, role_arn, function_uri, wiring): http_method = wiring['method']['httpMethod'] _clear_method(api_id, resource_id, http_method) apigateway('put_method', restApiId=api_id, resourceId=resource_id, authorizationType='NONE', **wiring['method']) apigateway('put_integration', restApiId=api_id, resourceId=resource_id, httpMethod=http_method, type='AWS', integrationHttpMethod='POST', credentials=role_arn, uri=function_uri, requestTemplates=REQUEST_TEMPLATE) apigateway('put_method_response', restApiId=api_id, resourceId=resource_id, httpMethod=http_method, statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": False, "method.response.header.Pragma": False, "method.response.header.Cache-Control": False }, responseModels={'application/json': 'Empty'}) apigateway('put_integration_response', restApiId=api_id, resourceId=resource_id, httpMethod=http_method, statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": "'*'", "method.response.header.Pragma": "'no-cache'", "method.response.header.Cache-Control": "'no-cache, no-store, must-revalidate'" }, responseTemplates={'application/json': ''})
def cors(api_id, resource_id): _clear_method(api_id, resource_id, 'OPTIONS') apigateway('put_method', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', authorizationType='NONE', apiKeyRequired=False) apigateway('put_integration', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', type='MOCK', integrationHttpMethod='POST', requestTemplates={'application/json': '{"statusCode": 200}'}) apigateway('put_method_response', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": False, "method.response.header.Access-Control-Allow-Methods": False, "method.response.header.Access-Control-Allow-Headers": False }, responseModels={'application/json': 'Empty'}) apigateway( 'put_integration_response', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": "'*'", "method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'", "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" }, # noqa responseTemplates={'application/json': ''})
def api_method(api_id, resource_id, role_arn, function_uri, wiring): http_method = wiring["method"]["httpMethod"] _clear_method(api_id, resource_id, http_method) apigateway("put_method", restApiId=api_id, resourceId=resource_id, authorizationType="NONE", **wiring["method"]) apigateway( "put_integration", restApiId=api_id, resourceId=resource_id, httpMethod=http_method, type="AWS", integrationHttpMethod="POST", credentials=role_arn, uri=function_uri, requestTemplates=REQUEST_TEMPLATE, ) apigateway( "put_method_response", restApiId=api_id, resourceId=resource_id, httpMethod=http_method, statusCode="200", responseParameters={ "method.response.header.Access-Control-Allow-Origin": False, "method.response.header.Pragma": False, "method.response.header.Cache-Control": False, }, responseModels={"application/json": "Empty"}, ) apigateway( "put_integration_response", restApiId=api_id, resourceId=resource_id, httpMethod=http_method, statusCode="200", responseParameters={ "method.response.header.Access-Control-Allow-Origin": "'*'", "method.response.header.Pragma": "'no-cache'", "method.response.header.Cache-Control": "'no-cache, no-store, must-revalidate'", }, responseTemplates={"application/json": ""}, )
def cors(api_id, resource_id): _clear_method(api_id, resource_id, 'OPTIONS') apigateway('put_method', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', authorizationType='NONE', apiKeyRequired=False) apigateway('put_integration', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', type='MOCK', integrationHttpMethod='POST', requestTemplates={'application/json': '{"statusCode": 200}'}) apigateway('put_method_response', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": False, "method.response.header.Access-Control-Allow-Methods": False, "method.response.header.Access-Control-Allow-Headers": False}, responseModels={'application/json': 'Empty'}) apigateway('put_integration_response', restApiId=api_id, resourceId=resource_id, httpMethod='OPTIONS', statusCode='200', responseParameters={ "method.response.header.Access-Control-Allow-Origin": "'*'", "method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'", "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"}, # noqa responseTemplates={'application/json': ''})
def get_create_api(): api_id = apigateway("get_rest_apis", query="items[?name==`gimel`] | [0].id") if not api_id: api_id = apigateway("create_rest_api", name="gimel", description="Gimel API", query="id") logger.debug("api_id={}".format(api_id)) return api_id
def get_api_key(): return apigateway('get_api_keys', query='items[?name==`gimel`] | [0].id')
def get_api_key(): return apigateway("get_api_keys", query="items[?name==`gimel`] | [0].id")