Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

feat: Adding code samples and tests for them #55

Merged
merged 18 commits into from Jun 6, 2021
Merged

Conversation

m-strzelczyk
Copy link
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Hi, I tried uploading those samples to the python-docs-samples repo, but I was told that it'd be better to put them here. So here I am, with the first batch of code samples for our documentation :)

@m-strzelczyk m-strzelczyk requested review from a team as code owners May 19, 2021 14:24
@product-auto-label product-auto-label bot added the api: compute Issues related to the googleapis/python-compute API. label May 19, 2021
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label May 19, 2021
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label May 19, 2021
@snippet-bot
Copy link

snippet-bot bot commented May 19, 2021

Here is the summary of changes.

You are about to add 10 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

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.

I've opened cl/374662458 to enable samples CI.

.github/CODEOWNERS Outdated Show resolved Hide resolved
samples/snippets/quickstart.py Outdated Show resolved Hide resolved
samples/snippets/quickstart.py Outdated Show resolved Hide resolved
samples/snippets/requirements-test.txt Outdated Show resolved Hide resolved
samples/snippets/requirements-test.txt Outdated Show resolved Hide resolved
samples/snippets/quickstart.py Outdated Show resolved Hide resolved
print(f"Creating the {machine_name} instance in {zone}...")
operation = instance_client.insert(request=request)
# wait_result = operation_client.wait(operation=operation.name, zone=zone, project=project)
operation = wait_for_operation(operation, project)
Copy link
Contributor

Choose a reason for hiding this comment

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

The LRO experience looks different from existing GAPICs. @vam-google Does the Compute API have a non-standard LRO representation? It doesn't look like the generator produced the usual google.api_core.Operation type.

For comparison, see videointelligence where you can do operation.result(timeout=90) to poll for the result.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how I saw the team use the operations in their test code. It is an instance of google.cloud.compute_v1.types.Operation and not google.api_core.Operation.

# [START compute_instances_operation_check]
import typing

import google.cloud.compute_v1 as gce
Copy link
Contributor

Choose a reason for hiding this comment

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

Style suggestion for consistency with existing python samples.

Suggested change
import google.cloud.compute_v1 as gce
from google.cloud import compute_v1

samples/snippets/quickstart.py Outdated Show resolved Hide resolved
samples/snippets/test_quickstart.py Show resolved Hide resolved
m-strzelczyk and others added 2 commits May 20, 2021 13:23
Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
@m-strzelczyk
Copy link
Contributor Author

I think there's something wrong with Kokoro -samples tests. They're trying to use samples/snippets/noxfile.py which doesn't exist. The tests can be run from the root directory with nox -s samples.

@m-strzelczyk
Copy link
Contributor Author

@busunkim96 I read the configs in .kokoro/samples and I think I could create a noxfile.py to match what they expect to find. However, I think it's a nicer solution to have one noxfile.py script in the root directory to handle all the testing.

Please let me know how should I proceed. Should I create noxfile.py in the snippets directory or modify the scripts in .kokoro/samples to match what I did now with the main noxfile.py.

@m-strzelczyk
Copy link
Contributor Author

@busunkim96 I have adjusted my tests to work with the automated kokoro scripts :)

@m-strzelczyk
Copy link
Contributor Author

@dinagraves Can you review this PR?

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.

The LRO handling still looks a little odd to me - I'll poke at the existing tests to see if there's a nicer option.

Otherwise this looks very nice!


print(f"Creating the {instance_name} instance in {zone}...")
operation = instance_client.insert(request=request)
# wait_result = operation_client.wait(operation=operation.name, zone=zone, project=project)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a stray comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Changed that part already.

Comment on lines 192 to 196
if operation.error:
pass
if operation.warnings:
pass
print(f"Instance {machine_name} deleted.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the sample show error handling? (Even if it is just printing out the error/warnings?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@dinagraves dinagraves left a comment

Choose a reason for hiding this comment

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

Generally LGTM. A few suggestions.

1. If you haven't already, set up a Python Development Environment by following the [python setup guide](https://cloud.google.com/python/setup) and
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).

1. Create a service account with the 'Editor' permissions by following these
Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend using gcloud auth application-default login instead. It's more secure if you don't have to download a JSON key. It's also easier. You can skip the next 3 steps with this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. Good point. Done.

"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
)
else:
test_instance_name = "i" + uuid.uuid4().hex[:10]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this called test_instance_name?

Copy link
Contributor

Choose a reason for hiding this comment

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

Other note, can you concatenate something like "quickstart" to the instance name, so it's easier to identify? I ran this on a current project and got a little confused and worried before I recognized which one was the quickstart.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to just instance_name and made the name start with quickstart-

dinagraves and others added 3 commits June 2, 2021 15:25
* README suggests using `gcloud auth application-default login` which is safer than Service Account key.
* The name of created instance now starts with "quickstart-".
* Changed one variable name.
@m-strzelczyk m-strzelczyk merged commit 14cd352 into master Jun 6, 2021
@m-strzelczyk m-strzelczyk deleted the samples branch June 6, 2021 13:07
@m-strzelczyk m-strzelczyk mentioned this pull request Jul 20, 2021
1 task
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: compute Issues related to the googleapis/python-compute API. cla: yes This human has signed the Contributor License Agreement. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants