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/394/lib/googlecloudsdk/api_lib/service_directory/namespaces.py
# -*- coding: utf-8 -*- #
# Copyright 2019 Google Inc. 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 Service Directory namespaces API."""

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

from apitools.base.py import list_pager
from googlecloudsdk.api_lib.service_directory import base as sd_base
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.iam import iam_util


class NamespacesClient(sd_base.ServiceDirectoryApiLibBase):
  """Client for namespaces in the Service Directory API."""

  def __init__(self, release_track=base.ReleaseTrack.GA):
    super(NamespacesClient, self).__init__(release_track)
    self.service = self.client.projects_locations_namespaces

  def Create(self, namespace_ref, labels=None):
    """Namespaces create request."""
    namespace = self.msgs.Namespace(labels=labels)
    create_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesCreateRequest(
        parent=namespace_ref.Parent().RelativeName(),
        namespace=namespace,
        namespaceId=namespace_ref.namespacesId)
    return self.service.Create(create_req)

  def Delete(self, namespace_ref):
    """Namespaces delete request."""
    delete_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesDeleteRequest(
        name=namespace_ref.RelativeName())
    return self.service.Delete(delete_req)

  def Describe(self, namespace_ref):
    """Namespaces describe request."""
    describe_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesGetRequest(
        name=namespace_ref.RelativeName())
    return self.service.Get(describe_req)

  def List(self, location_ref, filter_=None, order_by=None, page_size=None):
    """Namespaces list request."""
    list_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesListRequest(
        parent=location_ref.RelativeName(),
        filter=filter_,
        orderBy=order_by,
        pageSize=page_size)
    return list_pager.YieldFromList(
        self.service,
        list_req,
        batch_size=page_size,
        field='namespaces',
        batch_size_attribute='pageSize')

  def Update(self, namespace_ref, labels=None):
    """Namespaces update request."""
    mask_parts = []
    if labels:
      mask_parts.append('labels')

    namespace = self.msgs.Namespace(labels=labels)
    update_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesPatchRequest(
        name=namespace_ref.RelativeName(),
        namespace=namespace,
        updateMask=','.join(mask_parts))
    return self.service.Patch(update_req)

  def AddIamPolicyBinding(self, namespace_ref, member, role):
    """Namespaces add iam policy binding request."""
    policy = self.GetIamPolicy(namespace_ref)
    iam_util.AddBindingToIamPolicy(self.msgs.Binding, policy, member, role)
    return self.SetIamPolicy(namespace_ref, policy)

  def GetIamPolicy(self, namespace_ref):
    """Namespaces get iam policy request."""
    get_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesGetIamPolicyRequest(
        resource=namespace_ref.RelativeName())
    return self.service.GetIamPolicy(get_req)

  def RemoveIamPolicyBinding(self, namespace_ref, member, role):
    """Namespaces remove iam policy binding request."""
    policy = self.GetIamPolicy(namespace_ref)
    iam_util.RemoveBindingFromIamPolicy(policy, member, role)
    return self.SetIamPolicy(namespace_ref, policy)

  def SetIamPolicy(self, namespace_ref, policy):
    """Namespaces set iam policy request."""
    set_req = self.msgs.ServicedirectoryProjectsLocationsNamespacesSetIamPolicyRequest(
        resource=namespace_ref.RelativeName(),
        setIamPolicyRequest=self.msgs.SetIamPolicyRequest(policy=policy))
    return self.service.SetIamPolicy(set_req)