Azure/azure-iot-cli-extension

[bug] az iot hub state export fails with unexpected error, traceback: 'resourceGroup'

Agular opened this issue · 3 comments

Describe the bug
Running the command az iot hub state export ends up with an error in one of the azure-iot providers python script file, state.py .

Error returned from command:

Command group 'iot hub state' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Saving ADM configurations and Edge Deployments: 100%|##############################################################################################| 18/18 [00:00<?, ?it/s]
Saving devices and modules: 100%|############################################################################################################| 2/2 [00:04<00:00,  2.38s/it]
The command failed with an unexpected error. Here is the traceback:
'resourceGroup'
Traceback (most recent call last):
  File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke
  File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 663, in execute
  File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 726, in _run_jobs_serially
  File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 697, in _run_job
  File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 333, in __call__
  File "D:\a\_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py", line 121, in handler
  File "C:\Users\{redacted}\.azure\cliextensions\azure-iot\azext_iot\iothub\commands_state.py", line 29, in state_export
    sp.save_state(state_file, replace, hub_aspects)
  File "C:\Users\{redacted}\.azure\cliextensions\azure-iot\azext_iot\iothub\providers\state.py", line 102, in save_state
    hub_state = self.process_hub_to_dict(self.target, hub_aspects)
  File "C:\Users\{redacted}\.azure\cliextensions\azure-iot\azext_iot\iothub\providers\state.py", line 252, in process_hub_to_dict
    self.check_controlplane(hub_resource=hub_resource)
  File "C:\Users\{redacted}\.azure\cliextensions\azure-iot\azext_iot\iothub\providers\state.py", line 701, in check_controlplane
    ep["resourceGroup"],
KeyError: 'resourceGroup'

I've traced this back to how the ARM template is fetched via az group export on line 249 in the state.py file.
On line 692, the eventHub endpoints are looped over and the script tries to access the resourceGroup property of the eventHub.

The problem is that the eventHub does not contain a resourceGroup property in the response and therefore fails.
I've checked the event hub in question in Azure portal and I see that it has a resource group just fine in the UI

So either the az group export returns incomplete data about the Iot Hub, the script needs to be fixed or creation of the Event Hub went wrong / was buggy.

To Reproduce
Steps to reproduce the behavior:

az iot hub state export --hub-name myHubName --state-file myHub.json

Expected behavior
Iot Hub is correctly exported

Screenshots
image

Environment (please complete the following information):

  • OS: [Windows 10 Enterprise]
  • Shell: cmd
  • Az CLI version: 2.52.0
  • IoT extension version: 0.21.5
  • Python version (if pip installed): 3.10.10

I managed to recreate the issue by creating an event hub endpoint with no resource group or subscription id - meaning that the endpoint was created not using the CLI or the portal (both auto populate the fields). This does create some broken behavior (unable to update the endpoint properties in portal).

In this scenario, the state command should drop this eventhub since there is no simple way to check if the event hub still exists and retrieve the most recent connection string (due to the missing resource group). We plan to add some custom error handling flags in the future to let you ignore these kinds of errors and keep these kinds of endpoints.

I will double check the other endpoint types for the same issue.

@vilit1 So I checked also and noticed that our EventHub was created via Terraform and saw that our script did not fill in the resource group which is optional there. Fixed it in our script, updated the EventHub and now the iot hub state export succeeded.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub#resource_group_name

Alright - I made a pr to make state a bit more robust in these kinds of scenarios: #684

I will close this issue since you resolved it but if you have any other questions (or issues), feel free to create a new issue