A client of mine recently asked me something seemingly simple: how can I quickly check which of my namespaces are cloud optimized?
The built in way of going through the admin GUI does not scale very well beyond a handful of tenants. You have to jump into each tenant to see the namespaces and then navigate to the settings tab of each namespace. So at least a couple of clicks to find out if the namespace is cloud optimized or not. This client has many tenants with hundreds of namespaces so some serious clicking would be needed...
Fortunately there is a better way: automate this through the use of our management API (MAPI). Instead of clicking around we fire some requests against our API to gather the information, collect everything and export in some sensible format. The documentation for the MAPI can be easily accessed through the admin help in the HCP GUI itself.
I am by no means a capable programmer but I decided to cobble together a few lines of python to make the HCP admins life easier. The flow to get to the data is something like this:
- get the list of tenants
- get the list of namespaces for each tenant
- get the list of set options for each namespace
- aggregate and export as .csv
There were a couple of difficulties I had to solve with my limited programming skills:
- the number of fields returned are not the same for every namespace. This makes it difficult to produce a nice .csv with a single header row and properly aligned data fields. I ended up using a list of the fields to be included in the output.
- the MAPI returns nested JSON for some of the attributes, which again makes it hard to include in a table format. Here i used the json_normalize function from pandas.io.json to flatten the result.
The result looks something like this for a subset of namespace properties:
Instead of putting this into a .csv file a database could also be a good target. Together with a timestamp this then allows to capture configuration drift.
The script is attached below. Some things you need to adjust to adapt for your environment:
- make sure the management API is enabled
- swap out URLs and authorization tokens
- adjust output name and path
- enable certificate validation for your production environment
Feel free to use and modify. Also feel free to share back if you enhance it or spot mistakes.