Skip to Content
Technical Articles
Author's profile photo Florian Pfeffer

Start your SAP HANA Cloud Trial instance with an iOS shortcut

Motivation

As many of you I have on my SAP Business Technology Platform Trial account an instance of the SAP HANA Cloud Trial running. This trial instance is stopped everyday. Sometimes I am a little bit “angry” when I switch to my private workplace – enthusiastic to try out some stuff on SAP HANA Cloud Trial – and recognize that the instance was shut down. I have to manually start the instance by doing a lot of navigation steps in the SAP BTP cockpit to finally find the instance start button.

Of course I could avoid that “issue” with a simple bash script, but that would be to simple, right?

So I tried to start my SAP HANA Cloud Trial instance using an iOS shortcut. Target goal was to make a simple “Hey Siri. Start SAP HANA Cloud Trial” request to my iPhone when I wake up at morning. And yes that works.

Following I will describe the necessary steps. See it like some kind of “Weekend Fun” implementation without any productive value :-).

Pre-Conditions

  • You must have a SAP BTP Trial Account with a SAP HANA Cloud Trial instance.
  • You must own e.g. an iPhone or an iPad.
  • On your iOS/iPadOS device the Shortcuts app must be installed (if not available, you can find it in the Apple App Store; it is an official Apple app).

Create the shortcut

The shortcut uses Cloud Foundry REST API endpoints to get a necessary access token and to start the SAP HANA Cloud Trial instance. In each of the following steps I will describe the relevant requests. They can be tried out using the REST Client for Visual Studio Code (you can transform the requests easily to a curl command if you like that more).

Create a new shortcut

In the Shortcuts app press the “+” button to create a new shortcut. Name it e.g. “Start SAP HANA Cloud Trial”. With the three dots on the short cut tile you can edit the short cut actions.

Get an access token for the Cloud Foundry environment

The first action is to get an access token for the Cloud Foundry endpoint. The login endpoint for my SAP BTP, CF environment is https://login.cf.eu10.hana.ondemand.com. So I request an access token from that endpoint like following (the username and the password are the credentials you are using to logon to the SAP BTP cockpit or which you are using for the CF CLI tooling):

POST https://login.cf.eu10.hana.ondemand.com/oauth/token
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Authorization: Basic Y2Y6

grant_type=password
&password=<password>
&username=<username>

In the shortcut first the URL needs to be set.

Then with the action Get contents of URL the request needs to be executed.

After the request is executed, from the reponse the access_token is extracted using the action Get dictionary value.

Determine the service instance guid for the SAP HANA Cloud Trial instance

To be able to start the SAP HANA Cloud instance using a REST API call, the service instance guid is necessary. I did not integrate the determination in the shortcut itself, because it will not change so often. Following request determines the service instances from your Cloud Foundry account. Execute it and find the guid for your SAP HANA Cloud Trial instance based on the name in the response.

GET https://api.cf.eu10.hana.ondemand.com/v3/service_instances
Authorization: Bearer <your_access_token>

Start the SAP HANA Cloud Trial instance

To start the SAP HANA Cloud Trial instance several actions are necessary.

First the relevant URL is set. Keep in mind to replace the guid in the URL with your own service instance guid.

Then with a text action the JSON body is defined which will be sent to the URL. With the serviceStopped: false setting it is said that the instance must be started (if not already started).

With a PATCH request on the defined service instance URL the instance will be started. As authorizration header the Bearer access token is passed which was extracted before with the Get Dictionary value action. The request body must be set as “File” with a “Magic Variable” pointing to the text action which contains the JSON body.

The request (for execution in VS Code REST Client) looks like following:

PATCH https://api.cf.eu10.hana.ondemand.com/v3/service_instances/<service_instance_guid>
Authorization: Bearer <your_access_token>
Content-Type: application/json

{
    "parameters": {
        "data": {
            "serviceStopped": false
        }
    }
}

Confirmation of instance starting with a speak action

To not let the shortcut end without any feedback, a speak action responds with the text “SAP HANA Cloud Trial starting”.

Complete shortcut

In summary the complete shortcut has 8 actions:

Test the shortcut

Before the shortcut is executed I check that my SAP HANA Cloud Trial instance is stopped:

Then I start the instance using my iPad (or iPhone) by activating Siri and saying “Hey Siri. Start SAP HANA Cloud Trial”. It can be done too by pressing manually the relevant shortcut tile in the Shortcuts app.

The shortcut is executed and Siri responds with “SAP HANA Cloud Trial starting”.

The status of the instance is immediately switching to “Starting” (I promise I have not pressed the start button manually in the cockpit :-)).

Final Words

I hope you had some fun reading and trying out that “weekend fun” implementation. Of course there is room for improvement, e.g. checking and reacting on error response codes; extending the solution to accept an input parameter to steer if the instance should be started or stopped. But I think this little example shows that with the usage of the Cloud Foundry REST API you can do a lot of (fun) stuff. And not just related to the SAP HANA Cloud Trial instance.

 

Assigned tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Maxime Simon
      Maxime Simon

      Fun & well-explained use case of Cloud Foundry APIs !
      Thank you ^^

      Author's profile photo Gregor Wolf
      Gregor Wolf

      Thank you Florian for sharing this option. For some users the CLI approach documented in Start and Stop an Instance Using the CLI might be also interresting.

      Author's profile photo Craig Cmehil
      Craig Cmehil

      Really interesting read, I'm a huge fan and user of Shortcuts and with the latest iOS 15 and next Mac OS release this could be even nicer to have! Great job!