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/surface/container/images/list_gcr_usage.py
# -*- coding: utf-8 -*- #
# Copyright 2024 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.

"""Command to list Container Registry usage."""

import frozendict
from googlecloudsdk.api_lib.container.images import gcr_utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.asset import flags as asset_flags
from googlecloudsdk.command_lib.asset import utils as asset_utils


_DETAILED_HELP = frozendict.frozendict({
    'DESCRIPTION': '{description}',
    'EXAMPLES': """ \
        To list Container Registry usage in a project:

          $ {command} --project=my-proj

        To list Container Registry usage in an organization:

          $ {command} --organization=my-org

        To list Container Registry usage in a folder:

          $ {command} --folder=my-folder

        To list all active Container Registry usage in an organization:

          $ {command} --organization=my-org --filter="usage=ACTIVE"

        To list all projects that aren't redirected yet:

          $ {command} --organization=my-org --filter="usage!=REDIRECTED"
        """,
})


@base.ReleaseTracks(base.ReleaseTrack.GA)
class ListGCRUsage(base.ListCommand):
  """List Container Registry usage.

  List Container Registry usage for all projects in the specified scope
  (project/organization/folder). Caller must have
  `cloudasset.assets.searchAllResources` permission on the requested scope and
  `storage.objects.list permission` on the Cloud Storage buckets used by
  Container Registry.

  The tool returns the following lists of usage states:

  ACTIVE: Container Registry usage has occurred in the last 30 days. The host
  location and project are not redirected.

  INACTIVE: No Container Registry usage has occurred in the last 30 days. The
  host location and project are not redirected.

  REDIRECTED: The project has been redirected to Artifact Registry but still has
  Container Registry Cloud Storage buckets. This project will continue to
  function after Container Registry is turned down and no further action is
  required. You can reduce costs by deleting the Container Registry Cloud
  Storage buckets.

  REDIRECTION_INCOMPLETE: Requests are redirected to Artifact Registry, but data
  is still being copied from Container Registry.

  LEGACY: Container Registry usage is unknown. This state is caused by legacy
  Container Registry projects that store container image metadata files in Cloud
  Storage buckets. For more information on legacy Container Registry projects,
  see
  https://cloud.google.com/container-registry/docs/deprecations/feature-deprecations#container_image_metadata_storage_change.
  """

  detailed_help = _DETAILED_HELP

  @staticmethod
  def Args(parser):
    asset_flags.AddParentArgs(
        parser,
        'Project ID.',
        'Organization ID.',
        'Folder ID.',
    )
    base.URI_FLAG.RemoveFromParser(parser)

  def Run(self, args):
    parent = asset_utils.GetParentNameForExport(
        args.organization,
        args.project,
        args.folder,
    )
    gcr_repos = gcr_utils.ListGCRRepos(parent)
    for gcr_repo in gcr_repos:
      yield gcr_utils.CheckGCRUsage(gcr_repo)