zsh completion with nested control functions - autocomplete

I'm writing completion function for zsh. I took cargo completion function as a basis for mine. For the most part it works fine except for -h and --help options. Completion ouput for these functions is unaligned and repeated multiple times (presented below). This only happens when control functions (_describe, _arguments etc) are present in case structure.
Why this is happening and how can I fix this behavior?
Completion function:
#compdef test
_test() {
local context state state_descr line
typeset -A opt_args
_arguments \
"(- 1 *)"{-h,--help}"[Help]" \
"1: :->command" \
"*:: :->args"
case $state in
command)
_alternative 'arguments:custom arg:(a b c)'
;;
args)
_arguments \
"-a[All]" \
"-n[None]"
esac
}
_test
Shell output:
> test -[TAB]
--help
-h
-- Help
--help
-h
-- Help
--help
-h
-- Help

I've just encountered the same issue. The solution is to return zero from your completion function:
#compdef test
_test() {
...
return 0
}
_test
All completion scripts that I've seen use ret variable which is initially 1 and then set to 0 if any completion function succeeded:
#compdef test
_test() {
local context state state_descr line
local ret=1
typeset -A opt_args
_arguments \
"(- 1 *)"{-h,--help}"[Help]" \
"1: :->command" \
"*:: :->args" && ret=0
case $state in
command)
_alternative 'arguments:custom arg:(a b c)' && ret=0
;;
args)
_arguments \
"-a[All]" \
"-n[None]" && ret=0
esac
return ret
}
_test
I'm not sure why they do it, though.

Related

How can I set makefile variables (CC, CFLAGS etc) to terminal or cmd using powershell scripts?

I want to write platform independent Makefile's environment variable setup script which can set Makefile's environment variable like CC, CFLAGS, LDFLAGS, LD etc. This is my make file. I want to run it on Window or Linux as per the user need. So instead of setting CC, CFLAGS, LDFLAGS, LD every time, I want to write a script which can set the variable for user depending on which platform they are using.
LDFLAG=-L..\..\test\lib
LIBS=-ltestlibs
INCLUDES = ..\..\test\inc
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
EXECUTABLE = targetImage.exe
.PHONY: clean
all: myprog
myprog: $(OBJS)
$(CC) $(OBJS) $(LDFLAG) $(LIBS) -o $(EXECUTABLE)
$(OBJS): $(SRCS) $(INCLUDES)
$(CC) $(CFLAGS) -I$(INCLUDES) -c $(SRCS)
clean:
$(RM) $(OBJS) $(EXECUTABLE)
I know the .sh script but it can be run on CYGWIN or MINGW.
#!/bin/sh
echo "Finding the current OS type"
echo
osType="$(uname -s)"
#osType=$1
case "${osType}" in
"CYGWIN")
{
echo "Running on CYGWIN."
CURRENT_OS=CYGWIN
export CC=""
} ;;
Linux*)
{
echo "Running on Linux."
CURRENT_OS=Linux
export CC="-g -Wall"
} ;;
"MINGW")
{
echo "Running on MINGW."
CURRENT_OS=MINGW
} ;;
"Thor96")
{
source /opt/fsl-imx-xwayland/4.14-sumo/environment-setup-aarch64-poky-linux
CURRENT_OS=THOR96
};;
*)
{
echo "Unsupported OS:${osType}, exiting"
exit
} ;;
esac
echo ${CURRENT_OS}
But it can run on linux only. So how can I achieve the same using powershell .ps1 scripts ? So same script can work on any platform.
I have drafted this .ps1 file for reference but need to update it as I am not sure it's correct or not. Please guide me with proper solution. I am not able to find the proper solution for this.
Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
$Env:CC = "gcc"
}
Function RunOn-Linux
{
Write-Host 'The Script is Running on a Linux Machine'
$Env:CC = "gcc"
}
Function RunOn-Mac
{
Write-Host 'The Script is Running on a Mac'
}
Function RunOn-Other
{
Write-Host 'The Script is Running on a Other'
$Env:CC = "aarch64-poky-linux-gcc"
}
If ($IsWindows)
{RunOn-Windows}
elseif ($IsLinux)
{RunOn-Linux}
elseif ($IsMacOS)
{RunOn-Mac}
else
{RunOn-Other}
I don't have a Windows box to do tests but if yours has GNU make available there is a chance that you can do what you want from inside your Makefile:
# List supported OS (as returned by uname -s, case sensitive)
SUPPORTED_OS := CYGWIN Linux MINGW Darwin
# Get OS
OS := $(shell uname -s)
# Check if OS is supported
ifneq ($(filter-out $(SUPPORTED_OS),$(OS)),)
$(error Unsupported OS: $(OS))
endif
# Define all OS-dependent make variables as OS_VARIABLE
# Linux:
Linux_CC := gcc
Linux_CFLAGS := -g -Wall
Linux_LDFLAGS := ...
# CIGWIN:
CYGWIN_CC := ...
CYGWIN_CFLAGS := ...
CYGWIN_LDFLAGS := ...
...
# Assign make variables
CC := $($(OS)_CC)
CFLAGS := $($(OS)_CFLAGS)
LDFLAGS := $($(OS)_LDFLAGS)

