Hello and welcome to the SAP HANA Academy.
The topic of this video tutorial series is SAP HANA Cockpit and in this video we will
show how you can work with Cockpit APIs.
This is part two, about POST API calls.
This video has been recorded on release SAP HANA cockpit 2.0 SP 07 from August 2018.
For earlier or later releases, please check the playlist on the SAP HANA Academy.
Hi, I am Denys van Kempen.
OK— in the previous video, we showed you have you can make GET API calls, to query
SAP HANA Cockpit for its resources, where to get the token for authentication and, purely
as an example, how you could use Postman to try them out, the API calls.
In this video, we will look at the POST calls, which will allow us to register resources,
create groups, create cockpit users, or get rid of them.
As mentioned, POST API calls are new for SP 07.
GET calls were introduced in SP 04.
Now, before we turn to the POST calls, let’s briefly show you an example where this is
being used.
Here, I am connected to the SAP Cloud Platform, SAP’s enterprise Platform-as-a-Service and
we are looking at the Cloud Platform Cockpit, another type of cockpit —it is more of a
portal—, and we are connected to the Cloud Foundry environment.
If you are not familiar with Cloud Platform or Cloud Foundry, no worries, that’s not
important here.
If we then open our space and go to service instances, you can see that we have a Hana-enterprise
service,This is an instance of the HANA-as-a-Service offering you can sign up for, standard or
enterprise.
From here we can open the Dashboard.
This will list the endpoint APIs to connect to cloud database instance but also — and
that’s what I would like to point out — we can open the SAP HANA cockpit from here.
It looks like an older version, but it is, in fact, a 2.7 release.
SAP HANA, if we scroll down a bit, is even SPS 04, which on-premise will not be released
until next year.
But I am getting sidetracked, the point is that when create an instance of HANA (as a
service), the Cockpit connection, the resource registration, will be configured for us automatically.
This is done with API POST calls.
And on the topic, here we are back again with Postman.
In the previous video, I showed you how to get the access token and we looked at a couple
of GET calls.
What I did not show, maybe most of you are already familiar with this, if you are familiar
with Postman you would be, is the Code link here.
If you select it, you get the actual HTTP call and using the selector here, you can
easily convert this into curl, or into Go, Java, NodeJS, Objective-C (maybe for a new
iOS app) or Python.
Very easy, so you can try it out with Postman and put into actual use using Generate Code
Snippets.
We will look at a Python example at the end of this video.
OK —Enough said.
Let’s run our first POST call.
You can look them up here, in the Administration guide for Cockpit: the APIs, and the input
parameters.
I am going to start with registration/SystemRegister.
Here we have the Body, the input parameters; quite a list.
This is JSON formatting, so it needs to be exactly like this —well, you can change
the order— we have the hostname, the instance number, the technical user, database name
and security settings.
If you have registered a resource before in the Cockpit Manager, this will be familiar
and if not, we have video tutorials on this topic.
OK —Send the request.
Status: 200 OK
Result returns —the output is also in JSON— the resource id and resource name.
If we then return to the Cockpit Manager, select Registered Resources, we now have resource
Beta registered.
In the details, we recognise the host, the database, the instance number, technical user,
and the security settings, encryptJDBC = False, so none are selected.
OK — Here we have our Resource Groups, currently only one: Academy with 4 resources (not include
Beta) and 4 users.
Here we have the POST API GroupCreate, and all we need to specify is the group name and,
optionally, a description.
Send the request.
200 OK
Result returns the ID, Name, Description and CreatedBy (some POST APIs are more talkative
than others).
OK - back to cockpit.
Refresh.
And here we have our Group.
The group does not have any resources or any users, obviously, as we did not specify any,
but, there’s an API for that: GroupResourceAdd.
We need to specify the group ID and the resource ID, we can get this from the output of our
previous POST calls.
Send
OK
Nothing is returned this time, so let’s double check in Cockpit Manager, refresh the
page, yes, resource Beta now has been added.
We can remove resources from groups in the UI, of course, here, but we could also do
this programmatically: ResourceUnregister, GroupDelete, etc.
We can also create Cockpit Users, this is done with the CockpitUserCreate API.
Specify username, password, email and role collections, this is an enumerator, so you
can specify multiple values.
Let’s verify our users first, so you don’t think I am photoshopping everything here,
yep, four users.
OK — Let’s add Benny.
Oops, 403 Forbidden.
Forgot to update my token.
They last for about 4 hours.
Let’s go back to authorization, and paste that back in.
OK - Send again, that’s better, status 200, with the result, cockpit ID 366
Back to Cockpit Manager.
Cockpit Users, reopen the view, and here we have Benny.
Benny does not have access to any resource groups yet, there are APIs for that as well,
of course, but for now, I’ll leave at that.
Let me also show CockpitUserDelete.
Here we need to specify username twice.
Why is that?
Well, Benny is not only added to Cockpit as a user, but there will also be an entry in
the UAA, for authentication and authorisation, with the different role collections at the
XS Advanced level.
So, you know.
OK — as promised, I would show you an API implementation in Python.
It will be available soon on the SAP Developer Center in the Tutorial section.
This is work in progress, so the final version may be a bit different.
Here we see how the base URL is constructed, the url from cockpit-adminui-svc.
We saw this in the previous video.
We will be prompted for it here, you could also automate this, of course.
It doesn’t change.
Here we get the service key, we also looked at that, with the client secret and client
ID.
This will allow us to get a token, which will then be used to populate the authorization
variable.
Here we have the available GET APIs, registeredResourcesGet and so on.
The POST calls, here we have systemRegister, with an IF/ELSE construct depending on whether
the database is multitenant or not.
You can also register resources via a port.
Plus all the other available POST calls.
And here we have the main section, where all the actual work happens.
So, how does this work?
Let’s switch to my Windows computer.
You need to install Python.
The command is python script name.
We are getting prompted for the cockpit hostname and port for the cockpit-adminui-svc URL.
Username and password.
I am prompted for the servicekey.json file which contains the token (this could also
be automated) and then we need to specify whether we want to GET or POST.
Let’s GET.
OK - here we have the available APIs.
Let’s pick number 2: GroupsForUserGet, and here we have the output, in JSON, of the two
groups we can access: Academy and Test, the one we just created.
Awesome.
You might find this very useful when you want to explore working with the Cockpit APIs.
Excellent, well that’s it for now.
Thanks for watching.
You can find more video tutorials on our YouTube channel.
If you would like to be informed about new video tutorials, please subscribe to our channel.
You can connect with us on LinkedIn or follow us on Twitter, as well for updates, and if
you are watching this video on YouTube, do not hesitate to leave your comments to the
video page and, if you like, give us your vote on this video.
Thank you for watching.