-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathgenerate_report.py
More file actions
165 lines (132 loc) · 5.57 KB
/
generate_report.py
File metadata and controls
165 lines (132 loc) · 5.57 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
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
"""
Use the Nutanix v4 API SDKs to demonstrate AI Ops report creation
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 uuid
import sys
from pprint import pprint
import urllib3
from rich import print
import ntnx_opsmgmt_py_client
from ntnx_opsmgmt_py_client import Configuration as OpsMgmtConfiguration
from ntnx_opsmgmt_py_client import ApiClient as OpsMgmtClient
from ntnx_opsmgmt_py_client.rest import ApiException as ReportingException
from ntnx_opsmgmt_py_client import Report
from ntnx_opsmgmt_py_client.models.opsmgmt.v4.config.ReportFormat import ReportFormat
from ntnx_opsmgmt_py_client.models.opsmgmt.v4.config.Recipient import Recipient
# small library that manages commonly-used tasks across these code samples
from tme.utils import Utils
from tme.apiclient import ApiClient
def main():
"""
suppress warnings about insecure connections
please consider the security implications before
doing this in a production environment
"""
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
utils = Utils()
script_config = utils.get_environment()
opsmgmt_config = OpsMgmtConfiguration()
for config in [
opsmgmt_config,
]:
config.host = script_config.pc_ip
config.username = script_config.pc_username
config.password = script_config.pc_password
config.verify_ssl = False
opsmgmt_client = OpsMgmtClient(configuration=opsmgmt_config)
for client in [
opsmgmt_client,
]:
client.add_default_header(
header_name="Accept-Encoding", header_value="gzip, deflate, br"
)
opsmgmt_instance = ntnx_opsmgmt_py_client.api.ReportConfigApi(api_client=opsmgmt_client)
default_start = "2025-01-01T00:00:00Z"
default_end = "2025-01-02T00:00:00Z"
# prompt for report start and end time
# include sensible defaults
print("You will now be prompted for the report start and end times.")
print("Press enter at each prompt if you want to accept the defaults.")
print(f"Default start time: {default_start}")
print(f"Default end time: {default_end}")
start_time = input("Enter the report start time in ISO-8601 format: ")
if not start_time:
start_time = default_start
print("Default start time used ...")
end_time = input("Enter the report end time in ISO-8601 format: ")
if not end_time:
end_time = default_end
print("Default end time used ...")
try:
print("This demo uses the Nutanix v4 API `opsmgmt` namespace's \
reporting APIs to create an AIOps report from an existing \
report configuration. You will now be prompted for some \
report-specific details.")
# get a list of existing report configurations
print("Building list of existing user-defined report configurations ...")
config_list = opsmgmt_instance.list_report_configs(
async_req=False, _filter="isSystemDefined eq null", _limit=100
)
if config_list.data:
print(f"{len(config_list.data)} user-defined report configurations found.")
else:
print("No report configurations found. Exiting ...")
sys.exit()
recipient_name = input("Enter the report recipient name: ")
recipient_email = input("Enter the report recipient email address: ")
user_configs = []
for report_config in config_list.data:
user_configs.append(
{"name": report_config.name, "ext_id": report_config.ext_id}
)
print("Available report configurations:")
pprint(user_configs)
config_ext_id = input("Enter the ext_id of the report configuration to use for this report: ")
report_name = f"sdk_new_report_{str(uuid.uuid4())}"
print("Report configuration will be as follows:")
print(f" Report name: {report_name}")
print(f" Config ext_id: {config_ext_id}")
print(f" Start time: {start_time}")
print(f" End time: {end_time}")
print(" Persistent: No")
print(f" Recipient: {recipient_name}, {recipient_email}")
print(" Report format: PDF")
PDF = ReportFormat.PDF
new_report = Report(
name=f"{report_name}",
description="report configuration created from v4 python SDK",
config_ext_id=config_ext_id,
start_time=start_time,
end_time=end_time,
is_persistant=False,
recipients=[
Recipient(email_address=recipient_email, recipient_name=recipient_name)
],
recipient_formats=[PDF],
)
print("Submitting report creation request ...")
opsmgmt_instance = ntnx_opsmgmt_py_client.api.ReportsApi(api_client=opsmgmt_client)
create_new_report = opsmgmt_instance.create_report(
async_req=False, body=new_report
)
reporting_ext_id = create_new_report.data.ext_id
utils.monitor_task(
task_ext_id=reporting_ext_id,
prefix="",
task_name="Report config creation",
pc_ip=script_config.pc_ip,
username=script_config.pc_username,
password=script_config.pc_password,
)
print("Report generated.")
except ReportingException as reporting_exception:
print(
f"Unable to authenticate using the supplied credentials. \
Check your username and/or password, then try again. \
Exception details: {reporting_exception}"
)
if __name__ == "__main__":
main()