Repository: Azure/azure-cli-extensions

Issue:
Setting registry for container app results in python error
After creating Container App we now get a python error when trying to set the registry with a system assigned identity. 
We have been using the same script for a few months, but we are suddenly getting this error with any changes to this part of our deploy script. 

**Command Name**
`az containerapp registry set
Extension Name: containerapp. Version: 0.3.16.`

**Errors:**
```
The command failed with an unexpected error. Here is the traceback:
'NoneType' object is not iterable
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 718, in _run_job
  File "C:\Users\DennisPresent\.azure\cliextensions\containerapp\azext_containerapp\_client_factory.py", line 28, in _polish_bad_errors
    raise ex
  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\DennisPresent\.azure\cliextensions\containerapp\azext_containerapp\custom.py", line 2119, in set_registry
    for r in registries_def:
TypeError: 'NoneType' object is not iterable

```

## To Reproduce:
Steps to reproduce the behavior. Note that argument values have been redacted, as they may contain sensitive information.

- _Put any pre-requisite steps here..._
- `az containerapp registry set --resource-group {} --name {} --server {} --identity {}`

## Expected Behavior

## Environment Summary
```
Windows-10-10.0.22621-SP0
Python 3.10.8
Installer: MSI

azure-cli 2.43.0

Extensions:
containerapp 0.3.16
kusto 0.5.0

Dependencies:
msal 1.20.0
azure-mgmt-resource 21.1.0b1
```
## Additional Context

<!--Please don't remove this:-->
<!--auto-generated-->




Discussion:
route to CXP team
@ValdoBE Thanks for reaching out to us and reporting this issue. We are looking into this issue and we will provide an update.
This is blocking us as well -- we are seeing the same exact behavior
Blocker for me as well. 

Resolving patch:
diff --git a/src/containerapp/azext_containerapp/_utils.py b/src/containerapp/azext_containerapp/_utils.py
--- a/src/containerapp/azext_containerapp/_utils.py
+++ b/src/containerapp/azext_containerapp/_utils.py
@@ -1059,7 +1059,9 @@ def safe_get(model, *keys, default=None):
     if not model:
         return default
     for k in keys[:-1]:
-        model = model.get(k, {})
+        model = model.get(k)
+        if model is None:
+            return default
     value = model.get(keys[-1], default)
     return default if not value else value
 
diff --git a/src/containerapp/azext_containerapp/custom.py b/src/containerapp/azext_containerapp/custom.py
--- a/src/containerapp/azext_containerapp/custom.py
+++ b/src/containerapp/azext_containerapp/custom.py
@@ -2093,13 +2093,10 @@ def set_registry(cmd, name, resource_group_name, server, username=None, password
 
     _get_existing_secrets(cmd, resource_group_name, name, containerapp_def)
 
-    registries_def = None
     registry = None
 
-    if "registries" not in containerapp_def["properties"]["configuration"]:
-        containerapp_def["properties"]["configuration"]["registries"] = []
-
-    registries_def = containerapp_def["properties"]["configuration"]["registries"]
+    registries_def = safe_get(containerapp_def, "properties", "configuration", "registries", default=[])
+    containerapp_def["properties"]["configuration"]["registries"] = registries_def
 
     if (not username or not password) and not identity:
         # If registry is Azure Container Registry, we can try inferring credentials
diff --git a/src/containerapp/setup.py b/src/containerapp/setup.py
--- a/src/containerapp/setup.py
+++ b/src/containerapp/setup.py
@@ -17,7 +17,7 @@
 # TODO: Confirm this is the right version number you want and it matches your
 # HISTORY.rst entry.
 
-VERSION = '0.3.17'
+VERSION = '0.3.18'
 
 # The full list of classifiers is available at
 # https://pypi.python.org/pypi?%3Aaction=list_classifiers


Test patch: