-
Notifications
You must be signed in to change notification settings - Fork 19
/
via_httplib.py
169 lines (136 loc) · 4.35 KB
/
via_httplib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
#
# Copyright (c) 2018 by Delphix. All rights reserved.
#
from __future__ import print_function
import argparse
import httplib
import json
import os
import sys
import urllib
from argparse import RawTextHelpFormatter
SCRIPT_DESCRIPTION = """
Connect to Delphix engine to run some queries using the http lib library
"""
# globals used by helper functions
dlpx_host = ""
dlpx_user = ""
dlpx_password = ""
dlpx_cookie = None
major = 1 # API Major version number
minor = 6 # API Minor version number
micro = 0 # API micro version number
def main():
global dlpx_host
global dlpx_user
global dlpx_password
global dlpx_cookie
# parse args and print usage message if necessary
parser = argparse.ArgumentParser(
description=SCRIPT_DESCRIPTION, formatter_class=RawTextHelpFormatter
)
parser.add_argument("dlpxHost", help="The target Delphix Engine.", type=str)
parser.add_argument(
"dlpxUser",
help="The username to use to log into the Delphix Engine.",
type=str,
nargs="?",
default="delphix_admin",
)
parser.add_argument(
"dlpxPassword",
help="The password to use to log into the Delphix Engine.",
type=str,
nargs="?",
default="delphix",
)
args = parser.parse_args()
# save args to variables with shorter names
dlpx_host = args.dlpxHost
dlpx_user = args.dlpxUser
dlpx_password = args.dlpxPassword
api_version = {"type": "APIVersion", "major": major, "minor": minor, "micro": micro}
# log into the Delphix Engine in order to set cookie
print("Logging into " + dlpx_host + "...")
log_into_dlpx_engine(api_version)
print("SUCCESS - Logged in as " + dlpx_user)
response = dlpx_get("delphix/user")
for item in response["result"]:
print(item["name"])
# exit with success
sys.exit(0)
def check_response(response):
if response.status is not 200:
sys.stderr.write(
"ERROR: Expected a response of HTTP status 200 (Success) but received something different.\n"
)
sys.stderr.write("Response status: " + str(response.status) + "\n")
sys.stderr.write("Response reason: " + response.reason + "\n")
sys.exit(1)
def dlpx_post_json(resource, payload):
global dlpx_host
global dlpx_user
global dlpx_password
global dlpx_cookie
# encode payload for request
data = json.dumps(payload)
# form http header, add cookie if one has been set
headers = {"Content-type": "application/json"}
if dlpx_cookie is not None:
headers["Cookie"] = dlpx_cookie
# issue request
h = httplib.HTTPConnection(dlpx_host)
h.request("POST", "/resources/json/" + resource, data, headers)
r = h.getresponse()
check_response(r)
# save cookie if one was received
if r.getheader("set-cookie", None) is not None:
dlpx_cookie = r.getheader("set-cookie")
# return response as parsed json
r_payload = r.read()
return json.loads(r_payload)
def dlpx_get(resource, payload=None):
global dlpx_host
global dlpx_user
global dlpx_password
global dlpx_cookie
if payload:
# encode payload for request
data = json.dumps(payload)
else:
data = None
# form http header, add cookie if one has been set
headers = {"Content-type": "application/json"}
if dlpx_cookie is not None:
headers["Cookie"] = dlpx_cookie
# issue request
h = httplib.HTTPConnection(dlpx_host)
h.request("GET", "/resources/json/" + resource, data, headers)
r = h.getresponse()
check_response(r)
# save cookie if one was received
if r.getheader("set-cookie", None) is not None:
dlpx_cookie = r.getheader("set-cookie")
# return response as parsed json
r_payload = r.read()
return json.loads(r_payload)
def log_into_dlpx_engine(api_version):
dlpx_post_json(
"delphix/session",
{
"type": "APISession",
"version": {
"type": "APIVersion",
"major": api_version["major"],
"minor": api_version["minor"],
"micro": api_version["micro"],
},
},
)
dlpx_post_json(
"delphix/login",
{"type": "LoginRequest", "username": dlpx_user, "password": dlpx_password},
)
if __name__ == "__main__":
main()