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,33 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 commands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA, base.ReleaseTrack.GA)
class Insights(base.Group):
"""Manage Cloud Storage inventory reports."""
def Filter(self, context, args):
# TODO(b/190541521): Determine if command group works with project number
base.RequireProjectID(args)
del context, args

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."""

View File

@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 inventory report configurations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
class InventoryReports(base.Group):
"""Manage Cloud Storage inventory report configurations."""

View File

@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 inventory reports."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import datetime
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage import errors
from googlecloudsdk.command_lib.storage import flags
from googlecloudsdk.command_lib.storage import storage_url
from googlecloudsdk.core import log
@base.DefaultUniverseOnly
class Create(base.Command):
"""Create a new inventory report config."""
detailed_help = {
'DESCRIPTION': """
Create an inventory report config that defines how often
inventory reports are generated, the metadata fields you want the reports
to include, and a bucket/prefix in which to store the reports, also known
as the destination.
""",
'EXAMPLES': """
To create an inventory report about "my-bucket" that will store report
details in "report-bucket" with the prefix "save-path/".
$ {command} gs://my-bucket --destination=gs://report-bucket/save-path/
""",
}
@staticmethod
def Args(parser):
parser.add_argument(
'source_bucket_url',
type=str,
help='URL of the source bucket that will contain the '
'inventory report configuration.')
flags.add_inventory_reports_flags(parser, require_create_flags=True)
def Run(self, args):
source_bucket = storage_url.storage_url_from_string(
storage_url.add_gcs_scheme_if_missing(args.source_bucket_url))
if (not isinstance(source_bucket, storage_url.CloudUrl) or
not source_bucket.is_bucket()):
raise errors.InvalidUrlError(
'Invalid bucket URL: {}. Only bucket URLs are accepted'
' for SOURCE_BUCKET_URL. Example: "gs://bucket"'.format(
args.source_bucket_url))
if args.destination is not None:
destination = storage_url.storage_url_from_string(
storage_url.add_gcs_scheme_if_missing(args.destination))
else:
destination = storage_url.CloudUrl(
scheme=source_bucket.scheme,
bucket_name=source_bucket.bucket_name,
resource_name='inventory_reports/')
if args.schedule_starts is not None:
start_date = args.schedule_starts
else:
start_date = (datetime.datetime.now(datetime.timezone.utc) +
datetime.timedelta(days=1)).date()
if args.schedule_repeats_until is not None:
end_date = args.schedule_repeats_until
else:
end_date = start_date + datetime.timedelta(days=365)
report_config = insights_api.InsightsApi().create_inventory_report(
source_bucket=source_bucket.bucket_name,
destination_url=destination,
metadata_fields=list(args.metadata_fields),
start_date=start_date,
end_date=end_date,
frequency=args.schedule_repeats,
csv_delimiter=args.csv_delimiter,
csv_separator=args.csv_separator,
csv_header=args.csv_header,
parquet=args.parquet,
display_name=args.display_name,
)
log.status.Print(
'Created report configuration: {}'.format(report_config.name))

View File

@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 command for deleting inventory report configurations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.inventory_reports import resource_args
from googlecloudsdk.core import log
@base.DefaultUniverseOnly
class Delete(base.Command):
"""Delete an inventory report config."""
detailed_help = {
'DESCRIPTION':
"""
Delete an inventory report config.
""",
'EXAMPLES':
"""
To delete an inventory report config with ID=1234,
location=us-central1 and project=foo:
$ {command} 1234 --location=us-central1 --project=foo
To delete the same inventory report config with fully specified name:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234
To delete the report config with all generated report details:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234 --force
""",
}
@staticmethod
def Args(parser):
resource_args.add_report_config_resource_arg(parser, 'to delete')
parser.add_argument(
'--force',
action='store_true',
help='If set, all report details for this report config'
' will be deleted.'
)
def Run(self, args):
report_config_name = args.CONCEPTS.report_config.Parse().RelativeName()
insights_api.InsightsApi().delete_inventory_report(
report_config_name, args.force
)
log.status.Print('Deleted report config: {}'.format(
report_config_name))

View File

@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 insights inventory-reports describe command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.inventory_reports import resource_args
@base.DefaultUniverseOnly
class Describe(base.DescribeCommand):
"""Describe an inventory report config."""
detailed_help = {
'DESCRIPTION':
"""
Describe an inventory report config.
""",
'EXAMPLES':
"""
To describe an inventory report config with ID=1234,
location=us-central1, and project=foo:
$ {command} 1234 --location=us-central1 --project=foo
To describe the same inventory report config with fully specified name:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234
Describe the same inventory report config with JSON formatting, only
returning the "displayName" field:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234 --format="json(displayName)"
""",
}
@staticmethod
def Args(parser):
resource_args.add_report_config_resource_arg(parser, 'to describe')
def Run(self, args):
report_config_ref = args.CONCEPTS.report_config.Parse()
return insights_api.InsightsApi().get_inventory_report(
report_config_ref.RelativeName()
)

View File

@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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.
"""Retrieve details of Cloud Storage inventory reports."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
class Details(base.Group):
"""Retrieve details of inventory reports."""

