Technical Articles
Run faster with SAP Cloud Platform’s new CLI-based JSON output
By adding the –format json parameter to the command like this sapcp –format json list accounts/subaccount, we’ll get the response in JSON format. Harder to read yes, but a treasure hiding in plain sight.
Let’s take it up one notch and use the JSON response for completing other tasks. In my example, I‘ll be using Microsoft Powershell and my favorite tool, jq, for parsing JSON, but any other tool will do.
If want to view all subaccounts GUIDs, I can have it in one line using :jq -r “.value[].guid“. The complete command would look like this:
sapcp –format json list accounts/subaccount | jq -r “.value[].guid”
Take note that with the “-r“ option in jq, if the filter’s result is a string then it‘ll be written directly to standard output rather than being formatted as a JSON string with quotes. If you want more info about this, you can read up about in the official jq documentation.
Now let’s work with what we have. One of the more tedious tasks that I often encounter in my role as a product owner at SAP, especially when preparing customer demos, is when I need to create multiple subaccounts in my global account and entitle all of them with the same service.
If I want to entitle one of my subaccounts with 1 unit of the “free” plan of the Audit Log Viewer service, this is pretty straightforward in the sapcp CLI. The command would look like something like this, with the subaccount‘s ID that I need to include in the command‘s syntax:
$subacconut = <subaccountGUID>
sapcp assign accounts/entitlement –to-subaccount $subacconut –for-service “auditlog-viewer” –plan free –amount 1
Easy enough for one subaccount, but what about if I have 30 subaccounts and I want to entitle all of them with the Audit Log Viewer service? There’s no way I am going to run this 30 times. By using jq and the JSON response from the sapcp –format json list accounts/subaccount command, I can do this in a single line!
foreach ($subaccount in sapcp –format json list accounts/subaccount | jq -r “.value[].guid”) {sapcp assign accounts/entitlement –to-subaccount $subaccount –for-service “auditlog-viewer” –plan free –amount 1}
Pretty cool, right?
As you can see there is a lot of power using JSON response and combining it with JSON slice and filter 3rd party tools.
That was just a small taste. Now it’s your turn to create your own queries, and if you find them useful feel free to share it with the entire community.
That’s all for today, keep on clouding…
This is already my fav post so far of SAP TechEd week 🙂
Great to see, can't wait to try it out!
May I request you add the 'terminaltip' user tag to this post too? Thanks in advance!
dj (see also https://twitter.com/qmacro/status/1336658376848191493)
Thanks, dj ! I am honored