-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrepub-sub.sh
More file actions
executable file
·137 lines (110 loc) · 4.57 KB
/
Copy pathrepub-sub.sh
File metadata and controls
executable file
·137 lines (110 loc) · 4.57 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
#! /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
RED='\n\033[1;31m'
GREEN='\n\033[1;32m'
END_COLOR='\033[0m'
# ******************** 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 "${RED}FAIL${END_COLOR}"
echo 'Error' $RESPONSE
elif [[ $TOKEN_RESPONSE == '' ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Obtained token '
fi
# ******************** Step 2. Get Application ********************
nice_echo "Step 2. 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 "${RED}FAIL${END_COLOR}"
echo 'Error' $RESPONSE
#exit
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Retrieved item ' $RESPONSE_URL
fi
# ******************** Step 3. Get API Product ********************
nice_echo "Step 3. 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/${API_PRODUCT_NAME}`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.results[0].url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Error' $RESPONSE
#exit
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Retrieved item ' $RESPONSE_URL
fi
# ******************** Step 4. Publish API Product ********************
nice_echo "Step 4. 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 "${RED}FAIL${END_COLOR}"
echo 'Error' $RESPONSE
#exit
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Published API product in Drafts ' $RESPONSE_URL
fi
# ******************** Step 5. Create Application Subscription ********************
nice_echo "Step 5. 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 "${RED}FAIL${END_COLOR}"
echo 'Error' $RESPONSE
#exit
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Modified item ' $RESPONSE_URL
fi
nice_echo "Script actions completed. Check logs for more details."