Skip to content

Commit 4536af5

Browse files
chore(compute): migrate region tags step 1 in metadata (watch maintenance notices) (GoogleCloudPlatform#13072)
* chore(compute): migrate regions step 1 - add regions to metadata/main.py * chore(compute): remove whitespace in metadata/vm_identity.py * chore(compute): fix code style to comply with PEP-8 and code-review-assist
1 parent fd7cc7e commit 4536af5

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

compute/metadata/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
For more information, see the README.md under /compute.
2020
"""
2121

22+
# [START compute_metadata_watch_maintenance_notices]
2223
# [START all]
23-
2424
import time
2525
from typing import Callable, NoReturn, Optional
2626

@@ -32,8 +32,7 @@
3232

3333

3434
def wait_for_maintenance(callback: Callable[[Optional[str]], None]) -> NoReturn:
35-
"""
36-
Start an infinite loop waiting for maintenance signal.
35+
"""Start an infinite loop waiting for maintenance signal.
3736
3837
Args:
3938
callback: Function to be called when a maintenance is scheduled.
@@ -44,6 +43,7 @@ def wait_for_maintenance(callback: Callable[[Optional[str]], None]) -> NoReturn:
4443
url = METADATA_URL + "instance/maintenance-event"
4544
last_maintenance_event = None
4645
# [START hanging_get]
46+
# [START compute_metadata_hanging_get_etag]
4747
last_etag = "0"
4848

4949
while True:
@@ -62,6 +62,7 @@ def wait_for_maintenance(callback: Callable[[Optional[str]], None]) -> NoReturn:
6262

6363
last_etag = r.headers["etag"]
6464
# [END hanging_get]
65+
# [END compute_metadata_hanging_get_etag]
6566

6667
if r.text == "NONE":
6768
maintenance_event = None
@@ -74,8 +75,7 @@ def wait_for_maintenance(callback: Callable[[Optional[str]], None]) -> NoReturn:
7475

7576

7677
def maintenance_callback(event: Optional[str]) -> None:
77-
"""
78-
Example callback function to handle the maintenance event.
78+
"""Example callback function to handle the maintenance event.
7979
8080
Args:
8181
event: details about scheduled maintenance.
@@ -93,3 +93,4 @@ def main():
9393
if __name__ == "__main__":
9494
main()
9595
# [END all]
96+
# [END compute_metadata_watch_maintenance_notices]

compute/metadata/vm_identity.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""
16-
Example of verifying Google Compute Engine virtual machine identity.
15+
"""Example of verifying Google Compute Engine virtual machine identity.
1716
1817
This sample will work only on a GCE virtual machine, as it relies on
19-
communication with metadata server (https://cloud.google.com/compute/docs/storing-retrieving-metadata).
18+
communication with metadata server
19+
(https://cloud.google.com/compute/docs/storing-retrieving-metadata).
2020
21-
Example is used on: https://cloud.google.com/compute/docs/instances/verifying-instance-identity
21+
Example is used on:
22+
https://cloud.google.com/compute/docs/instances/verifying-instance-identity
2223
"""
2324
import pprint
2425

2526
# [START compute_vm_identity_verify_token]
2627
import google.auth.transport.requests
2728
from google.oauth2 import id_token
28-
2929
# [END compute_vm_identity_verify_token]
3030

3131
# [START compute_vm_identity_acquire_token]
@@ -78,17 +78,12 @@ def acquire_token(
7878
# Extract and return the token from the response.
7979
r.raise_for_status()
8080
return r.text
81-
82-
8381
# [END compute_vm_identity_acquire_token]
8482

8583

8684
# [START compute_vm_identity_verify_token]
87-
88-
8985
def verify_token(token: str, audience: str) -> dict:
90-
"""
91-
Verify token signature and return the token payload.
86+
"""Verify token signature and return the token payload.
9287
9388
Args:
9489
token: the JSON Web Token received from the metadata server to
@@ -102,8 +97,6 @@ def verify_token(token: str, audience: str) -> dict:
10297
request = google.auth.transport.requests.Request()
10398
payload = id_token.verify_token(token, request=request, audience=audience)
10499
return payload
105-
106-
107100
# [END compute_vm_identity_verify_token]
108101

109102

compute/metadata/vm_identity_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import vm_identity
2121

22+
2223
AUDIENCE = "http://www.testing.com"
2324

2425

0 commit comments

Comments
 (0)