def _PossiblyCreateApp(api_client, project, app_create): """Returns an app resource, and creates it if the stars are aligned. App creation happens only if the current project is app-less, app_create is True, we are running in interactive mode and the user explicitly wants to. Args: api_client: Admin API client. project: The GCP project/app id. app_create: True if interactive app creation should be allowed. Returns: An app object (never returns None). Raises: MissingApplicationError: If an app does not exist and cannot be created. """ try: return api_client.GetApplication() except api_lib_exceptions.NotFoundError: # Invariant: GCP Project does exist but (singleton) GAE app is not yet # created. Offer to create one if the following conditions are true: # 1. `app_create` is True (currently `beta` only) # 2. We are currently running in interactive mode msg = output_helpers.CREATE_APP_PROMPT.format(project=project) if (app_create and console_io.CanPrompt() and console_io.PromptContinue(message=msg)): # Equivalent to running `gcloud beta app create` create_util.CreateAppInteractively(api_client, project) # App resource must be fetched again return api_client.GetApplication() raise exceptions.MissingApplicationError(project)
def _PossiblyCreateApp(api_client, project): """Returns an app resource, and creates it if the stars are aligned. App creation happens only if the current project is app-less, we are running in interactive mode and the user explicitly wants to. Args: api_client: Admin API client. project: The GCP project/app id. Returns: An app object (never returns None). Raises: MissingApplicationError: If an app does not exist and cannot be created. """ try: return api_client.GetApplication() except api_lib_exceptions.NotFoundError: # Invariant: GCP Project does exist but (singleton) GAE app is not yet # created. # # Check for interactive mode, since this action is irreversible and somewhat # surprising. CreateAppInteractively will provide a cancel option for # interactive users, and MissingApplicationException includes instructions # for non-interactive users to fix this. log.debug('No app found:', exc_info=True) if console_io.CanPrompt(): # Equivalent to running `gcloud app create` create_util.CreateAppInteractively(api_client, project) # App resource must be fetched again return api_client.GetApplication() raise exceptions.MissingApplicationError(project)
def Run(self, args): project = properties.VALUES.core.project.Get(required=True) api_client = appengine_api_client.GetApiClient() if args.region: create_util.CreateApp(api_client, project, args.region) elif console_io.CanPrompt(): create_util.CreateAppInteractively(api_client, project) else: raise create_util.UnspecifiedRegionError( 'Prompts are disabled. Region must be specified either by the ' '`--region` flag or interactively. Use `gcloud app regions ' 'list` to list available regions.') log.status.Print('Success! The app is now created. Please use ' '`gcloud app deploy` to deploy your first app.')
def _CreateApp(self, project): """Walks the user through creating an AppEngine app.""" if console_io.PromptContinue( message=('There is no App Engine app in project [{}].'.format(project)), prompt_string=('Would you like to create one'), throw_if_unattended=True): try: app_engine_api_client = app_engine_api.GetApiClientForTrack( calliope_base.ReleaseTrack.GA) create_util.CreateAppInteractively(app_engine_api_client, project) except create_util.AppAlreadyExistsError: raise create_util.AppAlreadyExistsError( 'App already exists in project [{}]. This may be due a race ' 'condition. Please try again.'.format(project)) else: return self._GetLocation(project) return None
def _PossiblyCreateApp(api_client, project): """Returns an app resource, and creates it if the stars are aligned. App creation happens only if the current project is app-less, we are running in interactive mode and the user explicitly wants to. Args: api_client: Admin API client. project: The GCP project/app id. Returns: An app object (never returns None). Raises: MissingApplicationError: If an app does not exist and cannot be created. """ try: return api_client.GetApplication() except apitools_exceptions.HttpNotFoundError: # Invariant: GCP Project does exist but (singleton) GAE app is not yet # created. # # Check for interactive mode, since this action is irreversible and somewhat # surprising. CreateAppInteractively will provide a cancel option for # interactive users, and MissingApplicationException includes instructions # for non-interactive users to fix this. log.debug('No app found:', exc_info=True) if console_io.CanPrompt(): # Equivalent to running `gcloud app create` create_util.CreateAppInteractively(api_client, project) # App resource must be fetched again return api_client.GetApplication() raise exceptions.MissingApplicationError(project) except apitools_exceptions.HttpForbiddenError: active_account = properties.VALUES.core.account.Get() # pylint: disable=protected-access raise core_api_exceptions.HttpException( ('Permissions error fetching application [{}]. Please ' 'make sure that you have permission to view applications on the ' 'project and that {} has the App Engine Deployer ' '(roles/appengine.deployer) role.'.format(api_client._FormatApp(), active_account)))
def _CreateApp(app_engine_api_client): """Walks the user through creating an AppEngine app.""" project = properties.VALUES.core.project.GetOrFail() if console_io.PromptContinue( message=( 'There is no App Engine app in project [{}].'.format(project)), prompt_string=('Would you like to create one'), throw_if_unattended=True): try: create_util.CreateAppInteractively( app_engine_api_client, project, regions=constants.VALID_REGIONS, extra_warning=_MORE_REGIONS_AVAILABLE_WARNING) except create_util.AppAlreadyExistsError: raise create_util.AppAlreadyExistsError( 'App already exists in project [{}]. This may be due a race ' 'condition. Please try again.'.format(project)) else: return _GetApp(app_engine_api_client) return None