HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
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,
Upload Files
File: //snap/google-cloud-cli/current/lib/googlecloudsdk/command_lib/secrets/fmt.py
# -*- coding: utf-8 -*- #
# Copyright 2019 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.
"""Commonly used display formats."""

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

from googlecloudsdk.calliope import parser_arguments
from googlecloudsdk.calliope import parser_extensions
from googlecloudsdk.command_lib.secrets import args as secrets_args


_LOCATION_TABLE = """
table(
  name.basename():label=NAME,
  displayName:label=LOCATION
)
"""

_SECRET_DATA = """
value[terminator="",private](
  payload.data.decode(base64).decode(utf8)
)
"""

_SECRET_TABLE = """
table(
  name.basename():label=NAME,
  createTime.date():label=CREATED,
  policy_transform():label=REPLICATION_POLICY,
  locations_transform():label=LOCATIONS
)
"""

_REGIONAL_SECRET_TABLE = """
table(
  name.basename():label=NAME,
  createTime.date():label=CREATED
)
"""

_VERSION_TABLE = """
table(
  name.basename():label=NAME,
  state.enum(secrets.StateVersionJobState).color('destroyed', 'disabled', 'enabled', 'unknown'):label=STATE,
  createTime.date():label=CREATED,
  destroyTime.date(undefined='-'):label=DESTROYED
)
"""

_VERSION_STATE_TRANSFORMS = {
    'secrets.StateVersionJobState::enum': {
        'STATE_UNSPECIFIED': 'unknown',
        'ENABLED': 'enabled',
        'DISABLED': 'disabled',
        'DESTROYED': 'destroyed',
    }
}


def _TransformReplicationPolicy(r):
  if 'replication' not in r:
    return 'ERROR'
  if 'automatic' in r['replication']:
    return 'automatic'
  if 'userManaged' in r['replication']:
    return 'user_managed'
  return 'ERROR'


def _TransformLocations(r):
  if 'replication' not in r:
    return 'ERROR'
  if 'automatic' in r['replication']:
    return '-'
  if 'userManaged' in r['replication'] and 'replicas' in r['replication'][
      'userManaged']:
    locations = []
    for replica in r['replication']['userManaged']['replicas']:
      locations.append(replica['location'])
    return ','.join(locations)
  return 'ERROR'


_SECRET_TRANSFORMS = {
    'policy_transform': _TransformReplicationPolicy,
    'locations_transform': _TransformLocations,
}


def UseLocationTable(
    parser: parser_arguments.ArgumentInterceptor, api_version: str = 'v1'
):
  """Table format to display locations.

  Args:
    parser: arguments interceptor
    api_version: api version to be included in resource name
  """
  parser.display_info.AddFormat(_LOCATION_TABLE)
  parser.display_info.AddUriFunc(
      secrets_args.MakeGetUriFunc(
          'secretmanager.projects.locations', api_version=api_version
      )
  )


def UseSecretTable(parser: parser_arguments.ArgumentInterceptor):
  """Table format to display secrets.

  Args:
    parser: arguments interceptor
  """
  parser.display_info.AddFormat(_SECRET_TABLE)
  parser.display_info.AddTransforms(_SECRET_TRANSFORMS)
  parser.display_info.AddUriFunc(
      lambda r: secrets_args.ParseSecretRef(r.name).SelfLink()
  )


def SecretTableUsingArgument(
    args: parser_extensions.Namespace, api_version: str = 'v1'
):
  """Table format to display global secrets.

  Args:
    args: arguments interceptor
    api_version: api version to be included in resource name
  """
  args.GetDisplayInfo().AddFormat(_SECRET_TABLE)
  args.GetDisplayInfo().AddTransforms(_SECRET_TRANSFORMS)
  args.GetDisplayInfo().AddUriFunc(
      secrets_args.MakeGetUriFunc(
          'secretmanager.projects.secrets', api_version=api_version
      )
  )


def RegionalSecretTableUsingArgument(
    args: parser_extensions.Namespace, api_version: str = 'v1'
):
  """Table format to display regional secrets.

  Args:
    args: arguments interceptor
    api_version: api version to be included in resource name
  """
  args.GetDisplayInfo().AddFormat(_REGIONAL_SECRET_TABLE)
  args.GetDisplayInfo().AddTransforms(_SECRET_TRANSFORMS)
  args.GetDisplayInfo().AddUriFunc(
      secrets_args.MakeGetUriFunc(
          'secretmanager.projects.locations.secrets', api_version=api_version
      )
  )


def UseSecretData(parser):
  parser.display_info.AddFormat(_SECRET_DATA)


def UseVersionTable(
    parser: parser_arguments.ArgumentInterceptor, api_version='v1'
):
  """Table format to display secret versions.

  Args:
    parser: arguments interceptor
    api_version: api version to be included in resource name
  """
  parser.display_info.AddFormat(_VERSION_TABLE)
  parser.display_info.AddTransforms(_VERSION_STATE_TRANSFORMS)
  secrets_args.MakeGetUriFunc(
      'secretmanager.projects.locations.secrets.versions',
      api_version=api_version,
  )


def UseRegionalVersionTable(
    parser: parser_arguments.ArgumentInterceptor, api_version='v1'
):
  """Table format to display regional secret versions.

  Args:
    parser: arguments interceptor
    api_version: api version to be included in resource name
  """
  parser.display_info.AddFormat(_VERSION_TABLE)
  parser.display_info.AddTransforms(_VERSION_STATE_TRANSFORMS)
  secrets_args.MakeGetUriFunc(
      'secretmanager.projects.locations.secrets.versions',
      api_version=api_version,
  )


def SecretVersionTableUsingArgument(
    args: parser_extensions.Namespace, api_version: str = 'v1'
):
  """Table format to display global secret version.

  Args:
    args: arguments interceptor
    api_version: api version to be included in resource name
  """
  args.GetDisplayInfo().AddFormat(_VERSION_TABLE)
  args.GetDisplayInfo().AddTransforms(_VERSION_STATE_TRANSFORMS)
  args.GetDisplayInfo().AddUriFunc(
      secrets_args.MakeGetUriFunc(
          'secretmanager.projects.secrets.versions', api_version=api_version
      )
  )


def RegionalSecretVersionTableUsingArgument(
    args: parser_extensions.Namespace, api_version: str = 'v1'
):
  """Table format to display regional secrets.

  Args:
    args: arguments interceptor
    api_version: api version to be included in resource name
  """
  args.GetDisplayInfo().AddFormat(_VERSION_TABLE)
  args.GetDisplayInfo().AddTransforms(_VERSION_STATE_TRANSFORMS)
  args.GetDisplayInfo().AddUriFunc(
      secrets_args.MakeGetUriFunc(
          'secretmanager.projects.locations.secrets.versions',
          api_version=api_version,
      )
  )