I'm building a component that uses Azure REST APIs to delete VMs, it was working fine before but now it's throwing an error 'charmap' codec can't decode byte 0x9d in position 5928: character maps to -undefined>
Not sure if this comes from my code or from a configuration of pycharm, the last seems more possible since I have changed the code multiple times and get the same error. Postman throws a 400 Bad Request Error when I call the endpoint. This is what I have written:
@app.route("/host/v1/vm/<hostname>", methods=["DELETE"])
@xlogger.func_logger("cloudx_vp_service", "execute_delete")
def execute_delete(hostname, **kwargs):
try:
execute_delete_db = ExecuteDeleteDB(hostname=hostname)
response = execute_delete_db.get_vm_details(hostname=hostname)
response = make_response(jsonify(response), http.HTTPStatus.OK)
except ValueError as e:
log.error(str("There's a Value error"))
response = make_response(jsonify(str(e.args[0])), http.HTTPStatus.BAD_REQUEST)
This call the class in another class:
class ExecuteDeleteDB:
"""Execute VM Delete"""
@func_config("cloudx_vpcx_service")
@xlogger.set_logger("cloudx_vpcx_service", "executing_vm_deletion")
def __init__(self, hostname, **kwargs):
"""This method loads the configuration settings"""
self.config = kwargs["config"]
self.subscription_id = self.config["execute_delete"]["subscription_id"]
self.resource_group = self.config["execute_delete"]["resource_group"]
self.vm_name = hostname
self.scope = self.config["execute_delete"]["scope"]
self.headers = {"content-type": "application/json"}
self.get_vm_url = self.config["execute_delete"]["get_vm_url"]
self.get_locks_list_url = self.config["execute_delete"]["get_locks_list_url"]
self.delete_locks_url = self.config["execute_delete"]["delete_locks_url"]
self.get_disk_list_url = self.config["execute_delete"]["get_disks_list_url"]
self.get_network_interface_url = self.config["execute_delete"]["get_network_interface_url"]
self.delete_vm_url = self.config["execute_delete"]["delete_vm_url"]
self.delete_disk_url = self.config["execute_delete"]["delete_disks_url"]
self.delete_network_interface_url = self.config["execute_delete"]["delete_network_interface_url"]
# logger setting
self.log = self.logger
def get_vm_details(self, hostname):
"""
This method is to fetch the details of the VM, network interfaces and disks.
:return: None
"""
log = xlogger.getChildLogger(self.log, "vpcx_execute_delete_db.get_vm_details")
event = "get_vm_details"
self.log.info(f"Event Details: Initiated {event}")
try:
azr_tkn = self.azr_auth_token()
self.headers["authorization"] = f"Bearer {azr_tkn}"
get_vm_url = self.get_vm_url.format(self.subscription_id, self.resource_group, hostname)
response = requests.get(url=get_vm_url, headers=self.headers)
event = {"event": str(event),
"event_details": f"Fetching VMs"}
self.log.info(f"{event}")
if response.status_code == http.HTTPStatus.ACCEPTED or http.HTTPStatus.OK:
return response.json()
event = {"event": str(event),
"event_details": f"VM fetched successfully"}
self.log.info(f"{event}")
data_disks_list = response.json()["properties"]["storageProfile"]["dataDisks"]
if data_disks_list:
self.data_disks = []
event = {"event": str(event),
"event_details": f"Fetching VM data disks"}
self.log.info(f"{event}")
for data_disk in data_disks_list:
self.data_disks.append(data_disk["name"])
network_interface_list = response.json()["properties"]["networkProfile"]["networkInterfaces"]
if network_interface_list:
self.network_interface_list = []
event = {"event": str(event),
"event_details": f"Fetching VM Network Interface"}
self.log.info(f"{event}")
for network in network_interface_list:
self.network_interface_list.append(network["id"].split("/")[8])
os_disk_list = response.json()["properties"]["storageProfile"]["osDisk"]
if os_disk_list["managedDisk"]:
event = {"event": str(event),
"event_details": f"Fetching VM managed OS disk"}
self.log.info(f"{event}")
self.managed_os_disk = os_disk_list["name"]
else:
log_msg = f"Failed to fetch list of VMs under that Subscription_ID \n {response.content}"
log.info(log_msg)
event = {"event": str(event), "event_details": log_msg}
self.log.info(f"{event}")
raise Exception(response.content)
except Exception as error:
log.exception(f'Exception occured while fetching VM : {str(error)}')
msg = f"Exception occured while searching for specific VM : {str(error)}"
raise Exception(msg)
Terminal shows the following issue:
2022-09-08 07:52:57,978 - vpcx_apis.execute_delete(line:282) - MainProcess(20512):Thread-2(13424) - execute_delete - ERROR -There's a Value error:
This line that's fails refers to:
execute_delete_db = ExecuteDeleteDB(hostname=hostname)