HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //proc/thread-self/root/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/tasks/app.py
# -*- coding: utf-8 -*- #
# Copyright 2017 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for App Engine apps for `gcloud tasks` commands."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from apitools.base.py import exceptions as apitools_exceptions
from googlecloudsdk.api_lib.app import appengine_api_client as app_engine_api
from googlecloudsdk.api_lib.tasks import GetApiAdapter
from googlecloudsdk.calliope import base as calliope_base
from googlecloudsdk.command_lib.app import create_util
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import properties
from googlecloudsdk.core.console import console_io


class RegionResolvingError(exceptions.Error):
  """Error for when the app's region cannot be ultimately determined."""


def ResolveAppLocation(project_ref):
  """Determines Cloud Tasks location for the project or creates an app.

  Args:
    project_ref: The project resource to look up the location for.

  Returns:
    The existing or created app's locationId.

  Raises:
    RegionResolvingError: If the region of the app could not be determined.
  """
  location = _GetLocation(project_ref) or _CreateApp(project_ref)
  if location is not None:
    return location
  raise RegionResolvingError(
      'Could not determine the location for the project. Please try again. It '
      'is possible an AppEngine App does not exist for this project.')


def _GetLocation(project_ref):
  """Gets the location from the Cloud Tasks API."""
  try:
    locations_client = GetApiAdapter(calliope_base.ReleaseTrack.GA).locations
    locations = list(locations_client.List(project_ref, page_size=2))
    if len(locations) > 1:
      # Projects currently can only use Cloud Tasks in single region, so this
      # should never happen for now, but that will change in the future.
      raise RegionResolvingError('Multiple locations found for this project. '
                                 'Please specify an exact location.')
    if len(locations) == 1:
      return locations[0].labels.additionalProperties[0].value
    return None
  except apitools_exceptions.HttpNotFoundError:
    return None


def _CreateApp(project_ref):
  """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'),
      default=False,
      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 _GetLocation(project_ref)
  return None