- 
                Notifications
    
You must be signed in to change notification settings  - Fork 3
 
Create and email VAC tenants report
In this example app, we will create and email a VAC tenants report.
To get started either git clone the project and cd into the examples/vac-tenant-email example or follow the steps from the Home page of the Wiki to get your project setup.
To use the endpoint of getting VAC tenants we must have the company id and location that they belong to.
We can see that we need the company id and location id from the documentation on the endpoint here.
So how do we get the company id and location?
One way is to look at iland's console URL, ie. http://console.iland.com/#/company/123456789/backup/location/dal22.ilandcloud.com/dashboard in which the company id would be 123456789 and the location id would be dal22.ilandcloud.com.
The other way is to get the user's inventory and look for the entity of type VCC_BACKUP_LOCATION
We can do that easily with this code:
inventory = api.get('/users/%s/inventory' % USERNAME)['inventory']
for item in inventory:
    vac_locations = item['entities']['VCC_BACKUP_LOCATION']
After doing this we will have a list of VAC backup location entities that have UUIDs that contain the company id and the location.
All we have to do now is split the UUID from the VAC backup location entity and get the relevant information that we want.
Now that we have the company id and location for the VAC tenants we want for this endpoint /companies/{companyId}/location/{location}/vac-companies we can get the VAC tenants.
From the documentation, we see that we get an array of VAC tenants returned.
    vac_tenants = api.get(
        '/companies/%s/location/%s/vac-companies' % (company_id, location_id))['data']
So now all we have to do is parse the information that we want and create a CSV report from the data.
    tenant_report = open('vac_tenants.csv', 'w+')
    tenant_report.write(
        'name, allocated storage (GB), storage used (GB), used storage %, '
        'last active, last result, VMs protected, agents protected, insider '
        'protection')
    for tenant in vac_tenants:
        if tenant['name'] in tenants:
            # Prevent a divide by 0
            if tenant['total_storage_quota'] == 0:
                used_percentage = 0
            else:
                used_percentage = tenant['used_storage_quota'] / tenant[
                    'total_storage_quota']
            tenant_report.write('\n%s, %s, %s, %s, %s, %s,%s, %s, %s' % (
                tenant['name'], tenant['total_storage_quota'],
                tenant['used_storage_quota'], used_percentage,
                tenant['last_active'],
                tenant['last_result'], tenant['vms_backed_up'],
                tenant['agent_and_sub_tenant_count'],
                tenant['backup_protection_enabled']))
Above is just basic Python I/O and a for loop to loop through the tenants we just got adding the CSV rows with the data we want.
To email the report just use your favorite Python email library and attach the file we just created.
I followed this guide here to use my local SMTP server to send an email with an attached file but feel free to use whatever Python email library that you are familiar with.