network_security_rules
access_control_policies
availability_zones
batch
blackouts
categories
clusters
reports
directory_service
hosts
idempotence_identifiers
identity_providers
images
network_function_chains
alerts
ngt_policies
permissions
projects
protection_rules
recovery_plan_jobs
recovery_plans
roles
subnets
tasks
user_groups
users
routing_policies
vms
Models
Powered by Stoplight

AUTHENTICATION

1. Basic Authentication : The user provides user-id and password every time a request is send as the auth-header.

2. Session Authentication : The user credentials are stored in a cookie.

class TestRestApi():

    def __init__(self, ip_addr, username, password):
        # Initialise the options.
        self.ip_addr = ip_addr
        self.username = username
        self.password = password
        self.rest_params_init()

    # Initialize REST API parameters
    def rest_params_init(self, sub_url="", method="",
                         body=None, content_type="application/json"):
        self.sub_url = sub_url
        self.body = body
        self.method = method
        self.content_type = content_type

    # Create a REST client session.
    def rest_call(self):
        base_url = 'https://%s:9440/api/nutanix/v3/%s' % (
            self.ip_addr, self.sub_url)
        if self.body and self.content_type == "application/json":
            self.body = json.dumps(self.body)
        request = urllib2.Request(base_url, data=self.body)
        base64string = base64.encodestring(
            '%s:%s' %
            (self.username, self.password)).replace(
            '\n', '')
        request.add_header("Authorization", "Basic %s" % base64string)

        request.add_header(
            'Content-Type',
            '%s; charset=utf-8' %
            self.content_type)
        request.get_method = lambda: self.method

        try:
            if sys.version_info >= (2, 7, 9):
                ssl_context = ssl._create_unverified_context()
                response = urllib2.urlopen(request, context=ssl_context)
            else:
                response = urllib2.urlopen(request)
            result = response.read()
            if result:
                result = json.loads(result)
            return response.code, result
        except urllib2.HTTPError as e:
            err_result = e.read()
            if err_result:
                try:
                    err_result = json.loads(err_result)
                except:
                    print "Error: %s" % e
                    return "408", None
            return "408", err_result
        except Exception as e:
            print "Error: %s" % e
            return "408", None