Skip to content

Instantly share code, notes, and snippets.

@marcosborges
Created September 12, 2022 13:36
Show Gist options
  • Save marcosborges/308b42d388d5d277fc4ece89e024074a to your computer and use it in GitHub Desktop.
Save marcosborges/308b42d388d5d277fc4ece89e024074a to your computer and use it in GitHub Desktop.
FROM python:3.8
RUN pip install fire
import fire
import json
template = {
"x-amazon-apigateway-integration" : {
"type" : "http",
"connectionId" : "$${stageVariables.vpc_link}",
"httpMethod" : "ANY",
"uri" : "https://$${stageVariables.target_hostname}",
"responses" : {
"default" : {
"statusCode" : "200"
}
},
"passthroughBehavior" : "when_no_match",
"connectionType" : "VPC_LINK"
}
}
class ApiIntegrationTools:
def generate(self, openapi_file="openapi.json"):
with open(openapi_file) as file:
data = json.load(file)
for path in data.get('paths'):
for method in data.get('paths').get(path):
if("x-amazon-apigateway-integration" not in data.get('paths').get(path).get(method)):
template['httpMethod'] = method
data['paths'][path][method] = {
**data['paths'][path][method],
**template
}
print(json.dumps(data, indent=4))
def list_all_without_integration(self, openapi_file = "openapi.json"):
with open(openapi_file) as file:
data = json.load(file)
for path in data.get('paths'):
for method in data.get('paths').get(path):
if("x-amazon-apigateway-integration" not in data.get('paths').get(path).get(method)):
print(f'[{method}] {path}')
def list_all_with_integration(self, openapi_file = "openapi.json"):
with open(openapi_file) as file:
data = json.load(file)
for path in data.get('paths'):
for method in data.get('paths').get(path):
if("x-amazon-apigateway-integration" in data.get('paths').get(path).get(method)):
print(f'[{method}] {path}')
def list_all(self, openapi_file = "openapi.json"):
with open(openapi_file) as file:
data = json.load(file)
for path in data.get('paths'):
for method in data.get('paths').get(path):
print(f'[{method}] {path}')
def describe(self, openapi_file = "openapi.json"):
with open(openapi_file) as file:
data = json.load(file)
for path in data.get('paths'):
for method in data.get('paths').get(path):
if("x-amazon-apigateway-integration" in data.get('paths').get(path).get(method)):
conn_id = {data['paths'][path][method]['x-amazon-apigateway-integration']['connectionId']}
uri = {data['paths'][path][method]['x-amazon-apigateway-integration']['uri']}
print(f"[{method}] {path} -> ({conn_id}) {uri}")
else:
print(f'[{method}] {path} -> Sem integração')
if __name__ == '__main__':
fire.Fire(ApiIntegrationTools)
build-container:
docker build -t python:tool .
args = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}`
run-script:
@docker run -v $(shell pwd):$(shell pwd) -w $(shell pwd) python:tool python ./script.py $(call args)
%:
@echo ""
@:
@echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment