From 604d55c5a54c98fd30b5e2e465d28fce4cc7f708 Mon Sep 17 00:00:00 2001 From: Anayeli Malvaez Colin Date: Wed, 8 Jul 2026 21:28:12 +0000 Subject: [PATCH] chore(compute): clean up unused code migrated to client_library --- compute/api/README.md | 21 --- compute/api/create_instance.py | 265 ---------------------------- compute/api/create_instance_test.py | 35 ---- compute/api/requirements-test.txt | 2 - compute/api/requirements.txt | 3 - compute/api/startup-script.sh | 37 ---- 6 files changed, 363 deletions(-) delete mode 100644 compute/api/README.md delete mode 100644 compute/api/create_instance.py delete mode 100644 compute/api/create_instance_test.py delete mode 100644 compute/api/requirements-test.txt delete mode 100644 compute/api/requirements.txt delete mode 100644 compute/api/startup-script.sh diff --git a/compute/api/README.md b/compute/api/README.md deleted file mode 100644 index 757a4a2a79a..00000000000 --- a/compute/api/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# Compute Engine API Samples - -[![Open in Cloud Shell][shell_img]][shell_link] - -[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png -[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=compute/api/README.md - - -These samples are used on the following documentation pages: - -> -* https://cloud.google.com/compute/docs/tutorials/python-guide -* https://cloud.google.com/compute/docs/instances/create-start-instance -* https://cloud.google.com/compute/docs/api/how-tos/api-requests-responses - - - - - - - diff --git a/compute/api/create_instance.py b/compute/api/create_instance.py deleted file mode 100644 index 9542ef7fd3a..00000000000 --- a/compute/api/create_instance.py +++ /dev/null @@ -1,265 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2015 Google Inc. -# -# 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. - -"""Example of using the Compute Engine API to create and delete instances. - -Creates a new compute engine instance and uses it to apply a caption to -an image. - - https://cloud.google.com/compute/docs/tutorials/python-guide - -For more information, see the README.md under /compute. -""" - -import argparse -import os -import time - -import googleapiclient.discovery - - -def list_instances( - compute: object, - project: str, - zone: str, -) -> list: - """Lists all instances in the specified zone. - - Args: - compute: an initialized compute service object. - project: the Google Cloud project ID. - zone: the name of the zone in which the instances should be listed. - - Returns: - A list of instances. - """ - result = compute.instances().list(project=project, zone=zone).execute() - return result["items"] if "items" in result else None - - -# [START compute_create_instance] -def create_instance( - compute: object, - project: str, - zone: str, - name: str, - bucket: str, -) -> str: - """Creates an instance in the specified zone. - - Args: - compute: an initialized compute service object. - project: the Google Cloud project ID. - zone: the name of the zone in which the instances should be created. - name: the name of the instance. - bucket: the name of the bucket in which the image should be written. - - Returns: - The instance object. - """ - # Get the latest Debian Jessie image. - image_response = ( - compute.images() - .getFromFamily(project="debian-cloud", family="debian-11") - .execute() - ) - source_disk_image = image_response["selfLink"] - - # Configure the machine - machine_type = "zones/%s/machineTypes/n1-standard-1" % zone - startup_script = open( - os.path.join(os.path.dirname(__file__), "startup-script.sh") - ).read() - image_url = "http://storage.googleapis.com/gce-demo-input/photo.jpg" - image_caption = "Ready for dessert?" - - config = { - "name": name, - "machineType": machine_type, - # Specify the boot disk and the image to use as a source. - "disks": [ - { - "boot": True, - "autoDelete": True, - "initializeParams": { - "sourceImage": source_disk_image, - }, - } - ], - # Specify a network interface with NAT to access the public - # internet. - "networkInterfaces": [ - { - "network": "global/networks/default", - "accessConfigs": [{"type": "ONE_TO_ONE_NAT", "name": "External NAT"}], - } - ], - # Allow the instance to access cloud storage and logging. - "serviceAccounts": [ - { - "email": "default", - "scopes": [ - "https://www.googleapis.com/auth/devstorage.read_write", - "https://www.googleapis.com/auth/logging.write", - ], - } - ], - # Metadata is readable from the instance and allows you to - # pass configuration from deployment scripts to instances. - "metadata": { - "items": [ - { - # Startup script is automatically executed by the - # instance upon startup. - "key": "startup-script", - "value": startup_script, - }, - {"key": "url", "value": image_url}, - {"key": "text", "value": image_caption}, - {"key": "bucket", "value": bucket}, - ] - }, - } - - return compute.instances().insert(project=project, zone=zone, body=config).execute() - - -# [END compute_create_instance] - - -def delete_instance( - compute: object, - project: str, - zone: str, - name: str, -) -> str: - """Deletes an instance. - - Args: - compute: An initialized compute service object. - project: The Google Cloud project ID. - zone: The name of the zone in which the instances should be deleted. - name: The name of the instance. - - Returns: - Execute to delete the instance object. - """ - return ( - compute.instances().delete(project=project, zone=zone, instance=name).execute() - ) - - -# [START compute_wait_for_operation] -def wait_for_operation( - compute: object, - project: str, - zone: str, - operation: str, -) -> dict: - """Waits for the given operation to complete. - - Args: - compute: an initialized compute service object. - project: the Google Cloud project ID. - zone: the name of the zone in which the operation should be executed. - operation: the operation ID. - - Returns: - The result of the operation. - """ - print("Waiting for operation to finish...") - while True: - result = ( - compute.zoneOperations() - .get(project=project, zone=zone, operation=operation) - .execute() - ) - - if result["status"] == "DONE": - print("done.") - if "error" in result: - raise Exception(result["error"]) - return result - - time.sleep(1) - - -# [END compute_wait_for_operation] - - -def main( - project: str, - bucket: str, - zone: str, - instance_name: str, - wait=True, -) -> None: - """Runs the demo. - - Args: - project: the Google Cloud project ID. - bucket: the name of the bucket in which the image should be written. - instance_name: the name of the instance. - wait: whether to wait for the operation to complete. - - Returns: - None. - """ - compute = googleapiclient.discovery.build("compute", "v1") - - print("Creating instance.") - - operation = create_instance(compute, project, zone, instance_name, bucket) - wait_for_operation(compute, project, zone, operation["name"]) - - instances = list_instances(compute, project, zone) - - print(f"Instances in project {project} and zone {zone}:") - for instance in instances: - print(f' - {instance["name"]}') - - print( - f""" -Instance created. -It will take a minute or two for the instance to complete work. -Check this URL: http://storage.googleapis.com/{bucket}/output.png -Once the image is uploaded press enter to delete the instance. -""" - ) - - if wait: - input() - - print("Deleting instance.") - - operation = delete_instance(compute, project, zone, instance_name) - wait_for_operation(compute, project, zone, operation["name"]) - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - parser.add_argument("project_id", help="Your Google Cloud project ID.") - parser.add_argument("bucket_name", help="Your Google Cloud Storage bucket name.") - parser.add_argument( - "--zone", default="us-central1-f", help="Compute Engine zone to deploy to." - ) - parser.add_argument("--name", default="demo-instance", help="New instance name.") - - args = parser.parse_args() - - main(args.project_id, args.bucket_name, args.zone, args.name) diff --git a/compute/api/create_instance_test.py b/compute/api/create_instance_test.py deleted file mode 100644 index 9c24f860656..00000000000 --- a/compute/api/create_instance_test.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2015 Google Inc. -# 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 os -import uuid - -import pytest - -from create_instance import main - -PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] -BUCKET = os.environ["CLOUD_STORAGE_BUCKET"] - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_main(capsys): - instance_name = f"test-instance-{uuid.uuid4()}" - main(PROJECT, BUCKET, "europe-west1-b", instance_name, wait=False) - - out, _ = capsys.readouterr() - - assert "Instances in project" in out - assert "zone europe-west1-b" in out - assert instance_name in out - assert "Deleting instance" in out diff --git a/compute/api/requirements-test.txt b/compute/api/requirements-test.txt deleted file mode 100644 index 23df1e03c7e..00000000000 --- a/compute/api/requirements-test.txt +++ /dev/null @@ -1,2 +0,0 @@ -pytest==9.0.3; python_version >= "3.10" -flaky==3.8.1 diff --git a/compute/api/requirements.txt b/compute/api/requirements.txt deleted file mode 100644 index 7f4398de541..00000000000 --- a/compute/api/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -google-api-python-client==2.131.0 -google-auth==2.38.0 -google-auth-httplib2==0.2.0 diff --git a/compute/api/startup-script.sh b/compute/api/startup-script.sh deleted file mode 100644 index eb9897e39fc..00000000000 --- a/compute/api/startup-script.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -# Copyright 2015 Google Inc. -# -# 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. - -apt-get update -apt-get -y install imagemagick - -# Use the metadata server to get the configuration specified during -# instance creation. Read more about metadata here: -# https://cloud.google.com/compute/docs/metadata#querying -IMAGE_URL=$(curl http://metadata/computeMetadata/v1/instance/attributes/url -H "Metadata-Flavor: Google") -TEXT=$(curl http://metadata/computeMetadata/v1/instance/attributes/text -H "Metadata-Flavor: Google") -CS_BUCKET=$(curl http://metadata/computeMetadata/v1/instance/attributes/bucket -H "Metadata-Flavor: Google") - -mkdir image-output -cd image-output -wget $IMAGE_URL -convert * -pointsize 30 -fill white -stroke black -gravity center -annotate +10+40 "$TEXT" output.png - -# Create a Google Cloud Storage bucket. -gcloud storage buckets create gs://$CS_BUCKET - -# Store the image in the Google Cloud Storage bucket and allow all users -# to read it. -gcloud storage cp --predefined-acl=publicRead output.png gs://$CS_BUCKET/output.png