Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Conversation

@changsongd
Copy link
Contributor

Add code snippets for sync and async APIs.

The region tags have been created: https://devrel.corp.google.com/snippets/region-tags?region_tag=%25optimization%25

@changsongd changsongd requested a review from a team as a code owner March 28, 2022 21:40
@product-auto-label product-auto-label bot added api: cloudoptimization Issues related to the googleapis/python-optimization API. samples Issues that are directly related to samples. labels Mar 28, 2022
@snippet-bot
Copy link

snippet-bot bot commented Mar 28, 2022

Here is the summary of possible violations 😱

There are 2 possible violations for not having product prefix.

The end of the violation section. All the stuff below is FYI purposes only.


Here is the summary of changes.

You are about to add 2 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@kweinmeister kweinmeister changed the title Add code samples docs: add code samples Mar 28, 2022
@kweinmeister kweinmeister added the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 28, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 28, 2022
@kweinmeister kweinmeister added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 28, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 28, 2022
Copy link
Contributor

@busunkim96 busunkim96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @changsongd! I took a first pass through this PR.

The test environment has a project. You can fetch the id through the google-auth library:

import google.auth

credentials, project_id = google.auth.default()

I'll open a CL to enable samples CI on this PR as well. If you haven't already, please review https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md.


with open(request_file_name, 'r') as f:
# The request must include the "parent" field with the value set to
# "projects/{YOUR_GCP_PROJECT_ID}".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid commented out variables, as they're prone to breaking without us noticing. Could you make project_id an argument to the function instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project_id is best to be set in resources/sync_request.json. I can make it an argument and let the function inject it into the request, but it might be too cumbersome for our users in real life use case. I can avoid explicitly calling out the variables in the comment. For example, I can change the comment to

Please replace the variables in "resources/sync_request.json" before calling the API.

Please let me know if that sounds good.

@changsongd changsongd requested a review from a team as a code owner March 28, 2022 23:24
@busunkim96 busunkim96 added kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Mar 28, 2022
@busunkim96 busunkim96 requested review from a team and engelke and removed request for a team March 28, 2022 23:31
@busunkim96 busunkim96 added kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Mar 28, 2022
@changsongd
Copy link
Contributor Author

Why is the snippet-bot complaining about the region tag prefix?

samples/snippets/async_api.py:15, tag cloudoptimization_async_api
samples/snippets/sync_api.py:15, tag cloudoptimization_sync_api

cloudoptimization_ is the correct prefix according to this page, no?

@dandhlee dandhlee added the snippet-bot:force-run Force snippet-bot runs its logic label Mar 29, 2022
@snippet-bot snippet-bot bot removed the snippet-bot:force-run Force snippet-bot runs its logic label Mar 29, 2022
@busunkim96 busunkim96 removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 29, 2022
@busunkim96 busunkim96 added kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Mar 29, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 29, 2022
@changsongd
Copy link
Contributor Author

Hi @busunkim96 , I'm getting a permission denied error from sample checks:

Failed: google.api_core.exceptions.PermissionDenied: 403 Cloud Optimization API has not been used in project 1012616486416 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudoptimization.googleapis.com/overview?project=1012616486416 then retry.

I don't have access to the project and can't enable the API. Could you grant me the access or help me enable the API?

@changsongd changsongd requested a review from busunkim96 March 30, 2022 00:11
@busunkim96 busunkim96 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 30, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 30, 2022
@busunkim96
Copy link
Contributor

@changsongd I've enabled the API. Could you let me know what IAM permissions I should grant to the service account so it can run the samples?

@busunkim96 busunkim96 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 30, 2022
@changsongd
Copy link
Contributor Author

@changsongd I've enabled the API. Could you let me know what IAM permissions I should grant to the service account so it can run the samples?

Hi, you might need to add the role of Cloud Optimization AI Editor.

@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 30, 2022
@busunkim96
Copy link
Contributor

@changsongd Hmm I can't find that role in the dropdown (or in https://cloud.google.com/iam/docs/permissions-reference), are the roles fully rolled out?

The tests look like they're failing now because the requests include the unresolved env var names:
https://source.cloud.google.com/results/invocations/46ba0a65-f637-4457-86e8-90880fae1a43/targets/github%2Fpython-optimization%2Fsamples%2Fsnippets/tests;group=samples.snippets.sync_api_test;test=test_call_sync_api;row=3

args = (parent: "projects/${YOUR_GCP_PROJECT_ID}"
model {
  shipments {
    pickups {
      arrival_location {
        latitu...ngitude: 2.341215
    }
    load_limits {
      key: "weight"
      value {
        max_load: 60
      }
    }
  }
}
,)
kwargs = {'metadata': [('x-goog-request-params', 'parent=projects/%24%7BYOUR_GCP_PROJECT_ID%7D'), ('x-goog-api-client', 'gl-python/3.7.12 grpc/1.44.0 gax/2.7.1 gapic/0.1.0')], 'timeout': 100}

    @functools.wraps(callable_)
    def error_remapped_callable(*args, **kwargs):
        try:
            return callable_(*args, **kwargs)
        except grpc.RpcError as exc:
>           raise exceptions.from_grpc_error(exc) from exc
E           google.api_core.exceptions.InvalidArgument: 400 Invalid request field: parent.

.nox/py-3-7/lib/python3.7/site-packages/google/api_core/grpc_helpers.py:68: InvalidArgument

@changsongd
Copy link
Contributor Author

@dizcology suggested using the generated code samples in the documentation. I will close this PR. Thank you @busunkim96 for all your efforts and helps!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: cloudoptimization Issues related to the googleapis/python-optimization API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants