How to pass JSON object as parameter in wsk (openwhisk) - openwhisk

I have OpenWhisk evironment in my laptop, setup through Vagrant. I have an action that accepts parameter, that is used to take JSON data. I followed the information given in the following URL for passing parameters.
https://console.bluemix.net/docs/openwhisk/parameters.html#working-with-parameters
Based on the information given in the above web site I executed the following command.
wsk action -i invoke addcoins -p coindata '{"name": "coin1", "price": "3" }'
When I execute this I am getting the following error:
error: Invalid argument(s): asfsds,, price:, 3, }'. An action name is required.
Run 'wsk --help' for usage.
I do not understand how should I pass an JSON object as parameter in wsk.

This is because of the single outer quotes when using the CLI from a Windows client. Try double quotes (and escaping the internal quotes). See https://github.com/apache/incubator-openwhisk/issues/1291.
wsk action -i invoke addcoins -p coindata "{\"name\": \"coin1\", \"price\": 3 }"

Related

AWS CLI on Windows won't work though using double quotes and escapes

I made an Amazon Event Bridge rule and I wanted to attach targets on it using AWS CLI.
I knew that when you use json string in aws cli on windows 10 command prompt, you need to
Use double quotes.(Single quotes are now allowed.)
Put back slashes before double quotes except for the first and the last one.
so, I call this on command prompt
aws events put-targets --rule xxxxxxxxxxxxxxxxxxxxxxxxx --targets "{\"Arn\":\"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance\",\"Input\":\"{\"InstanceId\":[\"i-xxxxxxxxxxxxxxxxx\"]}\",\"Id\":\"aaaaaaaa\",\"RoleArn\":\"arn:aws:iam::awsaccountid:role/some-ssm-role\"}"
but got error:
Error parsing parameter '--targets': Invalid JSON: Expecting ',' delimiter: line 1 column 104 (char 103)
JSON received: {"Arn":"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance","Input":"{"InstanceId":["i-xxxxxxxxxxxxxxxxx"]}","Id":"aaaaaaaa","RoleArn":"arn:aws:iam::awsaccountid:role/some-ssm-role"}
any suggestion?
p.s.
I made sure command below succeeded on Linux terminal.
aws events put-targets --rule xxxxxxxxxxxxxxxxxxxxxxxxx --targets '{"Arn": "arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StopEC2Instance","Input":"{\"InstanceId\":[\"i-xxxxxxxxxxxxxxxxx\"]}","Id":"aaaaaaaa","RoleArn":"arn:aws:iam::awsaccountid:role/some-ssm-role"}'
Could you try the following:
aws events put-targets --rule xxxxxxxxxxxxxxxxxxxxxxxxx --targets "{\"Arn\":\"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance\",\"Input\":\"{\\\"InstanceId\\\":[\\\"i-xxxxxxxxxxxxxxxxx\\\"]}\",\"Id\":\"aaaaaaaa\",\"RoleArn\":\"arn:aws:iam::awsaccountid:role/some-ssm-role\"}"
Or, same thing but marginally more readable this time with caret continuation markers:
aws events put-targets ^
--rule xxxxxxxxxxxxxxxxxxxxxxxxx ^
--targets "{\"Arn\":\"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance\",\"Input\":\"{\\\"InstanceId\\\":[\\\"i-xxxxxxxxxxxxxxxxx\\\"]}\",\"Id\":\"aaaaaaaa\",\"RoleArn\":\"arn:aws:iam::awsaccountid:role/some-ssm-role\"}"
The key thing to recognize is that the Input attribute in the JSON is itself a string containing JSON, so it needs to be double-escaped. This at least passes the initial format validation for me.

aws-cli won't work with a variable I created in bash

