Skip to content

Commit

Permalink
Merge branch 'main' of github.com:GoogleCloudPlatform/python-docs-sam…
Browse files Browse the repository at this point in the history
…ples into ppai-weather-forecasting
  • Loading branch information
davidcavazos committed Nov 29, 2022
2 parents b0adef8 + 07e8b81 commit a44f6bd
Show file tree
Hide file tree
Showing 43 changed files with 1,856 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/appengine/standard/django/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/appengine/flexible/django_cloudsql/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/appengine/standard_python3/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
/asset/**/* @GoogleCloudPlatform/python-samples-reviewers
/auth/**/* @arithmetic1728 @GoogleCloudPlatform/python-samples-reviewers
/automl/**/* @GoogleCloudPlatform/ml-apis @GoogleCloudPlatform/python-samples-reviewers
/batch/**/* @m-strzelczyk @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
Expand Down
1 change: 1 addition & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ assign_issues_by:
to:
- GoogleCloudPlatform/cloud-native-db-dpes
- labels:
- 'api: asset'
- 'api: datacatalog'
- 'api: dataproc'
- 'api: clouderrorreporting'
Expand Down
3 changes: 0 additions & 3 deletions asset/README.rst

This file was deleted.

125 changes: 125 additions & 0 deletions asset/snippets/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python
#
# 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.

import os
import uuid

import backoff
from google.api_core.exceptions import InternalServerError
from google.api_core.exceptions import NotFound
from google.cloud import pubsub_v1
import pytest

import quickstart_create_saved_query
import quickstart_createfeed
import quickstart_delete_saved_query
import quickstart_deletefeed


PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]


@pytest.fixture(scope="module")
def test_topic():
topic_id = f"topic-{uuid.uuid4().hex}"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(PROJECT, topic_id)
topic = publisher.create_topic(request={"name": topic_path})

yield topic

publisher.delete_topic(request={"topic": topic_path})


@pytest.fixture(scope="module")
def another_topic():
topic_id = f"topic-{uuid.uuid4().hex}"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(PROJECT, topic_id)
topic = publisher.create_topic(request={"name": topic_path})

yield topic

publisher.delete_topic(request={"topic": topic_path})


@pytest.fixture(scope="module")
def test_feed(test_topic):
from google.cloud import asset_v1

feed_id = f"feed-{uuid.uuid4().hex}"
asset_name = f"assets-{uuid.uuid4().hex}"

@backoff.on_exception(backoff.expo, InternalServerError, max_time=60)
def create_feed():
return quickstart_createfeed.create_feed(
PROJECT, feed_id, [asset_name], test_topic.name, asset_v1.ContentType.RESOURCE
)

feed = create_feed()

yield feed

try:
quickstart_deletefeed.delete_feed(feed.name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")


@pytest.fixture(scope="module")
def deleter():
feeds_to_delete = []

yield feeds_to_delete

for feed_name in feeds_to_delete:
try:
quickstart_deletefeed.delete_feed(feed_name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")


@pytest.fixture(scope="module")
def test_saved_query():
saved_query_id = f"saved-query-{uuid.uuid4().hex}"

@backoff.on_exception(backoff.expo, InternalServerError, max_time=60)
def create_saved_query():
return quickstart_create_saved_query.create_saved_query(
PROJECT, saved_query_id, "description foo"
)

saved_query = create_saved_query()

yield saved_query

try:
quickstart_delete_saved_query.delete_saved_query(saved_query.name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")


@pytest.fixture(scope="module")
def saved_query_deleter():
saved_querys_to_delete = []

yield saved_querys_to_delete

for saved_query_name in saved_querys_to_delete:
try:
quickstart_delete_saved_query.delete_saved_query(saved_query_name)
except NotFound as e:
print(f"Ignoring NotFound: {e}")
38 changes: 38 additions & 0 deletions asset/snippets/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2021 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/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": False,
# 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": {},
}
53 changes: 53 additions & 0 deletions asset/snippets/quickstart_analyzeiampolicy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse


def analyze_iam_policy(project_id):
# [START asset_quickstart_analyze_iam_policy]
from google.cloud import asset_v1

# TODO project_id = 'Your Google Cloud Project ID'

client = asset_v1.AssetServiceClient()
parent = "projects/{}".format(project_id)

# Build analysis query
analysis_query = asset_v1.IamPolicyAnalysisQuery()
analysis_query.scope = parent
analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}"
analysis_query.options.expand_groups = True
analysis_query.options.output_group_edges = True

response = client.analyze_iam_policy(
request={"analysis_query": analysis_query}
)
print(response)
# [END asset_quickstart_analyze_iam_policy]


if __name__ == "__main__":

parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("project_id", help="Your Google Cloud project ID")

args = parser.parse_args()

analyze_iam_policy(args.project_id)
27 changes: 27 additions & 0 deletions asset/snippets/quickstart_analyzeiampolicy_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import quickstart_analyzeiampolicy

PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]


