
                            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,                   ej.                  ej0                  j2                  ej0                  j4                  ej0                  j6                         G d dej8                                      Zy)z-The gcloud datastore indexes cleanup command.    )absolute_import)division)unicode_literals)yaml_parsing)	constants)	index_api)base)
exceptions)parser_arguments)output_helpers)flags)
properties)
console_ioc                       e Zd ZdZddddZedej                  ddfd	       Zdd
Z	de
de
ddfdZdej                  de
de
ddfdZy)Cleanupz Cleanup Cloud Datastore indexes.zHRemove unused datastore indexes based on your local index configuration.z
This command removes unused datastore indexes based on your local index
configuration. Any indexes that exist that are not in the index file will be
removed.
      z          To remove unused indexes based on your local configuration, run:

            $ {command} ~/myapp/index.yaml
          )briefDESCRIPTIONEXAMPLESparserreturnNc                 X    t        j                  |        t        j                  |        y)zsGet arguments for this command.

    Args:
      parser: argparse.ArgumentParser, the parser for this command.
    N)r   AddIndexFileFlagAddDatabaseIdFlag)r   s    (lib/surface/datastore/indexes/cleanup.pyArgszCleanup.Args9   s      
6"	F#    c                     |j                   r|j                   nt        j                  }| j                  |j                  |       y)z@Deletes indexes that have been removed from the index.yaml file.)
index_filedatabaseN)r   r   DEFAULT_NAMESPACEr   r   )selfargsdatabase_ids      r   RunzCleanup.RunC   s4     I,G,G  	LLDOOkLBr   r   r   c                    t         j                  j                  j                  j	                  d      }t
        j                  j                  |      }|r'|j                  t
        j                  j                  k7  rt        j                  dd      t        j                  ||g       t        j                  ddd       | j!                  |||       y)	z?Delete indexes that have been removed from the index.yaml file.T)requiredr   z5You must provide the path to a valid index.yaml file.)projectconfigsF)defaultthrow_if_unattendedcancel_on_noN)r   VALUEScorer'   Getr   ConfigYamlInfoFromFilenameINDEXr
   InvalidArgumentExceptionr    DisplayProposedConfigDeploymentsr   PromptContinue"PerformApiCleanupViaFirestoreAdmin)r!   r   r   r'   infos        r   r   zCleanup.CleanupK   s    $$,,00$0?G&&//
;D499 ; ; A AA//
O  33$ %d 	++D'8Dr   r7   r'   c                    t        j                  |j                  j                  xs g       }t	               }t        j
                  ||      }|D ]j  \  }}t        j                  |      }	|	|v r dj                  |j                               }
d}t        j                  |
|d      sZ|j                  |       l t        j                  |||       y)a  Cleans up removed indexes via the Firestore Admin API.

    Lists the database's existing indexes, and then compares them against the
    indexes that are defined in the given index.yaml file. Any discrepancies
    against the index.yaml file are deleted.

    Args:
      info: The parsed index.yaml information.
      project: The project we are operating on.
      database: The database within the project we are operating on.
    z<This index is no longer defined in your index.yaml file.
{0}z Do you want to delete this indexT)r)   N)r   NormalizeIndexesForFirestoreApiparsedindexesset#ListDatastoreIndexesViaFirestoreApiNormalizeIndexForFirestoreApiformatToYAMLr   r5   addDeleteIndexesViaFirestoreApi)r!   r7   r'   r   received_indexesindexes_to_delete_idscurrent_indexesindex_idindexnormalized_indexmsgprompts               r   r6   z*Cleanup.PerformApiCleanupViaFirestoreAdmin[   s     	11$++2E2E2KL   ECCO +% #@@G	-	-
I6%,,.! 
 2f		"	"3	=!!(+ + **0r   )r   N)__name__
__module____qualname____doc__detailed_helpstaticmethodr   ArgumentInterceptorr   r$   strr   r   r/   r6    r   r   r   r   !   s    
 )

-" $#77 $D $ $CE Es Et E $--$8;$GJ$$r   r   N)rN   
__future__r   r   r   googlecloudsdk.api_lib.appr    googlecloudsdk.api_lib.datastorer   r   googlecloudsdk.callioper	   r
   r   googlecloudsdk.command_lib.appr   $googlecloudsdk.command_lib.datastorer   googlecloudsdk.corer   googlecloudsdk.core.consoler   DefaultUniverseOnlyReleaseTracksReleaseTrackALPHABETAGACommandr   rS   r   r   <module>rc      s    4 &  ' 3 6 6 ( . 4 9 6 * 2 T..33T5F5F5I5IZdll Z Zr   