-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathlist_images.py
More file actions
131 lines (120 loc) · 4.31 KB
/
list_images.py
File metadata and controls
131 lines (120 loc) · 4.31 KB
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
"""
Use the Nutanix v4 REST APIs to request and parse a list of all available images
Requires Prism Central 7.5 or later and AOS 7.5 or later
Author: Chris Rasmussen, Senior Technical Marketing Engineer, Nutanix
Date: February 2026
"""
import requests
import urllib3
import getpass
import argparse
from base64 import b64encode
"""
suppress warnings about insecure connections
please consider the security implications before doing this in a production environment
"""
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
"""
setup the command line parameters
for this example only two parameters are required
- the Prism Central IP address or FQDN
- the Prism Central username; the script will prompt for the user's password
so that it never needs to be stored in plain text
"""
parser = argparse.ArgumentParser()
parser.add_argument("pc_ip", help="Prism Central IP address or FQDN")
parser.add_argument("username", help="Prism Central username")
args = parser.parse_args()
# get the cluster password
cluster_password = getpass.getpass(
prompt="Please enter your Prism Central \
password: ",
stream=None,
)
pc_ip = args.pc_ip
username = args.username
# make sure the user enters a password
if not cluster_password:
while not cluster_password:
print(
"Password cannot be empty. Please enter a password or Ctrl-C/Ctrl-D to exit."
)
cluster_password = getpass.getpass(
prompt="Please enter your Prism Central password: ", stream=None
)
try:
"""
setup the HTTP Basic Authorization header based on the
supplied username and password
"""
encoded_credentials = b64encode(
bytes(f"{username}:{cluster_password}", encoding="ascii")
).decode("ascii")
auth_header = f"Basic {encoded_credentials}"
# setup the URL that will be used for the API request
url_no_filter = f"https://{pc_ip}:9440/api/vmm/v4.2/content/images"
url_with_filter = f"https://{pc_ip}:9440/api/vmm/v4.2/content/images?$filter=startswith(name, 'x')"
"""
setup the request headers
note the use of {auth_header} i.e. the Basic Authorization
credentials we setup earlier
"""
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"{auth_header}",
"cache-control": "no-cache",
}
# submit the request without filters
try:
response = requests.request(
"GET", url_no_filter, headers=headers, verify=False, timeout=10
)
if response.ok:
# show a total count of images found
print(
f'{response.json()["metadata"]["totalAvailableResults"]} total images found.'
)
for image in response.json()["data"]:
print(f'- {image["name"]}')
else:
print(f"An error occurred while connecting to {pc_ip}.")
"""
the following line can be uncommented to show
detailed error information
"""
print(response.text)
except Exception as ex:
print(
f"An {type(ex).__name__} exception occurred while \
connecting to {pc_ip}.\nArgument: {ex.args}."
)
# submit the request with filters
try:
response = requests.request(
"GET", url_with_filter, headers=headers, verify=False, timeout=10
)
if response.ok:
# show a total count of images found
print(
f'\n{response.json()["metadata"]["totalAvailableResults"]} total images found, filtered by image name starting with "x".'
)
if response.json()["metadata"]["totalAvailableResults"] > 0:
for image in response.json()["data"]:
print(f'- {image["name"]}')
print("")
else:
print(f"An error occurred while connecting to {pc_ip}.")
"""
the following line can be uncommented to show
detailed error information
"""
print(response.text)
except Exception as ex:
print(
f"An {type(ex).__name__} exception occurred while \
connecting to {pc_ip}.\nArgument: {ex.args}."
)
# catching all exceptions like this should be generally be avoided
except Exception as e:
print(f"{e}")