Skip to content

Commit 117cceb

Browse files
committed
Add generation preprocess step to make OperationIDs simpler
1 parent a37bc3a commit 117cceb

File tree

6 files changed

+943
-873
lines changed

6 files changed

+943
-873
lines changed

GEN_README.md

Lines changed: 0 additions & 846 deletions
This file was deleted.

README.md

Lines changed: 846 additions & 1 deletion
Large diffs are not rendered by default.

README.prefix

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Kubernetes Python Client
2+
3+
Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster.
4+
5+
## Example
6+
7+
```python
8+
from __future__ import absolute_import
9+
10+
import k8sutil
11+
import k8sclient
12+
import os
13+
14+
# Configs can be set in Configuration class directly or using helper utility
15+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
16+
17+
# Prior to python 3.4 hosts with ip-addresses cannot be verified for SSL. this
18+
# utility function fixes that.
19+
k8sutil.fix_ssl_hosts_with_ipaddress()
20+
21+
v1=k8sclient.CoreV1Api()
22+
print "Listing pods with their IPs:"
23+
ret = v1.list_pod_for_all_namespaces()
24+
for i in ret.items:
25+
print "%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)
26+
```
27+
28+
More examples can be found in [examples](examples/) folder. To run examples, run this command:
29+
30+
```shell
31+
python -m examples.example1
32+
```
33+
34+
(replace example1 with the example base filename)
35+
36+
# Generated client README
37+
38+

scripts/ROOT_README.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

scripts/generate.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,22 @@ if [[ ! -n ${SWAGGER_FILE-} ]]; then
4747
SWAGGER_FILE="${KUBE_ROOT}/api/openapi-spec/swagger.json"
4848
fi
4949

50+
echo "--- Preprocessing OpenAPI spec to script directory"
51+
python "${SCRIPT_ROOT}/preprocess_spec.py" "$SWAGGER_FILE" "${SCRIPT_ROOT}/swagger.json"
52+
5053
echo "--- Cleaning up previously generated folders"
5154
rm -rf "${CLIENT_ROOT}/${PACKAGE_NAME}"
5255
rm -rf "${CLIENT_ROOT}/docs"
5356
rm -rf "${CLIENT_ROOT}/test"
5457

5558
echo "--- Generating client ..."
56-
mvn -f "${SCRIPT_ROOT}/pom.xml" clean generate-sources -Dgenerator.spec.path="${SWAGGER_FILE}" -Dgenerator.output.path="${CLIENT_ROOT}" -Dgenerator.package.name=${PACKAGE_NAME}
59+
mvn -f "${SCRIPT_ROOT}/pom.xml" clean generate-sources -Dgenerator.spec.path="${SCRIPT_ROOT}/swagger.json" -Dgenerator.output.path="${CLIENT_ROOT}" -Dgenerator.package.name=${PACKAGE_NAME}
5760

5861
echo "--- Patching generated code..."
59-
cp "${CLIENT_ROOT}/README.md" "${CLIENT_ROOT}/GEN_README.md"
60-
cp "${SCRIPT_ROOT}/ROOT_README.md" "${CLIENT_ROOT}/README.md"
62+
cat "${CLIENT_ROOT}/README.prefix" "${CLIENT_ROOT}/README.md" > "${CLIENT_ROOT}/README.new"
63+
rm "${CLIENT_ROOT}/README.md"
64+
mv "${CLIENT_ROOT}/README.new" "${CLIENT_ROOT}/README.md"
6165
cp "${SCRIPT_ROOT}/LICENSE" "${CLIENT_ROOT}"
6266
rm -rf "${CLIENT_ROOT}/test"
67+
6368
echo "---Done."

scripts/preprocess_spec.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import json
2+
import os.path
3+
import sys
4+
from collections import OrderedDict
5+
6+
_ops = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch']
7+
8+
9+
def _title(s):
10+
if len(s) == 0:
11+
return s
12+
return s[0].upper() + s[1:]
13+
14+
15+
def _to_camel_case(s):
16+
return ''.join(_title(y) for y in s.split("_"))
17+
18+
19+
def iterate_through_operations(spec, func):
20+
for k, v in spec['paths'].iteritems():
21+
for op in _ops:
22+
if op in v:
23+
func(v[op])
24+
25+
26+
def process_swagger(infile, outfile):
27+
with open(infile, 'r') as f:
28+
spec = json.load(f, object_pairs_hook=OrderedDict)
29+
30+
def strip_tags_from_operation_id(operation):
31+
operation_id = operation['operationId']
32+
for t in operation['tags']:
33+
operation_id = operation_id.replace(_to_camel_case(t), '')
34+
operation['operationId'] = operation_id
35+
36+
iterate_through_operations(spec, strip_tags_from_operation_id)
37+
38+
with open(outfile, 'w') as out:
39+
json.dump(spec, out, sort_keys=False, indent=2,
40+
separators=(',', ': '), ensure_ascii=True)
41+
42+
43+
def main():
44+
if len(sys.argv) < 3:
45+
print "Usage:\n\tpython %s infile outfile.\n" % sys.argv[0]
46+
sys.exit(0)
47+
if not os.path.isfile(sys.argv[1]):
48+
print "Input file %s does not exist." % sys.argv[1]
49+
process_swagger(sys.argv[1], sys.argv[2])
50+
51+
main()

0 commit comments

Comments
 (0)