Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
In today's blog post, I want to introduce a faster way to complete tasks using the CLI for SAP Cloud Platform (sapcp CLI).

For all Cloud Management service commands in the sapcp CLI (those related to working with global accounts, directories, subaccounts, applications, entitlements, and environment instances), we've now added a new way to get the command response in JSON format, instead of the standard text output.

Now, you might be asking yourself why is this such as big deal. Well, you can leverage this ability to make use of more powerful scripting possibilities and perform your regular account administration tasks in SAP Cloud Platform, such as setting subaccounts and managing entitlements, even faster and with less code.

Let's begin...

First, let’s take a look at how to turn on JSON output. Here, I want to start with viewing the details of my subaccounts with the sapcp list accounts/subaccount command. See how the response is standard text-based format.

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 tooljqfor 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 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...