Skip to content

Commit 93d5be8

Browse files
TrucHLegcf-owl-bot[bot]telpirion
authored
samples: enable pubsub notifications (#13)
* samples: initial update pubsub notifications samples * Make the sample closely resemble the public Curl snippet * Rearrange tech writer tags * Clean up tests * move region tags * use fstring * use built-in settings path builder * remove region tag from helper function * Fix code samples according to review * add pytest to requirements-test.txt * Use double quotes for consistency * Assert message in test * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * reorder import groups * lint * Use pytest fixture and autoformat with Nox * disable pubsub notifications when tearing down test Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Eric Schmidt <[email protected]>
0 parents  commit 93d5be8

File tree

5 files changed

+394
-0
lines changed

5 files changed

+394
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# [START contactcenterinsights_enable_pubsub_notifications]
16+
from google.api_core import protobuf_helpers
17+
from google.cloud import contact_center_insights_v1
18+
19+
20+
def enable_pubsub_notifications(
21+
project_id: str, topic_create_conversation: str, topic_create_analysis: str
22+
) -> None:
23+
# Construct a settings resource.
24+
settings = contact_center_insights_v1.Settings()
25+
settings.name = contact_center_insights_v1.ContactCenterInsightsClient.settings_path(
26+
project_id, "us-central1"
27+
)
28+
settings.pubsub_notification_settings = {
29+
"create-conversation": topic_create_conversation,
30+
"create-analysis": topic_create_analysis,
31+
}
32+
33+
update_mask = protobuf_helpers.field_mask(None, type(settings).pb(settings))
34+
35+
# Call the Insights client to enable Pub/Sub notifications.
36+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
37+
insights_client.update_settings(settings=settings, update_mask=update_mask)
38+
print("Enabled Pub/Sub notifications")
39+
40+
41+
# [END contactcenterinsights_enable_pubsub_notifications]
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
import os
18+
from pathlib import Path
19+
import sys
20+
from typing import Callable, Dict, List, Optional
21+
22+
import nox
23+
24+
25+
# WARNING - WARNING - WARNING - WARNING - WARNING
26+
# WARNING - WARNING - WARNING - WARNING - WARNING
27+
# DO NOT EDIT THIS FILE EVER!
28+
# WARNING - WARNING - WARNING - WARNING - WARNING
29+
# WARNING - WARNING - WARNING - WARNING - WARNING
30+
31+
BLACK_VERSION = "black==19.10b0"
32+
33+
# Copy `noxfile_config.py` to your directory and modify it instead.
34+
35+
# `TEST_CONFIG` dict is a configuration hook that allows users to
36+
# modify the test configurations. The values here should be in sync
37+
# with `noxfile_config.py`. Users will copy `noxfile_config.py` into
38+
# their directory and modify it.
39+
40+
TEST_CONFIG = {
41+
# You can opt out from the test for specific Python versions.
42+
"ignored_versions": [],
43+
# Old samples are opted out of enforcing Python type hints
44+
# All new samples should feature them
45+
"enforce_type_hints": False,
46+
# An envvar key for determining the project id to use. Change it
47+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
48+
# build specific Cloud project. You can also use your own string
49+
# to use your own Cloud project.
50+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
51+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
52+
# If you need to use a specific version of pip,
53+
# change pip_version_override to the string representation
54+
# of the version number, for example, "20.2.4"
55+
"pip_version_override": None,
56+
# A dictionary you want to inject into your test. Don't put any
57+
# secrets here. These values will override predefined values.
58+
"envs": {},
59+
}
60+
61+
62+
try:
63+
# Ensure we can import noxfile_config in the project's directory.
64+
sys.path.append(".")
65+
from noxfile_config import TEST_CONFIG_OVERRIDE
66+
except ImportError as e:
67+
print("No user noxfile_config found: detail: {}".format(e))
68+
TEST_CONFIG_OVERRIDE = {}
69+
70+
# Update the TEST_CONFIG with the user supplied values.
71+
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)
72+
73+
74+
def get_pytest_env_vars() -> Dict[str, str]:
75+
"""Returns a dict for pytest invocation."""
76+
ret = {}
77+
78+
# Override the GCLOUD_PROJECT and the alias.
79+
env_key = TEST_CONFIG["gcloud_project_env"]
80+
# This should error out if not set.
81+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
82+
83+
# Apply user supplied envs.
84+
ret.update(TEST_CONFIG["envs"])
85+
return ret
86+
87+
88+
# DO NOT EDIT - automatically generated.
89+
# All versions used to test samples.
90+
ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9"]
91+
92+
# Any default versions that should be ignored.
93+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
94+
95+
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
96+
97+
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in (
98+
"True",
99+
"true",
100+
)
101+
#
102+
# Style Checks
103+
#
104+
105+
106+
def _determine_local_import_names(start_dir: str) -> List[str]:
107+
"""Determines all import names that should be considered "local".
108+
109+
This is used when running the linter to insure that import order is
110+
properly checked.
111+
"""
112+
file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)]
113+
return [
114+
basename
115+
for basename, extension in file_ext_pairs
116+
if extension == ".py"
117+
or os.path.isdir(os.path.join(start_dir, basename))
118+
and basename not in ("__pycache__")
119+
]
120+
121+
122+
# Linting with flake8.
123+
#
124+
# We ignore the following rules:
125+
# E203: whitespace before ‘:’
126+
# E266: too many leading ‘#’ for block comment
127+
# E501: line too long
128+
# I202: Additional newline in a section of imports
129+
#
130+
# We also need to specify the rules which are ignored by default:
131+
# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121']
132+
FLAKE8_COMMON_ARGS = [
133+
"--show-source",
134+
"--builtin=gettext",
135+
"--max-complexity=20",
136+
"--import-order-style=google",
137+
"--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py",
138+
"--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202",
139+
"--max-line-length=88",
140+
]
141+
142+
143+
@nox.session
144+
def lint(session: nox.sessions.Session) -> None:
145+
if not TEST_CONFIG["enforce_type_hints"]:
146+
session.install("flake8", "flake8-import-order")
147+
else:
148+
session.install("flake8", "flake8-import-order", "flake8-annotations")
149+
150+
local_names = _determine_local_import_names(".")
151+
args = FLAKE8_COMMON_ARGS + [
152+
"--application-import-names",
153+
",".join(local_names),
154+
".",
155+
]
156+
session.run("flake8", *args)
157+
158+
159+
#
160+
# Black
161+
#
162+
163+
164+
@nox.session
165+
def blacken(session: nox.sessions.Session) -> None:
166+
session.install(BLACK_VERSION)
167+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
168+
169+
session.run("black", *python_files)
170+
171+
172+
#
173+
# Sample Tests
174+
#
175+
176+
177+
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
178+
179+
180+
def _session_tests(
181+
session: nox.sessions.Session, post_install: Callable = None
182+
) -> None:
183+
if TEST_CONFIG["pip_version_override"]:
184+
pip_version = TEST_CONFIG["pip_version_override"]
185+
session.install(f"pip=={pip_version}")
186+
"""Runs py.test for a particular project."""
187+
if os.path.exists("requirements.txt"):
188+
if os.path.exists("constraints.txt"):
189+
session.install("-r", "requirements.txt", "-c", "constraints.txt")
190+
else:
191+
session.install("-r", "requirements.txt")
192+
193+
if os.path.exists("requirements-test.txt"):
194+
if os.path.exists("constraints-test.txt"):
195+
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
196+
else:
197+
session.install("-r", "requirements-test.txt")
198+
199+
if INSTALL_LIBRARY_FROM_SOURCE:
200+
session.install("-e", _get_repo_root())
201+
202+
if post_install:
203+
post_install(session)
204+
205+
session.run(
206+
"pytest",
207+
*(PYTEST_COMMON_ARGS + session.posargs),
208+
# Pytest will return 5 when no tests are collected. This can happen
209+
# on travis where slow and flaky tests are excluded.
210+
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
211+
success_codes=[0, 5],
212+
env=get_pytest_env_vars(),
213+
)
214+
215+
216+
@nox.session(python=ALL_VERSIONS)
217+
def py(session: nox.sessions.Session) -> None:
218+
"""Runs py.test for a sample using the specified version of Python."""
219+
if session.python in TESTED_VERSIONS:
220+
_session_tests(session)
221+
else:
222+
session.skip(
223+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
224+
)
225+
226+
227+
#
228+
# Readmegen
229+
#
230+
231+
232+
def _get_repo_root() -> Optional[str]:
233+
""" Returns the root folder of the project. """
234+
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
235+
p = Path(os.getcwd())
236+
for i in range(10):
237+
if p is None:
238+
break
239+
if Path(p / ".git").exists():
240+
return str(p)
241+
# .git is not available in repos cloned via Cloud Build
242+
# setup.py is always in the library's root, so use that instead
243+
# https://github.com/googleapis/synthtool/issues/792
244+
if Path(p / "setup.py").exists():
245+
return str(p)
246+
p = p.parent
247+
raise Exception("Unable to detect repository root.")
248+
249+
250+
GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")])
251+
252+
253+
@nox.session
254+
@nox.parametrize("path", GENERATED_READMES)
255+
def readmegen(session: nox.sessions.Session, path: str) -> None:
256+
"""(Re-)generates the readme for a sample."""
257+
session.install("jinja2", "pyyaml")
258+
dir_ = os.path.dirname(path)
259+
260+
if os.path.exists(os.path.join(dir_, "requirements.txt")):
261+
session.install("-r", os.path.join(dir_, "requirements.txt"))
262+
263+
in_file = os.path.join(dir_, "README.rst.in")
264+
session.run(
265+
"python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file
266+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
google-auth==1.34.0
2+
google-cloud-pubsub==2.7.0
3+
protobuf==3.17.3
4+
pytest==6.2.4
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-api-core==1.31.1
2+
google-cloud-contact-center-insights==0.2.0
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
import uuid
16+
17+
import google.auth
18+
19+
from google.cloud import contact_center_insights_v1
20+
from google.cloud import pubsub_v1
21+
from google.protobuf import field_mask_pb2
22+
23+
import pytest
24+
25+
import enable_pubsub_notifications
26+
27+
UUID = uuid.uuid4().hex[:8]
28+
CONVERSATION_TOPIC_ID = "create-conversation-" + UUID
29+
ANALYSIS_TOPIC_ID = "create-analysis-" + UUID
30+
31+
32+
@pytest.fixture
33+
def project_id():
34+
_, project_id = google.auth.default()
35+
return project_id
36+
37+
38+
@pytest.fixture
39+
def pubsub_topics(project_id):
40+
# Create Pub/Sub topics.
41+
pubsub_client = pubsub_v1.PublisherClient()
42+
conversation_topic_path = pubsub_client.topic_path(
43+
project_id, CONVERSATION_TOPIC_ID
44+
)
45+
conversation_topic = pubsub_client.create_topic(
46+
request={"name": conversation_topic_path}
47+
)
48+
analysis_topic_path = pubsub_client.topic_path(project_id, ANALYSIS_TOPIC_ID)
49+
analysis_topic = pubsub_client.create_topic(request={"name": analysis_topic_path})
50+
yield conversation_topic.name, analysis_topic.name
51+
52+
# Delete Pub/Sub topics.
53+
pubsub_client.delete_topic(request={"topic": conversation_topic.name})
54+
pubsub_client.delete_topic(request={"topic": analysis_topic.name})
55+
56+
57+
@pytest.fixture
58+
def disable_pubsub_notifications(project_id):
59+
yield
60+
settings = contact_center_insights_v1.Settings()
61+
settings.name = contact_center_insights_v1.ContactCenterInsightsClient.settings_path(
62+
project_id, "us-central1"
63+
)
64+
settings.pubsub_notification_settings = {}
65+
update_mask = field_mask_pb2.FieldMask()
66+
update_mask.paths.append("pubsub_notification_settings")
67+
68+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
69+
insights_client.update_settings(settings=settings, update_mask=update_mask)
70+
71+
72+
def test_enable_pubsub_notifications(
73+
capsys, project_id, pubsub_topics, disable_pubsub_notifications
74+
):
75+
conversation_topic, analysis_topic = pubsub_topics
76+
77+
enable_pubsub_notifications.enable_pubsub_notifications(
78+
project_id, conversation_topic, analysis_topic
79+
)
80+
out, err = capsys.readouterr()
81+
assert "Enabled Pub/Sub notifications" in out

0 commit comments

Comments
 (0)