-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreate-app-subscription.sh
More file actions
executable file
·187 lines (146 loc) · 6.61 KB
/
Copy pathcreate-app-subscription.sh
File metadata and controls
executable file
·187 lines (146 loc) · 6.61 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /bin/sh
nice_echo() {
echo "\n\033[1;36m >>>>>>>>>> $1 <<<<<<<<<< \033[0m\n"
}
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# no parameters passed, using default config file
if [ $# -eq 0 ]; then
#using default config file
if [ -e config.cfg ]; then
source config.cfg
echo 'Using default config file at ' ${CURRENT_DIR}/config.cfg
else
echo 'No config file passed and default config file is not available at ' ${CURRENT_DIR}/config.cfg
exit
fi
# usage function
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "Usage: `basename $0` [OPTION] [FILE]...
Options:
-h, --help Display this help and exit
"
exit 0
# config file passed as argument
else
FILENAME=$1 #get filename
if [ -e $FILENAME ]; then
source $FILENAME; # load the file
echo 'Using config file located at ' $FILENAME
else
echo 'Bad config file passed at ' $FILENAME
exit
fi
fi
# ******************** Step 1. Login as the pOrg Owner and get token ********************
nice_echo "Step 1. Login as the pOrg Owner and get token"
CURL_BODY='{"username":"'"${pORG_USERNAME}"'","password":"'"${pORG_PASSWORD}"'","realm":"provider/default-idp-2","client_id":"'"${APIM_CLIENT_ID}"'","client_secret":"'"${APIM_CLIENT_SECRET}"'","grant_type":"password"}'
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $ACTIVATION" -d "$CURL_BODY" ${APIM_SERVER}/api/token`
TOKEN_RESPONSE=`echo "$RESPONSE" | jq -r '.access_token'`
if [[ $TOKEN_RESPONSE == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
elif [[ $TOKEN_RESPONSE == '' ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
else
echo 'Successfully login and obtained token ' #$TOKEN_RESPONSE
fi
# ******************** Step 2. Get Consumer Org ********************
nice_echo "Step 2. Get Consumer Org"
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" ${APIM_SERVER}/api/consumer-orgs/${pORG_NAME}/${CATALOG_NAME}/${CONSUMER_ORG}`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo 'Sucessfully retrieved item ' $RESPONSE_URL
fi
# ******************** Step 3. Get Application ********************
nice_echo "Step 3. Get Application"
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" ${APIM_SERVER}/api/consumer-orgs/${pORG_NAME}/${CATALOG_NAME}/${CONSUMER_ORG}/apps`
RESPONSE_URL=`echo "$RESPONSE" | tr '\r\n' ' ' | jq -r '.results[0].url'`
RESPONSE_URL1=`basename "$RESPONSE_URL"`
RESPONSE_URL2=`dirname "$RESPONSE_URL" | xargs basename`
if [[ $RESPONSE_URL1 == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo 'Sucessfully retrieved item ' $RESPONSE_URL
fi
# ******************** Step 4. Create Application Credentials ********************
nice_echo "Step 4. Create Application Credentials"
CURL_BODY='{ "name": "'"${CONSUMER_APP_CREDS}"'"}'
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/apps/${pORG_NAME}/${CATALOG_NAME}/${RESPONSE_URL2}/${RESPONSE_URL1}/credentials`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo "Application credential created."
CLIENT_ID=`echo "$RESPONSE" | jq -r '.client_id'`
nice_echo "Client ID"
echo ${CLIENT_ID}
CLIENT_SECRET=`echo "$RESPONSE" | jq -r '.client_secret'`
nice_echo "Client Secret"
echo ${CLIENT_SECRET}
fi
# ******************** Step 5. Modify Application ********************
nice_echo "Step 5. Modify Application with Consumer Info"
CURL_BODY='{
"title": "Sandbox Test App",
"name": "sandbox-test-app",
"summary": "Default Sandbox test application",
"redirect_endpoints": [
"'"${CONSUMER_REDIRECT_URL}"'"
],
"application_public_certificate_entry": "'"-----BEGIN CERTIFICATE-----\n${CONSUMER_APP_CERT}\n-----END CERTIFICATE-----\n"'"
}'
#
RESPONSE=`curl -s -k -X PATCH -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/apps/${pORG_NAME}/${CATALOG_NAME}/${RESPONSE_URL2}/${RESPONSE_URL1}`
RESPONSE_URL=`echo "$RESPONSE" | tr '\r\n' ' ' | jq -r '.name'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo 'Sucessfully modified item ' $RESPONSE_URL
fi
# ******************** Step 6. Get API Product ********************
nice_echo "Step 6. Get API Product"
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" ${APIM_SERVER}/api/orgs/${pORG_NAME}/drafts/draft-products`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.results[] | select(.name =="'"${API_PRODUCT_NAME}"'").url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo 'Sucessfully retrieved item ' $RESPONSE_URL
fi
# ******************** Step 7. Publish API Product ********************
nice_echo "Step 7. Publish API Product"
CURL_BODY='{"draft_product_url":"'"${RESPONSE_URL}"'"}'
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/catalogs/${pORG_NAME}/sandbox/publish-draft-product`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo 'Sucessfully published API product ' $RESPONSE_URL
fi
# ******************** Step 8. Create Application Subscription ********************
nice_echo "Step 8. Create Application Subscription"
CURL_BODY='{"product_url":"'"${RESPONSE_URL}"'","plan":"default-plan"}'
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/apps/${pORG_NAME}/${CATALOG_NAME}/${RESPONSE_URL2}/${RESPONSE_URL1}/subscriptions`
RESPONSE_URL=`echo "$RESPONSE" | tr '\r\n' ' ' | jq -r '.name'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo 'Failed call with error' $RESPONSE
#exit
else
echo 'Sucessfully modified item ' $RESPONSE_URL
fi
nice_echo "Script actions completed. Check logs for more details."