Delete the Oldest Snapshot on a VMstore

Happy New Year,

For the beginning of the New Year, I have coded an REST API example in Python.  This example queries for the oldest user generated snapshot and removes it.  This example shows how to use a query string with the GET snapshot API invoke, and a DELETE snapshot.  What is cool about this example is that there are functions that encapsulate the requests Python modules calls that hopefully will be reusable.  I have attached the example, and will go through some of the code below.

First off, you will need these Python modules:

import requests
import json
import sys
from datetime import datetime

Let’s look at the login function.  A user name and password is all that is needed over HTTPS.

def api_login(server_name, user_name, password):
    # Payload, header and URL for login call
    headers = {'content-type': 'application/json'}
    payload = {'username': user_name,   # payload needed for post.
               'password': password,
               'typeId': 'com.tintri.api.rest.vcommon.dto.rbac.RestApiCredentials'}   # 'typeId' is required.
    url_login = 'https://'+ server_name + '/api/v310/session/login'

    try:
        # Invoke the login API.
        r = requests.post(url_login, data=json.dumps(payload),
        headers=headers, verify=False)
    except requests.ConnectionError:
        print_error("Login: API Connection error occurred")
        sys.exit(-2)
    except requests.HTTPError:
        print_error("Login: HTTP error occurred")
        sys.exit(-3)
    except requests.Timeout:
        print_error("Login: Request timed out")
        sys.exit(-4)
    except:
        print_error("Login: An unexpected error " + sys.exc_info()[0] +
                    " occurred")
    sys.exit(-5)

    # if HTTP Response is not 200 then raise an exception
    if r.status_code != 200:
        print_error("The HTTP response for login call to the server " +
                    server_name + " is not 200, but is: " + str(r.status_code))
        print_error("url = " + url_login)
        print_error("payload = " + str(payload))
        print_error("response: " + r.text)
        sys.exit(-6)

    session_id = r.cookies['JSESSIONID']   # Session ID is return for subsequent invokes.
    return session_id

The info API does not require user name, password, or session ID.

r = api_get(server_name, '/api/info')
json_info = r.json()

The query below requests that the response returns the oldest user generated snapshot.

# Create filter to get the oldest user generated snapshot
q_filter = {'queryType': 'TOP_DOCS_BY_TIME',
'limit': '1', # There can only be one
'type': 'USER_GENERATED_SNAPSHOT'}

# Get the oldest user generated snapshot
r = api_get_query(server_name, '/api/v310/snapshot', q_filter, session_id)

This example could be used for the basis of a snapshot delete program.  Additions could be added to the query string, like VM name or before this time.

The code example is here: delete_snapshot.

Regards,

– Rick –

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s