Created
November 29, 2019 13:46
-
-
Save nabam/3a75762c7301b8876f0c78d5593ebc69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import yaml | |
import sys | |
import argparse | |
import re | |
swagger = yaml.load(sys.stdin.read(), Loader=yaml.SafeLoader) | |
http_api_spec = { | |
'apiVersion': 'config.istio.io/v1alpha2', | |
'kind': 'HTTPAPISpec', | |
'metadata': { | |
'name': '{{ .Chart.Name }}', | |
'labels': {}}, | |
'spec': { | |
'attributes': {'attributes': { | |
'api.service': { | |
'stringValue': '{{ .Chart.Name }}'}, | |
'api.version': { | |
'stringValue': swagger["info"]["version"]}}}, | |
'patterns': [{ | |
'attributes': {'attributes': { | |
'api.operation': {'stringValue': f"{m.upper()} {s}"}}}, | |
'httpMethod': m.upper(), | |
'uriTemplate': s, | |
} | |
for (s, k) in [(re.sub('/:([a-zA-Z0-9_]+)', '/{\\1}', k), k) | |
for k in swagger["paths"].keys()] | |
for m in swagger["paths"][k].keys()]}} | |
http_api_spec_binding = { | |
'apiVersion': 'config.istio.io/v1alpha2', | |
'kind': 'HTTPAPISpecBinding', | |
'metadata': { | |
'name': '{{ .Chart.Name }}', | |
'labels': {}}, | |
'spec': {}} | |
parser = argparse.ArgumentParser(description='Generate http api spec') | |
parser.add_argument('-a --all', action='store_true', | |
help='generatre all', dest="gen_all") | |
parser.add_argument('-b --binding', action='store_true', | |
help='generatre binding', dest="binding") | |
parser.add_argument('-n --name', help='service name', dest="name") | |
parser.add_argument('-v --version', help='service version', dest="version") | |
args = parser.parse_args() | |
if args.name: | |
http_api_spec['metadata']['name'] = args.name | |
http_api_spec['spec']['attributes']['attributes']['api.service']['stringValue'] = args.name | |
http_api_spec_binding['metadata']['name'] = args.name | |
http_api_spec_binding['spec'] = { | |
'services': [{ | |
'name': args.name, | |
'namespace': 'default' | |
}], | |
'api_specs': [{ | |
'name': args.name, | |
'namespace': 'default' | |
}]} | |
else: | |
http_api_spec['metadata']['labels']['app'] = "{{ .Chart.Name }}" | |
http_api_spec['metadata']['labels']['version'] = \ | |
"{{ .Values.deploymentVersion }}" | |
http_api_spec_binding['metadata']['labels']['app'] = "{{ .Chart.Name }}" | |
http_api_spec_binding['metadata']['labels']['version'] = \ | |
"{{ .Values.deploymentVersion }}" | |
http_api_spec_binding['spec'] = { | |
'services': [{ | |
'name': '{{ .Chart.Name }}', | |
'namespace': 'default' | |
}], | |
'api_specs': [{ | |
'name': '{{ .Chart.Name }}', | |
'namespace': 'default' | |
}]} | |
if args.version and args.name: | |
http_api_spec['metadata']['labels']['app'] = args.name | |
http_api_spec['metadata']['labels']['version'] = args.version | |
http_api_spec_binding['metadata']['labels']['app'] = args.name | |
http_api_spec_binding['metadata']['labels']['version'] = args.version | |
if not args.binding or args.gen_all: | |
print(yaml.dump(http_api_spec)) | |
if args.gen_all: | |
print("---") | |
if args.binding or args.gen_all: | |
print(yaml.dump(http_api_spec_binding)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment