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,27 @@
# -*- 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.
"""Cloud Storage Insights dataset configurations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
class DatasetConfigs(base.Group):
"""Manage Cloud Storage Insights dataset configurations."""

View File

@@ -0,0 +1,148 @@
# -*- 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.
"""Implementation of create command for Insights dataset config."""
from apitools.base.py import exceptions as apitools_exceptions
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage import flags
from googlecloudsdk.command_lib.storage.insights.dataset_configs import create_update_util
from googlecloudsdk.command_lib.storage.insights.dataset_configs import log_util
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Create(base.Command):
"""Create a new dataset config for Insights."""
detailed_help = {
'DESCRIPTION': """
Create a new dataset config for Insights.
""",
'EXAMPLES': """
To create a dataset config with config name as "my_config" in location
"us-central1" and project numbers "123456" and "456789" belonging to
organization number "54321":
$ {command} my_config --location=us-central1
--source-projects=123456,456789 --organization=54321 --retention-period-days=1
To create a dataset config that automatically adds new buckets into
config:
$ {command} my_config --location=us-central1
--source-projects=123456,456789 --organization=54321
--auto-add-new-buckets --retention-period-days=1
""",
}
@classmethod
def Args(cls, parser):
parser.add_argument(
'DATASET_CONFIG_NAME',
type=str,
help='Provide human readable config name.',
)
parser.add_argument(
'--organization',
type=int,
required=True,
metavar='SOURCE_ORG_NUMBER',
help='Provide the source organization number.',
)
parser.add_argument(
'--identity',
type=str,
metavar='IDENTITY_TYPE',
choices=['IDENTITY_TYPE_PER_CONFIG', 'IDENTITY_TYPE_PER_PROJECT'],
default='IDENTITY_TYPE_PER_CONFIG',
help='The type of service account used in the dataset config.',
)
parser.add_argument(
'--auto-add-new-buckets',
action='store_true',
help=(
'Automatically include any new buckets created if they satisfy'
' criteria defined in config settings.'
),
)
flags.add_dataset_config_location_flag(parser)
flags.add_dataset_config_create_update_flags(parser)
def Run(self, args):
source_projects_list = None
if args.source_projects is not None:
source_projects_list = args.source_projects
elif args.source_projects_file is not None:
source_projects_list = create_update_util.get_source_configs_list(
args.source_projects_file, create_update_util.ConfigType.PROJECTS
)
source_folders_list = None
if args.source_folders is not None:
source_folders_list = args.source_folders
elif args.source_folders_file is not None:
source_folders_list = create_update_util.get_source_configs_list(
args.source_folders_file, create_update_util.ConfigType.FOLDERS
)
api_client = insights_api.InsightsApi()
try:
dataset_config_operation = api_client.create_dataset_config(
dataset_config_name=args.DATASET_CONFIG_NAME,
location=args.location,
destination_project=properties.VALUES.core.project.Get(),
organization_scope=args.enable_organization_scope,
source_projects_list=source_projects_list,
source_folders_list=source_folders_list,
organization_number=args.organization,
include_buckets_name_list=args.include_bucket_names,
include_buckets_prefix_regex_list=args.include_bucket_prefix_regexes,
exclude_buckets_name_list=args.exclude_bucket_names,
exclude_buckets_prefix_regex_list=args.exclude_bucket_prefix_regexes,
include_source_locations=args.include_source_locations,
exclude_source_locations=args.exclude_source_locations,
auto_add_new_buckets=args.auto_add_new_buckets,
retention_period=args.retention_period_days,
activity_data_retention_period=getattr(
args, 'activity_data_retention_period_days', None
),
identity_type=args.identity,
description=args.description,
)
log_util.dataset_config_operation_started_and_status_log(
'Create', args.DATASET_CONFIG_NAME, dataset_config_operation.name
)
except apitools_exceptions.HttpBadRequestError:
log.status.Print(
'We caught an exception while trying to create the'
' dataset-configuration.\nPlease check that the flags are set with'
' valid values. For example, config name must start with an'
" alphanumeric value and only contain '_' as a special character"
)
raise
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class CreateAlpha(Create):
"""Create a new dataset config for Insights."""

View File

@@ -0,0 +1,62 @@
# -*- 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.
"""Implementation of create-link command for Insights dataset config."""
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.dataset_configs import log_util
from googlecloudsdk.command_lib.storage.insights.dataset_configs import resource_args
@base.DefaultUniverseOnly
class CreateLink(base.Command):
"""Create a link to a BigQuery instance."""
detailed_help = {
'DESCRIPTION': """
Create link to the customer BigQuery instance for Insights dataset config.
""",
'EXAMPLES': """
To create a link to the customer BigQuery instance for config name:
"my_config" in location "us-central1":
$ {command} my_config --location=us-central1
To create a link for the same dataset config with fully specified name:
$ {command} projects/foo/locations/us-central1/datasetConfigs/my_config
""",
}
@staticmethod
def Args(parser):
resource_args.add_dataset_config_resource_arg(parser, 'to create link')
def Run(self, args):
client = insights_api.InsightsApi()
dataset_config_relative_name = (
args.CONCEPTS.dataset_config.Parse().RelativeName()
)
create_dataset_config_link_operation = client.create_dataset_config_link(
dataset_config_relative_name,
)
log_util.dataset_config_operation_started_and_status_log(
'Create link',
dataset_config_relative_name,
create_dataset_config_link_operation.name,
)

View File

@@ -0,0 +1,104 @@
# -*- 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.
"""Implementation of delete command for Insights dataset config."""
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.dataset_configs import log_util
from googlecloudsdk.command_lib.storage.insights.dataset_configs import resource_args
from googlecloudsdk.core.console import console_io
@base.DefaultUniverseOnly
class Delete(base.Command):
"""Delete dataset config for Insights."""
detailed_help = {
'DESCRIPTION': """
Delete an Insights dataset config.
""",
'EXAMPLES': """
To delete a dataset config with config name "my_config" in location
"us-central1":
$ {command} my_config --location=us-central1
To delete the same dataset config with fully specified name:
${command} projects/foo/locations/us-central1/datasetConfigs/my_config
To delete the same dataset config and unlink it from the BigQuery
instance:
$ {command} my_config --location=us-central1 --auto-delete-link
To delete the same dataset config without taking user consent:
$ {command} my_config --location=us-central1 --auto-delete-link
--force
""",
}
@staticmethod
def Args(parser):
resource_args.add_dataset_config_resource_arg(parser, 'to delete')
parser.add_argument(
'--auto-delete-link',
action='store_true',
help=(
'Delete the BigQuery instance links before the config gets deleted'
' explicitly.'
),
)
parser.add_argument(
'--force',
action='store_true',
help='Force delete the config by skipping the consent.',
)
def Run(self, args):
client = insights_api.InsightsApi()
dataset_config_relative_name = (
args.CONCEPTS.dataset_config.Parse().RelativeName()
)
if not args.force:
message = 'You are about to delete dataset config: {}'.format(
dataset_config_relative_name
)
console_io.PromptContinue(
message=message, throw_if_unattended=True, cancel_on_no=True
)
if args.auto_delete_link:
delete_dataset_config_link_operation = client.delete_dataset_config_link(
dataset_config_relative_name,
)
log_util.dataset_config_operation_started_and_status_log(
'Delete link',
dataset_config_relative_name,
delete_dataset_config_link_operation.name,
)
delete_dataset_config_operation = client.delete_dataset_config(
dataset_config_relative_name
)
log_util.dataset_config_operation_started_and_status_log(
'Delete',
dataset_config_relative_name,
delete_dataset_config_operation.name,
)

View File

@@ -0,0 +1,62 @@
# -*- 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.
"""Implementation of delete-link command for Insights dataset config."""
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.dataset_configs import log_util
from googlecloudsdk.command_lib.storage.insights.dataset_configs import resource_args
@base.DefaultUniverseOnly
class DeleteLink(base.Command):
"""Delete a link to a BigQuery instance."""
detailed_help = {
'DESCRIPTION': """
Delete a link to a BigQuery instance.
""",
'EXAMPLES': """
To unlink a dataset config with config name "my_config" in location
"us-central1":
$ {command} my_config --location=us-central1
To delete a link for the same dataset config with fully specified name:
$ {command} projects/foo/locations/us-central1/datasetConfigs/my_config
""",
}
@staticmethod
def Args(parser):
resource_args.add_dataset_config_resource_arg(parser, 'to delete link')
def Run(self, args):
client = insights_api.InsightsApi()
dataset_config_relative_name = (
args.CONCEPTS.dataset_config.Parse().RelativeName()
)
delete_dataset_config_link_operation = client.delete_dataset_config_link(
dataset_config_relative_name,
)
log_util.dataset_config_operation_started_and_status_log(
'Delete link',
dataset_config_relative_name,
delete_dataset_config_link_operation.name,
)

View File

@@ -0,0 +1,52 @@
# -*- 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.
"""Implementation of describe command for Insights dataset config."""
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.dataset_configs import resource_args
@base.DefaultUniverseOnly
class Describe(base.DescribeCommand):
"""Describe dataset config for Insights."""
detailed_help = {
'DESCRIPTION': """
Describe the Insights dataset config.
""",
'EXAMPLES': """
To describe a dataset config with config name "my_config" in location
"us-central1":
$ {command} my_config --location=us-central1
To describe the same dataset config with fully specified name:
$ {command} projects/foo/locations/us-central1/datasetConfigs/my_config
""",
}
@staticmethod
def Args(parser):
resource_args.add_dataset_config_resource_arg(parser, 'to describe')
def Run(self, args):
dataset_config_ref = args.CONCEPTS.dataset_config.Parse()
return insights_api.InsightsApi().get_dataset_config(
dataset_config_ref.RelativeName()
)

View File

@@ -0,0 +1,85 @@
# -*- 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.
"""Implementation of list command for Insights dataset config."""
import re
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage import flags
LOCATION_REGEX_PATTERN = re.compile(r'locations/(.*)/.*/')
def _transform_location(dataset_config):
matched_result = re.search(LOCATION_REGEX_PATTERN, dataset_config['name'])
if matched_result and matched_result.group(1) is not None:
return matched_result.group(1)
else:
return 'N/A-Misformated Value'
_TRANSFORMS = {'location_transform': _transform_location}
@base.DefaultUniverseOnly
class List(base.ListCommand):
"""List returns all the Insights dataset configs for given location."""
detailed_help = {
'DESCRIPTION': """
List Cloud storage Insights dataset configs.
""",
'EXAMPLES': """
List all dataset configs in all locations:
$ {command}
List all dataset configs for location "us-central1":
$ {command} --location=us-central1
List all dataset configs with a page size of "20":
$ {command} --location=us-central1 --page-size=20
List all dataset configs with JSON formatting:
$ {command} --location=us-central1 --format=json
""",
}
@staticmethod
def Args(parser):
flags.add_dataset_config_location_flag(parser, is_required=False)
parser.display_info.AddFormat("""
table(
uid:label=DATASET_CONFIG_ID,
name.basename():label=DATASET_CONFIG_NAME,
location_transform():label=LOCATION,
sourceProjects.projectNumbers:label=SOURCE_PROJECTS,
organizationNumber:label=ORGANIZATION_NUMBER,
retentionPeriodDays:label=RETENTION_PERIOD_DAYS,
datasetConfigState:label=STATE
)
""")
parser.display_info.AddTransforms(_TRANSFORMS)
def Run(self, args):
return insights_api.InsightsApi().list_dataset_config(
location=args.location, page_size=args.page_size
)

View File

@@ -0,0 +1,170 @@
# -*- 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.
"""Implementation of update command for Insights dataset config."""
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage import flags
from googlecloudsdk.command_lib.storage.insights.dataset_configs import create_update_util
from googlecloudsdk.command_lib.storage.insights.dataset_configs import log_util
from googlecloudsdk.command_lib.storage.insights.dataset_configs import resource_args
from googlecloudsdk.core.console import console_io
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.GA)
class Update(base.Command):
"""Updates a dataset config for Insights."""
detailed_help = {
'DESCRIPTION': """
Update a dataset config for Insights.
""",
'EXAMPLES': """
To update the description for a dataset config "my_config" in
location "us-central1":
$ {command} my_config --location=us-central1 --description="a user provided description"
To update the same dataset config with fully specified name:
$ {command} projects/foo/locations/us-central1/datasetConfigs/my_config
To update the retention period days for the dataset config "my_config" in
location "us-central1":
$ {command} my_config --location=us-central1
--retention-period-days=20
""",
}
@classmethod
def Args(cls, parser):
parser.add_argument(
'--auto-add-new-buckets',
choices=['true', 'false'],
help=(
'Automatically include any new buckets created if they satisfy'
' criteria defined in config settings.'
),
)
resource_args.add_dataset_config_resource_arg(parser, 'to update')
flags.add_dataset_config_create_update_flags(parser, is_update=True)
def _get_source_projects_list(self, args):
if args.source_projects is not None:
return args.source_projects
elif args.source_projects_file is not None:
return create_update_util.get_source_configs_list(
args.source_projects_file, create_update_util.ConfigType.PROJECTS
)
return None
def _get_source_folders_list(self, args):
if args.source_folders is not None:
return args.source_folders
elif args.source_folders_file is not None:
return create_update_util.get_source_configs_list(
args.source_folders_file, create_update_util.ConfigType.FOLDERS
)
return None
def _get_auto_add_new_buckets(self, args):
if args.auto_add_new_buckets is not None:
if args.auto_add_new_buckets == 'true':
return True
elif args.auto_add_new_buckets == 'false':
return False
return None
def Run(self, args):
client = insights_api.InsightsApi()
dataset_config_relative_name = (
args.CONCEPTS.dataset_config.Parse().RelativeName()
)
if args.retention_period_days is not None:
if args.retention_period_days > 0:
message = (
'You are about to change retention period for dataset config: {}'
.format(dataset_config_relative_name)
)
console_io.PromptContinue(
message=message, throw_if_unattended=True, cancel_on_no=True
)
else:
raise ValueError('retention-period-days value must be greater than 0')
source_projects_list = self._get_source_projects_list(args)
source_folders_list = self._get_source_folders_list(args)
new_scope = create_update_util.get_new_source_config(
args.enable_organization_scope,
source_projects_list,
source_folders_list,
)
if new_scope is not None:
existing_scope = create_update_util.get_existing_source_config(
dataset_config_relative_name, client
)
message = (
'You are about to change scope of dataset config:'
f' {dataset_config_relative_name} from {existing_scope} to'
f' {new_scope}. Refer'
' https://cloud.google.com/storage/docs/insights/datasets#dataset-config'
' for more details.'
)
console_io.PromptContinue(
message=message, throw_if_unattended=True, cancel_on_no=True
)
auto_add_new_buckets = self._get_auto_add_new_buckets(args)
update_dataset_config_operation = client.update_dataset_config(
dataset_config_relative_name,
organization_scope=args.enable_organization_scope,
source_projects_list=source_projects_list,
source_folders_list=source_folders_list,
include_buckets_name_list=args.include_bucket_names,
include_buckets_prefix_regex_list=args.include_bucket_prefix_regexes,
exclude_buckets_name_list=args.exclude_bucket_names,
exclude_buckets_prefix_regex_list=args.exclude_bucket_prefix_regexes,
include_source_locations=args.include_source_locations,
exclude_source_locations=args.exclude_source_locations,
auto_add_new_buckets=auto_add_new_buckets,
retention_period=args.retention_period_days,
activity_data_retention_period=getattr(
args, 'activity_data_retention_period_days', None
),
description=args.description,
)
log_util.dataset_config_operation_started_and_status_log(
'Update',
dataset_config_relative_name,
update_dataset_config_operation.name,
)
return update_dataset_config_operation
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class UpdateAlpha(Update):
"""Updates a dataset config for Insights."""