def test_analyze_iam_policy(capsys):
quickstart_analyzeiampolicy.analyze_iam_policy(PROJECT)
out, _ = capsys.readouterr()
assert "fully_explored: true" in out
105 changes: 105 additions & 0 deletions asset/snippets/quickstart_analyzeiampolicylongrunning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse


def analyze_iam_policy_longrunning_gcs(project_id, dump_file_path):
# [START asset_quickstart_analyze_iam_policy_longrunning_gcs]
from google.cloud import asset_v1

# TODO project_id = 'Your Google Cloud Project ID'
# TODO dump_file_path = 'Your analysis dump file path'

client = asset_v1.AssetServiceClient()
parent = "projects/{}".format(project_id)

# Build analysis query
analysis_query = asset_v1.IamPolicyAnalysisQuery()
analysis_query.scope = parent
analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}"
analysis_query.options.expand_groups = True
analysis_query.options.output_group_edges = True

output_config = asset_v1.IamPolicyAnalysisOutputConfig()
output_config.gcs_destination.uri = dump_file_path
operation = client.analyze_iam_policy_longrunning(
request={"analysis_query": analysis_query, "output_config": output_config}
)

operation.result(300)
print(operation.done())
# [END asset_quickstart_analyze_iam_policy_longrunning_gcs]


def analyze_iam_policy_longrunning_bigquery(project_id, dataset, table):
# [START asset_quickstart_analyze_iam_policy_longrunning_bigquery]
from google.cloud import asset_v1

# TODO project_id = 'Your Google Cloud Project ID'
# TODO dataset = 'Your BigQuery dataset path'
# TODO table = 'Your BigQuery table name'

client = asset_v1.AssetServiceClient()
parent = "projects/{}".format(project_id)

# Build analysis query
analysis_query = asset_v1.IamPolicyAnalysisQuery()
analysis_query.scope = parent
analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}"
analysis_query.options.expand_groups = True
analysis_query.options.output_group_edges = True

output_config = asset_v1.IamPolicyAnalysisOutputConfig()
output_config.bigquery_destination.dataset = dataset
output_config.bigquery_destination.table_prefix = table
output_config.bigquery_destination.write_disposition = "WRITE_TRUNCATE"
operation = client.analyze_iam_policy_longrunning(
request={"analysis_query": analysis_query, "output_config": output_config}
)

operation.result(300)
print(operation.done())
# [END asset_quickstart_analyze_iam_policy_longrunning_bigquery]


if __name__ == "__main__":

parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("project_id", help="Your Google Cloud project ID")
parser.add_argument(
"dump_file_path",
help="The GCS file that the analysis results will be dumped to, "
"e.g.: gs://<bucket-name>/analysis_dump_file",
)
parser.add_argument(
"dataset",
help="The BigQuery dataset that analysis results will be exported to, "
"e.g.: my_dataset",
)
parser.add_argument(
"table_prefix",
help="The prefix of the BigQuery table that analysis results will be exported to, "
"e.g.: my_table",
)

args = parser.parse_args()

analyze_iam_policy_longrunning_gcs(args.project_id, args.dump_file_path)
analyze_iam_policy_longrunning_bigquery(args.project_id, args.dataset, args.table_prefix)
Loading

0 comments on commit a44f6bd

Please sign in to comment.