Let's say you have multiple vRealize Network Insight instances. Maybe a staging instance for trying new searches, alerts, or other configuration - and a production instance. Or you're migrating from an on-premises instance to a vRealize Network Insight Cloud instance. Or you just want a backup of the hard work you put into curating the applications. As with many things; there's an API and script for that!

Python SDK & Examples

The Python SDK is a library generated automatically by the OpenAPI specification that vRealize Network Insight has baked in. In addition, it has a ton of example scripts that use the SDK for various things. From importing data sources, exporting flows, running searches, to, you guessed it - creating backups of the applications, including restoring them. You can find the Python SDK on GitHub: https://github.com/vmware/network-insight-sdk-python/ 

Getting the Python SDK

Before you get started, python is a prerequisite. It can be both Python 2 or 3. I'll be using git in the example output, but you can also download the code in a zip file via the GitHub page. There is also a requirements.txt file that's used to get the right python modules in place.

~/tmp# git clone https://github.com/vmware/network-insight-sdk-python.git
Cloning into 'network-insight-sdk-python'...
remote: Enumerating objects: 9387, done.
remote: Counting objects: 100% (4812/4812), done.
remote: Compressing objects: 100% (736/736), done.
remote: Total 9387 (delta 4651), reused 4151 (delta 4074), pack-reused 4575
Receiving objects: 100% (9387/9387), 2.07 MiB | 16.72 MiB/s, done.
Resolving deltas: 100% (8974/8974), done.
~/tmp# cd network-insight-sdk-python
~/tmp/network-insight-sdk-python# pip3 install -r requirements.txt

Creating an Application Backup

With the SDK in place, we can now run the script named application_backups.py. Let's look at the built-in help first, to see what parameters are available:

~/tmp/network-insight-sdk-python# export PYTHONPATH=~/tmp/network-insight-sdk-python/swagger_client-py2.7.egg 
~/tmp/network-insight-sdk-python# python3 application_backups.py --help
usage: application_backups.py [-h] [--deployment_type DEPLOYMENT_TYPE] [--platform_ip PLATFORM_IP] [--username USERNAME] [--password PASSWORD] [--domain_type DOMAIN_TYPE] [--domain_value DOMAIN_VALUE] [--get_vidm_client_id] [--vidm_token VIDM_TOKEN] [--api_token API_TOKEN]
[--cloud_location CLOUD_LOCATION] [--cloud_api_url CLOUD_API_URL] [--application_backup_yaml APPLICATION_BACKUP_YAML]
[--application_backup_action APPLICATION_BACKUP_ACTION]

optional arguments:
-h, --help show this help message and exit
--deployment_type DEPLOYMENT_TYPE Setup deployment type: onprem or vrnic
--platform_ip PLATFORM_IP IP address of vRNI platform. In case of cluster IP address of Platform-1
--username USERNAME user name for authentication
--password PASSWORD password for authentication
--domain_type DOMAIN_TYPE
  domain type for authentication: LOCAL or LDAP or VIDM
--domain_value DOMAIN_VALUE
  domain value for LDAP user: example.com
--get_vidm_client_id Get client-id for making user access-token request to vIDM
--vidm_token VIDM_TOKEN
  Provide vidm_token for authentication
--api_token API_TOKEN
  Provide VRNIC api token
--cloud_location CLOUD_LOCATION
  vRNI Cloud location (US, UK, JP, AU, or DE. default: US)
--cloud_api_url CLOUD_API_URL
  Optional customer vRNI Cloud API URL. Overrides the cloud_location parameter. Example: uk.api.mgmt.cloud.vmware.com
--application_backup_yaml APPLICATION_BACKUP_YAML
  Applications and tiers are saved in this csv
--application_backup_action APPLICATION_BACKUP_ACTION
  Action can be 'save' or 'restore'

The export command is needed once per terminal session and tells the example scripts where to find the SDK. Most of these parameters are self-explanatory, most of them are for authentication. Pick and choose which parameters you need for your vRNI instance, whether it be an on-premises or Cloud instance. I'll be using an on-premises instance in the examples. 

There are two parameters that are very interesting: --application_backup_yaml and --application_backup_action. The action can be save or restore, which determine whether it's a backup (save), or if you're restoring to vRNI from an existing backup file. Application_backup_yaml points to a YAML file that is stored on disk, which is written too when you run a save action, and read from when you run a restore action.

~/tmp/network-insight-sdk-python# cd examples
~/tmp/network-insight-sdk-python/examples# python3 application_backups.py --deployment_type onprem --platform_ip --username myusername --password mypassword --application_backup_yaml apps.yaml --application_backup_action save
2022-03-18 16:02:31,603 : INFO : Getting api client for IP
2022-03-18 16:02:33,238 : INFO : Getting application '3TierApp01'
2022-03-18 16:02:33,708 : INFO : Getting application '3TierApp02'
2022-03-18 16:02:34,163 : INFO : Getting application 'ACI 3TierApp01'
2022-03-18 16:02:53,699 : INFO : Storing Application/Tier info to apps.yaml

It'll rattle off all the applications that are in the source vRNI instance and write the output to apps.yaml. Let's take a quick look:

~/tmp/network-insight-sdk-python# head apps.yaml
- name: 3TierApp01
  no_of_tiers: 5
  - application:
    entity_id: 10000:561:5536827695520564987
    entity_type: Application
  entity_id: 10000:562:6383790434494135694
  entity_type: Tier

I won't go through the entire file structure, it holds all of the apps including their tiers and definitions. The best part is that it's text, so you can put it anywhere (including version control!).

Restoring from an Application Backup

If needed, the same script can be used to restore applications from a YAML file as well. It's just a matter of pointing to the right vRNI instance and changing the application_backup_action to restore. One thing to note is that the script will suffix the application name with -restored. I.e., 3TierApp01-restored. You can change that on line 60 of the application_backups.py script and changing this:

body = {"name": app['name'] + '-restored'}
to this:
body = {"name": app['name']}

If you're fine with the suffix (which can always be changed in vRNI afterwards), you can leave the script alone. Here's an example of a restore run:

~/tmp/network-insight-sdk-python/examples# python3 application_backups.py --deployment_type onprem --platform_ip --username myusername --password mypassword --application_backup_yaml apps.yaml --application_backup_action restore
2022-03-18 16:02:31,603 : INFO : Getting api client for IP
2022-03-18 16:02:33,238 : INFO : Restoring app '3TierApp01'
2022-03-18 16:02:33,238 : INFO :        Restoring tier 'App'
2022-03-18 16:02:33,238 : INFO :        Restoring tier 'Web'
2022-03-18 16:02:33,238 : INFO :        Restoring tier 'DB'
2022-03-18 16:02:33,708 : INFO : Restoring app '3TierApp02'

After it's done running, log into the destination vRealize Network Insight instance and behold your applications!

Share the wealth!