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

Commit 7db9a9d

Browse files
m-strzelczykrsamborskivchudnov-g
authored
docs(README): Improving readme with some demos and more info (#394)
Co-authored-by: Remigiusz Samborski <[email protected]> Co-authored-by: Victor Chudnovsky <[email protected]>
1 parent 0ad0f13 commit 7db9a9d

File tree

1 file changed

+100
-7
lines changed

1 file changed

+100
-7
lines changed

README.rst

+100-7
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,116 @@ Windows
6969
<your-env>\Scripts\pip.exe install google-cloud-compute
7070
7171
72+
Authentication and Authorization
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
This client library supports authentication via `Google Application Default Credentials`_
75+
, or by providing a JSON key file for a Service Account. See examples below.
76+
77+
`Google Application Default Credentials`_ (ADC) is the recommended way to authorize and authenticate
78+
clients. Here is an example of a client using ADC to authenticate:
79+
80+
.. code-block:: python
81+
82+
from google.cloud import compute_v1
83+
84+
networks_client = compute_v1.NetworksClient()
85+
for network in networks_client.list(project='YOUR_PROJECT'):
86+
print(network)
87+
88+
89+
You can use a file with credentials to authenticate and authorize, such as a JSON key
90+
file associated with a Google service account. You can create service account keys and
91+
download them using the Google Cloud Console. For more information, see
92+
`Creating and managing Service Account keys`_.
93+
94+
The library used to create the credentials objects is ``google-auth``. This example uses
95+
the Networks Client, but the same steps apply to the other clients in this package.
96+
Example:
97+
98+
.. code-block:: python
99+
100+
from google.oauth2 import service_account
101+
from google.cloud import compute_v1
102+
103+
credentials = service_account.Credentials.from_service_account_file(
104+
'/path/to/key.json')
105+
106+
networks_client = compute_v1.NetworksClient(credentials=credentials)
107+
for network in networks_client.list(project='YOUR_PROJECT'):
108+
print(network)
109+
110+
111+
When you don't want to store secrets on disk, you can create credentials
112+
from in-memory JSON and use the ``from_service_account_info`` method. You can also limit the use of
113+
your credentials only to specified scopes. For more information about OAuth 2.0 scopes for Google APIs,
114+
see `Scopes documentation page`_. Example:
115+
116+
.. code-block:: python
117+
118+
import json
119+
120+
from google.oauth2 import service_account
121+
from google.cloud import compute_v1
122+
123+
json_acct_info = json.loads(function_to_get_json_creds())
124+
credentials = service_account.Credentials.from_service_account_info(
125+
json_acct_info)
126+
127+
scoped_credentials = credentials.with_scopes(
128+
['https://www.googleapis.com/auth/cloud-platform'])
129+
130+
networks_client = compute_v1.NetworksClient(credentials=scoped_credentials)
131+
for network in networks_client.list(project='YOUR_PROJECT'):
132+
print(network)
133+
134+
.. _Google Application Default Credentials: https://cloud.google.com/docs/authentication/production
135+
.. _Creating and managing Service Account keys: https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating
136+
.. _Scopes documentation page: https://developers.google.com/identity/protocols/oauth2/scopes
137+
138+
Long Running Operations
139+
~~~~~~~~~~~~~~~~~~~~~~~
140+
Long-Running Operations (LROs), like the many ``insert()`` operations, can be handled using
141+
the ``ExtendedOperation`` object that is returned when the LRO is started.
142+
143+
You can wait for the completion of an operation using its ``result()`` method. This method accepts
144+
a ``timeout`` argument, specifying how long you want your process to wait for completion of the
145+
operation (in seconds). When the call to ``result()`` times out, the operation is not automatically
146+
cancelled. At any time, you can check whether the operation is complete by using its ``done()`` method.
147+
148+
A sample method to handle LROs featuring error and warning reporting can be found in the Python
149+
code samples repository: `GoogleCloudPlatform/python-docs-samples`_
150+
151+
.. _GoogleCloudPlatform/python-docs-samples: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/compute/client_library/snippets/operations/wait_for_extended_operation.py
152+
153+
154+
Code Samples
155+
~~~~~~~~~~~~
156+
You can find useful code samples that will demonstrate the usage of this library on `the
157+
Google Cloud samples page`_.
158+
159+
.. _the Google Cloud samples page: https://cloud.google.com/docs/samples?language=python&product=computeengine
160+
161+
162+
72163
PyCharm/JetBrains IDEs
73164
~~~~~~~~~~~~~~~~~~~~~~
74-
Since the library has grown in size, the files it consists of have outgrown the [default size limit of ~2.5Mb](https://www.jetbrains.com/help/pycharm/file-idea-properties.html).
75-
As a result, the code completion in JetBrains products can fail to work with the classes from our library. To
76-
fix this, you need to update the `idea.max.intellisense.filesize` setting in custom properties
77-
(Help -> Edit custom properties...). Just add a line like `idea.max.intellisense.filesize = 10000` to change this
165+
This library has now grown in size past the `JetBrains default size limit of ~2.5Mb`_.
166+
As a result, code completion in JetBrains products can fail to work with the classes from our library. To
167+
fix this, you need to update the ``idea.max.intellisense.filesize`` setting in custom properties
168+
(Help -> Edit custom properties...). Just add the line ``idea.max.intellisense.filesize = 10000`` to change this
78169
limit to ~10Mb.
79170

171+
.. _JetBrains default size limit of ~2.5Mb: https://www.jetbrains.com/help/pycharm/file-idea-properties.html
172+
80173
Next Steps
81174
~~~~~~~~~~
82175

83176
- Read the `Client Library Documentation`_ for Compute Engine API
84-
API to see other available methods on the client.
177+
to see other available methods on the client.
85178
- Read the `Compute Engine API Product documentation`_ to learn
86179
more about the product and see How-to Guides.
87180
- View this `README`_ to see the full list of Cloud
88181
APIs that we cover.
89182

90-
.. _Compute Engine API Product documentation: https://cloud.google.com/compute/
91-
.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst
183+
.. _Compute Engine API Product documentation: https://cloud.google.com/compute/docs/api/libraries
184+
.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst

0 commit comments

Comments
 (0)