-
Notifications
You must be signed in to change notification settings - Fork 4
/
pushapi
executable file
·710 lines (626 loc) · 21.2 KB
/
pushapi
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#!/bin/bash
# -*- mode:shell-script; coding:utf-8; -*-
#
# pushapi
#
# A bash script that imports and possibly deploys API bundles to Apigee Edge.
#
# Invoke with -h for help.
# usage examples:
#
# import api proxy named 'foo' to org 'org1':
# ./pushapi -v -o org1 foo
#
# import api proxy named 'foo' to org 'org1', and deploy it to env 'test':
# ./pushapi -v -o org1 -e test -d foo
#
#
# Last Saved: <2017-March-08 16:36:34>
#
# ----------------------------------------------------------------
#
# Copyright (c) 2015 Dino Chiesa and Apigee Corp
# All Rights Reserved.
#
# This code is licensed under a Revised BSD-style 3-clause license:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the disclaimer that follows.
#
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the disclaimer that follows in
# the documentation and/or other materials provided with the
# distribution.
#
# - The name of the contributors may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
version="Thursday, 15 December 2016, 19:32"
## defaults
#environment=test
environment=""
mgmtserver=https://api.enterprise.apigee.com
verbosity=2
netrccreds=0
credentials=""
import=1
deploy=0
undeployonly=0
removeall=0
ziponly=0
revstokeep=-1
TAB=$'\t'
# import .pushapi
# This is just a file with default settings in it.
# For example, you could include your credentials there.
#
# NB: you should add .pushapi to the .gitignore to avoid uploading
# apigee creds to github.
# credentials=username:password
# org=org_name
# environment=environment_name (e.g. test)
# url=api_management_url (eg, https://api.enterprise.apigee.com)
PDIRNAME=`dirname $0`
if [[ -f "$PDIRNAME/.pushapi" ]]; then
. $PDIRNAME/.pushapi
fi
function echoerror() { echo "$@" 1>&2; }
function usage() {
local CMD=`basename $0`
echo "$CMD: Import and optionally deploy an API proxy to Apigee Edge. "
echo " Or maybe delete older revisions of an API Proxy."
echo " Uses the curl utility, which must be available on the path."
echo " $CMD version: ${version}"
echo "usage: "
echo " $CMD [options] apiproxy"
echo "options: "
echo " -h display this message."
echo " -o org the org to use."
echo " -e env the environment to deploy to."
echo " -d deploy the revision. (default: no deploy). Requires environment (-e)"
echo " -p path the basepath at which to deploy the apiproxy. (default: use /)"
echo " -n name override the name of the apiproxy. (default: use the directory name)"
echo " -S url the base server url to use."
echo " -u creds authn credentials for the API calls to the Edge management server."
echo " -c retrieve authn credentials from .netrc"
echo " -x undeploy any revs of the API in the environment; do not deploy a new rev."
echo " -X undeploy & DELETE all revs of the API; do not deploy a new revision."
echo " -K num delete all but last num revs of the API (if not deployed)"
echo " -I do not import. Sometimes useful with -K."
echo " -z just zip the bundle; do not deploy a new revision."
echo " -q quiet; decrease verbosity by 1"
echo " -v verbose; increase verbosity by 1"
echo
echo "Current parameter values:"
echo " mgmt api url: ${mgmtserver}"
echo " organization: $org"
echo " environment: $environment"
echo " apiproxy: $apiproxy"
echo " verbosity: $verbosity"
echo
echo "You can specify default settings in the .pushapi file."
echo
exit 1
}
## function MYCURL
## Print the curl command then run it.
##
## Implicit input:
## $credentials = must contain one of the following:
## -u username:password
## -n
##
## output by side effect:
## 1. puts curl output into file named ${CURL_OUT}. If the CURL_OUT
## env var is not set prior to calling this function, it is created
## and the name of a tmp file in /tmp is placed there.
## 2. puts curl http_status into variable CURL_RC
##
MYCURL() {
local allargs
local ix=0
# grab the curl args
while [[ "$1" ]]; do
allargs[$ix]=$1
let "ix+=1"
shift
done
[[ -z "${CURL_OUT}" ]] && CURL_OUT=`mktemp /tmp/apigee-pushapi.curl.out.XXXXXX`
[[ -f "${CURL_OUT}" ]] && rm ${CURL_OUT}
[[ $verbosity -gt 1 ]] && echo && echo "curl ${allargs[@]}"
# run the curl command
CURL_RC=`curl $credentials -s -w "%{http_code}" -o "${CURL_OUT}" "${allargs[@]}"`
[[ $verbosity -gt 1 ]] && echo "==> ${CURL_RC}"
}
timer() {
if [[ $# -eq 0 ]]; then
echo $(date '+%s')
else
local stime=$1
etime=$(date '+%s')
if [[ -z "$stime" ]]; then stime=$etime; fi
dt=$((etime - stime))
ds=$((dt % 60))
dm=$(((dt / 60) % 60))
dh=$((dt / 3600))
printf '%d:%02d:%02d' $dh $dm $ds
fi
}
CleanupAndFinish() {
[[ -f ${CURL_OUT} ]] && rm -rf ${CURL_OUT}
if [[ $verbosity -gt 1 ]]; then
echo
echo "done"
date +"%Y-%m-%d %H:%M:%S"
fi
[[ $verbosity -gt 0 ]] && printf 'Elapsed time: %s\n\n' $(timer $starttime)
}
DeleteApiproxyArchive() {
if [[ -f "$zipname" ]]; then
[[ $verbosity -gt 0 ]] && echo "Deleting temp zip ${zipname} ..."
rm -rf "$zipname"
fi
}
ProduceAndMaybeShowZip() {
local curdir zipout TIMESTAMP=$(date '+%Y%m%d-%H%M%S')
zipname="apiproxy-${apiproxy}-${TIMESTAMP}.zip"
[[ $ziponly -eq 0 ]] && zipname="/tmp/${zipname}"
if [[ -f $zipname ]]; then
[[ $verbosity -gt 0 ]] && echo "removing the existing zip (${zipname})..."
rm -rf $zipname
fi
[[ $verbosity -gt 0 ]] && echo "Creating the zip..."
curdir=$(pwd)
# check for package.json
if [[ -f "$appdir/apiproxy/resources/node/package.json" ]]; then
[[ $verbosity -gt 0 ]] && echo "zipping node_modules..."
cd "$appdir/apiproxy/resources/node"
if [[ -f package.json ]]; then
[[ -f node_modules.zip ]] && rm -rf node_modules.zip
npmout=`npm install 2>&1`
[[ -f npm-debug.log ]] && rm npm-debug.log
zipout=`zip node_modules.zip -r node_modules/ -x "*/Icon*" 2>&1`
fi
[[ -d node_modules ]] && rm -rf node_modules
fi
cd "$curdir"
cd "$appdir"
otherfiles=""
for f in LICENSE License.txt README.md Readme.txt ; do
[[ -f "${f}" ]] && otherfiles="${otherfiles} ${f}"
done
if [[ ! -d apiproxy ]]; then
echo "Error: there is no apiproxy directory in "
pwd
echo
exit 1
fi
zipout=$(zip -r "${zipname}" -r apiproxy $otherfiles -x "*/*.*~" -x "*/.tern-port" -x "*/.DS_Store" -x "*/Icon*" -x "*/#*.*#" -x "*/node_modules/*")
[[ $ziponly -eq 1 ]] && mv "${zipname}" "$curdir"
cd "$curdir"
[[ $verbosity -gt 1 ]] && unzip -l ${zipname} && echo
[[ $verbosity -gt 1 ]] && echo "Temporary zip: ${zipname}"
}
choose_credentials() {
local username password
read -p "orgadmin username for org ${org} at ${mgmtserver} ? (blank to use .netrc): " username
echo
if [[ "$username" = "" ]] ; then
credentials="-n"
else
echo -n "Org Admin Password: "
read -s password
echo
credentials="-u ${username}:${password}"
fi
}
GetDeploymentStatus() {
local env_array rev_array
# inquire deployment status
MYCURL -X GET "${mgmtserver}/v1/o/$org/apis/${proxyname}/deployments"
if [[ ${CURL_RC} -eq 200 ]]; then
[[ $verbosity -gt 1 ]] && cat ${CURL_OUT} && echo
## extract the environment names and revision numbers from the list of deployments.
env_array=(`cat ${CURL_OUT} | grep revision -B 1 | grep name | tr -d '\n' | sed -E 's/"name"|[":,]//g'`)
rev_array=(`cat ${CURL_OUT} | grep revision -A 5 | grep name | tr -d '\n' | sed -E 's/"name"|[":,]//g'`)
m=${#rev_array[@]}
[[ $verbosity -gt 1 ]] && echo "found ${m} deployed revisions"
deployments=()
let m-=1
while [[ $m -ge 0 ]]; do
echo "${env_array[$m]}=${rev_array[$m]}"
deployments+=("${env_array[$m]}=${rev_array[$m]}")
let m-=1
done
have_deployments=1
elif [[ ${CURL_RC} -ne 404 ]]; then
cat ${CURL_OUT}
echo
if [[ ${CURL_RC} -eq 403 || ${CURL_RC} -eq 401 ]]; then
echo You do not have permissions to do that.
else
echo An unknown error occurred.
fi
echo
exit 1
else
deployments=()
have_deployments=1
echo
fi
GetDeploymentRevisions
GetDeploymentEnvironments
}
GetDeploymentEnvironments() {
local d env
# inquire deployment status
[[ ${have_deployments} -eq 0 ]] && GetDeploymentStatus
deployed_envs=()
for d in "${!deployments[@]}"; do
env=`expr "${deployments[d]}" : '\([^=]*\)'`
deployed_envs+=($env)
done
}
GetDeploymentRevisions() {
local deployment rev
# inquire deployment status
[[ ${have_deployments} -eq 0 ]] && GetDeploymentStatus
deployed_revs=()
for deployment in ${deployments[@]}; do
rev=`expr "$deployment" : '[^=]*=\([^=]*\)'`
deployed_revs+=($rev)
done
}
InquireDeployment() {
local rev1 env1 is_deployed deployment env rev
rev1=$1
env1=$2
is_deployed=0
for deployment in ${deployments[@]}; do
env=`expr "$deployment" : '\([^=]*\)'`
if [[ "X$env" = "X$env1" ]]; then
rev=`expr "$deployment" : '[^=]*=\([^=]*\)'`
[[ "X$rev" = "X$rev1" ]] && is_deployed=1
fi
done
echo $is_deployed
}
DeleteRevIfNotDeployed() {
# maybe delete revisions of an apiproxy
local rev1=$1 env1=$2 is_deployed=0
[[ ${have_deployments} -eq 0 ]] && GetDeploymentStatus
if [[ ${#deployed_envs[@]} -gt 0 ]]; then
for env in ${deployed_envs[@]}; do
if [[ $is_deployed -eq 0 ]]; then
is_deployed=`InquireDeployment $rev1 $env`
fi
done
fi
if [[ $is_deployed -eq 0 ]]; then
[[ $verbosity -gt 0 ]] && echo "revision ${rev1}: Deleting the revision ..."
MYCURL -X DELETE "${mgmtserver}/v1/o/$org/apis/$proxyname/revisions/${rev1}"
if [[ $verbosity -gt 1 ]]; then
cat ${CURL_OUT}
echo
fi
if [[ ${CURL_RC} -ne 200 ]]; then
echo The delete failed.
exit
fi
else
[[ $verbosity -gt 0 ]] && echo "revision ${rev1}: NOT Deleting the revision, it is currently deployed ..."
fi
}
MaybeUndeployAndMaybeDelete() {
# maybe undeploy and maybe delete revisions of an apiproxy
local rev1=$1 env1=$2 want_undeploy=0
[[ ${have_deployments} -eq 0 ]] && GetDeploymentStatus
if [[ ${#deployed_envs[@]} -gt 0 ]]; then
for env in ${deployed_envs[@]}; do
if [[ -z "$env1" ]]; then
want_undeploy=1
elif [[ "X$env1" = "X$env" ]]; then
want_undeploy=`InquireDeployment $rev1 $env`
[[ $want_undeploy -eq 0 && $verbosity -gt 0 ]] && echo "revision $rev1: Not deployed to env $env1. No un-deployment necessary..."
fi
if [[ $want_undeploy -gt 0 ]]; then
[[ $verbosity -gt 0 ]] && echo "revision $rev1: Undeploying from [${env}]..."
MYCURL -X POST "${mgmtserver}/v1/o/$org/apis/$proxyname/revisions/${rev1}/deployments?action=undeploy&env=${env}"
[[ $verbosity -gt 1 ]] && cat ${CURL_OUT} && echo
## check return status, exit on fail.
if [[ ${CURL_RC} -ne 200 ]]; then
echo The undeploy failed.
echo
echo CURL_RC = ${CURL_RC}
echo
CleanupAndFinish
exit 1
fi
fi
done
else
[[ $verbosity -gt 0 ]] && echo "revision $rev1: Not deployed; No un-deployment necessary."
fi
if [[ $removeall -gt 0 ]]; then
[[ $verbosity -gt 0 ]] && echo "revision ${rev1}: Deleting the revision ..."
MYCURL -X DELETE "${mgmtserver}/v1/o/$org/apis/$proxyname/revisions/${rev1}"
[[ $verbosity -gt 1 ]] && cat ${CURL_OUT} && echo
if [[ ${CURL_RC} -ne 200 ]]; then
echo The delete failed.
echo
echo CURL_RC = ${CURL_RC}
echo
CleanupAndFinish
exit 1
fi
fi
}
SetProxyName() {
# # old way - just use the dir that contains apiproxy as the name
# proxyname=$apiproxy
# new way: extract the name from the xxx.xml file
local curdir=$(pwd) xmlfiles tokens
appdir=${appdir%/}
if [[ ! -d "${appdir}/apiproxy" ]]; then
echo "Error: there is no apiproxy directory in $appdir"
echo
exit 1
fi
xmlfiles=($(find "${appdir}/apiproxy" -maxdepth 1 -name '*.xml'))
# glob not working ?
#xmlfiles=("${appdir}/apiproxy/*.xml")
if [[ ${#xmlfiles[@]} -ne 1 ]]; then
printf "Error: expected to find a single XML file in %s, found %d instead\n" "${appdir}/apiproxy" ${#xmlfiles[@]}
printf ""
exit 1
fi
[[ $verbosity -gt 1 ]] && printf "found one file: %s\n" ${xmlfiles[0]}
tokens=($(grep APIProxy "${xmlfiles[0]}" | grep name | tr = ' ' | sed -e "s/['\"<>]//g"))
proxyname="${tokens[${#tokens[@]} - 1]}"
[[ $verbosity -gt 1 ]] && printf "using %s as proxy name\n" $proxyname
}
#------------------------------------------------------
#
# Start of execution
#
starttime=$(timer)
have_deployments=0
while getopts "hqve:o:n:u:cdp:S:K:IxXz" opt; do
case $opt in
h) usage ;;
q) verbosity=$(($verbosity-1)) ;;
v) verbosity=$(($verbosity+1)) ;;
e) environment=$OPTARG ;;
o) org=$OPTARG ;;
n) proxyname=$OPTARG ;;
u) credentials=$OPTARG && netrccreds=0 ;;
c) netrccreds=1 ;;
d) deploy=1 ;;
p) basepath=$OPTARG ;;
S) mgmtserver=$OPTARG ;;
K) revstokeep=$OPTARG ;;
I) import=0 ;;
x) undeployonly=1 && import=0 ;;
X) removeall=1 && import=0 ;;
z) ziponly=1 && import=0 ;;
*) echo "unknown arg" && usage ;;
esac
done
shift $(($OPTIND - 1))
## allow for an apiproxy directory that is not local
if [[ $# -gt 0 ]]; then
appdir=$1
apiproxy=`basename "$1"`
fi
if [[ "X$apiproxy" = "X" && revstokeep -eq -1 ]]; then
echoerror "You must specify an apiproxy."
echoerror
usage
exit 1
fi
if [[ "X$proxyname" = "X" ]]; then
SetProxyName
fi
if [[ $import -gt 0 || $ziponly -gt 0 || $revstokeep -eq -1 ]]; then
echo import $import
echo ziponly $ziponly
echo revstokeep $revstokeep
if [[ ! -d "$1" ]]; then
echoerror "The directory $1 does not exist."
echoerror "The apiproxy name you specify must refer to a directory."
echoerror
usage
exit 1
fi
fi
if [[ $ziponly -gt 0 && $undeployonly -gt 0 ]]; then
echoerror "Mutually exclusive. Choose at most one of -x or -z ."
echoerror
usage
exit 1
fi
if [[ $deploy -gt 0 || $import -gt 0 || $undeployonly -gt 0 || $removeall -gt 0 || $revstokeep -gt 0 ]]; then
## need credentials for any of these actions
if [[ ${netrccreds} -eq 1 ]]; then
echo "using credentials from .netrc"
credentials='-n'
elif [[ "X$credentials" = "X" ]]; then
choose_credentials
credentials="-u $credentials"
else
credentials="-u $credentials"
fi
# if ! [[ "$rev" =~ ^[0-9]+$ ]] ; then
# echoerror "Bad revision: $rev"
# echoerror "The revision must be a number."
# echoerror
# usage
# exit 1
# fi
if ! [[ "${mgmtserver}" =~ ^https?://.+$ ]] ; then
echoerror "Bad or missing mgmt api url: ${mgmtserver}"
echoerror
usage
exit 1
fi
if [[ -z "$org" ]]; then
echoerror "missing org"
echoerror
usage
exit 1
fi
if [[ $deploy -gt 0 ]]; then
# to deploy, we need to know the environment
if [[ -z "$environment" ]]; then
echoerror "missing environment"
echoerror
usage
exit 1
fi
fi
fi
# arguments appear to be valid
[[ $verbosity -gt 1 ]] && echo "verbosity = $verbosity"
if [[ $ziponly -gt 0 ]]; then
ProduceAndMaybeShowZip
else
# We need to maybe remove old revisions, maybe import (and maybe deploy) or possibly undeploy
if [[ $undeployonly -gt 0 || $removeall -gt 0 || $revstokeep -gt 0 ]]; then
[[ $verbosity -gt 0 ]] && echo && echo "Inquiring existing revisions..."
MYCURL -X GET "${mgmtserver}/v1/o/$org/apis/$proxyname"
[[ $verbosity -gt 1 ]] && cat ${CURL_OUT} && echo
if [[ ${CURL_RC} -eq 200 ]]; then
# extract the list of revisions from the output
rev_array=(`cat ${CURL_OUT} | grep revision | sed -E 's/"revision"|[]":,[]//g'`)
m=${#rev_array[@]}
if [[ $m -gt 0 ]]; then
echo "There are existing revisions of that API Proxy"
echo "${rev_array[@]}"
if [[ $revstokeep -gt 0 ]]; then
numExistingRevs=${#rev_array[*]}
index=$((numExistingRevs-revstokeep))
if [[ $index -lt 1 ]]; then
echo "Nothing to do. retaining all $numExistingRevs existing revisions."
else
echo deleting revisions ${rev_array[@]::$index} ...
for r1 in ${rev_array[@]::$index}; do
#echo deleting rev $r1
DeleteRevIfNotDeployed ${r1}
done
fi
else
for r1 in ${rev_array[@]}; do
MaybeUndeployAndMaybeDelete ${r1} ${environment}
done
fi
else
echo "There are no existing revisions of that API Proxy."
fi
if [[ $removeall -gt 0 ]]; then
[[ $verbosity -gt 0 ]] && echo "Deleting the API Proxy..."
MYCURL -X DELETE "${mgmtserver}/v1/o/$org/apis/$proxyname"
[[ $verbosity -gt 1 ]] && cat ${CURL_OUT} && echo
if [ ${CURL_RC} -eq 200 ]; then
[[ $verbosity -gt 0 ]] && echo "deleted."
else
[[ $verbosity -lt 0 ]] && cat ${CURL_OUT} && echo
echo "The DELETE failed."
fi
fi
elif [[ ${CURL_RC} -eq 401 ]]; then
echo "Not authorized to do that."
elif [[ ${CURL_RC} -eq 404 ]]; then
echo "No deployments found for that API proxy."
else
echo "status =" ${CURL_RC}
echo "Failed querying that API Proxy."
fi
else
# we want to import and maybe deploy
# create the bundle zip
ProduceAndMaybeShowZip
# import
[[ $verbosity -gt 0 ]] && echo "Importing the bundle as $proxyname..."
MYCURL -X POST "${mgmtserver}/v1/o/$org/apis?action=import&name=$proxyname" -T $zipname -H "Content-Type: application/octet-stream"
[[ $verbosity -gt 1 ]] && cat ${CURL_OUT} && echo && echo
if [[ ${CURL_RC} -ne 201 ]]; then
echoerror "There was an error importing that API bundle..."
echo
echo CURL_RC = ${CURL_RC}
echo
[[ $verbosity -le 1 ]] && cat ${CURL_OUT} && echo
DeleteApiproxyArchive
echo
CleanupAndFinish
exit 1
fi
## what revision did we just import?
rev=`cat ${CURL_OUT} | grep \"revision\" | tr '\r\n' ' ' | sed -E 's/"revision"|[:, "]//g'`
echo This is revision $rev
if [[ $deploy -gt 0 ]]; then
# # if the request is to deploy, first we must un-deploy any
# # existing revision from the target environment.
# if [ $verbosity -gt 0 ]; then
# echo
# echo "Inquiring existing revisions..."
# fi
# MYCURL -X GET "${mgmtserver}/v1/o/$org/apis/$proxyname"
# if [ $verbosity -gt 1 ]; then
# cat ${CURL_OUT}
# echo
# fi
#
# if [ ${CURL_RC} -eq 200 ]; then
# output_parsed=`cat ${CURL_OUT} | json_tokenize | json_parse`
#
# echo "${output_parsed}" | grep --silent \"revision\"
# if [ $? -eq 0 ]; then
#
# existing_revs=`echo "${output_parsed}" | grep \"revision\" | sed -E 's/\"//g' | sed -E 's/.+revision,[0-9]+.'"${TAB}"'//g'`
# #echo "${existing_revs}"
# declare -a rev_array=(${existing_revs})
#
# for r1 in ${rev_array[@]}; do
# if [ $r1 -ne $rev ]; then
# MaybeUndeployAndMaybeDelete ${r1} $environment
# fi
# done
# fi
# fi
[[ -z "${basepath}" ]] && basepath="/"
[[ $verbosity -gt 0 ]] && echo "revision $rev: Deploying..."
# override will implicitly undeploy any existing deployed revisions
formparams="override=true&delay=60"
[[ ! -z "${basepath}" ]] && formparams="${formparams}&basepath=${basepath}"
MYCURL -X POST \
-H content-type:application/x-www-form-urlencoded \
"${mgmtserver}/v1/o/${org}/e/${environment}/apis/${proxyname}/revisions/${rev}/deployments" \
-d "${formparams}"
if [[ ! ${CURL_RC} =~ 200 ]]; then
echo
echoerror "There was an error deploying revision $rev of $proxyname."
cat ${CURL_OUT} 1>&2;
echo
fi
fi
DeleteApiproxyArchive
fi
fi
CleanupAndFinish