# Buggy code
def validate_deleted_vault_or_hsm_name(cmd, ns):
    """
    Validate a deleted vault name; populate or validate location and resource_group_name
    """
    from azure.cli.core.profiles import ResourceType
    from msrestazure.tools import parse_resource_id

    vault_name = getattr(ns, "vault_name", None)
    hsm_name = getattr(ns, "hsm_name", None)

    if not vault_name and not hsm_name:
        raise CLIError("Please specify --vault-name or --hsm-name.")

    if vault_name:
        resource_name = vault_name
    else:
        resource_name = hsm_name
    resource = None

    if vault_name:
        client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_KEYVAULT).vaults
    else:
        client = get_mgmt_service_client(
            cmd.cli_ctx, ResourceType.MGMT_KEYVAULT
        ).managed_hsms

    # if the location is specified, use get_deleted rather than list_deleted
    if ns.location:
        resource = client.get_deleted(resource_name, ns.location)
        if vault_name:
            id_comps = parse_resource_id(resource.properties.vault_id)
        else:
            id_comps = parse_resource_id(resource.properties.id)

    # otherwise, iterate through deleted vaults to find one with a matching name
    else:
        for v in client.list_deleted():
            if vault_name:
                id_comps = parse_resource_id(v.properties.vault_id)
            else:
                id_comps = parse_resource_id(v.properties.id)
            if id_comps["name"].lower() == resource_name.lower():
                resource = v
                ns.location = resource.properties.location
                break

    # if the vault was not found, throw an error
    if not resource:
        raise CLIError("No deleted Vault or HSM was found with name " + resource_name)

    if "keyvault purge" not in cmd.name:
        setattr(
            ns,
            "resource_group_name",
            getattr(ns, "resource_group_name", None) or id_comps["resource_group"],
        )

        # resource_group_name must match the resource group of the deleted vault
        if id_comps["resource_group"] != ns.resource_group_name:
            raise CLIError(
                "The specified resource group does not match that of the deleted vault or hsm %s. The vault "
                "or hsm must be recovered to the original resource group %s."
                % (vault_name, id_comps["resource_group"])
            )


# Traceback (AttributeError)
'ManagedHsmsOperations' object has no attribute 'list_deleted'
Traceback (most recent call last):
python3.6/site-packages/knack/cli.py, ln 215, in invoke
cmd_result = self.invocation.execute(args)
cli/core/commands/__init__.py, ln 654, in execute
raise ex
cli/core/commands/__init__.py, ln 718, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
cli/core/commands/__init__.py, ln 711, in _run_job
six.reraise(*sys.exc_info())
...
cli/core/commands/__init__.py, ln 325, in __call__
return self.handler(*args, **kwargs)
azure/cli/core/__init__.py, ln 784, in default_command_handler
return op(**command_args)
cli/command_modules/keyvault/custom.py, ln 250, in list_deleted_vault_or_hsm
return hsm_client.list_deleted()
AttributeError: 'ManagedHsmsOperations' object has no attribute 'list_deleted'

# Fixed code
def validate_deleted_vault_or_hsm_name(cmd, ns):
    """
    Validate a deleted vault name; populate or validate location and resource_group_name
    """
    from azure.cli.core.profiles import ResourceType
    from msrestazure.tools import parse_resource_id

    vault_name = getattr(ns, "vault_name", None)
    hsm_name = getattr(ns, "hsm_name", None)

    if hsm_name:
        raise InvalidArgumentValueError(
            'Operation "purge" has not been supported for HSM.'
        )

    if not vault_name and not hsm_name:
        raise CLIError("Please specify --vault-name or --hsm-name.")

    if vault_name:
        resource_name = vault_name
    else:
        resource_name = hsm_name
    resource = None

    if vault_name:
        client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_KEYVAULT).vaults
    else:
        client = get_mgmt_service_client(
            cmd.cli_ctx, ResourceType.MGMT_KEYVAULT
        ).managed_hsms

    # if the location is specified, use get_deleted rather than list_deleted
    if ns.location:
        resource = client.get_deleted(resource_name, ns.location)
        if vault_name:
            id_comps = parse_resource_id(resource.properties.vault_id)
        else:
            id_comps = parse_resource_id(resource.properties.id)

    # otherwise, iterate through deleted vaults to find one with a matching name
    else:
        for v in client.list_deleted():
            if vault_name:
                id_comps = parse_resource_id(v.properties.vault_id)
            else:
                id_comps = parse_resource_id(v.properties.id)
            if id_comps["name"].lower() == resource_name.lower():
                resource = v
                ns.location = resource.properties.location
                break

    # if the vault was not found, throw an error
    if not resource:
        raise CLIError("No deleted Vault or HSM was found with name " + resource_name)

    if "keyvault purge" not in cmd.name:
        setattr(
            ns,
            "resource_group_name",
            getattr(ns, "resource_group_name", None) or id_comps["resource_group"],
        )

        # resource_group_name must match the resource group of the deleted vault
        if id_comps["resource_group"] != ns.resource_group_name:
            raise CLIError(
                "The specified resource group does not match that of the deleted vault or hsm %s. The vault "
                "or hsm must be recovered to the original resource group %s."
                % (vault_name, id_comps["resource_group"])
            )