@@ -449,6 +449,92 @@ def list_vaults(client, resource_group_name=None):
449449 return client .list_by_subscription_id ()
450450
451451
452+ def list_deleted_vaults (cmd , client , resource_group_name = None , location = None ):
453+ """List soft-deleted Recovery Services vaults."""
454+ subscription_id = get_subscription_id (cmd .cli_ctx )
455+ return client .list_by_subscription_id (subscription_id , location = location )
456+
457+
458+ def undelete_vault (cmd , client , vault_name , resource_group_name = None , location = None , vault_id = None ):
459+ """Restore a soft-deleted Recovery Services vault."""
460+ # Parse vault ID if provided
461+ if vault_id :
462+ if not is_valid_resource_id (vault_id ):
463+ raise InvalidArgumentValueError ("Invalid vault ID format." )
464+
465+ # Parse resource ID to extract subscription, resource group, and vault name
466+ vault_id_parts = vault_id .split ('/' )
467+ if len (vault_id_parts ) < 9 :
468+ raise InvalidArgumentValueError ("Invalid vault ID format." )
469+
470+ parsed_subscription_id = vault_id_parts [2 ]
471+ parsed_resource_group = vault_id_parts [4 ]
472+ parsed_vault_name = vault_id_parts [8 ]
473+
474+ # Override parameters if vault_id is provided
475+ resource_group_name = parsed_resource_group
476+ vault_name = parsed_vault_name
477+
478+ # Validate subscription ID matches current context
479+ current_subscription_id = get_subscription_id (cmd .cli_ctx )
480+ if parsed_subscription_id != current_subscription_id :
481+ raise InvalidArgumentValueError (
482+ f"Vault ID subscription ({ parsed_subscription_id } ) does not match current subscription ({ current_subscription_id } )."
483+ )
484+
485+ if not vault_name or not resource_group_name :
486+ raise RequiredArgumentMissingError ("Either provide vault-name and resource-group-name, or a valid vault-id." )
487+
488+ if not location :
489+ # If location not provided, try to get it from the deleted vault list
490+ subscription_id = get_subscription_id (cmd .cli_ctx )
491+ deleted_vaults = client .list_by_subscription_id (subscription_id )
492+
493+ for deleted_vault in deleted_vaults :
494+ if (deleted_vault .name == vault_name and
495+ deleted_vault .id and resource_group_name in deleted_vault .id ):
496+ location = deleted_vault .location
497+ break
498+
499+ if not location :
500+ raise RequiredArgumentMissingError (
501+ "Location is required. Could not determine location from deleted vault list."
502+ )
503+
504+ return client .begin_undelete (resource_group_name , vault_name , location = location )
505+
506+
507+ def list_deleted_vault_containers (cmd , vault_name , resource_group_name ):
508+ """List backup containers in a soft-deleted vault using Azure Resource Graph."""
509+ from azure .mgmt .resourcegraph import ResourceGraphClient
510+ from azure .mgmt .resourcegraph .models import QueryRequest
511+
512+ subscription_id = get_subscription_id (cmd .cli_ctx )
513+ vault_resource_id = f"/subscriptions/{ subscription_id } /resourceGroups/{ resource_group_name } /providers/Microsoft.RecoveryServices/vaults/{ vault_name } "
514+
515+ # Create Resource Graph client
516+ resource_graph_client = get_mgmt_service_client (cmd .cli_ctx , ResourceGraphClient )
517+
518+ # Construct the query to find backup containers in the deleted vault
519+ query = f"""
520+ recoveryservicesresources
521+ | where type == "microsoft.recoveryservices/vaults/backupfabrics/protectioncontainers"
522+ | where id startswith "{ vault_resource_id } "
523+ | project id, name, type, properties, location
524+ """
525+
526+ query_request = QueryRequest (
527+ subscriptions = [subscription_id ],
528+ query = query
529+ )
530+
531+ try :
532+ response = resource_graph_client .resources (query_request )
533+ return response .data
534+ except Exception as ex :
535+ raise CLIError (f"Failed to query backup containers: { str (ex )} " )
536+
537+
452538def assign_identity (client , resource_group_name , vault_name , system_assigned = None , user_assigned = None ):
453539 vault_details = client .get (resource_group_name , vault_name )
454540
0 commit comments