View File

@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 insights inventory-reports details describe command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.inventory_reports import resource_args
from googlecloudsdk.core import log
@base.DefaultUniverseOnly
class Describe(base.DescribeCommand):
"""Describe inventory reports detail."""
detailed_help = {
'DESCRIPTION':
"""
Describe the inventory report detail.
""",
'EXAMPLES':
"""
To describe an inventory report detail with ID=4568,
location=us-central1, project=foo, and report config ID=1234:
$ {command} 1234 --location=us-central1 --project=foo --report-config=1234
To describe the same inventory report detail with fully specified name:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234/reportDetails/5678
To describe the same inventory report detail with JSON formatting, only returning
the "status" field:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234/reportDetails/5678 --format="json(status)"
""",
}
@staticmethod
def Args(parser):
resource_args.add_report_detail_resource_arg(parser, 'to describe')
def Run(self, args):
report_detail_ref = args.CONCEPTS.report_detail.Parse()
report_details = insights_api.InsightsApi().get_report_details(
report_detail_ref.RelativeName())
if report_details:
log.status.Print(
'To download the reports, use the `gcloud storage cp` command.')
return report_details

View File

@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 insights inventory."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage.insights.inventory_reports import resource_args
@base.DefaultUniverseOnly
class List(base.ListCommand):
"""List inventory report details."""
detailed_help = {
'DESCRIPTION':
"""
List all inventory report details generated by a given inventory report
config.
""",
'EXAMPLES':
"""
To list all inventory report details for report config ID=1234,
location=us-central1, and project=foo:
$ {command} 1234 --location=us-central1 --project=foo
To list all the same inventory report details with fully specified name
of the report config:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234
To list all inventory reports, only returning the "status" key:
$ {command} projects/a/locations/b/reportConfigs/some-id --format="json(status)"
""",
}
@staticmethod
def Args(parser):
resource_args.add_report_config_resource_arg(
parser, 'for which the report details should be listed')
parser.display_info.AddFormat(
"""
table(
format('{}',name.basename()):label=REPORT_DETAIL_ID,
snapshotTime,
status.message:label=STATUS
)
"""
)
def Run(self, args):
report_config_name = args.CONCEPTS.report_config.Parse().RelativeName()
return insights_api.InsightsApi().list_report_details(
report_config_name, args.page_size)

View File

