From 6fd3c4c183033471709b9bba69ba2016fe2de7e7 Mon Sep 17 00:00:00 2001 From: changsongd <101151583+changsongd@users.noreply.github.com> Date: Fri, 8 Apr 2022 13:54:21 -0700 Subject: [PATCH] docs: add get_operation code snippets (#12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add get_operation code snippets * update comment * docs: add sync api samples with json request (#13) * add code snippets for sync and async api * remove async test samples * use f-string * change project_id to input arg * add noxfile * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * rename noxfile and add requirements * rm noxfile * rm noxfile local * add root noxfile * Update noxfile.py * Update noxfile.py * Update noxfile.py Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> * Apply suggestions from code review * Update noxfile.py * Update noxfile.py * Update noxfile.py * Update noxfile.py * Update noxfile.py * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add commit to trigger kokoro * add indentation * add type annotations Co-authored-by: Jeffrey Rennie Co-authored-by: Owl Bot Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Anthonios Partheniou * rebase and add type annotations * fix operation_id type * Update samples/snippets/get_operation.py Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> * Update samples/snippets/get_operation_test.py Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> * move TODO outside func, create operation in test * lint fix * fix asyncmodelconfig * change model_config to list * add blank line * change request to dict * change parent to project id * remove model config * Update samples/snippets/get_operation.py Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> * add TODO back Co-authored-by: Jeffrey Rennie Co-authored-by: Owl Bot Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Anthonios Partheniou Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> --- .../samples/snippets/get_operation.py | 32 +++++++++++++++ .../samples/snippets/get_operation_test.py | 39 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 packages/google-cloud-optimization/samples/snippets/get_operation.py create mode 100644 packages/google-cloud-optimization/samples/snippets/get_operation_test.py diff --git a/packages/google-cloud-optimization/samples/snippets/get_operation.py b/packages/google-cloud-optimization/samples/snippets/get_operation.py new file mode 100644 index 000000000000..8189f69ab700 --- /dev/null +++ b/packages/google-cloud-optimization/samples/snippets/get_operation.py @@ -0,0 +1,32 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# 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. + +# [START cloudoptimization_get_operation] +from google.cloud import optimization_v1 + + +def get_operation(operation_full_id: str) -> None: + """Get operation details and status.""" + # TODO(developer): Uncomment and set the following variables + # operation_full_id = \ + # "projects/[projectId]/locations/operations/[operationId]" + + client = optimization_v1.FleetRoutingClient() + # Get the latest state of a long-running operation. + response = client.transport.operations_client.get_operation(operation_full_id) + + print("Name: {}".format(response.name)) + print("Operation details:") + print(response) +# [END cloudoptimization_get_operation] diff --git a/packages/google-cloud-optimization/samples/snippets/get_operation_test.py b/packages/google-cloud-optimization/samples/snippets/get_operation_test.py new file mode 100644 index 000000000000..378c99f0391f --- /dev/null +++ b/packages/google-cloud-optimization/samples/snippets/get_operation_test.py @@ -0,0 +1,39 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import google.auth +from google.cloud import optimization_v1 +import pytest + +import get_operation + + +@pytest.fixture(scope="function") +def operation_id() -> str: + fleet_routing_client = optimization_v1.FleetRoutingClient() + + _, project_id = google.auth.default() + fleet_routing_request = {"parent": f"projects/{project_id}"} + + # Make the request + operation = fleet_routing_client.batch_optimize_tours(fleet_routing_request) + + yield operation.operation.name + + +def test_get_operation_status(capsys: pytest.LogCaptureFixture, operation_id: str) -> None: + get_operation.get_operation(operation_id) + out, _ = capsys.readouterr() + assert "Operation details" in out