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/bms/util.py
# -*- coding: utf-8 -*- #
# Copyright 2021 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 bms commands."""

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

from googlecloudsdk.core import properties
from googlecloudsdk.core import resources


def DefaultToGlobal():
  """Returns 'global' to be used as a fallthrough hook in resources.yaml."""
  return 'global'


def NFSNetworkFullName(nfs_share_resource, allowed_client_dict):
  """Returns the full GCP name of the NFS allowed client network."""
  # We default to NFS project ID if network project ID is not specified.
  nfs_region = nfs_share_resource.Parent()
  nfs_project = nfs_region.Parent()
  network_project_id = allowed_client_dict.get('network-project-id',
                                               nfs_project.Name())
  return resources.REGISTRY.Parse(
      allowed_client_dict['network'],
      params={
          'projectsId': network_project_id,
          'locationsId': nfs_region.Name(),
      },
      collection='baremetalsolution.projects.locations.networks').RelativeName()


def RemoveAllowedClients(nfs_share_resource, allowed_clients,
                         remove_key_dicts):
  """Removes the allowed clients specified by remove_key_dicts from allowed_clients."""
  keys_to_remove = set()
  for key_dict in remove_key_dicts:
    key_network_full_name = NFSNetworkFullName(
        nfs_share_resource=nfs_share_resource,
        allowed_client_dict=key_dict,
    )
    keys_to_remove.add((key_network_full_name, key_dict['cidr']))

  out = []
  for allowed_client in allowed_clients:
    curr_key = (allowed_client.network, allowed_client.allowedClientsCidr)
    if curr_key in keys_to_remove:
      keys_to_remove.remove(curr_key)
    else:
      out.append(allowed_client)
  for key in keys_to_remove:
    raise LookupError('Cannot find an existing allowed client for network'
                      ' [{}] and CIDR [{}]'.format(key[0],
                                                   key[1]))
  return out


def FixParentPathWithGlobalRegion(region: str) -> str:
  """Returns projects/$project/location/$location parent path based on region."""
  if region is not None:
    return region.RelativeName()
  project = properties.VALUES.core.project.Get(required=True)
  return 'projects/{}/locations/global'.format(project)