Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/python-securitycenter #8493

Merged
merged 111 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
160aff4
chore(samples): move samples and use standard templates for testing (…
busunkim96 Jul 14, 2020
288e035
chore(deps): update dependency google-cloud-pubsub to v1.7.0 (#42)
renovate-bot Jul 15, 2020
fb71400
docs: Update Security Command Center UpdateNotificationConfig sample,…
hannah-tsai Jul 23, 2020
9127d9c
feat: add field severity to findings; update retry configs (#53)
yoshi-automation Sep 10, 2020
0c27bf8
fix(sample): fix a broken test (#63)
Sep 18, 2020
02def84
chore(deps): update dependency google-cloud-securitycenter to v0.7.1 …
renovate-bot Sep 29, 2020
52b3040
chore(deps): update dependency google-cloud-pubsub to v2 (#61)
renovate-bot Oct 2, 2020
21e6a58
feat!: generate with microgenerator (#49)
busunkim96 Oct 8, 2020
fdd8f94
chore(deps): update dependency google-cloud-securitycenter to v1 (#70)
renovate-bot Oct 8, 2020
15815bb
docs: update snippets_findings tags (#78)
Pglinsman Oct 30, 2020
8024423
docs: update snippets_list_assets tags (#77)
Pglinsman Oct 30, 2020
21a7879
chore(deps): update dependency google-cloud-pubsub to v2.2.0 (#84)
renovate-bot Dec 5, 2020
8bce842
docs: add securitycenter prefix to samples, wrap published samples an…
Strykrol Dec 15, 2020
4315441
chore(deps): update dependency google-cloud-securitycenter to v1.1.0 …
renovate-bot Dec 22, 2020
7523bbb
chore: update templates (#88)
yoshi-automation Dec 22, 2020
dce59da
chore(deps): update dependency google-cloud-pubsub to v2.3.0 (#102)
renovate-bot Feb 9, 2021
40fee09
docs: standardize new tag with existing tags from other languages (#104)
Feb 18, 2021
5f50a2d
chore(deps): update dependency google-cloud-pubsub to v2.4.0 (#107)
renovate-bot Feb 24, 2021
98165b9
docs: Fix conflict tag introduced in PR 104 (#106)
Feb 24, 2021
b568a86
fix: fix retry deadlines (#116)
yoshi-automation Mar 30, 2021
aabac32
chore(deps): update dependency google-cloud-pubsub to v2.4.1 (#119)
renovate-bot Mar 31, 2021
a2a3061
chore: migrate to owlbot (#129)
parthea Apr 27, 2021
6a3b706
chore(deps): update dependency google-cloud-securitycenter to v1.2.0 …
renovate-bot May 20, 2021
1f25195
test: log message.data for debugging (#126)
dandhlee May 20, 2021
3d88ea6
chore(deps): update dependency google-cloud-pubsub to v2.5.0 (#138)
renovate-bot May 20, 2021
264fc19
docs: remove unused region tags (#108)
May 20, 2021
5e74c01
chore: new owl bot post processor docker image (#155)
gcf-owl-bot[bot] May 22, 2021
21da4bc
chore(deps): update dependency google-cloud-securitycenter to v1.3.0 …
renovate-bot Jun 4, 2021
ee6df16
chore(deps): update dependency google-cloud-securitycenter to v1.3.1 …
renovate-bot Jun 18, 2021
552a6a1
chore(deps): update dependency google-cloud-pubsub to v2.6.0 (#168)
renovate-bot Jun 26, 2021
8202aea
chore(deps): update dependency google-cloud-pubsub to v2.6.1 (#175)
renovate-bot Jul 12, 2021
2683324
chore(deps): update dependency google-cloud-securitycenter to v1.4.0 …
renovate-bot Jul 15, 2021
f1ac850
feat: add Samples section to CONTRIBUTING.rst (#195)
gcf-owl-bot[bot] Jul 22, 2021
8396af5
chore(deps): update dependency google-cloud-securitycenter to v1.5.0 …
renovate-bot Jul 28, 2021
0fcc028
chore(deps): update dependency google-cloud-pubsub to v2.7.0 (#203)
renovate-bot Jul 29, 2021
eac0296
chore: fix INSTALL_LIBRARY_FROM_SOURCE in noxfile.py (#205)
gcf-owl-bot[bot] Aug 12, 2021
1c668cb
chore: drop mention of Python 2.7 from templates (#207)
gcf-owl-bot[bot] Aug 13, 2021
d11c7c2
chore(samples): use timezone aware datetimes (#208)
busunkim96 Aug 27, 2021
8bec4dc
chore(deps): update dependency google-cloud-pubsub to v2.8.0 (#216)
renovate-bot Sep 7, 2021
e1590d7
chore: blacken samples noxfile template (#218)
gcf-owl-bot[bot] Sep 17, 2021
a2271a7
chore(deps): update dependency google-cloud-securitycenter to v1.5.1 …
renovate-bot Sep 27, 2021
0afba93
chore: fail samples nox session if python version is missing (#226)
gcf-owl-bot[bot] Sep 30, 2021
bcaa465
chore(deps): update dependency google-cloud-securitycenter to v1.5.2 …
renovate-bot Sep 30, 2021
ba98724
chore(deps): update dependency google-cloud-securitycenter to v1.6.0 …
renovate-bot Oct 7, 2021
86213ab
chore(python): Add kokoro configs for python 3.10 samples testing (#234)
gcf-owl-bot[bot] Oct 8, 2021
c45994a
chore(deps): update dependency google-cloud-securitycenter to v1.7.0 …
renovate-bot Oct 11, 2021
bf7a61f
feat: add support for python 3.10 (#238)
parthea Nov 5, 2021
3801f2f
chore(deps): update dependency google-cloud-pubsub to v2.9.0 (#251)
renovate-bot Nov 12, 2021
5881049
chore(python): add .github/CODEOWNERS as a templated file (#252)
gcf-owl-bot[bot] Nov 12, 2021
5078179
chore(samples): Add check for tests in directory (#264)
gcf-owl-bot[bot] Jan 11, 2022
15ee3c4
chore(deps): update dependency google-cloud-securitycenter to v1.8.0 …
renovate-bot Jan 19, 2022
c0c0b63
chore(python): Noxfile recognizes that tests can live in a folder (#270)
gcf-owl-bot[bot] Jan 19, 2022
6811864
chore(deps): update all dependencies (#286)
renovate-bot Mar 1, 2022
0186b1a
chore: Adding support for pytest-xdist and pytest-parallel (#295)
gcf-owl-bot[bot] Mar 4, 2022
af74c17
chore(deps): update dependency google-cloud-pubsub to v2.10.0 (#297)
renovate-bot Mar 5, 2022
a7e9b82
chore(deps): update dependency google-cloud-securitycenter to v1.10.0…
renovate-bot Mar 7, 2022
3248759
chore(deps): update dependency google-cloud-pubsub to v2.11.0 (#299)
renovate-bot Mar 10, 2022
01653f4
docs(samples): added mute config samples (#276)
Sita04 Mar 25, 2022
6444a15
chore(python): use black==22.3.0 (#307)
gcf-owl-bot[bot] Mar 29, 2022
952308a
docs(samples): included snippet for unmute finding (#308)
Sita04 Mar 30, 2022
99a2a6f
chore(deps): update dependency google-cloud-pubsub to v2.12.0 (#318)
renovate-bot Apr 7, 2022
9f8100f
chore(python): add nox session to sort python imports (#324)
gcf-owl-bot[bot] Apr 21, 2022
517104d
docs(samples): add bigquery export samples (#315)
Sita04 Apr 26, 2022
9629a04
chore(deps): update all dependencies (#327)
renovate-bot Apr 26, 2022
a2f239e
chore(deps): update dependency google-cloud-securitycenter to v1.11.0…
renovate-bot May 5, 2022
2b03dd3
chore(deps): update dependency google-cloud-bigquery to v3.1.0 (#332)
renovate-bot May 9, 2022
c5e53a9
chore(deps): update dependency google-cloud-pubsub to v2.12.1 (#333)
renovate-bot May 12, 2022
ff62ae4
fix: require python 3.7+ (#349)
gcf-owl-bot[bot] Jul 10, 2022
89ab1ac
chore(deps): update all dependencies (#342)
renovate-bot Jul 17, 2022
827589d
chore(deps): update all dependencies (#357)
renovate-bot Aug 2, 2022
66264df
chore(deps): update all dependencies (#359)
renovate-bot Aug 9, 2022
25c7217
chore(deps): update dependency google-cloud-pubsub to v2.13.5 (#361)
renovate-bot Aug 11, 2022
8ba7e2f
chore(deps): update dependency google-cloud-pubsub to v2.13.6 (#363)
renovate-bot Aug 12, 2022
bd51fdb
chore(deps): update dependency google-cloud-securitycenter to v1.13.0…
renovate-bot Aug 16, 2022
332b5c4
chore(deps): update dependency google-cloud-bigquery to v3.3.2 (#366)
renovate-bot Aug 19, 2022
8d8a3d3
chore(deps): update dependency google-cloud-securitycenter to v1.14.0…
renovate-bot Aug 24, 2022
476786f
chore(deps): update dependency google-cloud-securitycenter to v1.15.0…
renovate-bot Sep 2, 2022
781b387
chore(deps): update dependency pytest to v7.1.3 (#384)
renovate-bot Sep 6, 2022
0ed25e1
chore: detect samples tests in nested directories (#389)
gcf-owl-bot[bot] Sep 13, 2022
1c4e4ec
chore(deps): update dependency google-cloud-securitycenter to v1.16.0…
renovate-bot Sep 16, 2022
4203f60
chore(deps): update dependency google-cloud-pubsub to v2.13.7 (#393)
renovate-bot Sep 26, 2022
1f29105
chore(deps): update dependency google-cloud-bigquery to v3.3.3 (#396)
renovate-bot Oct 4, 2022
49fa28a
chore(deps): update dependency google-cloud-securitycenter to v1.16.1…
renovate-bot Oct 4, 2022
839d305
chore(deps): update dependency google-cloud-securitycenter to v1.16.2…
renovate-bot Oct 10, 2022
20e5f3f
chore(deps): update all dependencies (#402)
renovate-bot Oct 18, 2022
22b343b
chore(deps): update dependency pytest to v7.2.0 (#406)
renovate-bot Oct 26, 2022
c451317
docs(samples): update samples to include new parent levels (folder an…
Sita04 Oct 27, 2022
3648683
Merge remote-tracking branch 'migration/main' into python-securitycen…
donmccasland Nov 11, 2022
c9196d8
removing noxfile.py
donmccasland Nov 11, 2022
7f90301
Update securitycenter/snippets/noxfile_config.py
dandhlee Nov 11, 2022
7e6cfc5
revert noxfile config test
dandhlee Nov 11, 2022
50e6888
Merge branch 'main' into python-securitycenter-migration
dandhlee Nov 11, 2022
3b9c4ea
Merge branch 'main' into python-securitycenter-migration
donmccasland Nov 11, 2022
cccd33f
Adding CODEOWNERS and blunderbuss config
donmccasland Nov 11, 2022
c6b218c
updting noxfile to use old securitycenter test project
donmccasland Nov 11, 2022
b15434c
fixing noxfile config
donmccasland Nov 11, 2022
ed6f205
fixing noxfile config
donmccasland Nov 11, 2022
dd25c5b
Update securitycenter/snippets/noxfile_config.py
leahecole Nov 11, 2022
1cc7b7e
Merge branch 'main' into python-securitycenter-migration
donmccasland Nov 11, 2022
260943b
Merge branch 'main' into python-securitycenter-migration
Sita04 Nov 17, 2022
c308722
Merge branch 'main' into python-securitycenter-migration
donmccasland Nov 17, 2022
c823bc0
Merge branch 'main' into python-securitycenter-migration
donmccasland Jan 18, 2023
a76d04b
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 24, 2023
5b9c716
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 24, 2023
316cd0d
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 24, 2023
4df1c9f
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 25, 2023
b5b63b0
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 25, 2023
60ca150
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 25, 2023
0b26c3d
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 25, 2023
8139c00
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 25, 2023
95bef4f
Merge branch 'main' into python-securitycenter-migration
Sita04 Jan 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
/run/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/run/django/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/secretmanager/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/securitycenter/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
/storagetransfer/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
/texttospeech/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
Expand Down
4 changes: 4 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ assign_issues_by:
- 'api: pubsublite'
to:
- anguillanneuf
- labels:
- 'api: securitycenter'
to:
- GoogleCloudPlatform/dee-infra
- labels:
- 'api: spanner'
to:
Expand Down
1 change: 1 addition & 0 deletions securitycenter/AUTHORING_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md
1 change: 1 addition & 0 deletions securitycenter/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/CONTRIBUTING.md
40 changes: 40 additions & 0 deletions securitycenter/snippets/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2020 Google LLC
#
# 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.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be inported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7"],
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
# 'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {
"GCLOUD_ORGANIZATION": "1081635000895",
"GCLOUD_PROJECT": "project-a-id",
"GCLOUD_PUBSUB_TOPIC": "projects/project-a-id/topics/notifications-sample-topic",
"GCLOUD_PUBSUB_SUBSCRIPTION": "notification-sample-subscription",
},
dandhlee marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions securitycenter/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==7.2.0
google-cloud-bigquery==3.3.5
2 changes: 2 additions & 0 deletions securitycenter/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-cloud-pubsub==2.13.10
google-cloud-securitycenter==1.16.2
190 changes: 190 additions & 0 deletions securitycenter/snippets/snippets_bigquery_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#!/usr/bin/env python
#
# Copyright 2022 Google LLC
#
# 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
#
# https://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.

"""Snippets on exporting findings from Security Command Center to BigQuery."""


# [START securitycenter_create_bigquery_export]


def create_bigquery_export(
parent: str, export_filter: str, bigquery_dataset_id: str, bigquery_export_id: str
):

from google.cloud import securitycenter

"""
Create export configuration to export findings from a project to a BigQuery dataset.
Optionally specify filter to export certain findings only.

Args:
parent: Use any one of the following resource paths:
- organizations/{organization_id}
- folders/{folder_id}
- projects/{project_id}
export_filter: Expression that defines the filter to apply across create/update events of findings.
bigquery_dataset_id: The BigQuery dataset to write findings' updates to.
bigquery_export_id: Unique identifier provided by the client.
- example id: f"default-{str(uuid.uuid4()).split('-')[0]}"
For more info, see:
https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to
"""
client = securitycenter.SecurityCenterClient()

# Create the BigQuery export configuration.
bigquery_export = securitycenter.BigQueryExport()
bigquery_export.description = "Export low and medium findings if the compute resource has an IAM anomalous grant"
bigquery_export.filter = export_filter
bigquery_export.dataset = f"{parent}/datasets/{bigquery_dataset_id}"

request = securitycenter.CreateBigQueryExportRequest()
request.parent = parent
request.big_query_export = bigquery_export
request.big_query_export_id = bigquery_export_id

# Create the export request.
response = client.create_big_query_export(request)

print(f"BigQuery export request created successfully: {response.name}\n")


# [END securitycenter_create_bigquery_export]


# [START securitycenter_get_bigquery_export]
def get_bigquery_export(parent: str, bigquery_export_id: str):
from google.cloud import securitycenter

"""
Retrieve an existing BigQuery export.
Args:
parent: Use any one of the following resource paths:
- organizations/{organization_id}
- folders/{folder_id}
- projects/{project_id}
bigquery_export_id: Unique identifier that is used to identify the export.
"""

client = securitycenter.SecurityCenterClient()

request = securitycenter.GetBigQueryExportRequest()
request.name = f"{parent}/bigQueryExports/{bigquery_export_id}"

response = client.get_big_query_export(request)
print(f"Retrieved the BigQuery export: {response.name}")


# [END securitycenter_get_bigquery_export]


# [START securitycenter_list_bigquery_export]
def list_bigquery_exports(parent: str):
from google.cloud import securitycenter

"""
List BigQuery exports in the given parent.
Args:
parent: The parent which owns the collection of BigQuery exports.
Use any one of the following resource paths:
- organizations/{organization_id}
- folders/{folder_id}
- projects/{project_id}
"""

client = securitycenter.SecurityCenterClient()

request = securitycenter.ListBigQueryExportsRequest()
request.parent = parent

response = client.list_big_query_exports(request)

print("Listing BigQuery exports:")
for bigquery_export in response:
print(bigquery_export.name)


# [END securitycenter_list_bigquery_export]


# [START securitycenter_update_bigquery_export]
def update_bigquery_export(parent: str, export_filter: str, bigquery_export_id: str):
"""
Updates an existing BigQuery export.
Args:
parent: Use any one of the following resource paths:
- organizations/{organization_id}
- folders/{folder_id}
- projects/{project_id}
export_filter: Expression that defines the filter to apply across create/update events of findings.
bigquery_export_id: Unique identifier provided by the client.
For more info, see:
https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to
"""
from google.cloud import securitycenter
from google.protobuf import field_mask_pb2

client = securitycenter.SecurityCenterClient()

# Set the new values for export configuration.
bigquery_export = securitycenter.BigQueryExport()
bigquery_export.name = f"{parent}/bigQueryExports/{bigquery_export_id}"
bigquery_export.filter = export_filter

# Field mask to only update the export filter.
# Set the update mask to specify which properties should be updated.
# If empty, all mutable fields will be updated.
# For more info on constructing field mask path, see the proto or:
# https://googleapis.dev/python/protobuf/latest/google/protobuf/field_mask_pb2.html
field_mask = field_mask_pb2.FieldMask(paths=["filter"])

request = securitycenter.UpdateBigQueryExportRequest()
request.big_query_export = bigquery_export
request.update_mask = field_mask

response = client.update_big_query_export(request)

if response.filter != export_filter:
print("Failed to update BigQueryExport!")
return
print("BigQueryExport updated successfully!")


# [END securitycenter_update_bigquery_export]


# [START securitycenter_delete_bigquery_export]
def delete_bigquery_export(parent: str, bigquery_export_id: str):
"""
Delete an existing BigQuery export.
Args:
parent: Use any one of the following resource paths:
- organizations/{organization_id}
- folders/{folder_id}
- projects/{project_id}
bigquery_export_id: Unique identifier that is used to identify the export.
"""
from google.cloud import securitycenter

client = securitycenter.SecurityCenterClient()

request = securitycenter.DeleteBigQueryExportRequest()
request.name = f"{parent}/bigQueryExports/{bigquery_export_id}"

client.delete_big_query_export(request)
print(f"BigQuery export request deleted successfully: {bigquery_export_id}")


# [END securitycenter_delete_bigquery_export]
96 changes: 96 additions & 0 deletions securitycenter/snippets/snippets_bigquery_export_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python
#
# Copyright 2022 Google LLC
#
# 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
#
# https://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.


# TODO(developer): Replace these variables before running the sample.
import os
import re
import uuid

from _pytest.capture import CaptureFixture
import pytest

import snippets_bigquery_export

PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
GOOGLE_APPLICATION_CREDENTIALS = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
BIGQUERY_DATASET_ID = f"sampledataset{str(uuid.uuid4()).split('-')[0]}"


@pytest.fixture(scope="module")
def bigquery_export_id():
bigquery_export_id = f"default-{str(uuid.uuid4()).split('-')[0]}"

create_bigquery_dataset(BIGQUERY_DATASET_ID)
export_filter = 'severity="LOW" OR severity="MEDIUM"'
snippets_bigquery_export.create_bigquery_export(
f"projects/{PROJECT_ID}", export_filter, BIGQUERY_DATASET_ID, bigquery_export_id
)

yield bigquery_export_id

snippets_bigquery_export.delete_bigquery_export(
f"projects/{PROJECT_ID}", bigquery_export_id
)
delete_bigquery_dataset(BIGQUERY_DATASET_ID)


def create_bigquery_dataset(dataset_id: str):
from google.cloud import bigquery

bigquery_client = bigquery.Client()

dataset_id_full = "{}.{}".format(PROJECT_ID, dataset_id)
dataset = bigquery.Dataset(dataset_id_full)

dataset = bigquery_client.create_dataset(dataset)
print("Dataset {} created.".format(dataset.dataset_id))


def delete_bigquery_dataset(dataset_id: str):
from google.cloud import bigquery

bigquery_client = bigquery.Client()
bigquery_client.delete_dataset(dataset_id)
print("Dataset {} deleted.".format(dataset_id))


def test_get_bigquery_export(capsys: CaptureFixture, bigquery_export_id: str):
snippets_bigquery_export.get_bigquery_export(
f"projects/{PROJECT_ID}", bigquery_export_id
)
out, _ = capsys.readouterr()
assert re.search(
"Retrieved the BigQuery export",
out,
)
assert re.search(f"bigQueryExports/{bigquery_export_id}", out)


def test_list_bigquery_exports(capsys: CaptureFixture, bigquery_export_id: str):
snippets_bigquery_export.list_bigquery_exports(f"projects/{PROJECT_ID}")
out, _ = capsys.readouterr()
assert re.search("Listing BigQuery exports:", out)
assert re.search(bigquery_export_id, out)


def test_update_bigquery_exports(capsys: CaptureFixture, bigquery_export_id: str):
export_filter = 'severity="MEDIUM"'
snippets_bigquery_export.update_bigquery_export(
f"projects/{PROJECT_ID}", export_filter, bigquery_export_id
)
out, _ = capsys.readouterr()
assert re.search("BigQueryExport updated successfully!", out)
Loading