Report tests with excluded tags as skipped [duplicate]

I have some tests I marked with an appropriate marker. If I run pytest, by default they run, but I would like to skip them by default. The only option I know is to explicitly say "not marker" at pytest invocation, but I would like them not to run by default unless the marker is explicitly asked at command line.
A slight modification of the example in Control skipping of tests according to command line option:
# conftest.py
import pytest
def pytest_collection_modifyitems(config, items):
keywordexpr = config.option.keyword
markexpr = config.option.markexpr
if keywordexpr or markexpr:
return # let pytest handle this
skip_mymarker = pytest.mark.skip(reason='mymarker not selected')
for item in items:
if 'mymarker' in item.keywords:
item.add_marker(skip_mymarker)
Example tests:
import pytest
def test_not_marked():
pass
#pytest.mark.mymarker
def test_marked():
pass
Running the tests with the marker:
$ pytest -v -k mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...
Or:
$ pytest -v -m mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...
Without the marker:
$ pytest -v
...
collected 2 items
test_spam.py::test_not_marked PASSED
test_spam.py::test_marked SKIPPED
...
Instead of explicitly say "not marker" at pytest invocation, you can add following to pytest.ini
[pytest]
addopts = -m "not marker"

Dataflow template is not taking input parameters

I have a dataflow template created with below command
mvn compile exec:java \
-Dexec.mainClass=com.StarterPipeline \
-Dexec.args="--runner=DataflowRunner \
--project=jason-qa \
--stagingLocation=gs://jason_test/dataflow/staging \
--region=asia-east1 \
--zone=asia-east1-a \
--subnetwork=regions/asia-east1/subnetworks/dmz-asia-east1 \
--templateLocation=gs://jason_test/dataflow/Template \
--campaignId="
Executing Templates with below command
gcloud dataflow jobs run jason203 \
--project=jason-qa \
--region=asia-east1 \
--gcs-location gs://jason_test/dataflow/Template \
--parameters campaignId=run-test323,output=gs://jason_test/dataflow/counts
code copy from Count example and have few changes
public interface MyOptions extends PipelineOptions {
#Description("campaign id")
#Required
#Default.String("default-test123")
ValueProvider<String> getCampaignId();
void setCampaignId(ValueProvider<String> campaignId);
}
static void run(MyOptions options) {
Pipeline p = Pipeline.create(options);
String id = options.getCampaignId().get();
p.apply("ReadLines", TextIO.read().from(options.getInputFile()))
.apply(new Count())
.apply(MapElements.via(new FormatAsTextFn()))
.apply("WriteCounts", TextIO.write().to(options.getOutput() + id));
p.run();
}
from the DataFlow Job summary --> Pipeline options
I can find the info: campaignId run-test323
but the result in bucket, file name is -00000-of-00003(It should be run-test323-00000-of-00003)
should remove the .get() in the code, then it will work

Getting error in hack/update-all.sh script

