
    V                         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ZdZ e       ZdZddZd Zd Zd ZddZy)z8Contains utilities to support the `gcloud init` command.    )absolute_import)division)unicode_literals)projects_api)projects_util)
operations)
usage_text)util)log)	resources)
console_ioNzEnter a Project ID. Note that a Project ID CANNOT be changed later.
Project IDs must be 6-30 characters (lowercase ASCII, digits, or
hyphens) in length and start with a lowercase letter.    c                     	 t        j                  |       }t        |D cg c]  }|j                   c}      S c c}w # t        $ r3}t        j                  dt        j                  |             Y d}~yd}~ww xY w)zReturns a list of project IDs the current user can list.

  Args:
    limit: int, the maximum number of project ids to return.

  Returns:
    list of str, project IDs, or None (if the command fails).
  limitz%Listing available projects failed: %sN)	r   Listsorted	projectId	Exceptionr   warningsix	text_type)r   projectsprojecterrs       +lib/googlecloudsdk/command_lib/init_util.py_GetProjectIdsr   ,   se      u-HH=H7$$H=>>=	 KK7s9KLs%   A  ;A  A   	A<	)A77A<c                     t        j                  |       }	 t        j                  |      }t	        j
                  |      S # t        $ r Y yw xY w)NF)projects_command_utilParseProjectr   Getr   IsActiver   )
project_idproject_refr   s      r   _IsExistingProjectr%   =   sL    %22:>+{+G!!'**	  s   )A 	AAc                    | t        j                  d      xs dS | st        j                  dd      syt        S |rSt        j                  g dd      }|y|d	k(  rt        j
                  t        d
dd      S |dk(  rt        S t               } t        j                  | ddgz   ddt        j                               }|y|t        |       k(  rt        j
                  t        d
dd      S |t        |       dz   k(  rt        S | |   S )a  Prompt the user for a project ID, based on the list of available IDs.

  Also allows an option to create a project.

  Args:
    project_ids: list of str or None, the project IDs to prompt for. If this
      value is None, the listing was unsuccessful and we prompt the user
      free-form (and do not validate the input). If it's empty, we offer to
      create a project for the user.
    limit_exceeded: bool, whether or not the project list limit was reached. If
      this limit is reached, then user will be prompted with a choice to
      manually enter a project id, create a new project, or list all projects.

  Returns:
    str, the project ID to use, or _CREATE_PROJECT_SENTINEL (if a project should
      be created), or None
  Nz)Enter project ID you would like to use:  This account has no projects.zWould you like to create one?)prompt_string)Enter a project IDCreate a new projectzList projectszFThis account has a lot of projects! Listing them all can take a while.)messager   z+Project ID does not exist or is not active.T)allow_invalid   r)   r*   zPick cloud project to use: )r+   allow_freeformfreeform_suggester)r   PromptResponsePromptContinue_CREATE_PROJECT_SENTINELPromptChoicePromptWithValidatorr%   r   r	   TextChoiceSuggesterlen)project_idslimit_exceededidxs      r   _PromptForProjectIdr:   H   s8   $ $$35 =8<=$$'57 ##

!
!G!#C {	++

7
5	 
 
%%"$k)+ABB+#779		;#
 	[
c+))53	 
 c+""##	S	    c                    |r| |v rt        d      t        j                  j                  d|       }	 t	        j                  |      }	 t        j                  |      }| S # t
        $ rC}t        j                  dj                  t        j                  |      |              Y d}~yd}~ww xY w# t        j                  $ rC}t        j                  dj                  t        j                  |      |              Y d}~yd}~ww xY w)zBCreate a project and check that it isn't in the known project IDs.z3Attempting to create a project that already exists.zcloudresourcemanager.projects)r   zProject creation failed: {err}
Please make sure to create the project [{project}] using
    $ gcloud projects create {project}
or change to another project using
    $ gcloud config set project <PROJECT ID>)r   r   Nz8Project creation for project [{project}] failed:
  {err})
ValueErrorr   REGISTRYCreater   r   r   r   formatr   r   r   WaitForOperationOperationFailedException)r#   r7   r$   	create_opr   s        r   _CreateProjectrD      s   Z;.
J
KK""))% * =+	##K0I++I6I 
! 
 KK ? @Fvc*J @F @@	A  
	,	, KKCJJc"J 	K 	89 	s/   A! 
B0 !	B-*9B((B-0D9DDc                    t        t        dz         }d}|t        |      t        kD  rd}d}| r| }nt        ||      }|t        ur|}|s|||v s||r|S | rt        |       s|t        ur|S |t        u rt        j                  t              }|s?y|r"dj                  |dj                  |            }nd}t        j                  |d	
      syt        ||      S )zAllows user to select a project.

  Args:
    preselected: str, use this value if not None

  Returns:
    str, project_id or None if was not selected.
  r-   r   FNTz)[{0}] is not one of your projects [{1}]. ,r'   zWould you like to create it?)r+   r(   )r   _PROJECT_LIST_LIMITr6   r:   r2   r%   r   r0   _ENTER_PROJECT_ID_MESSAGEr@   joinr1   rD   )preselectedr7   r8   selectedr#   r+   s         r   PickProjectrL      s     %81%<=+.[!14G!GN(J$[.AJ11h	z[8h
 
+K822++**+DEJ;BB
chh{+-g 0g$$'EG	
K	00r;   )N)__doc__
__future__r   r   r   +googlecloudsdk.api_lib.cloudresourcemanagerr   r   'googlecloudsdk.api_lib.resource_managerr   googlecloudsdk.callioper	   #googlecloudsdk.command_lib.projectsr
   r   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   r   rH   objectr2   rG   r   r%   r:   rD   rL    r;   r   <module>rW      sb     ? &  ' D E > . N # ) 2 
 
 "8   "<~801r;   