@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 insights inventory-reports list command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage import errors
from googlecloudsdk.command_lib.storage import storage_url
@base.DefaultUniverseOnly
class List(base.ListCommand):
"""Lists all inventory report configs."""
detailed_help = {
'DESCRIPTION':
"""
List Cloud Storage inventory report configs.
""",
'EXAMPLES':
"""
List all inventory report configs in the source bucket
"my-bucket":
$ {command} --source=gs://my-bucket
List buckets with JSON formatting, only returning the "displayName" field:
$ {command} --source=gs://my-bucket --format="json(displayName)"
""",
}
@staticmethod
def Args(parser):
parser.add_argument(
'--source',
metavar='SOURCE_BUCKET_URL',
help='Specifies URL of the source bucket that contains the inventory '
'report configuration.')
parser.add_argument(
'--location',
help='The location of the report configs.')
parser.display_info.AddFormat(
"""
table(
format('{}',name.basename()):label=REPORT_CONFIG_ID,
format(
'{:04d}-{:02d}-{:02d}',
frequencyOptions.startDate.year,
frequencyOptions.startDate.month,
frequencyOptions.startDate.day):label=START_DATE,
format(
'{:04d}-{:02d}-{:02d}',
frequencyOptions.endDate.year,
frequencyOptions.endDate.month,
frequencyOptions.endDate.day):label=END_DATE,
format(
'gs://{}',
objectMetadataReportOptions.storageFilters.bucket
):label=SOURCE_BUCKET:wrap,
format(
'gs://{}/{}',
objectMetadataReportOptions.storageDestinationOptions.bucket,
objectMetadataReportOptions.storageDestinationOptions.
destinationPath.flatten()):label=DESTINATION:wrap
)
"""
)
def Run(self, args):
if args.source is None and args.location is None:
raise errors.Error(
'At least one of --source or --location is required.')
source_bucket = storage_url.storage_url_from_string(
args.source) if args.source is not None else None
return insights_api.InsightsApi().list_inventory_reports(
source_bucket, location=args.location, page_size=args.page_size
)

View File

