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/compute/routers/upload_route_policy.py
# -*- coding: utf-8 -*- #
# Copyright 2023 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 for uploading a route policy into a Compute Engine router."""

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

import base64
import json
import os

from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.util import messages as messages_util
from googlecloudsdk.calliope import base
from googlecloudsdk.calliope import exceptions
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags
from googlecloudsdk.core import yaml
from googlecloudsdk.core.util import files


@base.UniverseCompatible
class UploadRoutePolicy(base.SilentCommand):
  """Upload a route policy into a Compute Engine router."""

  ROUTER_ARG = None

  @classmethod
  def Args(cls, parser):
    UploadRoutePolicy.ROUTER_ARG = flags.RouterArgument()
    UploadRoutePolicy.ROUTER_ARG.AddArgument(parser, operation_type='upload')
    parser.add_argument(
        '--policy-name', help='Name of the route policy to add/replace.'
    )
    parser.add_argument(
        '--file-name',
        required=True,
        help='Local path to the file defining the policy',
    )
    parser.add_argument(
        '--file-format',
        choices=['json', 'yaml'],
        help='Format of the file passed to --file-name',
    )

  def Run(self, args):
    """Issues the request necessary for uploading a route policy into a Router.

    Args:
      args: contains arguments passed to the command.

    Returns:
      The result of patching the router uploading the route policy.
    """
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    router_ref = UploadRoutePolicy.ROUTER_ARG.ResolveAsResource(
        args,
        holder.resources,
        scope_lister=compute_flags.GetDefaultScopeLister(client),
    )

    route_policy = self.ParseRoutePolicyFromFile(
        args.file_name, args.file_format, client.messages
    )
    self.EnsureSamePolicyName(args.policy_name, route_policy)

    request = (
        client.apitools_client.routers,
        'UpdateRoutePolicy',
        client.messages.ComputeRoutersUpdateRoutePolicyRequest(
            **router_ref.AsDict(), routePolicy=route_policy
        ),
    )

    return client.MakeRequests([request])[0]

  def EnsureSamePolicyName(self, policy_name, route_policy):
    if policy_name is not None and hasattr(route_policy, 'name'):
      if policy_name != route_policy.name:
        raise exceptions.BadArgumentException(
            'policy-name',
            'The policy name provided [{0}] does not match the one from the'
            ' file [{1}]'.format(policy_name, route_policy.name),
        )
    if not hasattr(route_policy, 'name') and policy_name is not None:
      route_policy.name = policy_name

  def ParseRoutePolicyFromFile(self, input_file, file_format, messages):
    # Get the imported route policy config.
    resource = self.ParseFile(input_file, file_format)
    if 'resource' in resource:
      resource = resource['resource']
    route_policy = messages_util.DictToMessageWithErrorCheck(
        resource, messages.RoutePolicy
    )
    if 'fingerprint' in resource:
      route_policy.fingerprint = base64.b64decode(resource['fingerprint'])
    return route_policy

  def ParseFile(self, input_file, file_format):
    if os.path.isdir(input_file):
      raise exceptions.BadFileException(
          '[{0}] is a directory'.format(input_file)
      )
    if not os.path.isfile(input_file):
      raise exceptions.BadFileException('No such file [{0}]'.format(input_file))
    try:
      with files.FileReader(input_file) as import_file:
        if file_format == 'json':
          return json.load(import_file)
        return yaml.load(import_file)
    except Exception as exp:
      msg = (
          'Unable to read route policy config from specified file [{0}]'
          ' because {1}'.format(input_file, exp)
      )
      raise exceptions.BadFileException(msg)

UploadRoutePolicy.detailed_help = {
    'DESCRIPTION': """\
  *{command}* uploads a route policy into a Compute Engine router.
  """,
    'EXAMPLES': """\
        To upload a route policy `my-import-policy` from a file `my-import-policy.yaml` into a router `my-router` in region `us-central1`, run:

              $ {command} my-router --region=us-central1 --policy-name=my-import-policy --file-name=my-import-policy.yaml"

          """,
}