This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate-clients.sh
executable file
·124 lines (102 loc) · 5.14 KB
/
generate-clients.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
CONTAINER=openapitools/openapi-generator-cli:v5.0.1
CONTAINER_TOOL=$([ -x /usr/bin/podman ] && echo podman || echo docker)
usage() {
echo -e "Usage:\n $0 [ --docker | --podman ] [ --inventory ] [ --vmaas ] [ --rbac ]" >&2
}
OPTS=$(getopt --longoptions=docker,podman,help,inventory,vmaas,rbac -n ${0##*/} -- dphvir "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
declare -A GENERATE
while : ; do
case "$1" in
-d|--docker) CONTAINER_TOOL=docker
;;
-p|--podman) CONTAINER_TOOL=podman
;;
-i|--inventory) GENERATE[inventory]=1
;;
-v|--vmaas) GENERATE[vmaas]=1
;;
-r|--rbac) GENERATE[rbac]=1
;;
--) break
shift
;;
-h|--help|*) usage
exit 1
;;
esac
shift
done
# if none of API was explictly set generate all of them
if [ -z "${GENERATE[*]}" ] ; then
GENERATE=([inventory]=1 [vmaas]=1 [rbac]=1)
fi
# For now, we skip the dateTime parsing because of incompatible formats ( inventory does not produce fully compliant
function filter_oneof() {
# filter out oneOf types which openapi generator can't handle
sed -i '
# inventory
s|"additionalProperties":{"oneOf":\[{"$ref":"#/components/schemas/SystemProfileNestedObject","x-scope":\[""\]},{"not":{"type":"object"}}\]}|"additionalProperties":{"not":{"type":"object"}}|g;
s|{"oneOf":\[{"type":"string"},{"type":"boolean"}\]}|{"type":"string"}|g;
s|Fqdn|fqdn|g;
# rbac
s|"CrossAccountRequestDetail":{"oneOf":\[{"\$ref":"#/components/schemas/CrossAccountRequestDetailByAccount"},{"\$ref":"#/components/schemas/CrossAccountRequestDetailByUseId"}\]}|"CrossAccountRequestDetail":{"\$ref":"#/components/schemas/CrossAccountRequestDetailByAccount"}|g;
s|"items":{"oneOf":\[{"\$ref":"#/components/schemas/CrossAccountRequestByAccount"},{"\$ref":"#/components/schemas/CrossAccountRequestByUserId"}\]}|"items":{"\$ref":"#/components/schemas/CrossAccountRequestByAccount"}|g;
# vmaas
s|"type": {"oneOf": \[{"type": "string", "example": "security"}, {"type": "array", "items": {"type": "string", "example": "security"}}\]},|"type": {"type": "array", "items": {"type": "string", "example": "security"}},|;
s|"severity": {"oneOf": \[{"type": "string", "enum": \["Low", "Moderate", "Important", "Critical", null\], "nullable": true}, {"type": "array", "items": {"type": "string", "enum": \["Low", "Moderate", "Important", "Critical", null\], "nullable": true}}\]}},| "severity": {"type": "array", "items": {"type": "string", "enum": \["Low", "Moderate", "Important", "Critical", null\], "nullable": true}}},|;
s|"items":{"oneOf":\[{"properties":{"count":{"type":"integer"},"value":{"type":"string"}},"type":"object"},{"properties":{"count":{"type":"integer"},"value":{"type":"boolean"}},"type":"object"}\]},|"items":{"properties":{"count":{"type":"integer"},"value":{"type":"string"}},"type":"object"},|g;
s|{"\$ref": "#/components/schemas/VulnerabilityList"}|{"type": "array","items": { "type": "string","example": ""}}|g;
# workaround for converting nullable date-time to nullable strings
s|"format":"date-time","nullable":true,"type":"string"|"nullable":true,"type":"string"|g;
' $1
}
function generate_client() {
NAME=$1
SPEC_URL=$2
LOCAL_JSON="$NAME/api/openapi.json"
curl $SPEC_URL -o "$LOCAL_JSON"
filter_oneof "$LOCAL_JSON"
HERE=$(pwd)
# remove old code and doc
find "$HERE/$NAME" -name client_test.go -prune -o \( -name '*.go' -o -name '*.md' \) -print0 \
| xargs -0 rm -f
$CONTAINER_TOOL run --rm -v ${HERE}:/local:z --security-opt=label=disable $CONTAINER generate \
-i "/local/$LOCAL_JSON" \
-g go \
--api-package $NAME \
-p packageName=$NAME,isGoSubmodule=true \
--git-host "github.com" --git-user-id RedHatInsights --git-repo-id patchman-clients \
--type-mappings DateTime=string \
-o /local/$NAME
if [ "$NAME" == "inventory" ] ; then
sed -i 's/uNKNOWNBASETYPE/createCheckIn/g; s/UNKNOWN_BASE_TYPE/CreateCheckIn/g;' $NAME/api_hosts.go
fi
}
$CONTAINER_TOOL image exists $CONTAINER || $CONTAINER_TOOL pull $CONTAINER
# Generate Inventory client
if [ -n "${GENERATE[inventory]}" ] ; then
if [[ -z $INVENTORY_ADDRESS ]]; then
echo "Using default inventory address (CI)"
INVENTORY_ADDRESS=https://ci.cloud.redhat.com
fi
generate_client inventory "${INVENTORY_ADDRESS}/api/inventory/v1/openapi.json"
fi
# Generate Vmaas client
if [ -n "${GENERATE[vmaas]}" ] ; then
if [[ -z $VMAAS_ADDRESS ]]; then
echo "Using default vmaas address (CI)"
VMAAS_ADDRESS=https://webapp-vmaas-ci.5a9f.insights-dev.openshiftapps.com
fi
generate_client vmaas "${VMAAS_ADDRESS}/api/vmaas/v3/openapi.json"
fi
# Generate RBAC client
if [ -n "${GENERATE[rbac]}" ] ; then
if [[ -z $RBAC_ADDRESS ]]; then
echo "Using default RBAC address (CI)"
RBAC_ADDRESS=https://ci.cloud.redhat.com
fi
generate_client rbac "${RBAC_ADDRESS}/api/rbac/v1/openapi.json"
fi