
    [                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddl	mZ dd	l	mZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ej,                   G d dej.                               Zy)z&Command to delete named configuration.    )absolute_import)division)unicode_literals)base)
completers)config)log)
properties)named_configs)properties_file)
console_io)resource_printer)universe_descriptorc                   `    e Zd ZdZdddZed        ZdedefdZ	dedd	fd
Z
dedefdZd Zy	)DeletezDeletes a named configuration.an            {description} You cannot delete a configuration that is active, even
          when overridden with the --configuration flag.  To delete the current
          active configuration, first `gcloud config configurations activate`
          another one.

          See `gcloud topic configurations` for an overview of named
          configurations.
          a;            To delete an existing configuration named `my-config`, run:

            $ {command} my-config

          To delete more than one configuration, run:

            $ {command} my-config1 my-config2

          To list existing configurations, run:

            $ gcloud config configurations list
          )DESCRIPTIONEXAMPLESc                 J    | j                  ddt        j                  d       y)zAdds args for this command.configuration_names+zNName of the configuration to delete. Cannot be currently active configuration.)nargs	completerhelpN)add_argumentr   NamedConfigCompleter)parsers    +lib/surface/config/configurations/delete.pyArgszDelete.Args?   s+     118      universe_domainreturnc                 6   t         j                  j                         }t        |j	                               D ]_  \  }}t
        j                  j                  ddt        j                  |j                  g      d      }|d   j                  d      |k(  s_ y y)zDetermines whether the universe domain is set in any other config.

    Args:
      universe_domain: The universe domain to check for in any other config.

    Returns:
      True if the universe domain is set in any other config, False otherwise.
    T
list_unsetinclude_hiddenr   only_file_contentscorer    F)r   ConfigurationStore
AllConfigssorteditemsr
   VALUES	AllValuesr   PropertiesFile	file_pathget)selfr    all_configs_user_configpropss         r   _UniverseDomainSetInAnyConfigz$Delete._UniverseDomainSetInAnyConfigL   s      22==?K !2!2!45;)))88$$% " * e 
v		,	-	@ 6 r   Nc                 r    t        j                         }| j                  |      s|j                  |       yy)zDeletes the universe descriptor if it is not used in any other config.

    Args:
      universe_domain: The universe domain of the descriptor to delete.
    N)r   UniverseDescriptorr6   "DeleteDescriptorFromUniverseDomain)r1   r    universe_descriptor_objs      r   _DeleteUniverseDescriptorz Delete._DeleteUniverseDescriptorc   s7     2DDF--o>@@
 ?r   config_namec                    t         j                  j                         }t        |j	                               D ]  \  }}|j
                  |k(  st        j                  j                  ddt        j                  |j                  g      d      }|d   j                  d      xs. t        j                  j                  j                  j                  c S  t        j                  j                  j                  j                  S )zReturns the universe domain of the given configuration.

    Args:
      config_name: The name of the configuration to get the universe domain of.

    Returns:
      The universe domain of the given configuration or the default if not
      found.
    Tr#   r'   r    )r   r(   r)   r*   r+   namer
   r,   r-   r   r.   r/   r0   r'   r    default)r1   r<   all_named_configsr3   r4   r5   s         r   _GetConfigurationUniverseDomainz&Delete._GetConfigurationUniverseDomaino   s     &88CCE !2!8!8!:;;			[	(!!+++::&&'  $ , 
 &M/0 >  %%55==	
 < !!11999r   c                    t         j                  j                         }|j                  |j                  v r.t        j
                  dj                  |j                              d}t        j                  |j                  |t        j                         t        j                  dd       |j                  D ]~  }| j                  |      }t         j                  j                  |       t        j                   |      }|j                          	 | j#                  |       t        j*                  |        y # t$        j&                  $ r!}t        j(                  d||       Y d }~Gd }~ww xY w)NzDeleting named configuration failed because configuration [{0}] is set as active.  Use `gcloud config configurations activate` to change the active configuration.z;list[title="The following configurations will be deleted:"])outT)r?   cancel_on_noz?Failed to delete universe descriptor for universe domain %s: %s)r   r(   ActiveConfigr>   r   NamedConfigErrorformatr   Printr	   statusr   PromptContinuerA   DeleteConfigr   GetConfigStorer;   r   UniverseDescriptorErrorwarningDeletedResource)r1   argsactive_configfmtconfiguration_namedelete_config_universe_domainconfig_store_to_deletees           r   Runz
Delete.Run   s<    "44AACMT555**::@&  ;  HC433ScjjId>"66&*&J&J
'# &&334FG%445GH))+
&&'DE 
,- 7 !88 
M)	
 	

s   D<<E0E++E0)__name__
__module____qualname____doc__detailed_helpstaticmethodr   strboolr6   r;   rA   rW    r   r   r   r   "   sh    &-2 
 
3 4 .
s 
t 
: : :6 .r   r   N)r[   
__future__r   r   r   googlecloudsdk.callioper   !googlecloudsdk.command_lib.configr   googlecloudsdk.corer   r	   r
   "googlecloudsdk.core.configurationsr   r   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   'googlecloudsdk.core.universe_descriptorr   UniverseCompatibleSilentCommandr   r`   r   r   <module>rk      s[     - &  ' ( 8 & # * < > 2 9 G G.T G. G.r   