I have been trying to run ./hack/update-all.sh script and I am getting this error while updating codegen:
$ ./hack/update-all.sh
Running in the silent mode, run with -v if you want to see script logs.
Running in short-circuit mode; run with -a to force all scripts to run.
Updating generated-protobuf
Updating codegen
# runtime
/usr/local/go/src/runtime/os2_linux_generic.go:12: _SS_DISABLE redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:8
/usr/local/go/src/runtime/os2_linux_generic.go:13: _NSIG redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:9
/usr/local/go/src/runtime/os2_linux_generic.go:14: _SI_USER redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:10
/usr/local/go/src/runtime/os2_linux_generic.go:15: _SIG_BLOCK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:11
/usr/local/go/src/runtime/os2_linux_generic.go:16: _SIG_UNBLOCK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:12
/usr/local/go/src/runtime/os2_linux_generic.go:17: _SIG_SETMASK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:13
/usr/local/go/src/runtime/os2_linux_generic.go:18: _RLIMIT_AS redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:14
/usr/local/go/src/runtime/os2_linux_generic.go:24: sigset redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:20
/usr/local/go/src/runtime/os2_linux_generic.go:26: rlimit redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:22
/usr/local/go/src/runtime/panic1.go:11: paniclk redeclared in this block
previous declaration at /usr/local/go/src/runtime/panic.go:552
/usr/local/go/src/runtime/panic1.go:11: too many errors
!!! Error in /home/peeyush/work/kubernetes/hack/lib/golang.sh:435
'go install "${goflags[#]:+${goflags[#]}}" -ldflags "${goldflags}" "${nonstatics[#]:+${nonstatics[#]}}"' exited with status 2
Call stack:
1: /home/peeyush/work/kubernetes/hack/lib/golang.sh:435 kube::golang::build_binaries_for_platform(...)
2: /home/peeyush/work/kubernetes/hack/lib/golang.sh:574 kube::golang::build_binaries(...)
3: /home/peeyush/work/kubernetes/hack/build-go.sh:26 main(...)
Exiting with status 1
!!! Error in /home/peeyush/work/kubernetes/hack/lib/golang.sh:494
'( kube::golang::setup_env; echo "Go version: $(go version)"; local host_platform; host_platform=$(kube::golang::host_platform); local goflags goldflags; eval "goflags=(${KUBE_GOFLAGS:-})"; goldflags="${KUBE_GOLDFLAGS:-} $(kube::version::ldflags)"; local use_go_build; local -a targets=(); local arg; for arg in "$#";
do
if [[ "${arg}" == "--use_go_build" ]]; then
use_go_build=true;
else
if [[ "${arg}" == -* ]]; then
goflags+=("${arg}");
else
targets+=("${arg}");
fi;
fi;
done; if [[ ${#targets[#]} -eq 0 ]]; then
targets=("${KUBE_ALL_TARGETS[#]}");
fi; local -a platforms=("${KUBE_BUILD_PLATFORMS[#]:+${KUBE_BUILD_PLATFORMS[#]}}"); if [[ ${#platforms[#]} -eq 0 ]]; then
platforms=("${host_platform}");
fi; local binaries; binaries=($(kube::golang::binaries_from_targets "${targets[#]}")); local parallel=false; if [[ ${#platforms[#]} -gt 1 ]]; then
local gigs; gigs=$(kube::golang::get_physmem); if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then
kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"; parallel=true;
else
kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"; parallel=false;
fi;
fi; if [[ "${parallel}" == "true" ]]; then
kube::log::status "Building go targets for ${platforms[#]} in parallel (output will appear in a burst when complete):" "${targets[#]}"; local platform; for platform in "${platforms[#]}";
do
( kube::golang::set_platform_envs "${platform}"; kube::log::status "${platform}: go build started"; kube::golang::build_binaries_for_platform ${platform} ${use_go_build:-}; kube::log::status "${platform}: go build finished" ) &> "/tmp//${platform//\//_}.build" &
done; local fails=0; for job in $(jobs -p);
do
wait ${job} || let "fails+=1";
done; for platform in "${platforms[#]}";
do
cat "/tmp//${platform//\//_}.build";
done; exit ${fails};
else
for platform in "${platforms[#]}";
do
kube::log::status "Building go targets for ${platform}:" "${targets[#]}"; kube::golang::set_platform_envs "${platform}"; kube::golang::build_binaries_for_platform ${platform} ${use_go_build:-};
done;
fi )' exited with status 1
Call stack:
1: /home/peeyush/work/kubernetes/hack/lib/golang.sh:494 kube::golang::build_binaries(...)
2: /home/peeyush/work/kubernetes/hack/build-go.sh:26 main(...)
Exiting with status 1
!!! Error in ./hack/../hack/update-codegen.sh:32
'"${KUBE_ROOT}/hack/build-go.sh" ${BUILD_TARGETS[*]}' exited with status 1
Call stack:
1: ./hack/../hack/update-codegen.sh:32 main(...)
Exiting with status 1
Updating codegen FAILED
Any idea what could be the reason behind this? Or how to resolve this issue?
Looks like an environment issue. Cleared everything, cloned the repo afresh and everything is working fine.

MInimized deployment of WSO2 APIM

We're considering to provide our own UI for WSO2, and make it work with the APIM gateway by invoking Publisher/Store REST API's.
Is there a way to strip of the UI part of WSO2 APIM and have a deployment containing only
the gateway
the key manager
the publisher --> REST API only, no UI
the store --> REST API only, no UI
Is there such bundle available out of the box?
Otherwise, will it be possible to download either the GitHub source or the deployment package and tear off any UI related plugins and their dependent libraries?
If you don't need any UI components, you may remove publisher and store jaggary web applications from repository/deployment/server/jaggaryapps location. If you checkout the source code, then you will need to checkout product repo which is in [1] and component repo which is in [2] to perform necessary changes for remove UI stuff, but it will add more complexity and take time. Without UI part, you can use REST API in 1.10.0. There is no OOTB bundle.
[1]-https://github.com/wso2/product-apim
[2]-https://github.com/wso2/carbon-apimgt
This is our script for doing this (I will thank any bug or comment, currenty we are testing it)
#!/bin/bash
# WSO2 2.1.0
# Publish an API in several gateways, using internal REST API
# Reference
# https://docs.wso2.com/display/AM210/apidocs/publisher/
# IMPORTANT: Change these values according your WSO2 APIM versionāˆ«
# Version 2.1.0
# declare APP_CLIENT_REGISTRATION="/client-registration/v0.11/register"
# declare -r URI_API_CTX="/api/am/publisher/v0.11"
# Version 2.1.0 update 14
declare -r APP_CLIENT_REGISTRATION="/client-registration/v0.12/register"
declare -r URI_API_CTX="/api/am/publisher/v0.12"
# Constants
declare -r URI_TOKEN="/token"
declare -r URI_API_APIS="${URI_API_CTX}/apis"
declare -r URI_API_ENVIRONMENTS="${URI_API_CTX}/environments"
declare -r URI_API_PUBLISH="${URI_API_CTX}/apis/change-lifecycle?action=Publish&apiId="
declare -r API_SCOPE_VIEW="apim:api_view"
declare -r API_SCOPE_PUBLISH="apim:api_publish"
declare -r API_SCOPE_CREATE="apim:api_create"
# Parameters
declare APIUSER=""
declare APIPASSWORD=""
declare APIMANAGER=""
declare APINAME=""
declare APIVERSION=""
declare -a APIGATEWAY
declare -i MANAGER_SERVICES_PORT=9443
declare -i MANAGER_NIOPT_PORT=8243
# Variables
# User login for aplication registration. User:Password in base64 (default admin:admin)
declare APIAUTH="YWRtaW46YWRtaW4="
# Client application token. ClientId:ClientSecret in base64
declare CLIENTTOKEN
# User access token (view)
declare ACCESSVIEWTOKEN
# User access token type (view)
declare ACCESSVIEWTOKENTYPE="Bearer"
# User access token (publish)
declare ACCESSPUBLISHTOKEN
# User access token type (publish)
declare ACCESSVIEWPUBLISHTYPE="Bearer"
# User access token (create)
declare ACCESSCREATETOKEN
# User access token type (create)
declare ACCESSVIEWCREATETYPE="Bearer"
# API internal ID
declare APIID
# echoErr
# Send message to error stream (/dev/stderr by default)
function echoErr() {
printf "%s\n" "$*" >&2;
}
# showHelp
# Usage info
showHelp() {
cat <<-EOF
Usage: ${0##*/} [-u USER] [-p PASSWORD] APIMANAGER [-s ServicePort] [-n NioPTPort] APINAME APIVERSION APIGATEWAY [APIGATEWAY] ...
Publish an API in the selected gateways
-u USER User name (if not defined, will ask for it)
-p PASSWORD User password (if not defined, will ask for it)
-s ServicePort Services Port in api manager host (by default 9443)
-n NioPTPort Nio/PT Port in key manager host (by default 8243)
APIMANAGER API MANAGER / KEY MANAGER host name (e.g. apimanager.example.com)
APINAME API to publish (has to be in CREATED, PROTOTYPED or PUBLISH state)
APIVERSION API Version to publish
APIGATEWAYs All of the gateway to publish the API (one or more)
EOF
}
# getPassword
# get a password type field (without echo and double input)
function getPassword()
{
local pwd=${3:-"NoSet"}
local verify="_Set_No"
local default=""
if [ -z "$1" ] || [ -z "$2" ]
then
echo 'ERROR: Use getPassword "Message" VAR_NAME [default]'
exit 1
else
if [ -n ${3} ]
then
default=$'\e[31m['${3}$']\e[0m'
fi
while true
do
read -sp "$1 $default" pwd
echo ""
# if empty (=Intro) use default if available
if [ "$pwd" == "" ] && [ -n "$3" ]
then
pwd="$3"
break
fi
# check password length
if [ ${#pwd} -lt 6 ]
then
echo "Password too short. Minimum length is 6"
continue
else
read -sp "Verify - $1 " verify
echo ""
if [ "$pwd" != "$verify" ]
then
echo "Passwords do not match. Retype."
else
break
fi
fi
done
eval $2="$pwd"
fi
}
# showGateways
# Print the list of available gateways in a friendly form
function showGateways() {
local -i count
local name
local gwtype
local endpoint
if [ -z $1 ]
then
echo "Use: showGateways \$apiEnvironments"
else
count=$(echo $1|jq -r '.count')
if [ "$count" -gt "0" ]
then
printf "%-20s %-10s %s\n" "Name" "Type" "Endpoint HTTPS" >&2
printf "%-20s %-10s %s\n" "====================" "==========" "===============================================" >&2
for i in $(seq 0 $(( $count - 1 )) )
do
name=$(echo "$1"|jq -r '.list['$i'].name')
gwtype=$(echo "$1"|jq -r '.list['$i'].type')
endpoint=$(echo "$1"|jq -r '.list['$i'].endpoints.https')
printf "%-20s %-10s %s\n" "$name" "$gwtype" "$endpoint" >&2
done
fi
fi
}
# validateGateway
# validate if all the gateways names (passed as parameter - global variable) are in environments
function validateGateways() {
if [ -z $1 ]
then
echo "Use: validateGateways \$apiEnvironments"
exit 1
else
for gateway in ${APIGATEWAY[#]}
do
jq -er \
--arg gateway_name "$gateway" '
.list[] |
select(.name == $gateway_name)
' <<<"$1" >/dev/null
if [ $? -ne 0 ]
then
echo "ERROR: Gateway '$gateway' is not found" >&2
return 1
fi
done
fi
return 0
}
# getClientToken
# Parse the answer of client registration, to get client token
# return (echo to stdout) the clientToken
function getClientToken() {
local clientId
local clientSecret
local clientToken
if [ -z $1 ]
then
echo "Use: getClientToken \$clientRegistration" >&2
exit 1
else
# Parse answer to get ClientId and ClientSecret
clientId=$(echo $clientRegistration|jq -r '.clientId')
clientSecret=$(echo $clientRegistration|jq -r '.clientSecret')
if [ "$clientId" == "" ] || [ "$clientSecret" == "" ] || [ "$clientId" == "null" ] || [ "$clientSecret" == "null" ]
then
return 1
else
echo -n "$clientId:$clientSecret"|base64
return 0
fi
fi
}
# getAccessToken
# Parse the answer of client API Login, to get client token
# return (echo to stdout) the accessToken
function getAccessToken() {
local accessToken
if [ -z $1 ]
then
echo "Use: getAccessToken \$clientAPILoginView|\$clientAPILoginPublish" >&2
exit 1
else
# Parse answer to get ClientId and ClientSecret
accessToken=$(echo $1|jq -r '.access_token')
if [ "$accessToken" == "" ] || [ "$accessToken" == "null" ]
then
return 1
else
echo -n "$accessToken"
return 0
fi
fi
}
# getAccessTokenType
# Parse the answer of client API Login, to get client token type
# return (echo to stdout) the accessTokenType
function getAccessTokenType() {
local tokenType
if [ -z $1 ]
then
echo "Use: getAccessToken \$clientAPILoginView|\$clientAPILoginPublish" >&2
exit 1
else
# Parse answer to get ClientId and ClientSecret
tokenType=$(echo $1|jq -r '.token_type')
if [ "$tokenType" == "" ] || [ "$tokenType" == "null" ]
then
return 1
else
echo -n "$tokenType"
return 0
fi
fi
}
# getAPIId
# Parse the answer of query API to get the API ID (checking version name)
# Thanks to https://stackoverflow.com/users/14122/charles-duffy
# return (echo to stdout) the APIID
function getAPIId() {
if [ -z $1 ]
then
echo "Usage: getAPIId \$apiQuery" >&2
exit 1
else
# Parse answer to get API ID
jq -er \
--arg target_name "$APINAME" \
--arg target_version "$APIVERSION" '
.list[] |
select(.name == $target_name) |
select(.version == $target_version) |
.id' <<<"$1"
fi
}
# getAPIGatewayEnvironments
# Parse the answer of detailed query API to get the API gateway environments
# return (echo to stdout) the gateway environments
function getAPIGatewayEnvironments() {
if [ -z "$1" ]
then
echo "Usage: getAPIGatewayEnvironments \$apiResource" >&2
exit 1
else
# Parse answer to get API ID
jq -er '.gatewayEnvironments' <<<"$1"
fi
}
# getAPIStatus
# Parse the answer of detailed query API to get the API status
# return (echo to stdout) the status
function getAPIStatus() {
if [ -z "$1" ]
then
echo "Usage: getAPIStatus \$apiResource" >&2
exit 1
else
# Parse answer to get API ID
jq -er '.status' <<<"$1"
fi
}
# setGateways
# Update the field gatewayEnvironments in API resource from GATEWAY parameter array
# Return the new API resource update
function setGateways() {
local gateways
local oIFS
if [ -z "$1" ]
then
echo "Use: setGateways \$apiResource" >&2
exit 1
else
oIFS="$IFS";IFS=',';gateways="${APIGATEWAY[*]}";IFS="$oIFS"
jq -e '.gatewayEnvironments="'$gateways'"' <<<$1
fi
}
# checkGateways
# check if the gateways has been updated correctly
function checkGateways() {
local gateways
local apiResourceGateways
local oIFS
if [ -z "$1" ]
then
echo "Use: checkGateways \$apiResourceUpdated" >&2
exit 1
else
oIFS="$IFS";IFS=',';gateways="${APIGATEWAY[*]}";IFS="$oIFS"
apiResourceGateways=$(echo $1|jq -r '.gatewayEnvironments')
# Return value
if [ -z "$apiResourceGateways" ] || [ "$apiResrouceGateways" == "null" ]
then
return 1
fi
# TODO: The gateways are sorted in different manner (reverse as API Manager??)
#if [ "$gateways" != "$apiResourceGateways" ]
#then
# return 1
#fi
fi
return 0
}
# getParms
# Parse the parms and assign to variables
function getParms() {
local OPTIND=1
while getopts hu:p: opt $#
do
case $opt in
h)
showHelp
exit 0
;;
u)
APIUSER=$OPTARG
;;
p)
APIPASSWORD=$OPTARG
;;
s)
MANAGER_SERVICES_PORT=$OPTARG
;;
n)
MANAGER_NIOPT_PORT=$OPTARG
;;
*)
showHelp >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Discard the options and get parameter
APIMANAGER=$1
if [ "$APIMANAGER" == "" ]
then
echo "APIMANAGER host name is required"
showHelp >&2
exit 1
fi
shift 1
APINAME=$1
if [ "$APINAME" == "" ]
then
echo "API name to publish is required"
showHelp >&2
exit 1
fi
shift 1
APIVERSION=$1
if [ "$APIVERSION" == "" ]
then
echo "API version to publish is required"
showHelp >&2
exit 1
fi
shift 1
if [ "$1" == "" ]
then
echo "You must indicate 1 or more gateway to publish is required"
showHelp >&2
exit 1
else
local i=1
for arg in $#
do
APIGATEWAY[$i]="$1"
let i=(i+1)
shift 1
done
fi
}
###############################################################################
# Check required internal tools
if ! type -t jq >/dev/null
then
echo "jq not found. Install it, e.g. 'apt-get install jq'"
exit 2
fi
# Read and parse Parms. Request required values missing
getParms $#
if [ "$APIUSER" == "" ]
then
APIUSER=admin
read -p $'Publisher user: \e[31m['${APIUSER}$']\e[0m ' parm
APIUSER=${parm:-$APIUSER}
fi
if [ "$APIPASSWORD" == "" ]
then
APIPASSWORD=admin
read -sp $'Publisher password: \e[31m['${APIPASSWORD}$']\e[0m ' parm
APIPASSWORD=${parm:-$APIPASSWORD}
echo ""
fi
# TEST ONLY: Delete (show parameter values)
# echo "USER=$APIUSER"
# echo "PASSWORD=$APIPASSWORD"
# echo "APIMANAGER=$APIMANAGER"
# echo "APINAME=$APINAME"
# for GWY in ${!APIGATEWAY[#]}
# do
# echo "APIGATEWAY[$GWY]=${APIGATEWAY[$GWY]}"
# done
# Client registration
echo "Registering this script as a client application (rest_api_publisher)"
APIAUTH=$(echo -n $APIUSER:$APIPASSWORD|base64)
clientRegistration=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${APP_CLIENT_REGISTRATION}" \
-H "Authorization: Basic ${APIAUTH}" \
-H "Content-Type: application/json" \
-d #- <<-EOF
{
"callbackUrl": "www.google.lk",
"clientName": "rest_api_publisher",
"owner": "$APIUSER",
"grantType": "password refresh_token",
"saasApp": true
}
EOF
)
if [ "$clientRegistration" == "" ]
then
echo "ERROR: Empty answer from https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${APP_CLIENT_REGISTRATION}. Is APIMANAGER correct?" >&2
exit 3
fi
# Get Application Client Token
CLIENTTOKEN=$(getClientToken $clientRegistration)
if [ $? -ne 0 ]
then
echo $clientRegistration >&2
echo "ERROR: Cannot get ClientId/ClientSecret: Is user/password correct?" >&2
exit 4
fi
# TEST ONLY: Delete
# echo "CLIENTTOKEN=$CLIENTTOKEN"
echo "Aplication rest_api_publisher registered"
# Client Login for get Access Token (and Token Type) - View Scope
echo "Obtaining access token for API query (scope api_view)"
clientAPILoginView=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_NIOPT_PORT}${URI_TOKEN}" \
-H "Authorization: Basic ${CLIENTTOKEN}" \
-d "grant_type=password&username=${APIUSER}&password=${APIPASSWORD}&scope=${API_SCOPE_VIEW}"
)
ACCESSVIEWTOKEN=$(getAccessToken $clientAPILoginView) && ACCESSVIEWTOKENTYPE=$(getAccessTokenType $clientAPILoginView)
if [ $? -ne 0 ]
then
echo $clientAPILoginView >&2
echo "ERROR: Cannot get Access Token: Has the user '$APIUSER' in necesary role for scope ${API_SCOPE_VIEW}" >&2
exit 5
fi
# TEST ONLY: Delete
# echo "Access View Token=$ACCESSVIEWTOKEN"
# echo "Token View Type=$ACCESSVIEWTOKENTYPE"
# Client Login for get Access Token (and Token Type) - Publish Scope
echo "Obtaining access token for API publish (scope api_publish)"
clientAPILoginPublish=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_NIOPT_PORT}${URI_TOKEN}" \
-H "Authorization: Basic ${CLIENTTOKEN}" \
-d "grant_type=password&username=${APIUSER}&password=${APIPASSWORD}&scope=${API_SCOPE_PUBLISH}"
)
ACCESSPUBLISHTOKEN=$(getAccessToken $clientAPILoginPublish) && ACCESSPUBLISHTOKENTYPE=$(getAccessTokenType $clientAPILoginPublish)
if [ $? -ne 0 ]
then
echo $clientAPILoginPublish >&2
echo "ERROR: Cannot get Access Token: Has the user $APIUSER in necesary role for scope ${API_SCOPE_PUBLISH}" >&2
exit 5
fi
# TEST ONLY: Delete
# echo "Access Publish Token=$ACCESSPUBLISHTOKEN"
# echo "Token Publish Type=$ACCESSPUBLISHTOKENTYPE"
# Client Login for get Access Token (and Token Type) - Publish Scope
echo "Obtaining access token for API create (scope api_create)"
clientAPILoginCreate=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_NIOPT_PORT}${URI_TOKEN}" \
-H "Authorization: Basic ${CLIENTTOKEN}" \
-d "grant_type=password&username=${APIUSER}&password=${APIPASSWORD}&scope=${API_SCOPE_CREATE}"
)
ACCESSCREATETOKEN=$(getAccessToken $clientAPILoginCreate) && ACCESSCREATETOKENTYPE=$(getAccessTokenType $clientAPILoginCreate)
if [ $? -ne 0 ]
then
echo $clientAPILoginCreate|jq . >&2
echo "ERROR: Cannot get Access Token: Has the user $APIUSER in necesary role for scope ${API_SCOPE_CREATE}" >&2
exit 5
fi
# TEST ONLY: Delete
# echo "Access Create Token=$ACCESSCREATETOKEN"
# echo "Token Create Type=$ACCESSCREATETOKENTYPE"
echo "All tokens obtained"
# Get API info (exists?)
echo "Checking API with name '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'"
apiQuery=$(
curl -s "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}?query=name:$APINAME" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
# TEST ONLY: Delete
# echo "apiQuery=${apiQuery}"
APIID=$(getAPIId $apiQuery)
if [ $? -ne 0 ]
then
echo $apiQuery >&2
echo "ERROR: Cannot find an API ${APINAME} with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 6
fi
echo "API Found. APIID='$APIID'"
# Get availables gateways and validate gateways names
echo "Checking if requested gateways '${APIGATEWAY[#]}' are available in '${APIMANAGER}'"
apiEnvironments=$(
curl -s "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_ENVIRONMENTS}" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
# TEST ONLY: Delete
# echo "apiEnvironments=$apiEnvironments"
if ! validateGateways $apiEnvironments
then
echo "Valid gateways are:"
showGateways $apiEnvironments
exit 7
fi
echo "API required gateways checked"
# Get API detailed info
echo "Getting API detailed info of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'"
apiResource=$(
curl -s -S -f -X GET "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
if [ $? -ne 0 ]
then
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 8
fi
# TEST ONLY: Delete
# jq . <<<$apiResource
currentGatewayEnvironments=$(getAPIGatewayEnvironments "$apiResource") && currentStatus=$(getAPIStatus "$apiResource")
if [ $? -ne 0 ]
then
jq . <<<$apiResource >&2
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 8
fi
echo "API is currently configured for gateways: '${currentGatewayEnvironments}'"
echo "API is currently in status: '${currentStatus}'"
# Update API gateways info
apiResourceUpdated=$(setGateways "$apiResource")
if [ $? -ne 0 ]
then
echo $apiResourceUpdated | jq . >&2
echo "ERROR: Cannot update gateways in API resource" >&2
exit 9
fi
# TEST ONLY: Delete
jq . <<<$apiResouceUpdated >&2
# PENDING: Update also required information (e.g., Endpoints)
# Update gateways
echo "Updating API gateways of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}' to '${APIGATEWAY[#]}'"
apiResourceUpdatedResponse=$(
curl -s -S -f -X PUT "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Content-Type: application/json" \
-H "Authorization: ${ACCESSCREATETOKENTYPE} ${ACCESSCREATETOKEN}" \
-d "$apiResourceUpdated"
)
if [ $? -ne 0 ]
then
# Retry request to show error in console
curl -s -X PUT "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Content-Type: application/json" \
-H "Authorization: ${ACCESSCREATETOKENTYPE} ${ACCESSCREATETOKEN}" \
-d "$apiResourceUpdated"|jq .
echo "ERROR: Cannot update gateways in API resource. Check API for missing information (HTTP Endpoints, ...)" >&2
exit 10
fi
# TEST ONLY: Delete
# jq . <<<$apiResourceUpdatedResponse
if ! checkGateways "$apiResourceUpdatedResponse"
then
echo $apiResourceUpdated| jq . >&2
echo "ERROR: Error updating gateways in API resource" >&2
exit 9
fi
echo "API Updated"
# Publish
echo "Publishing '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}' "
apiResource=$(
curl -s -S -f -X POST "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_PUBLISH}${APIID}" \
-H "Authorization: ${ACCESSPUBLISHTOKENTYPE} ${ACCESSPUBLISHTOKEN}"
)
if [ $? -ne 0 ]
then
echo "ERROR: Publishing '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 10
fi
echo "API Published"
# Verify status and gateways
echo "Verify API detailed info of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'"
apiResource=$(
curl -s -S -f -X GET "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
if [ $? -ne 0 ]
then
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 11
fi
currentGatewayEnvironments=$(getAPIGatewayEnvironments "$apiResource") && currentStatus=$(getAPIStatus "$apiResource")
if [ $? -ne 0 ]
then
jq . <<<$apiResource >&2
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 12
fi
echo "API is now configured for gateways: '${currentGatewayEnvironments}'"
echo "API is now in status: '${currentStatus}'"