@@ -0,0 +1,122 @@
# -*- coding: utf-8 -*- #
# Copyright 2022 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 inventory reports."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import arg_parsers
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.storage import errors
from googlecloudsdk.command_lib.storage import flags
from googlecloudsdk.command_lib.storage import storage_url
from googlecloudsdk.command_lib.storage.insights.inventory_reports import resource_args
@base.DefaultUniverseOnly
class Update(base.Command):
"""Update an inventory report config."""
detailed_help = {
'DESCRIPTION': """
Update an inventory report config.
""",
'EXAMPLES': """
To update the display-name of an inventory report config with ID=1234,
location=us-central1, and project=foo:
$ {command} 1234 --location=us-central1 --project=foo --display-name=bar
To update the same inventory report config with fully specified name:
$ {command} /projects/foo/locations/us-central1/reportConfigs/1234 --display-name=bar
""",
}
@staticmethod
def Args(parser):
resource_args.add_report_config_resource_arg(parser, 'to update')
flags.add_inventory_reports_flags(parser)
metadata_fields_group = parser.add_group(mutex=True)
metadata_fields_add_remove_group = metadata_fields_group.add_group(
help='Add and Remove flags for metadata fields')
flags.add_inventory_reports_metadata_fields_flag(metadata_fields_group)
metadata_fields_add_remove_group.add_argument(
'--add-metadata-fields',
metavar='METADATA_FIELDS',
type=arg_parsers.ArgList(
choices=flags.OPTIONAL_INVENTORY_REPORTS_METADATA_FIELDS),
help='Adds fields to the metadata_fields list.')
metadata_fields_add_remove_group.add_argument(
'--remove-metadata-fields',
metavar='METADATA_FIELDS',
type=arg_parsers.ArgList(
choices=flags.OPTIONAL_INVENTORY_REPORTS_METADATA_FIELDS),
help='Removes fields from the metadata_fields list.')
# We don't addd --clear-metadata-fields because certain metadata-fields
# like name and project must be always present.
def Run(self, args):
client = insights_api.InsightsApi()
report_config_name = args.CONCEPTS.report_config.Parse().RelativeName()
if args.add_metadata_fields or args.remove_metadata_fields:
# Get the existing report config so that we can modify
# the metadata_fields list.
report_config = client.get_inventory_report(report_config_name)
metadata_fields = set(
report_config.objectMetadataReportOptions.metadataFields)
if args.add_metadata_fields is not None:
for field in args.add_metadata_fields:
metadata_fields.add(field)
if args.remove_metadata_fields is not None:
for field in args.remove_metadata_fields:
if field not in metadata_fields:
raise errors.Error(
'Cannot remove non-existing metadata field: {}'.format(field))
metadata_fields.remove(field)
metadata_fields_list = list(metadata_fields)
elif args.metadata_fields:
metadata_fields_list = list(args.metadata_fields)
else:
# messages.ReportConfig does not accept None value.
# This should be safe, as an empty list has no effect unless we add
# the field to the updateMask.
metadata_fields_list = []
if args.destination is not None:
destination_url = storage_url.storage_url_from_string(
storage_url.add_gcs_scheme_if_missing(args.destination))
else:
destination_url = None
return client.update_inventory_report(
report_config_name,
destination_url=destination_url,
metadata_fields=metadata_fields_list,
start_date=args.schedule_starts,
end_date=args.schedule_repeats_until,
frequency=args.schedule_repeats,
csv_separator=args.csv_separator,
csv_delimiter=args.csv_delimiter,
csv_header=args.csv_header,
parquet=args.parquet,
display_name=args.display_name,
)

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.
"""Storage Insights operations commands."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class Operations(base.Group):
"""Manage insights operations."""

View File

@@ -0,0 +1,57 @@
# -*- 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 to cancel an insights operation."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
from googlecloudsdk.core import log
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
@base.DefaultUniverseOnly
class Cancel(base.Command):
"""Cancel an insights operation."""
detailed_help = {
'DESCRIPTION': """\
Cancel an insights operation. Since operations are asynchronous, this
request is best effort and may fail in cases such as when the operation
is already complete.
""",
'EXAMPLES': """\
To cancel the operation "12345" in "us-central1" for the project
"my-project", run:
$ {command} projects/my-project/locations/us-central1/operations/12345
""",
}
@staticmethod
def Args(parser):
parser.add_argument(
'operation_name',
help=(
'The operation name in the format'
' "projects/PROJECT/locations/LOCATION/operations/OPERATION_ID".'
),
)
def Run(self, args):
insights_api.InsightsApi().cancel_operation(args.operation_name)
log.status.Print('Sent cancel request for {}'.format(args.operation_name))

View File

@@ -0,0 +1,53 @@
# -*- 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 to describe an insights operation."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class Describe(base.Command):
"""Describe an insights operation."""
detailed_help = {
'DESCRIPTION': """\
Get details about an insights operation.
""",
'EXAMPLES': """\
To describe the operation "12345" in "us-central1" for the project
"my-project", run:
$ {command} projects/my-project/locations/us-central1/operations/12345
""",
}
@staticmethod
def Args(parser):
parser.add_argument(
'operation_name',
help=(
'The operation name in the format'
' "projects/PROJECT/locations/LOCATION/operations/OPERATION_ID".'
),
)
def Run(self, args):
return insights_api.InsightsApi().get_operation(args.operation_name)

View File

@@ -0,0 +1,56 @@
# -*- 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 to list Storage Insights operations."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.storage import insights_api
from googlecloudsdk.calliope import base
@base.DefaultUniverseOnly
@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
class List(base.ListCommand):
"""List insights operations."""
detailed_help = {
'DESCRIPTION': """\
List storage operations.
""",
'EXAMPLES': """\
To list all operations in "us-central1" for the project "my-project", run:
$ {command} projects/my-project/locations/us-central1
To list operations in JSON format, run:
$ {command} projects/my-project/locations/us-central1 --format=json
""",
}
@staticmethod
def Args(parser):
parser.add_argument(
'parent_resource_name',
help=(
'The operation parent resource in the format'
' ""projects/PROJECT/locations/LOCATION".'
),
)
def Run(self, args):
return insights_api.InsightsApi().list_operations(args.parent_resource_name)