feat: Add new gcloud commands, API clients, and third-party libraries across various services.

This commit is contained in:
2026-01-01 20:26:35 +01:00
parent 5e23cbece0
commit a19e592eb7
25221 changed files with 8324611 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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.
"""Commands for reading and manipulating NATs."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
class Nats(base.Group):
"""List, create, describe, and delete Cloud NAT."""
Nats.detailed_help = {
'DESCRIPTION': """
List, create, describe, and delete Cloud NAT.
For more information about Cloud NAT, see the
[Cloud NAT documentation](https://cloud.google.com/nat/docs/using-nat).
See also: [Routers API](https://cloud.google.com/compute/docs/reference/rest/v1/routers).
""",
}

View File

@@ -0,0 +1,149 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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 adding a NAT to a Compute Engine router."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute.operations import poller
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import flags as nats_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
@base.UniverseCompatible
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Create(base.CreateCommand):
"""Add a NAT to a Compute Engine router."""
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
base.ASYNC_FLAG.AddToParser(parser)
compute_flags.AddRegionFlag(parser, 'NAT', operation_type='create')
nats_flags.AddNatNameArg(parser, operation_type='create')
nats_flags.AddTypeArg(parser)
nats_flags.AddEndpointTypesArg(parser)
nats_flags.AddCommonNatArgs(parser, for_create=True)
def Run(self, args):
"""See base.CreateCommand."""
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
messages = holder.client.messages
service = holder.client.apitools_client.routers
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request_type = messages.ComputeRoutersGetRequest
replacement = service.Get(request_type(**router_ref.AsDict()))
nat = nats_utils.CreateNatMessage(args, holder)
replacement.nats.append(nat)
result = service.Patch(
messages.ComputeRoutersPatchRequest(
project=router_ref.project,
region=router_ref.region,
router=router_ref.Name(),
routerResource=replacement))
operation_ref = resources.REGISTRY.Parse(
result.name,
collection='compute.regionOperations',
params={
'project': router_ref.project,
'region': router_ref.region,
})
if args.async_:
log.CreatedResource(
operation_ref,
kind='nat [{0}] in router [{1}]'.format(nat.name, router_ref.Name()),
is_async=True,
details='Run the [gcloud compute operations describe] command '
'to check the status of this operation.')
return result
target_router_ref = holder.resources.Parse(
router_ref.Name(),
collection='compute.routers',
params={
'project': router_ref.project,
'region': router_ref.region,
})
operation_poller = poller.Poller(service, target_router_ref)
return waiter.WaitFor(
operation_poller, operation_ref,
'Creating NAT [{0}] in router [{1}]'.format(nat.name,
router_ref.Name()))
Create.detailed_help = {
'DESCRIPTION':
"""
*{command}* is used to create a NAT on a Compute Engine router.
""",
'EXAMPLES':
"""\
Auto-allocate NAT for all IP addresses of all subnets in the region:
$ {command} nat1 --router=my-router
--auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges
Specify IP addresses for NAT:
Each IP address is the name of a reserved static IP address resource in
the same region.
$ {command} nat1 --router=my-router
--nat-external-ip-pool=ip-address1,ip-address2
Specify subnet ranges for NAT:
By default, NAT works for all primary and secondary IP ranges for all
subnets in the region for the given VPC network. You can restrict which
subnet primary and secondary ranges can use NAT.
$ {command} nat1 --router=my-router
--auto-allocate-nat-external-ips
--nat-custom-subnet-ip-ranges=subnet-1,subnet-3:secondary-range-1
""",
'API REFERENCE':
"""\
This command, when specified without alpha or beta, uses the compute/v1/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/v1/routers/
The beta command uses the compute/beta/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/beta/routers/
The alpha command uses the compute/alpha/routers API. Full documentation is not available for the alpha API.
"""
}

View File

@@ -0,0 +1,117 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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 removing a NAT from a Compute Engine router."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import textwrap
from apitools.base.py import encoding
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import flags as nats_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
class AlphaDelete(base.DeleteCommand):
"""Remove a NAT from a Compute Engine router.
*{command}* removes a NAT from a Compute Engine router.
"""
ROUTER_ARG = None
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
compute_flags.AddRegionFlag(
parser, 'NAT', operation_type='delete', plural=True)
nats_flags.AddNatNameArg(parser, operation_type='delete', plural=True)
def _GetPatchRequest(self, client, router_ref, replacement):
return (client.apitools_client.routers, 'Patch',
client.messages.ComputeRoutersPatchRequest(
router=router_ref.Name(),
routerResource=replacement,
region=router_ref.region,
project=router_ref.project))
def Modify(self, args, existing, cleared_fields):
"""Mutate the router and record any cleared_fields for Patch request."""
replacement = encoding.CopyProtoMessage(existing)
for name in args.name:
nat = nats_utils.FindNatOrRaise(replacement, name)
replacement.nats.remove(nat)
# If all NATs have been removed, add this field to cleared_fields.
if not replacement.nats:
cleared_fields.append('nats')
return replacement
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
objects = client.MakeRequests(
[(client.apitools_client.routers, 'Get',
client.messages.ComputeRoutersGetRequest(**router_ref.AsDict()))])
# Cleared list fields need to be explicitly identified for Patch API.
cleared_fields = []
new_object = self.Modify(args, objects[0], cleared_fields)
utils.PromptForDeletionHelper(
'NAT', ['{} in router {}'.format(args.name, router_ref.Name())])
with client.apitools_client.IncludeFields(cleared_fields):
# There is only one response because one request is made above
result = client.MakeRequests(
[self._GetPatchRequest(client, router_ref, new_object)])
return result
AlphaDelete.detailed_help = {
'DESCRIPTION':
textwrap.dedent("""\
*{command}* is used to delete a NAT on a Compute Engine router.
"""),
'EXAMPLES':
"""\
To delete NAT 'n1' in router 'r1', run:
$ {command} n1 --router=r1 --region=us-central1
""",
'API REFERENCE':
"""\
This command, when specified without alpha or beta, uses the compute/v1/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/v1/routers/
The beta command uses the compute/beta/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/beta/routers/
The alpha command uses the compute/alpha/routers API. Full documentation is not available for the alpha API.
"""
}

View File

@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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 describing a NAT in a Compute Engine router."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import textwrap
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import flags as nats_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
class Describe(base.DescribeCommand):
"""Describe a NAT in a Compute Engine router."""
ROUTER_ARG = None
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
compute_flags.AddRegionFlag(parser, 'NAT', operation_type='describe')
nats_flags.AddNatNameArg(parser, operation_type='describe')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request = client.messages.ComputeRoutersGetRequest(**router_ref.AsDict())
router = client.MakeRequests([(client.apitools_client.routers, 'Get',
request)])[0]
return nats_utils.FindNatOrRaise(router, args.name)
Describe.detailed_help = {
'DESCRIPTION':
textwrap.dedent("""
*{command}* is used to describe a NAT in a Compute Engine router.
"""),
'EXAMPLES':
"""\
To describe NAT 'n1' in router 'r1', run:
$ {command} n1 --router=r1 --region=us-central1
""",
'API REFERENCE':
"""\
This command, when specified without alpha or beta, uses the compute/v1/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/v1/routers/
The beta command uses the compute/beta/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/beta/routers/
The alpha command uses the compute/alpha/routers API. Full documentation is not available for the alpha API.
"""
}

View File

@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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 NATs on a Compute Engine router."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import textwrap
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import flags as nats_flags
class List(base.DescribeCommand):
"""Lists the NATs on a Compute Engine router."""
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
parser.display_info.AddFormat(nats_flags.DEFAULT_LIST_FORMAT)
compute_flags.AddRegionFlag(parser, 'NATs', operation_type='list')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
messages = holder.client.messages
service = holder.client.apitools_client.routers
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request_type = messages.ComputeRoutersGetRequest
router = service.Get(request_type(**router_ref.AsDict()))
return router.nats
List.detailed_help = {
'DESCRIPTION':
textwrap.dedent("""\
*{command}* is used to list the NATs on a Compute Engine router.
"""),
'EXAMPLES':
"""\
To list all NATs in router ``r1'' in region ``us-central1'', run:
$ {command} --router=r1 --region=us-central1.
""",
'API REFERENCE':
"""\
This command, when specified without alpha or beta, uses the compute/v1/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/v1/routers/
The beta command uses the compute/beta/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/beta/routers/
The alpha command uses the compute/alpha/routers API. Full documentation is not available for the alpha API.
"""
}

View File

@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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.
"""Commands for reading and manipulating rules in NATs."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
class Rules(base.Group):
"""List, create, update, describe, and delete Cloud NAT Rules."""
Rules.detailed_help = {
'DESCRIPTION':
"""
List, create, update, describe, and delete Cloud NAT Rules.
For more information about Cloud NAT, see the
[Cloud NAT documentation](https://cloud.google.com/nat/docs/using-nat).
""",
}

View File

@@ -0,0 +1,123 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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 adding a Rule to a Compute Engine NAT."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute.operations import poller
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.command_lib.compute.routers.nats.rules import flags as rules_flags
from googlecloudsdk.command_lib.compute.routers.nats.rules import rules_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Create(base.CreateCommand):
"""Add a Rule to a Compute Engine NAT."""
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
rules_flags.AddRuleNumberArg(parser, operation_type='create', plural=False)
rules_flags.AddNatNameArg(parser)
compute_flags.AddRegionFlag(parser, 'NAT', operation_type='create')
rules_flags.AddMatchArg(parser, required=True)
rules_flags.AddIpAndRangeArgsForCreate(parser)
base.ASYNC_FLAG.AddToParser(parser)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
messages = holder.client.messages
service = holder.client.apitools_client.routers
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request_type = messages.ComputeRoutersGetRequest
router = service.Get(request_type(**router_ref.AsDict()))
rule_number = args.rule_number
nat_name = args.nat
existing_nat = nats_utils.FindNatOrRaise(router, nat_name)
rule = rules_utils.CreateRuleMessage(args, holder, existing_nat)
existing_nat.rules.append(rule)
result = service.Patch(
messages.ComputeRoutersPatchRequest(
project=router_ref.project,
region=router_ref.region,
router=router_ref.Name(),
routerResource=router))
operation_ref = resources.REGISTRY.Parse(
result.name,
collection='compute.regionOperations',
params={
'project': router_ref.project,
'region': router_ref.region,
})
if args.async_:
log.CreatedResource(
operation_ref,
kind='Rule [{0}] in NAT [{1}]'.format(rule_number, nat_name),
is_async=True,
details='Run the [gcloud compute operations describe] command '
'to check the status of this operation.')
return result
target_router_ref = holder.resources.Parse(
router_ref.Name(),
collection='compute.routers',
params={
'project': router_ref.project,
'region': router_ref.region,
})
operation_poller = poller.Poller(service, target_router_ref)
return waiter.WaitFor(
operation_poller, operation_ref,
'Creating Rule [{0}] in NAT [{1}]'.format(rule_number, nat_name))
Create.detailed_help = {
'DESCRIPTION':
"""
*{command}* is used to create a Rule on a Compute Engine NAT.
""",
'EXAMPLES':
"""\
Create a rule to use the IP Address address-1 to talk to destination IPs
in the CIDR Range "203.0.113.0/24".
$ {command} 1 --nat=my-nat --router=my-router --region=us-central1
--match='inIpRange(destination.ip, "203.0.113.0/24")'
--source-nat-active-ips=a1
"""
}

View File

@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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 removing a Rule from a Compute Engine NAT."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import textwrap
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute import utils
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.command_lib.compute.routers.nats.rules import flags as rules_flags
from googlecloudsdk.command_lib.compute.routers.nats.rules import rules_utils
class Delete(base.DeleteCommand):
"""Delete a Rule in a Compute Engine NAT."""
ROUTER_ARG = None
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
rules_flags.AddRuleNumberArg(parser, plural=True)
rules_flags.AddNatNameArg(parser)
compute_flags.AddRegionFlag(
parser, 'NAT containing the Rule', operation_type='delete', plural=True)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
objects = client.MakeRequests([
(client.apitools_client.routers, 'Get',
client.messages.ComputeRoutersGetRequest(**router_ref.AsDict()))
])
router = objects[0]
nat_name = args.nat
rule_numbers = args.rule_number
nat = nats_utils.FindNatOrRaise(router, nat_name)
for rule_number in rule_numbers:
rule = rules_utils.FindRuleOrRaise(nat, rule_number)
nat.rules.remove(rule)
utils.PromptForDeletionHelper(
'Rule', ['{} in NAT {}'.format(args.rule_number, nat_name)])
return client.MakeRequests(
[self._GetPatchRequest(client, router_ref, router)])
def _GetPatchRequest(self, client, router_ref, router):
return (client.apitools_client.routers, 'Patch',
client.messages.ComputeRoutersPatchRequest(
router=router_ref.Name(),
routerResource=router,
region=router_ref.region,
project=router_ref.project))
Delete.detailed_help = {
'DESCRIPTION':
textwrap.dedent("""\
*{command}* is used to delete a Rule on a Compute Engine NAT.
"""),
'EXAMPLES':
"""\
To delete Rule 1 in NAT 'n1' in router 'r1', run:
$ {command} 1 --nat=n1 --router=r1 --region=us-central1
"""
}

View File

@@ -0,0 +1,76 @@
# -*- 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.
"""Command for describing a Rule from a Compute Engine NAT."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import textwrap
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.command_lib.compute.routers.nats.rules import flags as rules_flags
from googlecloudsdk.command_lib.compute.routers.nats.rules import rules_utils
class Describe(base.DescribeCommand):
"""Describe a Rule in a Compute Engine NAT."""
ROUTER_ARG = None
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
rules_flags.AddRuleNumberArg(parser)
rules_flags.AddNatNameArg(parser)
compute_flags.AddRegionFlag(
parser, 'NAT containing the Rule', operation_type='describe')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
client = holder.client
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
router = client.MakeRequests([
(client.apitools_client.routers, 'Get',
client.messages.ComputeRoutersGetRequest(**router_ref.AsDict()))
])[0]
nat_name = args.nat
rule_number = args.rule_number
nat = nats_utils.FindNatOrRaise(router, nat_name)
return rules_utils.FindRuleOrRaise(nat, rule_number)
Describe.detailed_help = {
'DESCRIPTION':
textwrap.dedent("""\
*{command}* is used to describe a Rule on a Compute Engine NAT.
"""),
'EXAMPLES':
"""\
To describe Rule 1 in NAT 'n1' in router 'r1', run:
$ {command} 1 --nat=n1 --router=r1 --region=us-central1
"""
}

View File

@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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 NATs on a Compute Engine router."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import textwrap
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.command_lib.compute.routers.nats.rules import flags as rules_flags
class List(base.DescribeCommand):
"""Lists the NATs on a Compute Engine router."""
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
rules_flags.AddNatNameArg(parser)
parser.display_info.AddFormat(rules_flags.DEFAULT_LIST_FORMAT)
compute_flags.AddRegionFlag(
parser, 'NAT containing the Rules', operation_type='list')
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
messages = holder.client.messages
service = holder.client.apitools_client.routers
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request_type = messages.ComputeRoutersGetRequest
router = service.Get(request_type(**router_ref.AsDict()))
nat_name = args.nat
nat = nats_utils.FindNatOrRaise(router, nat_name)
return nat.rules
List.detailed_help = {
'DESCRIPTION':
textwrap.dedent("""\
*{command}* is used to list the Rule on a Compute Engine NAT.
"""),
'EXAMPLES':
"""\
To list all Rules in Nat ``n1'' in router ``r1'' in region ``us-central1'',
run:
$ {command} --nat=n1 --router=r1 --region=us-central1.
"""
}

View File

@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*- #
# Copyright 2020 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 updating a Rule in a Compute Engine NAT."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute.operations import poller
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.command_lib.compute.routers.nats.rules import flags as rules_flags
from googlecloudsdk.command_lib.compute.routers.nats.rules import rules_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Update(base.UpdateCommand):
"""Update a Rule in a Compute Engine NAT."""
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
rules_flags.AddRuleNumberArg(parser, operation_type='update', plural=False)
rules_flags.AddNatNameArg(parser)
compute_flags.AddRegionFlag(
parser, 'NAT containing the Rule', operation_type='update'
)
rules_flags.AddMatchArg(parser, required=False)
rules_flags.AddIpAndRangeArgsForUpdate(parser)
base.ASYNC_FLAG.AddToParser(parser)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
messages = holder.client.messages
service = holder.client.apitools_client.routers
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request_type = messages.ComputeRoutersGetRequest
router = service.Get(request_type(**router_ref.AsDict()))
rule_number = args.rule_number
nat_name = args.nat
nat = nats_utils.FindNatOrRaise(router, nat_name)
rule = rules_utils.FindRuleOrRaise(nat, rule_number)
rules_utils.UpdateRuleMessage(rule, args, holder, nat)
result = service.Patch(
messages.ComputeRoutersPatchRequest(
project=router_ref.project,
region=router_ref.region,
router=router_ref.Name(),
routerResource=router))
operation_ref = resources.REGISTRY.Parse(
result.name,
collection='compute.regionOperations',
params={
'project': router_ref.project,
'region': router_ref.region,
})
if args.async_:
log.UpdatedResource(
operation_ref,
kind='Rule [{0}] in NAT [{1}]'.format(rule_number, nat_name),
is_async=True,
details='Run the [gcloud compute operations describe] command '
'to check the status of this operation.')
return result
target_router_ref = holder.resources.Parse(
router_ref.Name(),
collection='compute.routers',
params={
'project': router_ref.project,
'region': router_ref.region,
})
operation_poller = poller.Poller(service, target_router_ref)
return waiter.WaitFor(
operation_poller, operation_ref,
'Updating Rule [{0}] in NAT [{1}]'.format(rule_number, nat_name))
Update.detailed_help = {
'DESCRIPTION':
"""
*{command}* is used to update a Rule in a Compute Engine NAT.
""",
'EXAMPLES':
"""\
To drain connections established using address-1 and use address-2 for
all new connections matching Rule 1 in NAT nat-1, run:
$ {command} 1 --nat=nat1 --router=my-router --region=us-central1
--source-nat-drain-ips=address-1
--source-nat-active-ips=address-2
"""
}

View File

@@ -0,0 +1,145 @@
# -*- coding: utf-8 -*- #
# Copyright 2018 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 updating a NAT on a Compute Engine router."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.compute import base_classes
from googlecloudsdk.api_lib.compute.operations import poller
from googlecloudsdk.api_lib.util import waiter
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.compute import flags as compute_flags
from googlecloudsdk.command_lib.compute.routers import flags as routers_flags
from googlecloudsdk.command_lib.compute.routers.nats import flags as nats_flags
from googlecloudsdk.command_lib.compute.routers.nats import nats_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import resources
@base.UniverseCompatible
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Update(base.UpdateCommand):
"""Update a NAT on a Compute Engine router."""
@classmethod
def Args(cls, parser):
cls.ROUTER_ARG = routers_flags.RouterArgumentForNat()
cls.ROUTER_ARG.AddArgument(parser)
base.ASYNC_FLAG.AddToParser(parser)
compute_flags.AddRegionFlag(parser, 'NAT', operation_type='create')
nats_flags.AddNatNameArg(parser, operation_type='create')
nats_flags.AddCommonNatArgs(parser, for_create=False)
def Run(self, args):
holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
messages = holder.client.messages
service = holder.client.apitools_client.routers
router_ref = self.ROUTER_ARG.ResolveAsResource(args, holder.resources)
request_type = messages.ComputeRoutersGetRequest
replacement = service.Get(request_type(**router_ref.AsDict()))
# Retrieve specified NAT and update base fields.
existing_nat = nats_utils.FindNatOrRaise(replacement, args.name)
nat = nats_utils.UpdateNatMessage(existing_nat, args, holder)
request_type = messages.ComputeRoutersPatchRequest
result = service.Patch(
request_type(
project=router_ref.project,
region=router_ref.region,
router=router_ref.Name(),
routerResource=replacement))
operation_ref = resources.REGISTRY.Parse(
result.name,
collection='compute.regionOperations',
params={
'project': router_ref.project,
'region': router_ref.region,
})
if args.async_:
log.UpdatedResource(
operation_ref,
kind='nat [{0}] in router [{1}]'.format(nat.name, router_ref.Name()),
is_async=True,
details='Run the [gcloud compute operations describe] command '
'to check the status of this operation.')
return result
target_router_ref = holder.resources.Parse(
router_ref.Name(),
collection='compute.routers',
params={
'project': router_ref.project,
'region': router_ref.region,
})
operation_poller = poller.Poller(service, target_router_ref)
return waiter.WaitFor(
operation_poller, operation_ref,
'Updating nat [{0}] in router [{1}]'.format(nat.name,
router_ref.Name()))
Update.detailed_help = {
'DESCRIPTION': """
*{command}* is used to update a NAT in a Compute Engine router.
""",
'EXAMPLES': """\
Change subnetworks and IP address resources associated with NAT:
$ {command} nat1 --router=my-router
--nat-external-ip-pool=ip-address2,ip-address3
--nat-custom-subnet-ip-ranges=subnet-2,subnet-3:secondary-range-2
Change minimum default ports allocated per VM associated with NAT:
$ {command} nat1 --router=my-router --min-ports-per-vm=128
Change connection timeouts associated with NAT:
$ {command} nat1 --router=my-router
--udp-idle-timeout=60s
--icmp-idle-timeout=60s
--tcp-established-idle-timeout=60s
--tcp-transitory-idle-timeout=60s
Reset connection timeouts associated NAT to default values:
$ {command} nat1 --router=my-router
--clear-udp-idle-timeout --clear-icmp-idle-timeout
--clear-tcp-established-idle-timeout
--clear-tcp-transitory-idle-timeout
""",
'API REFERENCE': """\
This command, when specified without alpha or beta, uses the compute/v1/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/v1/routers/
The beta command uses the compute/beta/routers API. The full documentation
for this API can be found at: https://cloud.google.com/compute/docs/reference/rest/beta/routers/
The alpha command uses the compute/alpha/routers API. Full documentation is not available for the alpha API.
""",
}