-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_definitions.sh
145 lines (130 loc) · 4.85 KB
/
update_definitions.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /bin/bash
set -o pipefail
set -e
############################################################
# Updates the OpenAPI definitions
#
# DESCRIPTION:
# This scripts retrieves the latest definitions from
# cloud.uipath.com and stores them locally to package
# with the CLI
############################################################
organization="uipatcleitzc"
tenant="defaulttenant"
############################################################
# Downloads yq and installs it in bin/
# https://github.com/mikefarah/yq
############################################################
function install_yq()
{
local version="v4.30.8"
mkdir -p bin/
wget --quiet "https://github.com/mikefarah/yq/releases/download/$version/yq_linux_amd64.tar.gz" -O - \
| tar --extract --gunzip --directory=bin/ ./yq_linux_amd64 \
&& mv bin/yq_linux_amd64 bin/yq
}
############################################################
# Download an OpenAPI specification from the given URL
#
# Arguments:
# - The url to download from
# - The OpenAPI version, in case of "v2" the specification
# will be automatically converted to version 3
#
# Returns:
# The OpenAPI specification content
############################################################
function download_definition()
{
local url="$1"
local version="$2"
if [[ "$version" == "v2" ]]; then
wget --quiet "https://converter.swagger.io/api/convert?url=$url" -O - -o /dev/null
else
wget --quiet "$url" -O - -o /dev/null
fi
}
############################################################
# Saves the given definition as yaml in the
# definitions/ folder
#
# Arguments:
# - The definition name
############################################################
function save_definition()
{
local name="$1"
bin/yq eval -P > "definitions/$name.yaml"
}
############################################################
# Updates the server url
#
# Arguments:
# - The new url
############################################################
function update_server_url()
{
local url="$1"
local tenant_variable=""
if [[ "$url" == *"{tenant}"* ]]; then
tenant_variable=',
"tenant": {
"description": "The tenant name (or id)",
"default": "my-tenant"
}'
fi
bin/yq '.servers =
[
{
"url": "'"$url"'",
"description": "The production url",
"variables": {
"organization": {
"description": "The organization name (or id)",
"default": "my-org"
}'"$tenant_variable"'
}
}
]' \
| bin/yq 'del(.schemes)' \
| bin/yq 'del(.host)' \
| bin/yq 'del(.basePath)'
}
############################################################
# Sets a property for the given parameter
#
# Arguments:
# - The parameter name
# - The property to set
# - The new property value
############################################################
function set_parameter_property()
{
local name="$1"
local property_name="$2"
local property_value="$3"
bin/yq '.paths[] |= with(select(.get.parameters != null); (.get.parameters[] | select(.name == "'"$name"'"))."'"$property_name"'" = '"$property_value"')' \
| bin/yq '.paths[] |= with(select(.post.parameters != null); (.post.parameters[] | select(.name == "'"$name"'"))."'"$property_name"'" = '"$property_value"')' \
| bin/yq '.paths[] |= with(select(.put.parameters != null); (.put.parameters[] | select(.name == "'"$name"'"))."'"$property_name"'" = '"$property_value"')' \
| bin/yq '.paths[] |= with(select(.patch.parameters != null); (.patch.parameters[] | select(.name == "'"$name"'"))."'"$property_name"'" = '"$property_value"')' \
| bin/yq '.paths[] |= with(select(.delete.parameters != null); (.delete.parameters[] | select(.name == "'"$name"'"))."'"$property_name"'" = '"$property_value"')' \
| bin/yq '.paths[] |= with(select(.head.parameters != null); (.head.parameters[] | select(.name == "'"$name"'"))."'"$property_name"'" = '"$property_value"')'
}
if [ ! -f "bin/yq" ]; then
echo "Installing yq..."
install_yq
fi
mkdir -p definitions/
echo "Updating identity definition..."
download_definition "https://cloud.uipath.com/$organization/identity_/swagger/external/swagger.json" \
| save_definition "identity"
echo "Updating du.framework definition..."
download_definition "https://cloud.uipath.com/$organization/$tenant/du_/api/framework/swagger/v1/swagger.json" \
| update_server_url "https://cloud.uipath.com/{organization}/{tenant}/du_/api/framework" \
| save_definition "du.framework"
echo "Updating orchestrator definition..."
download_definition "https://cloud.uipath.com/$organization/$tenant/orchestrator_/swagger/v18.0/swagger.json" "v2" \
| update_server_url "https://cloud.uipath.com/{organization}/{tenant}/orchestrator_" \
| set_parameter_property "X-UIPATH-OrganizationUnitId" "x-uipathcli-name" "\"folder-id\"" \
| set_parameter_property "X-UIPATH-OrganizationUnitId" "required" "true" \
| save_definition "orchestrator"