From 4163e7dcb0995c6451c1902330a11050b5548760 Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot Date: Thu, 1 Apr 2021 16:49:32 -0700 Subject: [PATCH] fix: use correct retry deadlines (#66) --- .../google-cloud-billing-budgets/.coveragerc | 32 +- .../.github/header-checker-lint.yml | 15 + .../google-cloud-billing-budgets/.gitignore | 4 +- .../.kokoro/build.sh | 26 +- .../.kokoro/docs/docs-presubmit.cfg | 11 + .../samples/python3.6/periodic-head.cfg | 11 + .../samples/python3.7/periodic-head.cfg | 11 + .../samples/python3.8/periodic-head.cfg | 11 + .../.kokoro/test-samples-against-head.sh | 28 ++ .../.kokoro/test-samples-impl.sh | 102 ++++++ .../.kokoro/test-samples.sh | 96 +----- .../.pre-commit-config.yaml | 2 +- .../.trampolinerc | 1 + .../CONTRIBUTING.rst | 22 +- .../google-cloud-billing-budgets/MANIFEST.in | 4 +- .../google-cloud-billing-budgets/UPGRADING.md | 4 +- .../docs/budgets_v1/budget_service.rst | 11 + .../docs/budgets_v1/services.rst | 6 +- .../docs/budgets_v1/types.rst | 1 + .../docs/budgets_v1beta1/budget_service.rst | 11 + .../docs/budgets_v1beta1/services.rst | 6 +- .../docs/budgets_v1beta1/types.rst | 1 + .../budgets_v1/proto/budget_model.proto | 16 +- .../budgets_v1/proto/budget_service.proto | 5 +- .../services/budget_service/async_client.py | 65 +++- .../services/budget_service/client.py | 78 +++-- .../services/budget_service/pagers.py | 27 +- .../budget_service/transports/base.py | 22 +- .../budget_service/transports/grpc.py | 112 +++---- .../budget_service/transports/grpc_asyncio.py | 120 +++---- .../billing/budgets_v1/types/__init__.py | 16 +- .../billing/budgets_v1/types/budget_model.py | 35 +- .../budgets_v1/types/budget_service.py | 8 +- .../budgets_v1beta1/proto/budget_model.proto | 16 +- .../proto/budget_service.proto | 2 +- .../services/budget_service/async_client.py | 53 +++- .../services/budget_service/client.py | 58 ++-- .../services/budget_service/pagers.py | 27 +- .../budget_service/transports/base.py | 22 +- .../budget_service/transports/grpc.py | 112 +++---- .../budget_service/transports/grpc_asyncio.py | 120 +++---- .../billing/budgets_v1beta1/types/__init__.py | 16 +- .../budgets_v1beta1/types/budget_model.py | 36 ++- .../budgets_v1beta1/types/budget_service.py | 8 +- .../google-cloud-billing-budgets/noxfile.py | 63 +++- .../renovate.json | 3 +- .../google-cloud-billing-budgets/setup.py | 5 +- .../synth.metadata | 123 +------ .../google-cloud-billing-budgets/synth.py | 2 +- .../testing/constraints-3.6.txt | 4 +- .../tests/unit/gapic/budgets_v1/__init__.py | 15 + .../gapic/budgets_v1/test_budget_service.py | 299 +++++++++++++----- .../unit/gapic/budgets_v1beta1/__init__.py | 15 + .../budgets_v1beta1/test_budget_service.py | 299 +++++++++++++----- 54 files changed, 1345 insertions(+), 873 deletions(-) create mode 100644 packages/google-cloud-billing-budgets/.github/header-checker-lint.yml create mode 100644 packages/google-cloud-billing-budgets/.kokoro/samples/python3.6/periodic-head.cfg create mode 100644 packages/google-cloud-billing-budgets/.kokoro/samples/python3.7/periodic-head.cfg create mode 100644 packages/google-cloud-billing-budgets/.kokoro/samples/python3.8/periodic-head.cfg create mode 100755 packages/google-cloud-billing-budgets/.kokoro/test-samples-against-head.sh create mode 100755 packages/google-cloud-billing-budgets/.kokoro/test-samples-impl.sh create mode 100644 packages/google-cloud-billing-budgets/docs/budgets_v1/budget_service.rst create mode 100644 packages/google-cloud-billing-budgets/docs/budgets_v1beta1/budget_service.rst diff --git a/packages/google-cloud-billing-budgets/.coveragerc b/packages/google-cloud-billing-budgets/.coveragerc index 79365448d916..653d88b0af3a 100644 --- a/packages/google-cloud-billing-budgets/.coveragerc +++ b/packages/google-cloud-billing-budgets/.coveragerc @@ -1,36 +1,18 @@ -# -*- coding: utf-8 -*- -# -# 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 -# -# 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. - -# Generated by synthtool. DO NOT EDIT! [run] branch = True [report] fail_under = 100 show_missing = True +omit = + google/cloud/billing/budgets/__init__.py exclude_lines = # Re-enable the standard pragma pragma: NO COVER # Ignore debug-only repr def __repr__ - # Ignore abstract methods - raise NotImplementedError -omit = - */gapic/*.py - */proto/*.py - */core/*.py - */site-packages/*.py - google/cloud/billing/budgets/__init__.py + # Ignore pkg_resources exceptions. + # This is added at the module level as a safeguard for if someone + # generates the code and tries to run it without pip installing. This + # makes it virtually impossible to test properly. + except pkg_resources.DistributionNotFound diff --git a/packages/google-cloud-billing-budgets/.github/header-checker-lint.yml b/packages/google-cloud-billing-budgets/.github/header-checker-lint.yml new file mode 100644 index 000000000000..fc281c05bd55 --- /dev/null +++ b/packages/google-cloud-billing-budgets/.github/header-checker-lint.yml @@ -0,0 +1,15 @@ +{"allowedCopyrightHolders": ["Google LLC"], + "allowedLicenses": ["Apache-2.0", "MIT", "BSD-3"], + "ignoreFiles": ["**/requirements.txt", "**/requirements-test.txt"], + "sourceFileExtensions": [ + "ts", + "js", + "java", + "sh", + "Dockerfile", + "yaml", + "py", + "html", + "txt" + ] +} \ No newline at end of file diff --git a/packages/google-cloud-billing-budgets/.gitignore b/packages/google-cloud-billing-budgets/.gitignore index b9daa52f118d..b4243ced74e4 100644 --- a/packages/google-cloud-billing-budgets/.gitignore +++ b/packages/google-cloud-billing-budgets/.gitignore @@ -50,8 +50,10 @@ docs.metadata # Virtual environment env/ + +# Test logs coverage.xml -sponge_log.xml +*sponge_log.xml # System test environment variables. system_tests/local_test_setup diff --git a/packages/google-cloud-billing-budgets/.kokoro/build.sh b/packages/google-cloud-billing-budgets/.kokoro/build.sh index 8d81e509c1ba..a85ae085cac4 100755 --- a/packages/google-cloud-billing-budgets/.kokoro/build.sh +++ b/packages/google-cloud-billing-budgets/.kokoro/build.sh @@ -15,7 +15,11 @@ set -eo pipefail -cd github/python-billingbudgets +if [[ -z "${PROJECT_ROOT:-}" ]]; then + PROJECT_ROOT="github/python-billingbudgets" +fi + +cd "${PROJECT_ROOT}" # Disable buffering, so that the logs stream through. export PYTHONUNBUFFERED=1 @@ -30,16 +34,26 @@ export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-account.json export PROJECT_ID=$(cat "${KOKORO_GFILE_DIR}/project-id.json") # Remove old nox -python3.6 -m pip uninstall --yes --quiet nox-automation +python3 -m pip uninstall --yes --quiet nox-automation # Install nox -python3.6 -m pip install --upgrade --quiet nox -python3.6 -m nox --version +python3 -m pip install --upgrade --quiet nox +python3 -m nox --version + +# If this is a continuous build, send the test log to the FlakyBot. +# See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot. +if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"continuous"* ]]; then + cleanup() { + chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot + $KOKORO_GFILE_DIR/linux_amd64/flakybot + } + trap cleanup EXIT HUP +fi # If NOX_SESSION is set, it only runs the specified session, # otherwise run all the sessions. if [[ -n "${NOX_SESSION:-}" ]]; then - python3.6 -m nox -s "${NOX_SESSION:-}" + python3 -m nox -s ${NOX_SESSION:-} else - python3.6 -m nox + python3 -m nox fi diff --git a/packages/google-cloud-billing-budgets/.kokoro/docs/docs-presubmit.cfg b/packages/google-cloud-billing-budgets/.kokoro/docs/docs-presubmit.cfg index 1118107829b7..befd8646c37c 100644 --- a/packages/google-cloud-billing-budgets/.kokoro/docs/docs-presubmit.cfg +++ b/packages/google-cloud-billing-budgets/.kokoro/docs/docs-presubmit.cfg @@ -15,3 +15,14 @@ env_vars: { key: "TRAMPOLINE_IMAGE_UPLOAD" value: "false" } + +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/python-billingbudgets/.kokoro/build.sh" +} + +# Only run this nox session. +env_vars: { + key: "NOX_SESSION" + value: "docs docfx" +} diff --git a/packages/google-cloud-billing-budgets/.kokoro/samples/python3.6/periodic-head.cfg b/packages/google-cloud-billing-budgets/.kokoro/samples/python3.6/periodic-head.cfg new file mode 100644 index 000000000000..f9cfcd33e058 --- /dev/null +++ b/packages/google-cloud-billing-budgets/.kokoro/samples/python3.6/periodic-head.cfg @@ -0,0 +1,11 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "True" +} + +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/python-pubsub/.kokoro/test-samples-against-head.sh" +} diff --git a/packages/google-cloud-billing-budgets/.kokoro/samples/python3.7/periodic-head.cfg b/packages/google-cloud-billing-budgets/.kokoro/samples/python3.7/periodic-head.cfg new file mode 100644 index 000000000000..f9cfcd33e058 --- /dev/null +++ b/packages/google-cloud-billing-budgets/.kokoro/samples/python3.7/periodic-head.cfg @@ -0,0 +1,11 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "True" +} + +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/python-pubsub/.kokoro/test-samples-against-head.sh" +} diff --git a/packages/google-cloud-billing-budgets/.kokoro/samples/python3.8/periodic-head.cfg b/packages/google-cloud-billing-budgets/.kokoro/samples/python3.8/periodic-head.cfg new file mode 100644 index 000000000000..f9cfcd33e058 --- /dev/null +++ b/packages/google-cloud-billing-budgets/.kokoro/samples/python3.8/periodic-head.cfg @@ -0,0 +1,11 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "True" +} + +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/python-pubsub/.kokoro/test-samples-against-head.sh" +} diff --git a/packages/google-cloud-billing-budgets/.kokoro/test-samples-against-head.sh b/packages/google-cloud-billing-budgets/.kokoro/test-samples-against-head.sh new file mode 100755 index 000000000000..68575b6f4e43 --- /dev/null +++ b/packages/google-cloud-billing-budgets/.kokoro/test-samples-against-head.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# 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 +# +# 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. + +# A customized test runner for samples. +# +# For periodic builds, you can specify this file for testing against head. + +# `-e` enables the script to automatically fail when a command fails +# `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero +set -eo pipefail +# Enables `**` to include files nested inside sub-folders +shopt -s globstar + +cd github/python-billingbudgets + +exec .kokoro/test-samples-impl.sh diff --git a/packages/google-cloud-billing-budgets/.kokoro/test-samples-impl.sh b/packages/google-cloud-billing-budgets/.kokoro/test-samples-impl.sh new file mode 100755 index 000000000000..cf5de74c17a5 --- /dev/null +++ b/packages/google-cloud-billing-budgets/.kokoro/test-samples-impl.sh @@ -0,0 +1,102 @@ +#!/bin/bash +# 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 +# +# 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. + + +# `-e` enables the script to automatically fail when a command fails +# `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero +set -eo pipefail +# Enables `**` to include files nested inside sub-folders +shopt -s globstar + +# Exit early if samples directory doesn't exist +if [ ! -d "./samples" ]; then + echo "No tests run. `./samples` not found" + exit 0 +fi + +# Disable buffering, so that the logs stream through. +export PYTHONUNBUFFERED=1 + +# Debug: show build environment +env | grep KOKORO + +# Install nox +python3.6 -m pip install --upgrade --quiet nox + +# Use secrets acessor service account to get secrets +if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then + gcloud auth activate-service-account \ + --key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \ + --project="cloud-devrel-kokoro-resources" +fi + +# This script will create 3 files: +# - testing/test-env.sh +# - testing/service-account.json +# - testing/client-secrets.json +./scripts/decrypt-secrets.sh + +source ./testing/test-env.sh +export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json + +# For cloud-run session, we activate the service account for gcloud sdk. +gcloud auth activate-service-account \ + --key-file "${GOOGLE_APPLICATION_CREDENTIALS}" + +export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json + +echo -e "\n******************** TESTING PROJECTS ********************" + +# Switch to 'fail at end' to allow all tests to complete before exiting. +set +e +# Use RTN to return a non-zero value if the test fails. +RTN=0 +ROOT=$(pwd) +# Find all requirements.txt in the samples directory (may break on whitespace). +for file in samples/**/requirements.txt; do + cd "$ROOT" + # Navigate to the project folder. + file=$(dirname "$file") + cd "$file" + + echo "------------------------------------------------------------" + echo "- testing $file" + echo "------------------------------------------------------------" + + # Use nox to execute the tests for the project. + python3.6 -m nox -s "$RUN_TESTS_SESSION" + EXIT=$? + + # If this is a periodic build, send the test log to the FlakyBot. + # See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot. + if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then + chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot + $KOKORO_GFILE_DIR/linux_amd64/flakybot + fi + + if [[ $EXIT -ne 0 ]]; then + RTN=1 + echo -e "\n Testing failed: Nox returned a non-zero exit code. \n" + else + echo -e "\n Testing completed.\n" + fi + +done +cd "$ROOT" + +# Workaround for Kokoro permissions issue: delete secrets +rm testing/{test-env.sh,client-secrets.json,service-account.json} + +exit "$RTN" diff --git a/packages/google-cloud-billing-budgets/.kokoro/test-samples.sh b/packages/google-cloud-billing-budgets/.kokoro/test-samples.sh index 5cd6a8bc76fc..f3f36c9a9c68 100755 --- a/packages/google-cloud-billing-budgets/.kokoro/test-samples.sh +++ b/packages/google-cloud-billing-budgets/.kokoro/test-samples.sh @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +# The default test runner for samples. +# +# For periodic builds, we rewinds the repo to the latest release, and +# run test-samples-impl.sh. # `-e` enables the script to automatically fail when a command fails # `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero @@ -24,87 +28,19 @@ cd github/python-billingbudgets # Run periodic samples tests at latest release if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then + # preserving the test runner implementation. + cp .kokoro/test-samples-impl.sh "${TMPDIR}/test-samples-impl.sh" + echo "--- IMPORTANT IMPORTANT IMPORTANT ---" + echo "Now we rewind the repo back to the latest release..." LATEST_RELEASE=$(git describe --abbrev=0 --tags) git checkout $LATEST_RELEASE -fi - -# Exit early if samples directory doesn't exist -if [ ! -d "./samples" ]; then - echo "No tests run. `./samples` not found" - exit 0 -fi - -# Disable buffering, so that the logs stream through. -export PYTHONUNBUFFERED=1 - -# Debug: show build environment -env | grep KOKORO - -# Install nox -python3.6 -m pip install --upgrade --quiet nox - -# Use secrets acessor service account to get secrets -if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then - gcloud auth activate-service-account \ - --key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \ - --project="cloud-devrel-kokoro-resources" -fi - -# This script will create 3 files: -# - testing/test-env.sh -# - testing/service-account.json -# - testing/client-secrets.json -./scripts/decrypt-secrets.sh - -source ./testing/test-env.sh -export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json - -# For cloud-run session, we activate the service account for gcloud sdk. -gcloud auth activate-service-account \ - --key-file "${GOOGLE_APPLICATION_CREDENTIALS}" - -export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json - -echo -e "\n******************** TESTING PROJECTS ********************" - -# Switch to 'fail at end' to allow all tests to complete before exiting. -set +e -# Use RTN to return a non-zero value if the test fails. -RTN=0 -ROOT=$(pwd) -# Find all requirements.txt in the samples directory (may break on whitespace). -for file in samples/**/requirements.txt; do - cd "$ROOT" - # Navigate to the project folder. - file=$(dirname "$file") - cd "$file" - - echo "------------------------------------------------------------" - echo "- testing $file" - echo "------------------------------------------------------------" - - # Use nox to execute the tests for the project. - python3.6 -m nox -s "$RUN_TESTS_SESSION" - EXIT=$? - - # If this is a periodic build, send the test log to the FlakyBot. - # See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot. - if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then - chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot - $KOKORO_GFILE_DIR/linux_amd64/flakybot + echo "The current head is: " + echo $(git rev-parse --verify HEAD) + echo "--- IMPORTANT IMPORTANT IMPORTANT ---" + # move back the test runner implementation if there's no file. + if [ ! -f .kokoro/test-samples-impl.sh ]; then + cp "${TMPDIR}/test-samples-impl.sh" .kokoro/test-samples-impl.sh fi +fi - if [[ $EXIT -ne 0 ]]; then - RTN=1 - echo -e "\n Testing failed: Nox returned a non-zero exit code. \n" - else - echo -e "\n Testing completed.\n" - fi - -done -cd "$ROOT" - -# Workaround for Kokoro permissions issue: delete secrets -rm testing/{test-env.sh,client-secrets.json,service-account.json} - -exit "$RTN" +exec .kokoro/test-samples-impl.sh diff --git a/packages/google-cloud-billing-budgets/.pre-commit-config.yaml b/packages/google-cloud-billing-budgets/.pre-commit-config.yaml index a9024b15d725..32302e4883a1 100644 --- a/packages/google-cloud-billing-budgets/.pre-commit-config.yaml +++ b/packages/google-cloud-billing-budgets/.pre-commit-config.yaml @@ -12,6 +12,6 @@ repos: hooks: - id: black - repo: https://gitlab.com/pycqa/flake8 - rev: 3.8.4 + rev: 3.9.0 hooks: - id: flake8 diff --git a/packages/google-cloud-billing-budgets/.trampolinerc b/packages/google-cloud-billing-budgets/.trampolinerc index 995ee29111e1..383b6ec89fbc 100644 --- a/packages/google-cloud-billing-budgets/.trampolinerc +++ b/packages/google-cloud-billing-budgets/.trampolinerc @@ -24,6 +24,7 @@ required_envvars+=( pass_down_envvars+=( "STAGING_BUCKET" "V2_STAGING_BUCKET" + "NOX_SESSION" ) # Prevent unintentional override on the default image. diff --git a/packages/google-cloud-billing-budgets/CONTRIBUTING.rst b/packages/google-cloud-billing-budgets/CONTRIBUTING.rst index 30e55e397781..fca5c9566b51 100644 --- a/packages/google-cloud-billing-budgets/CONTRIBUTING.rst +++ b/packages/google-cloud-billing-budgets/CONTRIBUTING.rst @@ -70,9 +70,14 @@ We use `nox `__ to instrument our tests. - To test your changes, run unit tests with ``nox``:: $ nox -s unit-2.7 - $ nox -s unit-3.7 + $ nox -s unit-3.8 $ ... +- Args to pytest can be passed through the nox command separated by a `--`. For + example, to run a single test:: + + $ nox -s unit-3.8 -- -k + .. note:: The unit tests and system tests are described in the @@ -93,8 +98,12 @@ On Debian/Ubuntu:: ************ Coding Style ************ +- We use the automatic code formatter ``black``. You can run it using + the nox session ``blacken``. This will eliminate many lint errors. Run via:: + + $ nox -s blacken -- PEP8 compliance, with exceptions defined in the linter configuration. +- PEP8 compliance is required, with exceptions defined in the linter configuration. If you have ``nox`` installed, you can test that you have not introduced any non-compliant code via:: @@ -133,13 +142,18 @@ Running System Tests - To run system tests, you can execute:: - $ nox -s system-3.7 + # Run all system tests + $ nox -s system-3.8 $ nox -s system-2.7 + # Run a single system test + $ nox -s system-3.8 -- -k + + .. note:: System tests are only configured to run under Python 2.7 and - Python 3.7. For expediency, we do not run them in older versions + Python 3.8. For expediency, we do not run them in older versions of Python 3. This alone will not run the tests. You'll need to change some local diff --git a/packages/google-cloud-billing-budgets/MANIFEST.in b/packages/google-cloud-billing-budgets/MANIFEST.in index e9e29d12033d..e783f4c6209b 100644 --- a/packages/google-cloud-billing-budgets/MANIFEST.in +++ b/packages/google-cloud-billing-budgets/MANIFEST.in @@ -16,10 +16,10 @@ # Generated by synthtool. DO NOT EDIT! include README.rst LICENSE -recursive-include google *.json *.proto +recursive-include google *.json *.proto py.typed recursive-include tests * global-exclude *.py[co] global-exclude __pycache__ # Exclude scripts for samples readmegen -prune scripts/readme-gen \ No newline at end of file +prune scripts/readme-gen diff --git a/packages/google-cloud-billing-budgets/UPGRADING.md b/packages/google-cloud-billing-budgets/UPGRADING.md index 74e38b1a8d3b..1c2aa53eb4d3 100644 --- a/packages/google-cloud-billing-budgets/UPGRADING.md +++ b/packages/google-cloud-billing-budgets/UPGRADING.md @@ -39,10 +39,10 @@ client = budgets.BudgetServiceClient() Methods expect request objects. We provide a script that will convert most common use cases. -* Install the library +* Install the library with `libcst`. ```py -python3 -m pip install google-cloud-billing-budgets +python3 -m pip install google-cloud-billing-budgets[libcst] ``` * The script `fixup_budgets_v1beta1_keywords.py` is shipped with the library. It expects diff --git a/packages/google-cloud-billing-budgets/docs/budgets_v1/budget_service.rst b/packages/google-cloud-billing-budgets/docs/budgets_v1/budget_service.rst new file mode 100644 index 000000000000..487fede48631 --- /dev/null +++ b/packages/google-cloud-billing-budgets/docs/budgets_v1/budget_service.rst @@ -0,0 +1,11 @@ +BudgetService +------------------------------- + +.. automodule:: google.cloud.billing.budgets_v1.services.budget_service + :members: + :inherited-members: + + +.. automodule:: google.cloud.billing.budgets_v1.services.budget_service.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-billing-budgets/docs/budgets_v1/services.rst b/packages/google-cloud-billing-budgets/docs/budgets_v1/services.rst index ad9f4476b3b7..864c9aabe6e0 100644 --- a/packages/google-cloud-billing-budgets/docs/budgets_v1/services.rst +++ b/packages/google-cloud-billing-budgets/docs/budgets_v1/services.rst @@ -1,6 +1,6 @@ Services for Google Cloud Billing Budgets v1 API ================================================ +.. toctree:: + :maxdepth: 2 -.. automodule:: google.cloud.billing.budgets_v1.services.budget_service - :members: - :inherited-members: + budget_service diff --git a/packages/google-cloud-billing-budgets/docs/budgets_v1/types.rst b/packages/google-cloud-billing-budgets/docs/budgets_v1/types.rst index 632739cc17d6..51bbb1ee47e9 100644 --- a/packages/google-cloud-billing-budgets/docs/budgets_v1/types.rst +++ b/packages/google-cloud-billing-budgets/docs/budgets_v1/types.rst @@ -3,4 +3,5 @@ Types for Google Cloud Billing Budgets v1 API .. automodule:: google.cloud.billing.budgets_v1.types :members: + :undoc-members: :show-inheritance: diff --git a/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/budget_service.rst b/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/budget_service.rst new file mode 100644 index 000000000000..fffcb3451384 --- /dev/null +++ b/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/budget_service.rst @@ -0,0 +1,11 @@ +BudgetService +------------------------------- + +.. automodule:: google.cloud.billing.budgets_v1beta1.services.budget_service + :members: + :inherited-members: + + +.. automodule:: google.cloud.billing.budgets_v1beta1.services.budget_service.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/services.rst b/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/services.rst index 91a4cb79b521..418cbb1977b7 100644 --- a/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/services.rst +++ b/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/services.rst @@ -1,6 +1,6 @@ Services for Google Cloud Billing Budgets v1beta1 API ===================================================== +.. toctree:: + :maxdepth: 2 -.. automodule:: google.cloud.billing.budgets_v1beta1.services.budget_service - :members: - :inherited-members: + budget_service diff --git a/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/types.rst b/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/types.rst index ecd04b4311d3..1e21bfe09ba3 100644 --- a/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/types.rst +++ b/packages/google-cloud-billing-budgets/docs/budgets_v1beta1/types.rst @@ -3,4 +3,5 @@ Types for Google Cloud Billing Budgets v1beta1 API .. automodule:: google.cloud.billing.budgets_v1beta1.types :members: + :undoc-members: :show-inheritance: diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_model.proto b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_model.proto index a23e0801a079..eefe3db44582 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_model.proto +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_model.proto @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -74,9 +74,10 @@ message BudgetAmount { // Specification for what amount to use as the budget. oneof budget_amount { // A specified amount to use as the budget. - // `currency_code` is optional. If specified, it must match the - // currency of the billing account. The `currency_code` is provided on - // output. + // `currency_code` is optional. If specified when creating a budget, it must + // match the currency of the billing account. If specified when updating a + // budget, it must match the currency_code of the existing budget. + // The `currency_code` is provided on output. google.type.Money specified_amount = 1; // Use the last period's actual spend as the budget for the present period. @@ -138,9 +139,12 @@ message NotificationsRule { // for more details on Pub/Sub roles and permissions. string pubsub_topic = 1 [(google.api.field_behavior) = OPTIONAL]; - // Optional. The schema version of the notification sent to `pubsub_topic`. + // Optional. Required when + // [NotificationsRule.pubsub_topic][google.cloud.billing.budgets.v1.NotificationsRule.pubsub_topic] + // is set. The schema version of the notification sent to + // [NotificationsRule.pubsub_topic][google.cloud.billing.budgets.v1.NotificationsRule.pubsub_topic]. // Only "1.0" is accepted. It represents the JSON schema as defined in - // https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format + // https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format. string schema_version = 2 [(google.api.field_behavior) = OPTIONAL]; // Optional. Targets to send notifications to when a threshold is exceeded. diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_service.proto b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_service.proto index bb2be219e313..a4b512b8a6fa 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_service.proto +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/proto/budget_service.proto @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -123,7 +123,8 @@ message UpdateBudgetRequest { // updated. See // https://developers.google.com/protocol-buffers/docs/proto3#default for more // details about default values. - google.protobuf.FieldMask update_mask = 2 [(google.api.field_behavior) = OPTIONAL]; + google.protobuf.FieldMask update_mask = 2 + [(google.api.field_behavior) = OPTIONAL]; } // Request for GetBudget diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/async_client.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/async_client.py index 1ab8d15736ef..365815ca260f 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/async_client.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/async_client.py @@ -81,7 +81,36 @@ class BudgetServiceAsyncClient: BudgetServiceClient.parse_common_location_path ) - from_service_account_file = BudgetServiceClient.from_service_account_file + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + BudgetServiceAsyncClient: The constructed client. + """ + return BudgetServiceClient.from_service_account_info.__func__(BudgetServiceAsyncClient, info, *args, **kwargs) # type: ignore + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + BudgetServiceAsyncClient: The constructed client. + """ + return BudgetServiceClient.from_service_account_file.__func__(BudgetServiceAsyncClient, filename, *args, **kwargs) # type: ignore + from_service_account_json = from_service_account_file @property @@ -161,16 +190,17 @@ async def create_budget( create. Args: - request (:class:`~.budget_service.CreateBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1.types.CreateBudgetRequest`): The request object. Request for CreateBudget parent (:class:`str`): Required. The name of the billing account to create the budget in. Values are of the form ``billingAccounts/{billingAccountId}``. + This corresponds to the ``parent`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - budget (:class:`~.budget_model.Budget`): + budget (:class:`google.cloud.billing.budgets_v1.types.Budget`): Required. Budget to create. This corresponds to the ``budget`` field on the ``request`` instance; if ``request`` is provided, this @@ -183,7 +213,7 @@ async def create_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -252,22 +282,24 @@ async def update_budget( changed by this method. Args: - request (:class:`~.budget_service.UpdateBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1.types.UpdateBudgetRequest`): The request object. Request for UpdateBudget - budget (:class:`~.budget_model.Budget`): + budget (:class:`google.cloud.billing.budgets_v1.types.Budget`): Required. The updated budget object. The budget to update is specified by the budget name in the budget. + This corresponds to the ``budget`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - update_mask (:class:`~.field_mask.FieldMask`): + update_mask (:class:`google.protobuf.field_mask_pb2.FieldMask`): Optional. Indicates which fields in the provided budget to update. Read-only fields (such as ``name``) cannot be changed. If this is not provided, then only fields with non-default values from the request are updated. See https://developers.google.com/protocol-buffers/docs/proto3#default for more details about default values. + This corresponds to the ``update_mask`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -279,7 +311,7 @@ async def update_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -322,6 +354,7 @@ async def update_budget( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, @@ -358,11 +391,12 @@ async def get_budget( Cloud Console. Args: - request (:class:`~.budget_service.GetBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1.types.GetBudgetRequest`): The request object. Request for GetBudget name (:class:`str`): Required. Name of budget to get. Values are of the form ``billingAccounts/{billingAccountId}/budgets/{budgetId}``. + This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -374,7 +408,7 @@ async def get_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -415,6 +449,7 @@ async def get_budget( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, @@ -449,12 +484,13 @@ async def list_budgets( Cloud Console. Args: - request (:class:`~.budget_service.ListBudgetsRequest`): + request (:class:`google.cloud.billing.budgets_v1.types.ListBudgetsRequest`): The request object. Request for ListBudgets parent (:class:`str`): Required. Name of billing account to list budgets under. Values are of the form ``billingAccounts/{billingAccountId}``. + This corresponds to the ``parent`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -466,7 +502,7 @@ async def list_budgets( sent along with the request as metadata. Returns: - ~.pagers.ListBudgetsAsyncPager: + google.cloud.billing.budgets_v1.services.budget_service.pagers.ListBudgetsAsyncPager: Response for ListBudgets Iterating over this object will yield results and resolve additional pages @@ -502,6 +538,7 @@ async def list_budgets( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, @@ -538,12 +575,13 @@ async def delete_budget( deleted. Args: - request (:class:`~.budget_service.DeleteBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1.types.DeleteBudgetRequest`): The request object. Request for DeleteBudget name (:class:`str`): Required. Name of the budget to delete. Values are of the form ``billingAccounts/{billingAccountId}/budgets/{budgetId}``. + This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -583,6 +621,7 @@ async def delete_budget( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/client.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/client.py index 25ed583febf3..969fed9103e4 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/client.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/client.py @@ -113,6 +113,22 @@ def _get_default_mtls_endpoint(api_endpoint): DEFAULT_ENDPOINT ) + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + BudgetServiceClient: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_info(info) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + @classmethod def from_service_account_file(cls, filename: str, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -125,7 +141,7 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): kwargs: Additional arguments to pass to the constructor. Returns: - {@api.name}: The constructed client. + BudgetServiceClient: The constructed client. """ credentials = service_account.Credentials.from_service_account_file(filename) kwargs["credentials"] = credentials @@ -232,10 +248,10 @@ def __init__( credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. - transport (Union[str, ~.BudgetServiceTransport]): The + transport (Union[str, BudgetServiceTransport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (client_options_lib.ClientOptions): Custom options for the + client_options (google.api_core.client_options.ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT @@ -271,21 +287,17 @@ def __init__( util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) ) - ssl_credentials = None + client_cert_source_func = None is_mtls = False if use_client_cert: if client_options.client_cert_source: - import grpc # type: ignore - - cert, key = client_options.client_cert_source() - ssl_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) is_mtls = True + client_cert_source_func = client_options.client_cert_source else: - creds = SslCredentials() - is_mtls = creds.is_mtls - ssl_credentials = creds.ssl_credentials if is_mtls else None + is_mtls = mtls.has_default_client_cert_source() + client_cert_source_func = ( + mtls.default_client_cert_source() if is_mtls else None + ) # Figure out which api endpoint to use. if client_options.api_endpoint is not None: @@ -328,7 +340,7 @@ def __init__( credentials_file=client_options.credentials_file, host=api_endpoint, scopes=client_options.scopes, - ssl_channel_credentials=ssl_credentials, + client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, ) @@ -349,16 +361,17 @@ def create_budget( create. Args: - request (:class:`~.budget_service.CreateBudgetRequest`): + request (google.cloud.billing.budgets_v1.types.CreateBudgetRequest): The request object. Request for CreateBudget - parent (:class:`str`): + parent (str): Required. The name of the billing account to create the budget in. Values are of the form ``billingAccounts/{billingAccountId}``. + This corresponds to the ``parent`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - budget (:class:`~.budget_model.Budget`): + budget (google.cloud.billing.budgets_v1.types.Budget): Required. Budget to create. This corresponds to the ``budget`` field on the ``request`` instance; if ``request`` is provided, this @@ -371,7 +384,7 @@ def create_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -441,22 +454,24 @@ def update_budget( changed by this method. Args: - request (:class:`~.budget_service.UpdateBudgetRequest`): + request (google.cloud.billing.budgets_v1.types.UpdateBudgetRequest): The request object. Request for UpdateBudget - budget (:class:`~.budget_model.Budget`): + budget (google.cloud.billing.budgets_v1.types.Budget): Required. The updated budget object. The budget to update is specified by the budget name in the budget. + This corresponds to the ``budget`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - update_mask (:class:`~.field_mask.FieldMask`): + update_mask (google.protobuf.field_mask_pb2.FieldMask): Optional. Indicates which fields in the provided budget to update. Read-only fields (such as ``name``) cannot be changed. If this is not provided, then only fields with non-default values from the request are updated. See https://developers.google.com/protocol-buffers/docs/proto3#default for more details about default values. + This corresponds to the ``update_mask`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -468,7 +483,7 @@ def update_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -540,11 +555,12 @@ def get_budget( Cloud Console. Args: - request (:class:`~.budget_service.GetBudgetRequest`): + request (google.cloud.billing.budgets_v1.types.GetBudgetRequest): The request object. Request for GetBudget - name (:class:`str`): + name (str): Required. Name of budget to get. Values are of the form ``billingAccounts/{billingAccountId}/budgets/{budgetId}``. + This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -556,7 +572,7 @@ def get_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -624,12 +640,13 @@ def list_budgets( Cloud Console. Args: - request (:class:`~.budget_service.ListBudgetsRequest`): + request (google.cloud.billing.budgets_v1.types.ListBudgetsRequest): The request object. Request for ListBudgets - parent (:class:`str`): + parent (str): Required. Name of billing account to list budgets under. Values are of the form ``billingAccounts/{billingAccountId}``. + This corresponds to the ``parent`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -641,7 +658,7 @@ def list_budgets( sent along with the request as metadata. Returns: - ~.pagers.ListBudgetsPager: + google.cloud.billing.budgets_v1.services.budget_service.pagers.ListBudgetsPager: Response for ListBudgets Iterating over this object will yield results and resolve additional pages @@ -706,12 +723,13 @@ def delete_budget( deleted. Args: - request (:class:`~.budget_service.DeleteBudgetRequest`): + request (google.cloud.billing.budgets_v1.types.DeleteBudgetRequest): The request object. Request for DeleteBudget - name (:class:`str`): + name (str): Required. Name of the budget to delete. Values are of the form ``billingAccounts/{billingAccountId}/budgets/{budgetId}``. + This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/pagers.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/pagers.py index 4e3a453f7781..a68f005592d4 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/pagers.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/pagers.py @@ -15,7 +15,16 @@ # limitations under the License. # -from typing import Any, AsyncIterable, Awaitable, Callable, Iterable, Sequence, Tuple +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) from google.cloud.billing.budgets_v1.types import budget_model from google.cloud.billing.budgets_v1.types import budget_service @@ -25,7 +34,7 @@ class ListBudgetsPager: """A pager for iterating through ``list_budgets`` requests. This class thinly wraps an initial - :class:`~.budget_service.ListBudgetsResponse` object, and + :class:`google.cloud.billing.budgets_v1.types.ListBudgetsResponse` object, and provides an ``__iter__`` method to iterate through its ``budgets`` field. @@ -34,7 +43,7 @@ class ListBudgetsPager: through the ``budgets`` field on the corresponding responses. - All the usual :class:`~.budget_service.ListBudgetsResponse` + All the usual :class:`google.cloud.billing.budgets_v1.types.ListBudgetsResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ @@ -52,9 +61,9 @@ def __init__( Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (:class:`~.budget_service.ListBudgetsRequest`): + request (google.cloud.billing.budgets_v1.types.ListBudgetsRequest): The initial request object. - response (:class:`~.budget_service.ListBudgetsResponse`): + response (google.cloud.billing.budgets_v1.types.ListBudgetsResponse): The initial response object. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. @@ -87,7 +96,7 @@ class ListBudgetsAsyncPager: """A pager for iterating through ``list_budgets`` requests. This class thinly wraps an initial - :class:`~.budget_service.ListBudgetsResponse` object, and + :class:`google.cloud.billing.budgets_v1.types.ListBudgetsResponse` object, and provides an ``__aiter__`` method to iterate through its ``budgets`` field. @@ -96,7 +105,7 @@ class ListBudgetsAsyncPager: through the ``budgets`` field on the corresponding responses. - All the usual :class:`~.budget_service.ListBudgetsResponse` + All the usual :class:`google.cloud.billing.budgets_v1.types.ListBudgetsResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ @@ -114,9 +123,9 @@ def __init__( Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (:class:`~.budget_service.ListBudgetsRequest`): + request (google.cloud.billing.budgets_v1.types.ListBudgetsRequest): The initial request object. - response (:class:`~.budget_service.ListBudgetsResponse`): + response (google.cloud.billing.budgets_v1.types.ListBudgetsResponse): The initial response object. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/base.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/base.py index f0460b6196b0..19356cdcce01 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/base.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/base.py @@ -74,10 +74,10 @@ def __init__( scope (Optional[Sequence[str]]): A list of scopes. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. """ # Save the hostname. Default to port 443 (HTTPS) if none is specified. @@ -85,6 +85,9 @@ def __init__( host += ":443" self._host = host + # Save the scopes. + self._scopes = scopes or self.AUTH_SCOPES + # If no credentials are provided, then determine the appropriate # defaults. if credentials and credentials_file: @@ -94,20 +97,17 @@ def __init__( if credentials_file is not None: credentials, _ = auth.load_credentials_from_file( - credentials_file, scopes=scopes, quota_project_id=quota_project_id + credentials_file, scopes=self._scopes, quota_project_id=quota_project_id ) elif credentials is None: credentials, _ = auth.default( - scopes=scopes, quota_project_id=quota_project_id + scopes=self._scopes, quota_project_id=quota_project_id ) # Save the credentials. self._credentials = credentials - # Lifted into its own function so it can be stubbed out during tests. - self._prep_wrapped_messages(client_info) - def _prep_wrapped_messages(self, client_info): # Precompute the wrapped methods. self._wrapped_methods = { @@ -123,6 +123,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, @@ -136,6 +137,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, @@ -149,6 +151,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, @@ -162,6 +165,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc.py index 9da459da171f..6576d62cf8f6 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc.py @@ -61,6 +61,7 @@ def __init__( api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, ssl_channel_credentials: grpc.ChannelCredentials = None, + client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None, quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -91,6 +92,10 @@ def __init__( ``api_mtls_endpoint`` is None. ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials for grpc channel. It is ignored if ``channel`` is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure mutual TLS channel. It is + ignored if ``channel`` or ``ssl_channel_credentials`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): @@ -105,72 +110,60 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) if channel: - # Sanity check: Ensure that channel and credentials are not both - # provided. + # Ignore credentials if a channel was passed. credentials = False - # If a channel was explicitly provided, set it. self._grpc_channel = channel self._ssl_channel_credentials = None - elif api_mtls_endpoint: - warnings.warn( - "api_mtls_endpoint and client_cert_source are deprecated", - DeprecationWarning, - ) - host = ( - api_mtls_endpoint - if ":" in api_mtls_endpoint - else api_mtls_endpoint + ":443" - ) + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) - - # Create SSL credentials with client_cert_source or application - # default SSL credentials. - if client_cert_source: - cert, key = client_cert_source() - ssl_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) else: - ssl_credentials = SslCredentials().ssl_credentials - - # create a new channel. The provided one is ignored. - self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, - credentials_file=credentials_file, - ssl_credentials=ssl_credentials, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - self._ssl_channel_credentials = ssl_credentials - else: - host = host if ":" in host else host + ":443" + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + ) - # create a new channel. The provided one is ignored. + if not self._grpc_channel: self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, + self._host, + credentials=self._credentials, credentials_file=credentials_file, - ssl_credentials=ssl_channel_credentials, - scopes=scopes or self.AUTH_SCOPES, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, quota_project_id=quota_project_id, options=[ ("grpc.max_send_message_length", -1), @@ -178,17 +171,8 @@ def __init__( ], ) - self._stubs = {} # type: Dict[str, Callable] - - # Run the base constructor. - super().__init__( - host=host, - credentials=credentials, - credentials_file=credentials_file, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - client_info=client_info, - ) + # Wrap messages. This must be done after self._grpc_channel exists + self._prep_wrapped_messages(client_info) @classmethod def create_channel( @@ -202,7 +186,7 @@ def create_channel( ) -> grpc.Channel: """Create and return a gRPC channel object. Args: - address (Optional[str]): The host for the channel to use. + host (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc_asyncio.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc_asyncio.py index 6f365d0bc773..975ddbc96c01 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc_asyncio.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc_asyncio.py @@ -65,7 +65,7 @@ def create_channel( ) -> aio.Channel: """Create and return a gRPC AsyncIO channel object. Args: - address (Optional[str]): The host for the channel to use. + host (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If @@ -105,6 +105,7 @@ def __init__( api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, ssl_channel_credentials: grpc.ChannelCredentials = None, + client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None, quota_project_id=None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -136,12 +137,16 @@ def __init__( ``api_mtls_endpoint`` is None. ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials for grpc channel. It is ignored if ``channel`` is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure mutual TLS channel. It is + ignored if ``channel`` or ``ssl_channel_credentials`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -150,72 +155,60 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) if channel: - # Sanity check: Ensure that channel and credentials are not both - # provided. + # Ignore credentials if a channel was passed. credentials = False - # If a channel was explicitly provided, set it. self._grpc_channel = channel self._ssl_channel_credentials = None - elif api_mtls_endpoint: - warnings.warn( - "api_mtls_endpoint and client_cert_source are deprecated", - DeprecationWarning, - ) - host = ( - api_mtls_endpoint - if ":" in api_mtls_endpoint - else api_mtls_endpoint + ":443" - ) + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) - - # Create SSL credentials with client_cert_source or application - # default SSL credentials. - if client_cert_source: - cert, key = client_cert_source() - ssl_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) else: - ssl_credentials = SslCredentials().ssl_credentials - - # create a new channel. The provided one is ignored. - self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, - credentials_file=credentials_file, - ssl_credentials=ssl_credentials, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - self._ssl_channel_credentials = ssl_credentials - else: - host = host if ":" in host else host + ":443" + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + ) - # create a new channel. The provided one is ignored. + if not self._grpc_channel: self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, + self._host, + credentials=self._credentials, credentials_file=credentials_file, - ssl_credentials=ssl_channel_credentials, - scopes=scopes or self.AUTH_SCOPES, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, quota_project_id=quota_project_id, options=[ ("grpc.max_send_message_length", -1), @@ -223,17 +216,8 @@ def __init__( ], ) - # Run the base constructor. - super().__init__( - host=host, - credentials=credentials, - credentials_file=credentials_file, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - client_info=client_info, - ) - - self._stubs = {} + # Wrap messages. This must be done after self._grpc_channel exists + self._prep_wrapped_messages(client_info) @property def grpc_channel(self) -> aio.Channel: diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/__init__.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/__init__.py index 52e66d63a903..773dccd51978 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/__init__.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/__init__.py @@ -18,31 +18,31 @@ from .budget_model import ( Budget, BudgetAmount, + Filter, LastPeriodAmount, - ThresholdRule, NotificationsRule, - Filter, + ThresholdRule, ) from .budget_service import ( CreateBudgetRequest, - UpdateBudgetRequest, + DeleteBudgetRequest, GetBudgetRequest, ListBudgetsRequest, ListBudgetsResponse, - DeleteBudgetRequest, + UpdateBudgetRequest, ) __all__ = ( "Budget", "BudgetAmount", + "Filter", "LastPeriodAmount", - "ThresholdRule", "NotificationsRule", - "Filter", + "ThresholdRule", "CreateBudgetRequest", - "UpdateBudgetRequest", + "DeleteBudgetRequest", "GetBudgetRequest", "ListBudgetsRequest", "ListBudgetsResponse", - "DeleteBudgetRequest", + "UpdateBudgetRequest", ) diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_model.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_model.py index b4348541f06d..16d4562af410 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_model.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_model.py @@ -51,18 +51,18 @@ class Budget(proto.Message): display_name (str): User data for display name in UI. The name must be less than or equal to 60 characters. - budget_filter (~.budget_model.Filter): + budget_filter (google.cloud.billing.budgets_v1.types.Filter): Optional. Filters that define which resources are used to compute the actual spend against the budget. - amount (~.budget_model.BudgetAmount): + amount (google.cloud.billing.budgets_v1.types.BudgetAmount): Required. Budgeted amount. - threshold_rules (Sequence[~.budget_model.ThresholdRule]): + threshold_rules (Sequence[google.cloud.billing.budgets_v1.types.ThresholdRule]): Optional. Rules that trigger alerts (notifications of thresholds being crossed) when spend exceeds the specified percentages of the budget. - notifications_rule (~.budget_model.NotificationsRule): + notifications_rule (google.cloud.billing.budgets_v1.types.NotificationsRule): Optional. Rules to apply to notifications sent based on budget spend and thresholds. etag (str): @@ -95,12 +95,14 @@ class BudgetAmount(proto.Message): r"""The budgeted amount for each usage period. Attributes: - specified_amount (~.money.Money): + specified_amount (google.type.money_pb2.Money): A specified amount to use as the budget. ``currency_code`` - is optional. If specified, it must match the currency of the - billing account. The ``currency_code`` is provided on + is optional. If specified when creating a budget, it must + match the currency of the billing account. If specified when + updating a budget, it must match the currency_code of the + existing budget. The ``currency_code`` is provided on output. - last_period_amount (~.budget_model.LastPeriodAmount): + last_period_amount (google.cloud.billing.budgets_v1.types.LastPeriodAmount): Use the last period's actual spend as the budget for the present period. """ @@ -137,7 +139,7 @@ class ThresholdRule(proto.Message): Required. Send an alert when this threshold is exceeded. This is a 1.0-based percentage, so 0.5 = 50%. Validation: non-negative number. - spend_basis (~.budget_model.ThresholdRule.Basis): + spend_basis (google.cloud.billing.budgets_v1.types.ThresholdRule.Basis): Optional. The type of basis used to determine if spend has passed the threshold. Behavior defaults to CURRENT_SPEND if not set. @@ -175,10 +177,13 @@ class NotificationsRule(proto.Message): https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications for more details on Pub/Sub roles and permissions. schema_version (str): - Optional. The schema version of the notification sent to - ``pubsub_topic``. Only "1.0" is accepted. It represents the - JSON schema as defined in - https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format + Optional. Required when + [NotificationsRule.pubsub_topic][google.cloud.billing.budgets.v1.NotificationsRule.pubsub_topic] + is set. The schema version of the notification sent to + [NotificationsRule.pubsub_topic][google.cloud.billing.budgets.v1.NotificationsRule.pubsub_topic]. + Only "1.0" is accepted. It represents the JSON schema as + defined in + https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format. monitoring_notification_channels (Sequence[str]): Optional. Targets to send notifications to when a threshold is exceeded. This is in addition to default recipients who @@ -230,7 +235,7 @@ class Filter(proto.Message): is **not** INCLUDE_SPECIFIED_CREDITS, this field must be empty. See `a list of acceptable credit type values `__. - credit_types_treatment (~.budget_model.Filter.CreditTypesTreatment): + credit_types_treatment (google.cloud.billing.budgets_v1.types.Filter.CreditTypesTreatment): Optional. If not set, default behavior is ``INCLUDE_ALL_CREDITS``. services (Sequence[str]): @@ -248,7 +253,7 @@ class Filter(proto.Message): account, usage from the parent account will be included. If the field is omitted, the report will include usage from the parent account and all subaccounts, if they exist. - labels (Sequence[~.budget_model.Filter.LabelsEntry]): + labels (Sequence[google.cloud.billing.budgets_v1.types.Filter.LabelsEntry]): Optional. A single label and value pair specifying that usage from only this set of labeled resources should be included in the diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_service.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_service.py index 6fd3ff09ba75..5f1f54c10320 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_service.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_service.py @@ -43,7 +43,7 @@ class CreateBudgetRequest(proto.Message): Required. The name of the billing account to create the budget in. Values are of the form ``billingAccounts/{billingAccountId}``. - budget (~.budget_model.Budget): + budget (google.cloud.billing.budgets_v1.types.Budget): Required. Budget to create. """ @@ -56,11 +56,11 @@ class UpdateBudgetRequest(proto.Message): r"""Request for UpdateBudget Attributes: - budget (~.budget_model.Budget): + budget (google.cloud.billing.budgets_v1.types.Budget): Required. The updated budget object. The budget to update is specified by the budget name in the budget. - update_mask (~.field_mask.FieldMask): + update_mask (google.protobuf.field_mask_pb2.FieldMask): Optional. Indicates which fields in the provided budget to update. Read-only fields (such as ``name``) cannot be changed. If this is not provided, then only fields with @@ -116,7 +116,7 @@ class ListBudgetsResponse(proto.Message): r"""Response for ListBudgets Attributes: - budgets (Sequence[~.budget_model.Budget]): + budgets (Sequence[google.cloud.billing.budgets_v1.types.Budget]): List of the budgets owned by the requested billing account. next_page_token (str): diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_model.proto b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_model.proto index 050737b8c5b1..636f32903458 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_model.proto +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_model.proto @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -72,9 +72,10 @@ message BudgetAmount { // Specification for what amount to use as the budget. oneof budget_amount { // A specified amount to use as the budget. - // `currency_code` is optional. If specified, it must match the - // currency of the billing account. The `currency_code` is provided on - // output. + // `currency_code` is optional. If specified when creating a budget, it must + // match the currency of the billing account. If specified when updating a + // budget, it must match the existing budget currency_code. + // The `currency_code` is provided on output. google.type.Money specified_amount = 1; // Use the last period's actual spend as the budget for the present period. @@ -136,9 +137,12 @@ message AllUpdatesRule { // for more details on Pub/Sub roles and permissions. string pubsub_topic = 1 [(google.api.field_behavior) = OPTIONAL]; - // Optional. The schema version of the notification sent to `pubsub_topic`. + // Optional. Required when + // [AllUpdatesRule.pubsub_topic][google.cloud.billing.budgets.v1beta1.AllUpdatesRule.pubsub_topic] + // is set. The schema version of the notification sent to + // [AllUpdatesRule.pubsub_topic][google.cloud.billing.budgets.v1beta1.AllUpdatesRule.pubsub_topic]. // Only "1.0" is accepted. It represents the JSON schema as defined in - // https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format + // https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format. string schema_version = 2 [(google.api.field_behavior) = OPTIONAL]; // Optional. Targets to send notifications to when a threshold is exceeded. diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_service.proto b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_service.proto index fa52395984c4..d97934704831 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_service.proto +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/proto/budget_service.proto @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/async_client.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/async_client.py index 641321d16249..5bf5960f647d 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/async_client.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/async_client.py @@ -80,7 +80,36 @@ class BudgetServiceAsyncClient: BudgetServiceClient.parse_common_location_path ) - from_service_account_file = BudgetServiceClient.from_service_account_file + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + BudgetServiceAsyncClient: The constructed client. + """ + return BudgetServiceClient.from_service_account_info.__func__(BudgetServiceAsyncClient, info, *args, **kwargs) # type: ignore + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + BudgetServiceAsyncClient: The constructed client. + """ + return BudgetServiceClient.from_service_account_file.__func__(BudgetServiceAsyncClient, filename, *args, **kwargs) # type: ignore + from_service_account_json = from_service_account_file @property @@ -158,7 +187,7 @@ async def create_budget( number of budgets you can create. Args: - request (:class:`~.budget_service.CreateBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1beta1.types.CreateBudgetRequest`): The request object. Request for CreateBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -168,7 +197,7 @@ async def create_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1beta1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -219,7 +248,7 @@ async def update_budget( changed by this method. Args: - request (:class:`~.budget_service.UpdateBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1beta1.types.UpdateBudgetRequest`): The request object. Request for UpdateBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -229,7 +258,7 @@ async def update_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1beta1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -256,6 +285,7 @@ async def update_budget( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, @@ -291,7 +321,7 @@ async def get_budget( Cloud Console. Args: - request (:class:`~.budget_service.GetBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1beta1.types.GetBudgetRequest`): The request object. Request for GetBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -301,7 +331,7 @@ async def get_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1beta1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -328,6 +358,7 @@ async def get_budget( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, @@ -361,7 +392,7 @@ async def list_budgets( Cloud Console. Args: - request (:class:`~.budget_service.ListBudgetsRequest`): + request (:class:`google.cloud.billing.budgets_v1beta1.types.ListBudgetsRequest`): The request object. Request for ListBudgets retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -371,7 +402,7 @@ async def list_budgets( sent along with the request as metadata. Returns: - ~.pagers.ListBudgetsAsyncPager: + google.cloud.billing.budgets_v1beta1.services.budget_service.pagers.ListBudgetsAsyncPager: Response for ListBudgets Iterating over this object will yield results and resolve additional pages @@ -393,6 +424,7 @@ async def list_budgets( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, @@ -428,7 +460,7 @@ async def delete_budget( deleted. Args: - request (:class:`~.budget_service.DeleteBudgetRequest`): + request (:class:`google.cloud.billing.budgets_v1beta1.types.DeleteBudgetRequest`): The request object. Request for DeleteBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -452,6 +484,7 @@ async def delete_budget( predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=DEFAULT_CLIENT_INFO, diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/client.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/client.py index 93223ad056cc..12f84b3d9903 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/client.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/client.py @@ -112,6 +112,22 @@ def _get_default_mtls_endpoint(api_endpoint): DEFAULT_ENDPOINT ) + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + BudgetServiceClient: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_info(info) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + @classmethod def from_service_account_file(cls, filename: str, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -124,7 +140,7 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): kwargs: Additional arguments to pass to the constructor. Returns: - {@api.name}: The constructed client. + BudgetServiceClient: The constructed client. """ credentials = service_account.Credentials.from_service_account_file(filename) kwargs["credentials"] = credentials @@ -231,10 +247,10 @@ def __init__( credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. - transport (Union[str, ~.BudgetServiceTransport]): The + transport (Union[str, BudgetServiceTransport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (client_options_lib.ClientOptions): Custom options for the + client_options (google.api_core.client_options.ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT @@ -270,21 +286,17 @@ def __init__( util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) ) - ssl_credentials = None + client_cert_source_func = None is_mtls = False if use_client_cert: if client_options.client_cert_source: - import grpc # type: ignore - - cert, key = client_options.client_cert_source() - ssl_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) is_mtls = True + client_cert_source_func = client_options.client_cert_source else: - creds = SslCredentials() - is_mtls = creds.is_mtls - ssl_credentials = creds.ssl_credentials if is_mtls else None + is_mtls = mtls.has_default_client_cert_source() + client_cert_source_func = ( + mtls.default_client_cert_source() if is_mtls else None + ) # Figure out which api endpoint to use. if client_options.api_endpoint is not None: @@ -327,7 +339,7 @@ def __init__( credentials_file=client_options.credentials_file, host=api_endpoint, scopes=client_options.scopes, - ssl_channel_credentials=ssl_credentials, + client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, ) @@ -346,7 +358,7 @@ def create_budget( number of budgets you can create. Args: - request (:class:`~.budget_service.CreateBudgetRequest`): + request (google.cloud.billing.budgets_v1beta1.types.CreateBudgetRequest): The request object. Request for CreateBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -356,7 +368,7 @@ def create_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1beta1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -408,7 +420,7 @@ def update_budget( changed by this method. Args: - request (:class:`~.budget_service.UpdateBudgetRequest`): + request (google.cloud.billing.budgets_v1beta1.types.UpdateBudgetRequest): The request object. Request for UpdateBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -418,7 +430,7 @@ def update_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1beta1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -473,7 +485,7 @@ def get_budget( Cloud Console. Args: - request (:class:`~.budget_service.GetBudgetRequest`): + request (google.cloud.billing.budgets_v1beta1.types.GetBudgetRequest): The request object. Request for GetBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -483,7 +495,7 @@ def get_budget( sent along with the request as metadata. Returns: - ~.budget_model.Budget: + google.cloud.billing.budgets_v1beta1.types.Budget: A budget is a plan that describes what you expect to spend on Cloud projects, plus the rules to execute as @@ -536,7 +548,7 @@ def list_budgets( Cloud Console. Args: - request (:class:`~.budget_service.ListBudgetsRequest`): + request (google.cloud.billing.budgets_v1beta1.types.ListBudgetsRequest): The request object. Request for ListBudgets retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -546,7 +558,7 @@ def list_budgets( sent along with the request as metadata. Returns: - ~.pagers.ListBudgetsPager: + google.cloud.billing.budgets_v1beta1.services.budget_service.pagers.ListBudgetsPager: Response for ListBudgets Iterating over this object will yield results and resolve additional pages @@ -596,7 +608,7 @@ def delete_budget( deleted. Args: - request (:class:`~.budget_service.DeleteBudgetRequest`): + request (google.cloud.billing.budgets_v1beta1.types.DeleteBudgetRequest): The request object. Request for DeleteBudget retry (google.api_core.retry.Retry): Designation of what errors, if any, diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/pagers.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/pagers.py index b9456d313ecd..7551f27e182e 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/pagers.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/pagers.py @@ -15,7 +15,16 @@ # limitations under the License. # -from typing import Any, AsyncIterable, Awaitable, Callable, Iterable, Sequence, Tuple +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) from google.cloud.billing.budgets_v1beta1.types import budget_model from google.cloud.billing.budgets_v1beta1.types import budget_service @@ -25,7 +34,7 @@ class ListBudgetsPager: """A pager for iterating through ``list_budgets`` requests. This class thinly wraps an initial - :class:`~.budget_service.ListBudgetsResponse` object, and + :class:`google.cloud.billing.budgets_v1beta1.types.ListBudgetsResponse` object, and provides an ``__iter__`` method to iterate through its ``budgets`` field. @@ -34,7 +43,7 @@ class ListBudgetsPager: through the ``budgets`` field on the corresponding responses. - All the usual :class:`~.budget_service.ListBudgetsResponse` + All the usual :class:`google.cloud.billing.budgets_v1beta1.types.ListBudgetsResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ @@ -52,9 +61,9 @@ def __init__( Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (:class:`~.budget_service.ListBudgetsRequest`): + request (google.cloud.billing.budgets_v1beta1.types.ListBudgetsRequest): The initial request object. - response (:class:`~.budget_service.ListBudgetsResponse`): + response (google.cloud.billing.budgets_v1beta1.types.ListBudgetsResponse): The initial response object. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. @@ -87,7 +96,7 @@ class ListBudgetsAsyncPager: """A pager for iterating through ``list_budgets`` requests. This class thinly wraps an initial - :class:`~.budget_service.ListBudgetsResponse` object, and + :class:`google.cloud.billing.budgets_v1beta1.types.ListBudgetsResponse` object, and provides an ``__aiter__`` method to iterate through its ``budgets`` field. @@ -96,7 +105,7 @@ class ListBudgetsAsyncPager: through the ``budgets`` field on the corresponding responses. - All the usual :class:`~.budget_service.ListBudgetsResponse` + All the usual :class:`google.cloud.billing.budgets_v1beta1.types.ListBudgetsResponse` attributes are available on the pager. If multiple requests are made, only the most recent response is retained, and thus used for attribute lookup. """ @@ -114,9 +123,9 @@ def __init__( Args: method (Callable): The method that was originally called, and which instantiated this pager. - request (:class:`~.budget_service.ListBudgetsRequest`): + request (google.cloud.billing.budgets_v1beta1.types.ListBudgetsRequest): The initial request object. - response (:class:`~.budget_service.ListBudgetsResponse`): + response (google.cloud.billing.budgets_v1beta1.types.ListBudgetsResponse): The initial response object. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/base.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/base.py index 5901b3f5aab1..2b576460512a 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/base.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/base.py @@ -74,10 +74,10 @@ def __init__( scope (Optional[Sequence[str]]): A list of scopes. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. """ # Save the hostname. Default to port 443 (HTTPS) if none is specified. @@ -85,6 +85,9 @@ def __init__( host += ":443" self._host = host + # Save the scopes. + self._scopes = scopes or self.AUTH_SCOPES + # If no credentials are provided, then determine the appropriate # defaults. if credentials and credentials_file: @@ -94,20 +97,17 @@ def __init__( if credentials_file is not None: credentials, _ = auth.load_credentials_from_file( - credentials_file, scopes=scopes, quota_project_id=quota_project_id + credentials_file, scopes=self._scopes, quota_project_id=quota_project_id ) elif credentials is None: credentials, _ = auth.default( - scopes=scopes, quota_project_id=quota_project_id + scopes=self._scopes, quota_project_id=quota_project_id ) # Save the credentials. self._credentials = credentials - # Lifted into its own function so it can be stubbed out during tests. - self._prep_wrapped_messages(client_info) - def _prep_wrapped_messages(self, client_info): # Precompute the wrapped methods. self._wrapped_methods = { @@ -123,6 +123,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, @@ -136,6 +137,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, @@ -149,6 +151,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, @@ -162,6 +165,7 @@ def _prep_wrapped_messages(self, client_info): predicate=retries.if_exception_type( exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, ), + deadline=60.0, ), default_timeout=60.0, client_info=client_info, diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc.py index 6d7ab32cb87f..ed0d197129f5 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc.py @@ -61,6 +61,7 @@ def __init__( api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, ssl_channel_credentials: grpc.ChannelCredentials = None, + client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None, quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -91,6 +92,10 @@ def __init__( ``api_mtls_endpoint`` is None. ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials for grpc channel. It is ignored if ``channel`` is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure mutual TLS channel. It is + ignored if ``channel`` or ``ssl_channel_credentials`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): @@ -105,72 +110,60 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) if channel: - # Sanity check: Ensure that channel and credentials are not both - # provided. + # Ignore credentials if a channel was passed. credentials = False - # If a channel was explicitly provided, set it. self._grpc_channel = channel self._ssl_channel_credentials = None - elif api_mtls_endpoint: - warnings.warn( - "api_mtls_endpoint and client_cert_source are deprecated", - DeprecationWarning, - ) - host = ( - api_mtls_endpoint - if ":" in api_mtls_endpoint - else api_mtls_endpoint + ":443" - ) + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) - - # Create SSL credentials with client_cert_source or application - # default SSL credentials. - if client_cert_source: - cert, key = client_cert_source() - ssl_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) else: - ssl_credentials = SslCredentials().ssl_credentials - - # create a new channel. The provided one is ignored. - self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, - credentials_file=credentials_file, - ssl_credentials=ssl_credentials, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - self._ssl_channel_credentials = ssl_credentials - else: - host = host if ":" in host else host + ":443" + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + ) - # create a new channel. The provided one is ignored. + if not self._grpc_channel: self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, + self._host, + credentials=self._credentials, credentials_file=credentials_file, - ssl_credentials=ssl_channel_credentials, - scopes=scopes or self.AUTH_SCOPES, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, quota_project_id=quota_project_id, options=[ ("grpc.max_send_message_length", -1), @@ -178,17 +171,8 @@ def __init__( ], ) - self._stubs = {} # type: Dict[str, Callable] - - # Run the base constructor. - super().__init__( - host=host, - credentials=credentials, - credentials_file=credentials_file, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - client_info=client_info, - ) + # Wrap messages. This must be done after self._grpc_channel exists + self._prep_wrapped_messages(client_info) @classmethod def create_channel( @@ -202,7 +186,7 @@ def create_channel( ) -> grpc.Channel: """Create and return a gRPC channel object. Args: - address (Optional[str]): The host for the channel to use. + host (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc_asyncio.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc_asyncio.py index c84197df9de5..484dbc2c909e 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc_asyncio.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc_asyncio.py @@ -65,7 +65,7 @@ def create_channel( ) -> aio.Channel: """Create and return a gRPC AsyncIO channel object. Args: - address (Optional[str]): The host for the channel to use. + host (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If @@ -105,6 +105,7 @@ def __init__( api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, ssl_channel_credentials: grpc.ChannelCredentials = None, + client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None, quota_project_id=None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -136,12 +137,16 @@ def __init__( ``api_mtls_endpoint`` is None. ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials for grpc channel. It is ignored if ``channel`` is provided. + client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): + A callback to provide client certificate bytes and private key bytes, + both in PEM format. It is used to configure mutual TLS channel. It is + ignored if ``channel`` or ``ssl_channel_credentials`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -150,72 +155,60 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._grpc_channel = None self._ssl_channel_credentials = ssl_channel_credentials + self._stubs: Dict[str, Callable] = {} + + if api_mtls_endpoint: + warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) + if client_cert_source: + warnings.warn("client_cert_source is deprecated", DeprecationWarning) if channel: - # Sanity check: Ensure that channel and credentials are not both - # provided. + # Ignore credentials if a channel was passed. credentials = False - # If a channel was explicitly provided, set it. self._grpc_channel = channel self._ssl_channel_credentials = None - elif api_mtls_endpoint: - warnings.warn( - "api_mtls_endpoint and client_cert_source are deprecated", - DeprecationWarning, - ) - host = ( - api_mtls_endpoint - if ":" in api_mtls_endpoint - else api_mtls_endpoint + ":443" - ) + else: + if api_mtls_endpoint: + host = api_mtls_endpoint + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + self._ssl_channel_credentials = SslCredentials().ssl_credentials - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) - - # Create SSL credentials with client_cert_source or application - # default SSL credentials. - if client_cert_source: - cert, key = client_cert_source() - ssl_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) else: - ssl_credentials = SslCredentials().ssl_credentials - - # create a new channel. The provided one is ignored. - self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, - credentials_file=credentials_file, - ssl_credentials=ssl_credentials, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - self._ssl_channel_credentials = ssl_credentials - else: - host = host if ":" in host else host + ":443" + if client_cert_source_for_mtls and not ssl_channel_credentials: + cert, key = client_cert_source_for_mtls() + self._ssl_channel_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) - if credentials is None: - credentials, _ = auth.default( - scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id - ) + # The base transport sets the host, credentials and scopes + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + client_info=client_info, + ) - # create a new channel. The provided one is ignored. + if not self._grpc_channel: self._grpc_channel = type(self).create_channel( - host, - credentials=credentials, + self._host, + credentials=self._credentials, credentials_file=credentials_file, - ssl_credentials=ssl_channel_credentials, - scopes=scopes or self.AUTH_SCOPES, + scopes=self._scopes, + ssl_credentials=self._ssl_channel_credentials, quota_project_id=quota_project_id, options=[ ("grpc.max_send_message_length", -1), @@ -223,17 +216,8 @@ def __init__( ], ) - # Run the base constructor. - super().__init__( - host=host, - credentials=credentials, - credentials_file=credentials_file, - scopes=scopes or self.AUTH_SCOPES, - quota_project_id=quota_project_id, - client_info=client_info, - ) - - self._stubs = {} + # Wrap messages. This must be done after self._grpc_channel exists + self._prep_wrapped_messages(client_info) @property def grpc_channel(self) -> aio.Channel: diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/__init__.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/__init__.py index 4e7b3b3125bc..56a2e86751bc 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/__init__.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/__init__.py @@ -16,33 +16,33 @@ # from .budget_model import ( + AllUpdatesRule, Budget, BudgetAmount, + Filter, LastPeriodAmount, ThresholdRule, - AllUpdatesRule, - Filter, ) from .budget_service import ( CreateBudgetRequest, - UpdateBudgetRequest, + DeleteBudgetRequest, GetBudgetRequest, ListBudgetsRequest, ListBudgetsResponse, - DeleteBudgetRequest, + UpdateBudgetRequest, ) __all__ = ( + "AllUpdatesRule", "Budget", "BudgetAmount", + "Filter", "LastPeriodAmount", "ThresholdRule", - "AllUpdatesRule", - "Filter", "CreateBudgetRequest", - "UpdateBudgetRequest", + "DeleteBudgetRequest", "GetBudgetRequest", "ListBudgetsRequest", "ListBudgetsResponse", - "DeleteBudgetRequest", + "UpdateBudgetRequest", ) diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_model.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_model.py index e5a44ca2aedf..89526a9c1fbb 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_model.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_model.py @@ -51,18 +51,18 @@ class Budget(proto.Message): display_name (str): User data for display name in UI. Validation: <= 60 chars. - budget_filter (~.budget_model.Filter): + budget_filter (google.cloud.billing.budgets_v1beta1.types.Filter): Optional. Filters that define which resources are used to compute the actual spend against the budget. - amount (~.budget_model.BudgetAmount): + amount (google.cloud.billing.budgets_v1beta1.types.BudgetAmount): Required. Budgeted amount. - threshold_rules (Sequence[~.budget_model.ThresholdRule]): + threshold_rules (Sequence[google.cloud.billing.budgets_v1beta1.types.ThresholdRule]): Optional. Rules that trigger alerts (notifications of thresholds being crossed) when spend exceeds the specified percentages of the budget. - all_updates_rule (~.budget_model.AllUpdatesRule): + all_updates_rule (google.cloud.billing.budgets_v1beta1.types.AllUpdatesRule): Optional. Rules to apply to notifications sent based on budget spend and thresholds. etag (str): @@ -93,12 +93,13 @@ class BudgetAmount(proto.Message): r"""The budgeted amount for each usage period. Attributes: - specified_amount (~.money.Money): + specified_amount (google.type.money_pb2.Money): A specified amount to use as the budget. ``currency_code`` - is optional. If specified, it must match the currency of the - billing account. The ``currency_code`` is provided on - output. - last_period_amount (~.budget_model.LastPeriodAmount): + is optional. If specified when creating a budget, it must + match the currency of the billing account. If specified when + updating a budget, it must match the existing budget + currency_code. The ``currency_code`` is provided on output. + last_period_amount (google.cloud.billing.budgets_v1beta1.types.LastPeriodAmount): Use the last period's actual spend as the budget for the present period. """ @@ -135,7 +136,7 @@ class ThresholdRule(proto.Message): Required. Send an alert when this threshold is exceeded. This is a 1.0-based percentage, so 0.5 = 50%. Validation: non-negative number. - spend_basis (~.budget_model.ThresholdRule.Basis): + spend_basis (google.cloud.billing.budgets_v1beta1.types.ThresholdRule.Basis): Optional. The type of basis used to determine if spend has passed the threshold. Behavior defaults to CURRENT_SPEND if not set. @@ -173,10 +174,13 @@ class AllUpdatesRule(proto.Message): https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications for more details on Pub/Sub roles and permissions. schema_version (str): - Optional. The schema version of the notification sent to - ``pubsub_topic``. Only "1.0" is accepted. It represents the - JSON schema as defined in - https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format + Optional. Required when + [AllUpdatesRule.pubsub_topic][google.cloud.billing.budgets.v1beta1.AllUpdatesRule.pubsub_topic] + is set. The schema version of the notification sent to + [AllUpdatesRule.pubsub_topic][google.cloud.billing.budgets.v1beta1.AllUpdatesRule.pubsub_topic]. + Only "1.0" is accepted. It represents the JSON schema as + defined in + https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format. monitoring_notification_channels (Sequence[str]): Optional. Targets to send notifications to when a threshold is exceeded. This is in addition to default recipients who @@ -228,7 +232,7 @@ class Filter(proto.Message): is **not** INCLUDE_SPECIFIED_CREDITS, this field must be empty. See `a list of acceptable credit type values `__. - credit_types_treatment (~.budget_model.Filter.CreditTypesTreatment): + credit_types_treatment (google.cloud.billing.budgets_v1beta1.types.Filter.CreditTypesTreatment): Optional. If not set, default behavior is ``INCLUDE_ALL_CREDITS``. services (Sequence[str]): @@ -246,7 +250,7 @@ class Filter(proto.Message): account, usage from the parent account will be included. If omitted, the report will include usage from the parent account and all subaccounts, if they exist. - labels (Sequence[~.budget_model.Filter.LabelsEntry]): + labels (Sequence[google.cloud.billing.budgets_v1beta1.types.Filter.LabelsEntry]): Optional. A single label and value pair specifying that usage from only this set of labeled resources should be included in the diff --git a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_service.py b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_service.py index 1961e319f6da..eee85c5be37e 100644 --- a/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_service.py +++ b/packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/types/budget_service.py @@ -43,7 +43,7 @@ class CreateBudgetRequest(proto.Message): Required. The name of the billing account to create the budget in. Values are of the form ``billingAccounts/{billingAccountId}``. - budget (~.budget_model.Budget): + budget (google.cloud.billing.budgets_v1beta1.types.Budget): Required. Budget to create. """ @@ -56,11 +56,11 @@ class UpdateBudgetRequest(proto.Message): r"""Request for UpdateBudget Attributes: - budget (~.budget_model.Budget): + budget (google.cloud.billing.budgets_v1beta1.types.Budget): Required. The updated budget object. The budget to update is specified by the budget name in the budget. - update_mask (~.field_mask.FieldMask): + update_mask (google.protobuf.field_mask_pb2.FieldMask): Optional. Indicates which fields in the provided budget to update. Read-only fields (such as ``name``) cannot be changed. If this is not provided, then only fields with @@ -116,7 +116,7 @@ class ListBudgetsResponse(proto.Message): r"""Response for ListBudgets Attributes: - budgets (Sequence[~.budget_model.Budget]): + budgets (Sequence[google.cloud.billing.budgets_v1beta1.types.Budget]): List of the budgets owned by the requested billing account. next_page_token (str): diff --git a/packages/google-cloud-billing-budgets/noxfile.py b/packages/google-cloud-billing-budgets/noxfile.py index a57e24be11e3..4d37cd3a7eb6 100644 --- a/packages/google-cloud-billing-budgets/noxfile.py +++ b/packages/google-cloud-billing-budgets/noxfile.py @@ -18,6 +18,7 @@ from __future__ import absolute_import import os +import pathlib import shutil import nox @@ -30,6 +31,22 @@ SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"] UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9"] +CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute() + +# 'docfx' is excluded since it only needs to run in 'docs-presubmit' +nox.options.sessions = [ + "unit", + "system", + "cover", + "lint", + "lint_setup_py", + "blacken", + "docs", +] + +# Error if a python version is missing +nox.options.error_on_missing_interpreters = True + @nox.session(python=DEFAULT_PYTHON_VERSION) def lint(session): @@ -70,17 +87,21 @@ def lint_setup_py(session): def default(session): # Install all test dependencies, then install this package in-place. - session.install("asyncmock", "pytest-asyncio") - session.install( - "mock", "pytest", "pytest-cov", + constraints_path = str( + CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" ) - session.install("-e", ".") + session.install("asyncmock", "pytest-asyncio", "-c", constraints_path) + + session.install("mock", "pytest", "pytest-cov", "-c", constraints_path) + + session.install("-e", ".", "-c", constraints_path) # Run py.test against the unit tests. session.run( "py.test", "--quiet", + f"--junitxml=unit_{session.python}_sponge_log.xml", "--cov=google/cloud", "--cov=tests/unit", "--cov-append", @@ -101,6 +122,9 @@ def unit(session): @nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS) def system(session): """Run the system test suite.""" + constraints_path = str( + CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" + ) system_test_path = os.path.join("tests", "system.py") system_test_folder_path = os.path.join("tests", "system") @@ -110,6 +134,9 @@ def system(session): # Sanity check: Only run tests if the environment variable is set. if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""): session.skip("Credentials must be set via environment variable") + # Install pyopenssl for mTLS testing. + if os.environ.get("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true": + session.install("pyopenssl") system_test_exists = os.path.exists(system_test_path) system_test_folder_exists = os.path.exists(system_test_folder_path) @@ -122,16 +149,26 @@ def system(session): # Install all test dependencies, then install this package into the # virtualenv's dist-packages. - session.install( - "mock", "pytest", "google-cloud-testutils", - ) - session.install("-e", ".") + session.install("mock", "pytest", "google-cloud-testutils", "-c", constraints_path) + session.install("-e", ".", "-c", constraints_path) # Run py.test against the system tests. if system_test_exists: - session.run("py.test", "--quiet", system_test_path, *session.posargs) + session.run( + "py.test", + "--quiet", + f"--junitxml=system_{session.python}_sponge_log.xml", + system_test_path, + *session.posargs, + ) if system_test_folder_exists: - session.run("py.test", "--quiet", system_test_folder_path, *session.posargs) + session.run( + "py.test", + "--quiet", + f"--junitxml=system_{session.python}_sponge_log.xml", + system_test_folder_path, + *session.posargs, + ) @nox.session(python=DEFAULT_PYTHON_VERSION) @@ -142,7 +179,7 @@ def cover(session): test runs (not system test runs), and then erases coverage data. """ session.install("coverage", "pytest-cov") - session.run("coverage", "report", "--show-missing", "--fail-under=99") + session.run("coverage", "report", "--show-missing", "--fail-under=98") session.run("coverage", "erase") @@ -174,9 +211,7 @@ def docfx(session): """Build the docfx yaml files for this library.""" session.install("-e", ".") - # sphinx-docfx-yaml supports up to sphinx version 1.5.5. - # https://github.com/docascode/sphinx-docfx-yaml/issues/97 - session.install("sphinx==1.5.5", "alabaster", "recommonmark", "sphinx-docfx-yaml") + session.install("sphinx", "alabaster", "recommonmark", "gcp-sphinx-docfx-yaml") shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) session.run( diff --git a/packages/google-cloud-billing-budgets/renovate.json b/packages/google-cloud-billing-budgets/renovate.json index 4fa949311b20..f08bc22c9a55 100644 --- a/packages/google-cloud-billing-budgets/renovate.json +++ b/packages/google-cloud-billing-budgets/renovate.json @@ -1,5 +1,6 @@ { "extends": [ "config:base", ":preserveSemverRanges" - ] + ], + "ignorePaths": [".pre-commit-config.yaml"] } diff --git a/packages/google-cloud-billing-budgets/setup.py b/packages/google-cloud-billing-budgets/setup.py index 1887472fd2cc..5c46b6c172ff 100644 --- a/packages/google-cloud-billing-budgets/setup.py +++ b/packages/google-cloud-billing-budgets/setup.py @@ -25,11 +25,11 @@ version = "1.1.1" release_status = "Development Status :: 4 - Beta" dependencies = [ - "google-api-core[grpc] >= 1.22.0, < 2.0.0dev", + "google-api-core[grpc] >= 1.22.2, < 2.0.0dev", "proto-plus >= 1.10.0", - "libcst >= 0.2.5", ] +extras = {"libcst": "libcst >= 0.2.5"} package_root = os.path.abspath(os.path.dirname(__file__)) @@ -75,6 +75,7 @@ scripts=["scripts/fixup_budgets_v1beta1_keywords.py"], namespace_packages=namespaces, install_requires=dependencies, + extras_requires=extras, include_package_data=True, zip_safe=False, ) diff --git a/packages/google-cloud-billing-budgets/synth.metadata b/packages/google-cloud-billing-budgets/synth.metadata index 67f8821962b5..a4691ac83f52 100644 --- a/packages/google-cloud-billing-budgets/synth.metadata +++ b/packages/google-cloud-billing-budgets/synth.metadata @@ -3,23 +3,23 @@ { "git": { "name": ".", - "remote": "https://github.com/googleapis/python-billingbudgets.git", - "sha": "09a543dba8845a1fbb8759bb3b07b35be77fbaad" + "remote": "git@github.com:googleapis/python-billingbudgets", + "sha": "3b635366f5fe5806af3e3209fa06f26318b80c85" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "dd372aa22ded7a8ba6f0e03a80e06358a3fa0907", - "internalRef": "347055288" + "sha": "56fc6d43fed71188d7e18f3ca003544646c4ab35", + "internalRef": "366346972" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "41a4e56982620d3edcf110d76f4fcdfdec471ac8" + "sha": "6d76df2138f8f841e5a5b9ac427f81def520c15f" } } ], @@ -42,118 +42,5 @@ "generator": "bazel" } } - ], - "generatedFiles": [ - ".flake8", - ".github/CONTRIBUTING.md", - ".github/ISSUE_TEMPLATE/bug_report.md", - ".github/ISSUE_TEMPLATE/feature_request.md", - ".github/ISSUE_TEMPLATE/support_request.md", - ".github/PULL_REQUEST_TEMPLATE.md", - ".github/release-please.yml", - ".github/snippet-bot.yml", - ".gitignore", - ".kokoro/build.sh", - ".kokoro/continuous/common.cfg", - ".kokoro/continuous/continuous.cfg", - ".kokoro/docker/docs/Dockerfile", - ".kokoro/docker/docs/fetch_gpg_keys.sh", - ".kokoro/docs/common.cfg", - ".kokoro/docs/docs-presubmit.cfg", - ".kokoro/docs/docs.cfg", - ".kokoro/populate-secrets.sh", - ".kokoro/presubmit/common.cfg", - ".kokoro/presubmit/presubmit.cfg", - ".kokoro/publish-docs.sh", - ".kokoro/release.sh", - ".kokoro/release/common.cfg", - ".kokoro/release/release.cfg", - ".kokoro/samples/lint/common.cfg", - ".kokoro/samples/lint/continuous.cfg", - ".kokoro/samples/lint/periodic.cfg", - ".kokoro/samples/lint/presubmit.cfg", - ".kokoro/samples/python3.6/common.cfg", - ".kokoro/samples/python3.6/continuous.cfg", - ".kokoro/samples/python3.6/periodic.cfg", - ".kokoro/samples/python3.6/presubmit.cfg", - ".kokoro/samples/python3.7/common.cfg", - ".kokoro/samples/python3.7/continuous.cfg", - ".kokoro/samples/python3.7/periodic.cfg", - ".kokoro/samples/python3.7/presubmit.cfg", - ".kokoro/samples/python3.8/common.cfg", - ".kokoro/samples/python3.8/continuous.cfg", - ".kokoro/samples/python3.8/periodic.cfg", - ".kokoro/samples/python3.8/presubmit.cfg", - ".kokoro/test-samples.sh", - ".kokoro/trampoline.sh", - ".kokoro/trampoline_v2.sh", - ".pre-commit-config.yaml", - ".trampolinerc", - "CODE_OF_CONDUCT.md", - "CONTRIBUTING.rst", - "LICENSE", - "MANIFEST.in", - "docs/_static/custom.css", - "docs/_templates/layout.html", - "docs/budgets_v1/services.rst", - "docs/budgets_v1/types.rst", - "docs/budgets_v1beta1/services.rst", - "docs/budgets_v1beta1/types.rst", - "docs/conf.py", - "docs/multiprocessing.rst", - "google/cloud/billing/budgets/__init__.py", - "google/cloud/billing/budgets/py.typed", - "google/cloud/billing/budgets_v1/__init__.py", - "google/cloud/billing/budgets_v1/proto/budget_model.proto", - "google/cloud/billing/budgets_v1/proto/budget_service.proto", - "google/cloud/billing/budgets_v1/py.typed", - "google/cloud/billing/budgets_v1/services/__init__.py", - "google/cloud/billing/budgets_v1/services/budget_service/__init__.py", - "google/cloud/billing/budgets_v1/services/budget_service/async_client.py", - "google/cloud/billing/budgets_v1/services/budget_service/client.py", - "google/cloud/billing/budgets_v1/services/budget_service/pagers.py", - "google/cloud/billing/budgets_v1/services/budget_service/transports/__init__.py", - "google/cloud/billing/budgets_v1/services/budget_service/transports/base.py", - "google/cloud/billing/budgets_v1/services/budget_service/transports/grpc.py", - "google/cloud/billing/budgets_v1/services/budget_service/transports/grpc_asyncio.py", - "google/cloud/billing/budgets_v1/types/__init__.py", - "google/cloud/billing/budgets_v1/types/budget_model.py", - "google/cloud/billing/budgets_v1/types/budget_service.py", - "google/cloud/billing/budgets_v1beta1/__init__.py", - "google/cloud/billing/budgets_v1beta1/proto/budget_model.proto", - "google/cloud/billing/budgets_v1beta1/proto/budget_service.proto", - "google/cloud/billing/budgets_v1beta1/py.typed", - "google/cloud/billing/budgets_v1beta1/services/__init__.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/__init__.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/async_client.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/client.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/pagers.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/transports/__init__.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/transports/base.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc.py", - "google/cloud/billing/budgets_v1beta1/services/budget_service/transports/grpc_asyncio.py", - "google/cloud/billing/budgets_v1beta1/types/__init__.py", - "google/cloud/billing/budgets_v1beta1/types/budget_model.py", - "google/cloud/billing/budgets_v1beta1/types/budget_service.py", - "mypy.ini", - "noxfile.py", - "renovate.json", - "samples/AUTHORING_GUIDE.md", - "samples/CONTRIBUTING.md", - "scripts/decrypt-secrets.sh", - "scripts/fixup_budgets_v1_keywords.py", - "scripts/fixup_budgets_v1beta1_keywords.py", - "scripts/readme-gen/readme_gen.py", - "scripts/readme-gen/templates/README.tmpl.rst", - "scripts/readme-gen/templates/auth.tmpl.rst", - "scripts/readme-gen/templates/auth_api_key.tmpl.rst", - "scripts/readme-gen/templates/install_deps.tmpl.rst", - "scripts/readme-gen/templates/install_portaudio.tmpl.rst", - "setup.cfg", - "testing/.gitignore", - "tests/unit/gapic/budgets_v1/__init__.py", - "tests/unit/gapic/budgets_v1/test_budget_service.py", - "tests/unit/gapic/budgets_v1beta1/__init__.py", - "tests/unit/gapic/budgets_v1beta1/test_budget_service.py" ] } \ No newline at end of file diff --git a/packages/google-cloud-billing-budgets/synth.py b/packages/google-cloud-billing-budgets/synth.py index dc98aec5c7c6..adee98045be5 100644 --- a/packages/google-cloud-billing-budgets/synth.py +++ b/packages/google-cloud-billing-budgets/synth.py @@ -51,7 +51,7 @@ templated_files = common.py_library( samples=False, # set to True only if there are samples microgenerator=True, - cov_level=99, + cov_level=98, ) s.move(templated_files, excludes=[".coveragerc"]) # microgenerator has a good .coveragerc file diff --git a/packages/google-cloud-billing-budgets/testing/constraints-3.6.txt b/packages/google-cloud-billing-budgets/testing/constraints-3.6.txt index 08972fdc158a..88b0c2fec5fb 100644 --- a/packages/google-cloud-billing-budgets/testing/constraints-3.6.txt +++ b/packages/google-cloud-billing-budgets/testing/constraints-3.6.txt @@ -5,6 +5,6 @@ # # e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev", # Then this file should have foo==1.14.0 -google-api-core==1.22.0 +google-api-core==1.22.2 proto-plus==1.10.0 -libcst==0.2.5 \ No newline at end of file +libcst==0.2.5 diff --git a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/__init__.py b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/__init__.py index 8b137891791f..42ffdf2bc43d 100644 --- a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/__init__.py +++ b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/__init__.py @@ -1 +1,16 @@ +# -*- coding: utf-8 -*- +# 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. +# diff --git a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/test_budget_service.py b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/test_budget_service.py index 58bc53a403f6..525826425c46 100644 --- a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/test_budget_service.py +++ b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1/test_budget_service.py @@ -91,7 +91,24 @@ def test__get_default_mtls_endpoint(): @pytest.mark.parametrize( - "client_class", [BudgetServiceClient, BudgetServiceAsyncClient] + "client_class", [BudgetServiceClient, BudgetServiceAsyncClient,] +) +def test_budget_service_client_from_service_account_info(client_class): + creds = credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_info" + ) as factory: + factory.return_value = creds + info = {"valid": True} + client = client_class.from_service_account_info(info) + assert client.transport._credentials == creds + assert isinstance(client, client_class) + + assert client.transport._host == "billingbudgets.googleapis.com:443" + + +@pytest.mark.parametrize( + "client_class", [BudgetServiceClient, BudgetServiceAsyncClient,] ) def test_budget_service_client_from_service_account_file(client_class): creds = credentials.AnonymousCredentials() @@ -101,16 +118,21 @@ def test_budget_service_client_from_service_account_file(client_class): factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") assert client.transport._credentials == creds + assert isinstance(client, client_class) client = client_class.from_service_account_json("dummy/file/path.json") assert client.transport._credentials == creds + assert isinstance(client, client_class) assert client.transport._host == "billingbudgets.googleapis.com:443" def test_budget_service_client_get_transport_class(): transport = BudgetServiceClient.get_transport_class() - assert transport == transports.BudgetServiceGrpcTransport + available_transports = [ + transports.BudgetServiceGrpcTransport, + ] + assert transport in available_transports transport = BudgetServiceClient.get_transport_class("grpc") assert transport == transports.BudgetServiceGrpcTransport @@ -161,7 +183,7 @@ def test_budget_service_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -177,7 +199,7 @@ def test_budget_service_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -193,7 +215,7 @@ def test_budget_service_client_client_options( credentials_file=None, host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -221,7 +243,7 @@ def test_budget_service_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -272,29 +294,25 @@ def test_budget_service_client_mtls_env_auto( client_cert_source=client_cert_source_callback ) with mock.patch.object(transport_class, "__init__") as patched: - ssl_channel_creds = mock.Mock() - with mock.patch( - "grpc.ssl_channel_credentials", return_value=ssl_channel_creds - ): - patched.return_value = None - client = client_class(client_options=options) + patched.return_value = None + client = client_class(client_options=options) - if use_client_cert_env == "false": - expected_ssl_channel_creds = None - expected_host = client.DEFAULT_ENDPOINT - else: - expected_ssl_channel_creds = ssl_channel_creds - expected_host = client.DEFAULT_MTLS_ENDPOINT + if use_client_cert_env == "false": + expected_client_cert_source = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_client_cert_source = client_cert_source_callback + expected_host = client.DEFAULT_MTLS_ENDPOINT - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=expected_host, - scopes=None, - ssl_channel_credentials=expected_ssl_channel_creds, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + client_cert_source_for_mtls=expected_client_cert_source, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) # Check the case ADC client cert is provided. Whether client cert is used depends on # GOOGLE_API_USE_CLIENT_CERTIFICATE value. @@ -303,66 +321,53 @@ def test_budget_service_client_mtls_env_auto( ): with mock.patch.object(transport_class, "__init__") as patched: with mock.patch( - "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=True, ): with mock.patch( - "google.auth.transport.grpc.SslCredentials.is_mtls", - new_callable=mock.PropertyMock, - ) as is_mtls_mock: - with mock.patch( - "google.auth.transport.grpc.SslCredentials.ssl_credentials", - new_callable=mock.PropertyMock, - ) as ssl_credentials_mock: - if use_client_cert_env == "false": - is_mtls_mock.return_value = False - ssl_credentials_mock.return_value = None - expected_host = client.DEFAULT_ENDPOINT - expected_ssl_channel_creds = None - else: - is_mtls_mock.return_value = True - ssl_credentials_mock.return_value = mock.Mock() - expected_host = client.DEFAULT_MTLS_ENDPOINT - expected_ssl_channel_creds = ( - ssl_credentials_mock.return_value - ) - - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=expected_host, - scopes=None, - ssl_channel_credentials=expected_ssl_channel_creds, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + "google.auth.transport.mtls.default_client_cert_source", + return_value=client_cert_source_callback, + ): + if use_client_cert_env == "false": + expected_host = client.DEFAULT_ENDPOINT + expected_client_cert_source = None + else: + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_client_cert_source = client_cert_source_callback - # Check the case client_cert_source and ADC client cert are not provided. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} - ): - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.grpc.SslCredentials.__init__", return_value=None - ): - with mock.patch( - "google.auth.transport.grpc.SslCredentials.is_mtls", - new_callable=mock.PropertyMock, - ) as is_mtls_mock: - is_mtls_mock.return_value = False patched.return_value = None client = client_class() patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_ENDPOINT, + host=expected_host, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=False, + ): + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + @pytest.mark.parametrize( "client_class,transport_class,transport_name", @@ -388,7 +393,7 @@ def test_budget_service_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -418,7 +423,7 @@ def test_budget_service_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -437,7 +442,7 @@ def test_budget_service_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -484,6 +489,22 @@ def test_create_budget_from_dict(): test_create_budget(request_type=dict) +def test_create_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_budget), "__call__") as call: + client.create_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.CreateBudgetRequest() + + @pytest.mark.asyncio async def test_create_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.CreateBudgetRequest @@ -695,6 +716,22 @@ def test_update_budget_from_dict(): test_update_budget(request_type=dict) +def test_update_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_budget), "__call__") as call: + client.update_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.UpdateBudgetRequest() + + @pytest.mark.asyncio async def test_update_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.UpdateBudgetRequest @@ -908,6 +945,22 @@ def test_get_budget_from_dict(): test_get_budget(request_type=dict) +def test_get_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_budget), "__call__") as call: + client.get_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.GetBudgetRequest() + + @pytest.mark.asyncio async def test_get_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.GetBudgetRequest @@ -1103,6 +1156,22 @@ def test_list_budgets_from_dict(): test_list_budgets(request_type=dict) +def test_list_budgets_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_budgets), "__call__") as call: + client.list_budgets() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.ListBudgetsRequest() + + @pytest.mark.asyncio async def test_list_budgets_async( transport: str = "grpc_asyncio", request_type=budget_service.ListBudgetsRequest @@ -1429,6 +1498,22 @@ def test_delete_budget_from_dict(): test_delete_budget(request_type=dict) +def test_delete_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_budget), "__call__") as call: + client.delete_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.DeleteBudgetRequest() + + @pytest.mark.asyncio async def test_delete_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.DeleteBudgetRequest @@ -1749,6 +1834,54 @@ def test_budget_service_transport_auth_adc(): ) +@pytest.mark.parametrize( + "transport_class", + [ + transports.BudgetServiceGrpcTransport, + transports.BudgetServiceGrpcAsyncIOTransport, + ], +) +def test_budget_service_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/cloud-billing", + "https://www.googleapis.com/auth/cloud-platform", + ), + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + def test_budget_service_host_no_port(): client = BudgetServiceClient( credentials=credentials.AnonymousCredentials(), @@ -1770,7 +1903,7 @@ def test_budget_service_host_with_port(): def test_budget_service_grpc_transport_channel(): - channel = grpc.insecure_channel("http://localhost/") + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) # Check that channel is used if provided. transport = transports.BudgetServiceGrpcTransport( @@ -1782,7 +1915,7 @@ def test_budget_service_grpc_transport_channel(): def test_budget_service_grpc_asyncio_transport_channel(): - channel = aio.insecure_channel("http://localhost/") + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) # Check that channel is used if provided. transport = transports.BudgetServiceGrpcAsyncIOTransport( @@ -1793,6 +1926,8 @@ def test_budget_service_grpc_asyncio_transport_channel(): assert transport._ssl_channel_credentials == None +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( "transport_class", [ @@ -1805,7 +1940,7 @@ def test_budget_service_transport_channel_mtls_with_client_cert_source(transport "grpc.ssl_channel_credentials", autospec=True ) as grpc_ssl_channel_cred: with mock.patch.object( - transport_class, "create_channel", autospec=True + transport_class, "create_channel" ) as grpc_create_channel: mock_ssl_cred = mock.Mock() grpc_ssl_channel_cred.return_value = mock_ssl_cred @@ -1846,6 +1981,8 @@ def test_budget_service_transport_channel_mtls_with_client_cert_source(transport assert transport._ssl_channel_credentials == mock_ssl_cred +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( "transport_class", [ @@ -1861,7 +1998,7 @@ def test_budget_service_transport_channel_mtls_with_adc(transport_class): ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), ): with mock.patch.object( - transport_class, "create_channel", autospec=True + transport_class, "create_channel" ) as grpc_create_channel: mock_grpc_channel = mock.Mock() grpc_create_channel.return_value = mock_grpc_channel diff --git a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/__init__.py b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/__init__.py index 8b137891791f..42ffdf2bc43d 100644 --- a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/__init__.py +++ b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/__init__.py @@ -1 +1,16 @@ +# -*- coding: utf-8 -*- +# 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. +# diff --git a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/test_budget_service.py b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/test_budget_service.py index 42a2f9ab3cba..19f50cca6dfa 100644 --- a/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/test_budget_service.py +++ b/packages/google-cloud-billing-budgets/tests/unit/gapic/budgets_v1beta1/test_budget_service.py @@ -93,7 +93,24 @@ def test__get_default_mtls_endpoint(): @pytest.mark.parametrize( - "client_class", [BudgetServiceClient, BudgetServiceAsyncClient] + "client_class", [BudgetServiceClient, BudgetServiceAsyncClient,] +) +def test_budget_service_client_from_service_account_info(client_class): + creds = credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_info" + ) as factory: + factory.return_value = creds + info = {"valid": True} + client = client_class.from_service_account_info(info) + assert client.transport._credentials == creds + assert isinstance(client, client_class) + + assert client.transport._host == "billingbudgets.googleapis.com:443" + + +@pytest.mark.parametrize( + "client_class", [BudgetServiceClient, BudgetServiceAsyncClient,] ) def test_budget_service_client_from_service_account_file(client_class): creds = credentials.AnonymousCredentials() @@ -103,16 +120,21 @@ def test_budget_service_client_from_service_account_file(client_class): factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") assert client.transport._credentials == creds + assert isinstance(client, client_class) client = client_class.from_service_account_json("dummy/file/path.json") assert client.transport._credentials == creds + assert isinstance(client, client_class) assert client.transport._host == "billingbudgets.googleapis.com:443" def test_budget_service_client_get_transport_class(): transport = BudgetServiceClient.get_transport_class() - assert transport == transports.BudgetServiceGrpcTransport + available_transports = [ + transports.BudgetServiceGrpcTransport, + ] + assert transport in available_transports transport = BudgetServiceClient.get_transport_class("grpc") assert transport == transports.BudgetServiceGrpcTransport @@ -163,7 +185,7 @@ def test_budget_service_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -179,7 +201,7 @@ def test_budget_service_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -195,7 +217,7 @@ def test_budget_service_client_client_options( credentials_file=None, host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -223,7 +245,7 @@ def test_budget_service_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -274,29 +296,25 @@ def test_budget_service_client_mtls_env_auto( client_cert_source=client_cert_source_callback ) with mock.patch.object(transport_class, "__init__") as patched: - ssl_channel_creds = mock.Mock() - with mock.patch( - "grpc.ssl_channel_credentials", return_value=ssl_channel_creds - ): - patched.return_value = None - client = client_class(client_options=options) + patched.return_value = None + client = client_class(client_options=options) - if use_client_cert_env == "false": - expected_ssl_channel_creds = None - expected_host = client.DEFAULT_ENDPOINT - else: - expected_ssl_channel_creds = ssl_channel_creds - expected_host = client.DEFAULT_MTLS_ENDPOINT + if use_client_cert_env == "false": + expected_client_cert_source = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_client_cert_source = client_cert_source_callback + expected_host = client.DEFAULT_MTLS_ENDPOINT - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=expected_host, - scopes=None, - ssl_channel_credentials=expected_ssl_channel_creds, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + client_cert_source_for_mtls=expected_client_cert_source, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) # Check the case ADC client cert is provided. Whether client cert is used depends on # GOOGLE_API_USE_CLIENT_CERTIFICATE value. @@ -305,66 +323,53 @@ def test_budget_service_client_mtls_env_auto( ): with mock.patch.object(transport_class, "__init__") as patched: with mock.patch( - "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=True, ): with mock.patch( - "google.auth.transport.grpc.SslCredentials.is_mtls", - new_callable=mock.PropertyMock, - ) as is_mtls_mock: - with mock.patch( - "google.auth.transport.grpc.SslCredentials.ssl_credentials", - new_callable=mock.PropertyMock, - ) as ssl_credentials_mock: - if use_client_cert_env == "false": - is_mtls_mock.return_value = False - ssl_credentials_mock.return_value = None - expected_host = client.DEFAULT_ENDPOINT - expected_ssl_channel_creds = None - else: - is_mtls_mock.return_value = True - ssl_credentials_mock.return_value = mock.Mock() - expected_host = client.DEFAULT_MTLS_ENDPOINT - expected_ssl_channel_creds = ( - ssl_credentials_mock.return_value - ) - - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=expected_host, - scopes=None, - ssl_channel_credentials=expected_ssl_channel_creds, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + "google.auth.transport.mtls.default_client_cert_source", + return_value=client_cert_source_callback, + ): + if use_client_cert_env == "false": + expected_host = client.DEFAULT_ENDPOINT + expected_client_cert_source = None + else: + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_client_cert_source = client_cert_source_callback - # Check the case client_cert_source and ADC client cert are not provided. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} - ): - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.grpc.SslCredentials.__init__", return_value=None - ): - with mock.patch( - "google.auth.transport.grpc.SslCredentials.is_mtls", - new_callable=mock.PropertyMock, - ) as is_mtls_mock: - is_mtls_mock.return_value = False patched.return_value = None client = client_class() patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_ENDPOINT, + host=expected_host, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=False, + ): + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + client_cert_source_for_mtls=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + @pytest.mark.parametrize( "client_class,transport_class,transport_name", @@ -390,7 +395,7 @@ def test_budget_service_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -420,7 +425,7 @@ def test_budget_service_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -439,7 +444,7 @@ def test_budget_service_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - ssl_channel_credentials=None, + client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -486,6 +491,22 @@ def test_create_budget_from_dict(): test_create_budget(request_type=dict) +def test_create_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_budget), "__call__") as call: + client.create_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.CreateBudgetRequest() + + @pytest.mark.asyncio async def test_create_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.CreateBudgetRequest @@ -620,6 +641,22 @@ def test_update_budget_from_dict(): test_update_budget(request_type=dict) +def test_update_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.update_budget), "__call__") as call: + client.update_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.UpdateBudgetRequest() + + @pytest.mark.asyncio async def test_update_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.UpdateBudgetRequest @@ -754,6 +791,22 @@ def test_get_budget_from_dict(): test_get_budget(request_type=dict) +def test_get_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_budget), "__call__") as call: + client.get_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.GetBudgetRequest() + + @pytest.mark.asyncio async def test_get_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.GetBudgetRequest @@ -884,6 +937,22 @@ def test_list_budgets_from_dict(): test_list_budgets(request_type=dict) +def test_list_budgets_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_budgets), "__call__") as call: + client.list_budgets() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.ListBudgetsRequest() + + @pytest.mark.asyncio async def test_list_budgets_async( transport: str = "grpc_asyncio", request_type=budget_service.ListBudgetsRequest @@ -1143,6 +1212,22 @@ def test_delete_budget_from_dict(): test_delete_budget(request_type=dict) +def test_delete_budget_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = BudgetServiceClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_budget), "__call__") as call: + client.delete_budget() + call.assert_called() + _, args, _ = call.mock_calls[0] + + assert args[0] == budget_service.DeleteBudgetRequest() + + @pytest.mark.asyncio async def test_delete_budget_async( transport: str = "grpc_asyncio", request_type=budget_service.DeleteBudgetRequest @@ -1398,6 +1483,54 @@ def test_budget_service_transport_auth_adc(): ) +@pytest.mark.parametrize( + "transport_class", + [ + transports.BudgetServiceGrpcTransport, + transports.BudgetServiceGrpcAsyncIOTransport, + ], +) +def test_budget_service_grpc_transport_client_cert_source_for_mtls(transport_class): + cred = credentials.AnonymousCredentials() + + # Check ssl_channel_credentials is used if provided. + with mock.patch.object(transport_class, "create_channel") as mock_create_channel: + mock_ssl_channel_creds = mock.Mock() + transport_class( + host="squid.clam.whelk", + credentials=cred, + ssl_channel_credentials=mock_ssl_channel_creds, + ) + mock_create_channel.assert_called_once_with( + "squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/cloud-billing", + "https://www.googleapis.com/auth/cloud-platform", + ), + ssl_credentials=mock_ssl_channel_creds, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls + # is used. + with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): + with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: + transport_class( + credentials=cred, + client_cert_source_for_mtls=client_cert_source_callback, + ) + expected_cert, expected_key = client_cert_source_callback() + mock_ssl_cred.assert_called_once_with( + certificate_chain=expected_cert, private_key=expected_key + ) + + def test_budget_service_host_no_port(): client = BudgetServiceClient( credentials=credentials.AnonymousCredentials(), @@ -1419,7 +1552,7 @@ def test_budget_service_host_with_port(): def test_budget_service_grpc_transport_channel(): - channel = grpc.insecure_channel("http://localhost/") + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) # Check that channel is used if provided. transport = transports.BudgetServiceGrpcTransport( @@ -1431,7 +1564,7 @@ def test_budget_service_grpc_transport_channel(): def test_budget_service_grpc_asyncio_transport_channel(): - channel = aio.insecure_channel("http://localhost/") + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) # Check that channel is used if provided. transport = transports.BudgetServiceGrpcAsyncIOTransport( @@ -1442,6 +1575,8 @@ def test_budget_service_grpc_asyncio_transport_channel(): assert transport._ssl_channel_credentials == None +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( "transport_class", [ @@ -1454,7 +1589,7 @@ def test_budget_service_transport_channel_mtls_with_client_cert_source(transport "grpc.ssl_channel_credentials", autospec=True ) as grpc_ssl_channel_cred: with mock.patch.object( - transport_class, "create_channel", autospec=True + transport_class, "create_channel" ) as grpc_create_channel: mock_ssl_cred = mock.Mock() grpc_ssl_channel_cred.return_value = mock_ssl_cred @@ -1495,6 +1630,8 @@ def test_budget_service_transport_channel_mtls_with_client_cert_source(transport assert transport._ssl_channel_credentials == mock_ssl_cred +# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are +# removed from grpc/grpc_asyncio transport constructor. @pytest.mark.parametrize( "transport_class", [ @@ -1510,7 +1647,7 @@ def test_budget_service_transport_channel_mtls_with_adc(transport_class): ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), ): with mock.patch.object( - transport_class, "create_channel", autospec=True + transport_class, "create_channel" ) as grpc_create_channel: mock_grpc_channel = mock.Mock() grpc_create_channel.return_value = mock_grpc_channel