Skip to content

Commit 77de230

Browse files
committed
Add more request handling exceptions
1 parent 6793664 commit 77de230

File tree

6 files changed

+212
-165
lines changed

6 files changed

+212
-165
lines changed

cli/network/virtual_network.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,46 @@ def populate_payload(vlanid, vswitchid, vswitchname):
2828
'''
2929

3030
def get_network_uuid(config, cookies, system_uuid):
31-
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/VirtualNetwork/quick/All"
32-
url = "https://" + util.get_host_address(config) + uri
33-
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml, type=VirtualNetwork"}
34-
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
35-
if response.status_code != 200:
36-
raise NetworkError(f"failed to list VLAN, error: {response.text}")
37-
uuid = ""
38-
network_name = util.get_vnetwork_name(config)
39-
for nw in response.json():
40-
if nw["NetworkName"] == network_name:
41-
uuid = nw["UUID"]
42-
break
31+
try:
32+
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/VirtualNetwork/quick/All"
33+
url = "https://" + util.get_host_address(config) + uri
34+
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml, type=VirtualNetwork"}
35+
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
36+
response.raise_for_status()
4337

44-
if "" == uuid:
45-
raise NetworkError(f"no virtual network available with name '{network_name}'")
46-
else:
47-
logger.debug(f"Network UUID for the virtual network {network_name}: {uuid}")
38+
uuid = ""
39+
network_name = util.get_vnetwork_name(config)
40+
for nw in response.json():
41+
if nw["NetworkName"] == network_name:
42+
uuid = nw["UUID"]
43+
break
44+
45+
if "" == uuid:
46+
raise NetworkError(f"no virtual network available with name '{network_name}'")
47+
else:
48+
logger.debug(f"Network UUID for the virtual network {network_name}: {uuid}")
49+
except requests.exceptions.RequestException as e:
50+
raise NetworkError(f"failed to list VLAN while making http request, error: {e}, response: {e.response.text}")
51+
except Exception as e:
52+
raise NetworkError(f"failed to list VLAN, error: {e}")
4853
return uuid
4954

5055
def get_vlan_details(config, cookies, system_uuid):
51-
nw_uuid = get_network_uuid(config, cookies, system_uuid)
52-
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/VirtualNetwork/{nw_uuid}"
53-
url = "https://" + util.get_host_address(config) + uri
54-
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml, type=VirtualNetwork"}
55-
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
56-
if response.status_code != 200:
57-
raise NetworkError(f"failed to get VLAN details, error: {response.text}")
58-
59-
soup = BeautifulSoup(response.text, 'xml')
60-
vlan_id = soup.find("NetworkVLANID")
61-
vswitch_id = soup.find("VswitchID")
62-
56+
try:
57+
nw_uuid = get_network_uuid(config, cookies, system_uuid)
58+
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/VirtualNetwork/{nw_uuid}"
59+
url = "https://" + util.get_host_address(config) + uri
60+
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml, type=VirtualNetwork"}
61+
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
62+
response.raise_for_status()
63+
64+
soup = BeautifulSoup(response.text, 'xml')
65+
vlan_id = soup.find("NetworkVLANID")
66+
vswitch_id = soup.find("VswitchID")
67+
except requests.exceptions.RequestException as e:
68+
raise NetworkError(f"failed to get VLAN details while making http request, error: {e}, response: {e.response.text}")
69+
except Exception as e:
70+
raise NetworkError(f"failed to get VLAN details, error: {e}")
6371
return vlan_id.text, vswitch_id.text
6472

6573
def check_network_adapter(config, cookies, partition_uuid, vlan_id, vswitch_id):

cli/partition/activation.py

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,37 @@ def shutdown_payload():
111111
"""
112112

113113
def get_lpar_profile_id(config, cookies, partition_uuid):
114-
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}/LogicalPartitionProfile"
115-
url = "https://" + util.get_host_address(config) + uri
116-
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartitionProfile"}
117-
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
118-
if response.status_code != 200:
119-
raise PartitionError(f"failed to get LPAR profile ID, error: {response.text}")
120-
soup = BeautifulSoup(response.text, 'xml')
121-
entry_node = soup.find('entry')
122-
lpar_profile_id = entry_node.find('id')
114+
try:
115+
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}/LogicalPartitionProfile"
116+
url = "https://" + util.get_host_address(config) + uri
117+
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartitionProfile"}
118+
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
119+
response.raise_for_status()
120+
121+
soup = BeautifulSoup(response.text, 'xml')
122+
entry_node = soup.find('entry')
123+
lpar_profile_id = entry_node.find('id')
124+
except requests.exceptions.RequestException as e:
125+
raise PartitionError(f"ffailed to get LPAR profile ID while making http request, error: {e}, response: {e.response.text}")
126+
except Exception as e:
127+
raise PartitionError(f"failed to get LPAR profile ID, error: {e}")
123128
return lpar_profile_id.text
124129

125130
def poll_job_status(config, cookies, job_id):
126-
uri = f"/rest/api/uom/jobs/{job_id}"
127-
url = "https://" + util.get_host_address(config) + uri
128-
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.web+xml; type=JobRequest"}
129-
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
130-
if response.status_code != 200:
131-
raise PartitionError(f"failed to get job completion, error: {response.text}")
132-
soup = BeautifulSoup(response.text, 'xml')
133-
status = soup.find("Status").text
131+
try:
132+
uri = f"/rest/api/uom/jobs/{job_id}"
133+
url = "https://" + util.get_host_address(config) + uri
134+
headers = {"x-api-key": util.get_session_key(config), "Content-Type": "application/vnd.ibm.powervm.web+xml; type=JobRequest"}
135+
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
136+
response.raise_for_status()
137+
138+
soup = BeautifulSoup(response.text, 'xml')
139+
status = soup.find("Status").text
140+
except requests.exceptions.RequestException as e:
141+
raise PartitionError(f"failed to get job completion while making http request, error: {e}, response: {e.response.text}")
142+
except Exception as e:
143+
raise PartitionError(f"failed to get job completion, error: {e}")
144+
134145
if status == "COMPLETED_OK":
135146
return True
136147
else:
@@ -156,39 +167,48 @@ def check_job_status(config, cookies, response):
156167
return False
157168

158169
def activate_partition(config, cookies, partition_uuid):
159-
# Check partition state,don't activate if its in 'running' state
160-
lpar_state = check_lpar_status(config, cookies, partition_uuid)
161-
if lpar_state == "running":
162-
logger.debug("Partition already in 'running' state, skipping activation")
163-
return
164-
165-
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}/do/PowerOn"
166-
url = "https://" + util.get_host_address(config) + uri
167-
lpar_profile_id = get_lpar_profile_id(config, cookies, partition_uuid)
168-
payload = populated_payload(lpar_profile_id)
169-
170-
headers = {"x-api-key": util.get_session_key(config), "Content-Type": CONTENT_TYPE}
171-
response = requests.put(url, headers=headers, cookies=cookies, data=payload, verify=False)
172-
if response.status_code != 200:
173-
raise PartitionError(f"failed to activate partition, error: {response.text}")
174-
# check job status for COMPLETED_OK
175-
status = check_job_status(config, cookies, response.text)
176-
if not status:
177-
raise PartitionError(f"failed to activate partition, activate job returned false")
178-
logger.debug("Partition activated successfully.")
170+
try:
171+
# Check partition state,don't activate if its in 'running' state
172+
lpar_state = check_lpar_status(config, cookies, partition_uuid)
173+
if lpar_state == "running":
174+
logger.debug("Partition already in 'running' state, skipping activation")
175+
return
176+
177+
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}/do/PowerOn"
178+
url = "https://" + util.get_host_address(config) + uri
179+
lpar_profile_id = get_lpar_profile_id(config, cookies, partition_uuid)
180+
payload = populated_payload(lpar_profile_id)
181+
182+
headers = {"x-api-key": util.get_session_key(config), "Content-Type": CONTENT_TYPE}
183+
response = requests.put(url, headers=headers, cookies=cookies, data=payload, verify=False)
184+
response.raise_for_status()
185+
186+
# check job status for COMPLETED_OK
187+
status = check_job_status(config, cookies, response.text)
188+
if not status:
189+
raise PartitionError(f"activate job returned false")
190+
logger.debug("Partition activated successfully.")
191+
except requests.exceptions.RequestException as e:
192+
raise PartitionError(f"failed to activate partition while making http request, error: {e}, response: {e.response.text}")
193+
except Exception as e:
194+
raise PartitionError(f"failed to activate partition, error: {e}")
179195
return
180196

181197
def check_lpar_status(config, cookies, partition_uuid):
182-
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}"
183-
url = "https://" + util.get_host_address(config) + uri
184-
headers = {"x-api-key": util.get_session_key(config)}
185-
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
186-
if response.status_code != 200:
187-
raise PartitionError(f'failed to get LPAR details for {partition_uuid}, error: {response.text}')
188-
soup = BeautifulSoup(response.text, 'xml')
189-
state = soup.find("PartitionState")
190-
if state == None:
191-
raise PartitionError(f"partition state of LPAR '{partition_uuid}' found to be None")
198+
try:
199+
uri = f"/rest/api/uom/LogicalPartition/{partition_uuid}"
200+
url = "https://" + util.get_host_address(config) + uri
201+
headers = {"x-api-key": util.get_session_key(config)}
202+
response = requests.get(url, headers=headers, cookies=cookies, verify=False)
203+
response.raise_for_status()
204+
soup = BeautifulSoup(response.text, 'xml')
205+
state = soup.find("PartitionState")
206+
if state == None:
207+
raise PartitionError(f"partition state of LPAR '{partition_uuid}' found to be None")
208+
except requests.exceptions.RequestException as e:
209+
raise PartitionError(f"failed to get LPAR details for '{partition_uuid}' while making http request, error: {e}, response: {e.response.text}")
210+
except Exception as e:
211+
raise PartitionError(f"failed to get LPAR details for '{partition_uuid}', error: {e}")
192212
return state.text
193213

194214
def shutdown_partition(config, cookies, partition_uuid):

cli/partition/partition.py

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,18 @@ def convert_gb_to_mb(value):
125125

126126

127127
def get_all_partitions(config, cookies, system_uuid):
128-
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition/quick/All"
129-
url = "https://" + util.get_host_address(config) + uri
130-
headers = {"x-api-key": util.get_session_key(config)}
131-
response = requests.get(url, headers=headers,
132-
cookies=cookies, verify=False)
133-
if response.status_code != 200:
134-
raise PartitionError(
135-
f"failed to get partition list, error: {response.text}")
136-
return response.json()
128+
try:
129+
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition/quick/All"
130+
url = "https://" + util.get_host_address(config) + uri
131+
headers = {"x-api-key": util.get_session_key(config)}
132+
response = requests.get(url, headers=headers,
133+
cookies=cookies, verify=False)
134+
response.raise_for_status()
135+
return response.json()
136+
except requests.exceptions.RequestException as e:
137+
raise PartitionError(f"failed to get partition list, while making http request, error: {e}, response: {e.response.text}")
138+
except Exception as e:
139+
raise PartitionError(f"failed to get partition list, error: {e}")
137140

138141

139142
# Checks if partition exists, returns exists and if partition is created by PIM
@@ -164,37 +167,43 @@ def check_partition_exists(config, cookies, system_uuid):
164167

165168

166169
def create_partition(config, cookies, system_uuid):
167-
logger.debug(
168-
f"Creating partition with name '{util.get_partition_name(config)}'")
169-
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition"
170-
url = "https://" + util.get_host_address(config) + uri
171-
payload = populate_payload(config)
172-
headers = {"x-api-key": util.get_session_key(config),
173-
"Content-Type": CONTENT_TYPE}
174-
response = requests.put(url, headers=headers,
175-
data=payload, cookies=cookies, verify=False)
176-
if response.status_code != 200:
177-
raise PartitionError(
178-
f"failed to create partition, error: {response.text}")
179-
180-
soup = BeautifulSoup(response.text, 'xml')
181-
partition_uuid = soup.find("PartitionUUID")
182-
return partition_uuid.text
170+
try:
171+
logger.debug(
172+
f"Creating partition with name '{util.get_partition_name(config)}'")
173+
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition"
174+
url = "https://" + util.get_host_address(config) + uri
175+
payload = populate_payload(config)
176+
headers = {"x-api-key": util.get_session_key(config),
177+
"Content-Type": CONTENT_TYPE}
178+
response = requests.put(url, headers=headers,
179+
data=payload, cookies=cookies, verify=False)
180+
response.raise_for_status()
181+
182+
soup = BeautifulSoup(response.text, 'xml')
183+
partition_uuid = soup.find("PartitionUUID")
184+
return partition_uuid.text
185+
except requests.exceptions.RequestException as e:
186+
raise PartitionError(f"failed to create partition, while making http request, error: {e}, response: {e.response.text}")
187+
except Exception as e:
188+
raise PartitionError(f"failed to create partition, error: {e}")
183189

184190

185191
def get_partition_details(config, cookies, system_uuid, partition_uuid):
186-
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition/{partition_uuid}"
187-
url = "https://" + util.get_host_address(config) + uri
188-
headers = {"x-api-key": util.get_session_key(config),
189-
"Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartition"}
190-
response = requests.get(url, headers=headers,
191-
cookies=cookies, verify=False)
192-
if response.status_code != 200:
193-
raise PartitionError(
194-
f"failed to get partition details, error: {response.text}")
195-
soup = BeautifulSoup(response.text, 'xml')
196-
lpar = str(soup.find('LogicalPartition'))
197-
return lpar
192+
try:
193+
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition/{partition_uuid}"
194+
url = "https://" + util.get_host_address(config) + uri
195+
headers = {"x-api-key": util.get_session_key(config),
196+
"Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartition"}
197+
response = requests.get(url, headers=headers,
198+
cookies=cookies, verify=False)
199+
response.raise_for_status()
200+
soup = BeautifulSoup(response.text, 'xml')
201+
lpar = str(soup.find('LogicalPartition'))
202+
return lpar
203+
except requests.exceptions.RequestException as e:
204+
raise PartitionError(f"failed to get partition details, while making http request, error: {e}, response: {e.response.text}")
205+
except Exception as e:
206+
raise PartitionError(f"failed to get partition details, error: {e}")
198207

199208

200209
def edit_lpar_compute(config, cookies, system_uuid, partition_uuid):
@@ -215,30 +224,33 @@ def edit_lpar_compute(config, cookies, system_uuid, partition_uuid):
215224
"Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartition"}
216225
response = requests.post(url, headers=headers, cookies=cookies, data=str(
217226
updated_lpar_payload), verify=False)
218-
if response.status_code != 200:
219-
raise PartitionError(
220-
f"failed to edit lpar compute, error: {response.text}")
227+
response.raise_for_status()
228+
except requests.exceptions.RequestException as e:
229+
raise PartitionError(f"failed to edit lpar compute, while making http request, error: {e}, response: {e.response.text}")
221230
except Exception as e:
222-
raise e
231+
raise Exception(f"failed to edit lpar compute, error: {e}")
223232
logger.debug(
224233
f"Compute for partition: {partition_uuid} is updated successfully")
225234
return
226235

227236

228237
def set_partition_boot_string(config, cookies, system_uuid, partition_uuid, partition_payload, boot_string):
229-
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition/{partition_uuid}"
230-
url = "https://" + util.get_host_address(config) + uri
231-
headers = {"x-api-key": util.get_session_key(config),
232-
"Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartition"}
233-
payload = BeautifulSoup(partition_payload, 'xml')
234-
pending_boot = payload.find("PendingBootString")
235-
pending_boot.append(boot_string)
236-
237-
response = requests.post(url, headers=headers,
238-
cookies=cookies, data=str(payload), verify=False)
239-
if response.status_code != 200:
240-
raise PartitionError(
241-
f"failed to update boot order for the partition: '{partition_uuid}', error: {response.text}")
238+
try:
239+
uri = f"/rest/api/uom/ManagedSystem/{system_uuid}/LogicalPartition/{partition_uuid}"
240+
url = "https://" + util.get_host_address(config) + uri
241+
headers = {"x-api-key": util.get_session_key(config),
242+
"Content-Type": "application/vnd.ibm.powervm.uom+xml; Type=LogicalPartition"}
243+
payload = BeautifulSoup(partition_payload, 'xml')
244+
pending_boot = payload.find("PendingBootString")
245+
pending_boot.append(boot_string)
246+
247+
response = requests.post(url, headers=headers,
248+
cookies=cookies, data=str(payload), verify=False)
249+
response.raise_for_status()
250+
except requests.exceptions.RequestException as e:
251+
raise PartitionError(f"failed to update boot order for the partition: '{partition_uuid}' while making http request, error: {e}, response: {e.response.text}")
252+
except Exception as e:
253+
raise Exception(f"failed to update boot order for the partition: '{partition_uuid}', error: {e}")
242254
logger.debug(
243255
f"Updated the boot order for the partition: '{partition_uuid}'")
244256
return

0 commit comments

Comments
 (0)