I'm trying to write a bash script for creating an AWS SQS queue and then check that the queue is empty. For that I created the following script that should return "null":
aws sqs create-queue --queue-name MessagesQueue
queue_url=$(aws sqs list-queues --query QueueUrls[0])
queue_check=$(aws sqs get_queue_attributes --queue_url $queue_url --attribute-names ApproximateNumberOfMessages --query Attributes[0].ApproximateNumberOfMessages
And I receive this error message instead:
An error occurred (InvalidAddress) when calling the GetQueueAttributes operation: The address "https://eu-west-3.queue.amazonaws.com/XXXXXXXXXXXX/MessagesQueue" is not valid for this endpoint.
But if I explicitly write the same address in the command instead of using $queue_url it works fine and returns 'null' as it should.
Why isn't it accepting $queue_url but accepts the same URL address if I explicitly write it?
Edit: It seems like the aws-cli command reads the variable $queue_url as the URL between single and double-quotes, and when I write it explicitly it reads the URL with no quotes so that's why it accepts it. How can I use the bash variable I created so the aws-cli reads it with no quotes?
I solved it, I just had to remove the quotes from the output and I did it like this:
queue_url=$(aws sqs list-queues --query QueueUrls[0])
queue_url="${queue_url%\"}"
queue_url="${queue_url#\"}"
This other stackoverflow question helped: Shell script - remove first and last quote (") from a variable
Once I tried to run your code, part by part (started with aws sqs list_queues
), I have noticed:
aws: error: argument operation: Invalid choice, valid choices are:
...
Invalid choice: 'list_queues', maybe you meant:
* list-queues
Try out list-queues instead list_queues and let know how it goes.

Test AWS lambda function via cli

I want to test a lambda function via CLI instead of the AWS Management console. (Looking to automate function testing by making a bash script)
I've read the documentation: http://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html
I am trying to invoke the lambda function with a json event payload. My bashcode looks like this:
#!/bin/bash
name="arn:aws:lambda:euwest1:100000000000:function:foo"
invoketype="Event"
payload="{'account':'100261334439', 'region':'eu-west-1', 'detail-type':'Scheduled Event', 'source':'aws.events', 'time':'2017-07-16T03:00:00Z', 'id':'178710aa-6871-11e7-b6ef-e9b95183cfc9', 'resources':['arn:aws:events:eu-west-1:100000000000:rule/run_everyday']}"
aws lambda invoke --function-name "$name" --invocation-type "$invoketype" --payload "$payload" testresult.log
I am getting the error:
An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unexpected character ('a'
(code 97)): was expecting double-quote to start field name at [Source: [B#7ac4c2fa; line: 1, column: 3]
I believe I am providing the payload in wrong format, but the documentation has no example, it just says that the datatype is 'blob'. I did some searching but only found info on BLOB (binary large object), but I'm pretty sure this is not what the documentation is referring to.
I have tried without the outer double quotes and replacing all the single quotes with double quotes, but all of these give the same error.
tl;dr
Correct (normally): $ aws lambda invoke --function-name myFunction --payload '{"key1":"value1"}' outputfile.txt
Correct (Windows): aws lambda invoke --function-name myFunction --payload "{""key1"": ""value1""}" outputfile.txt
OP's specific question is already answered correctly; this answer is for the benefit of those ending up here with slightly different circumstances from OP. What OS are you using? If its Windows, then you are likely pulling your hair out over single and double quote issues.
Windows command line does not understand single quotes, and double
quotes within a quoted string must be escaped as two double quotes
("").
Credit (normal): AWS documentation
Credit (Windows): Hail Reddit!
valid JSon should have key and value between double quotes
so you should have the payload attribute written as
payload='{"account":"100261334439", "region":"eu-west-1", "detail-type":"Scheduled Event", "source":"aws.events", "time":"2017-07-16T03:00:00Z", "id":"178710aa-6871-11e7-b6ef-e9b95183cfc9", "resources":["arn:aws:events:eu-west-1:100000000000:rule/run_everyday"]}'

variables inside CURL POST command using shell script and while loop

Hi I have to call curl POST method for a number of records, using bash script. For that i am using while loop.
Actual posting with hardcoded values:
curl -X POST -u user:pass --data '<automation><operation action="assemble" package="service Item"/></automation>' http://localhost:8080/form
When i try to use while loop for a set of different actions and different packages, the $packagename field is not working. When i use $packagename as a variable to package option of data section, it is not taking, since we must pass data in double quotes. (" ") It is always taking the same value which is in double quotes.
Could someone suggest a solution for this?
End the single quoted string and switch to double quotes around the variable.
curl -X POST -u user:pass --data '<automation><operation action="assemble" package="'"$packagename"'"/></automation>' http://localhost:8080/form

Shell script to accept "jenkins job name" and triggers that job form commandline(jobs differs in the name and count of parameters)

Hi I have written a shellscript which accepts the "jenkins job name" and starts triggering that build via command line , In my Jenkins server I have different jobs which differs in count and names of parameters.
$build_name= Jenkins_job_name ##(which is the only input)
java -jar jenkins-cli.jar -s http://mydomain:8080/ build $build_name -s -p PARA1=$paravalue
will work only if i am giving the exact name and count of the parameter which the job expects
I want to generalize this build triggering script, which fetches the names of parameter for a specific job,accepts it and then triggers the build
NB: When I try the above script with different parameter name ("KEY1" instead of "PARA1"), I am getting the message like:
'KEY1' is not a valid parameter. Did you mean "PARA1"?
You can use the get-job $build_name on the cli to get the job configuration
The parameters are under:
<parameterDefinitions>
<name>PARA1</name>
There will be multiple instances of <name>...</name>
Alternatively, you can make a call to
http://mydomain:8080/job/$build_name/api/json
The parameters will be in format:
"parameterDefinitions":[{..., "name":"PARA1", ...}, ...]
There will be multiple instances of {..., "name":"...", ...},
Parse the file to get all parameters defined for the job. The json format is either for shell since it is all on a single line. A RegEx can detect the start of parameterDefinitions and get the following name values, before the end of brackets.

Resources