def delete_instance(project_id: str, zone: str, machine_name: str) -> None: """ Send an instance deletion request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: “us-west3-b” machine_name: name of the machine you want to delete. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() print(f"Deleting {machine_name} from {zone}...") operation = instance_client.delete(project=project_id, zone=zone, instance=machine_name) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait(operation=operation.name, zone=zone, project=project_id) if operation.error: print("Error during deletion:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during deletion:", operation.warnings, file=sys.stderr) print(f"Instance {machine_name} deleted.") return
def delete_instance(project_id: str, zone: str, machine_name: str) -> None: """ Sends a delete request to GCP and waits for it to complete. Args: project_id: ID or number of the project you want to use. zone: Name of the zone you want to use, for example: us-west3-b machine_name: Name of the machine you want to delete. """ instance_client = compute_v1.InstancesClient() print(f"Deleting {machine_name} from {zone}...") operation = instance_client.delete(project=project_id, zone=zone, instance=machine_name) if operation.status == compute_v1.Operation.Status.RUNNING: operation_client = compute_v1.ZoneOperationsClient() operation = operation_client.wait(operation=operation.name, zone=zone, project=project_id) if operation.error: print("Error during deletion:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during deletion:", operation.warnings, file=sys.stderr) print(f"Instance {machine_name} deleted.") return
def wait_for_operation(operation: compute_v1.Operation, project_id: str) -> compute_v1.Operation: """ This method waits for an operation to be completed. Calling this function will block until the operation is finished. Args: operation: The Operation object representing the operation you want to wait on. project_id: ID or number of the project owning the operation. Returns: Finished Operation object. """ kwargs = {"project": project_id, "operation": operation.name} if operation.zone: client = compute_v1.ZoneOperationsClient() # Operation.zone is a full URL address of a zone, so we need to extract just the name kwargs["zone"] = operation.zone.rsplit("/", maxsplit=1)[1] elif operation.region: client = compute_v1.RegionOperationsClient() # Operation.region is a full URL address of a zone, so we need to extract just the name kwargs["region"] = operation.region.rsplit("/", maxsplit=1)[1] else: client = compute_v1.GlobalOperationsClient() return client.wait(**kwargs)
def create_instance_from_template( project_id: str, zone: str, instance_name: str, instance_template_url: str) -> compute_v1.Instance: """ Creates a Compute Engine VM instance from an instance template. Args: project_id: ID or number of the project you want to use. zone: Name of the zone you want to check, for example: us-west3-b instance_name: Name of the new instance. instance_template_url: URL of the instance template used for creating the new instance. It can be a full or partial URL. Examples: - https://www.googleapis.com/compute/v1/projects/project/global/instanceTemplates/example-instance-template - projects/project/global/instanceTemplates/example-instance-template - global/instanceTemplates/example-instance-template Returns: Instance object. """ operation_client = compute_v1.ZoneOperationsClient() instance_client = compute_v1.InstancesClient() instance_insert_request = compute_v1.InsertInstanceRequest() instance_insert_request.project = project_id instance_insert_request.zone = zone instance_insert_request.source_instance_template = instance_template_url instance_insert_request.instance_resource.name = instance_name op = instance_client.insert_unary(instance_insert_request) operation_client.wait(project=project_id, zone=zone, operation=op.name) return instance_client.get(project=project_id, zone=zone, instance=instance_name)
def add_extended_memory_to_instance(project_id: str, zone: str, instance_name: str, new_memory: int): """ Modify an existing VM to use extended memory. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. new_memory: the amount of memory for the VM instance, in megabytes. Returns: Instance object. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() instance = instance_client.get(project=project_id, zone=zone, instance=instance_name) # Make sure that the machine is turned off if instance.status not in ( instance.Status.TERMINATED.name, instance.Status.STOPPED.name, ): op = instance_client.stop_unary(project=project_id, zone=zone, instance=instance_name) operation_client.wait(project=project_id, zone=zone, operation=op.name) while instance.status not in ( instance.Status.TERMINATED.name, instance.Status.STOPPED.name, ): # Waiting for the instance to be turned off. instance = instance_client.get(project=project_id, zone=zone, instance=instance_name) time.sleep(2) # Modify the machine definition, remember that extended memory is available only for N1, N2 and N2D CPUs start, end = instance.machine_type.rsplit("-", maxsplit=1) instance.machine_type = start + f"-{new_memory}-ext" # Using CustomMachineType helper # cmt = CustomMachineType.from_str(instance.machine_type) # cmt.memory_mb = new_memory # cmt.extra_memory_used = True # instance.machine_type = str(cmt) op = instance_client.update_unary( project=project_id, zone=zone, instance=instance_name, instance_resource=instance, ) operation_client.wait(project=project_id, zone=zone, operation=op.name) return instance_client.get(project=project_id, zone=zone, instance=instance_name)
def _delete_instance(instance: compute_v1.Instance): instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() operation = instance_client.delete_unary( project=PROJECT, zone=INSTANCE_ZONE, instance=instance.name ) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait( operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT )
def _create_instance(request: compute_v1.InsertInstanceRequest): instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() operation = instance_client.insert_unary(request=request) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait( operation=operation.name, zone=INSTANCE_ZONE, project=PROJECT ) return instance_client.get( project=PROJECT, zone=INSTANCE_ZONE, instance=request.instance_resource.name )
def compute(_): op_client = compute_v1.ZoneOperationsClient() inst_client = compute_v1.InstancesClient() inst = inst_client.get(project=PROJ, zone=ZONE, instance=INST) if (inst.status == 'RUNNING'): return 'VM already running' opr = inst_client.start_unary(project=PROJ, zone=ZONE, instance=INST) start = time.time() while opr.status != compute_v1.Operation.Status.DONE: opr = op_client.wait(operation=opr.name, zone=ZONE, project=PROJ) if time.time() - start >= 300: # 5 minutes raise TimeoutError() return 'VM Started'
def __init__(self, infra_spec, options): super().__init__(infra_spec, options) self.project = 'couchbase-qe' self.credentials, _ = google.auth.default() self.storage_client = storage.Client(project=self.project, credentials=self.credentials) self.instance_client = compute.InstancesClient() self.network_client = compute.NetworksClient() self.subnet_client = compute.SubnetworksClient() self.firewall_client = compute.FirewallsClient() self.zone_ops_client = compute.ZoneOperationsClient() self.region_ops_client = compute.RegionOperationsClient() self.global_ops_client = compute.GlobalOperationsClient() with open(self.generated_cloud_config_path) as f: self.deployed_infra = json.load(f) self.zone = self.deployed_infra['zone'] self.region = self.zone.rsplit('-', 1)[0]
def stop_instance(project_id: str, zone: str, instance_name: str): """ Stops a stopped Google Compute Engine instance. Args: project_id: project ID or project number of the Cloud project your instance belongs to. zone: name of the zone your instance belongs to. instance_name: name of the instance your want to stop. """ instance_client = compute_v1.InstancesClient() op_client = compute_v1.ZoneOperationsClient() op = instance_client.stop(project=project_id, zone=zone, instance=instance_name) while op.status != compute_v1.Operation.Status.DONE: op = op_client.wait(operation=op.name, zone=zone, project=project_id) return
def start_instance_with_encryption_key(project_id: str, zone: str, instance_name: str, key: bytes): """ Starts a stopped Google Compute Engine instance (with encrypted disks). Args: project_id: project ID or project number of the Cloud project your instance belongs to. zone: name of the zone your instance belongs to. instance_name: name of the instance your want to start. key: bytes object representing a raw base64 encoded key to your machines boot disk. For more information about disk encryption see: https://cloud.google.com/compute/docs/disks/customer-supplied-encryption#specifications """ instance_client = compute_v1.InstancesClient() op_client = compute_v1.ZoneOperationsClient() instance_data = instance_client.get(project=project_id, zone=zone, instance=instance_name) # Prepare the information about disk encryption disk_data = compute_v1.CustomerEncryptionKeyProtectedDisk() disk_data.source = instance_data.disks[0].source disk_data.disk_encryption_key = compute_v1.CustomerEncryptionKey() # Use raw_key to send over the key to unlock the disk # To use a key stored in KMS, you need to provide `kms_key_name` and `kms_key_service_account` disk_data.disk_encryption_key.raw_key = key enc_data = compute_v1.InstancesStartWithEncryptionKeyRequest() enc_data.disks = [disk_data] op = instance_client.start_with_encryption_key_unary( project=project_id, zone=zone, instance=instance_name, instances_start_with_encryption_key_request_resource=enc_data, ) while op.status != compute_v1.Operation.Status.DONE: op = op_client.wait(operation=op.name, zone=zone, project=project_id) return
def list_zone_operations(project_id: str, zone: str, filter: str = "") -> pagers.ListPager: """ List all recent operations the happened in given zone in a project. Optionally filter those operations by providing a filter. More about using the filter can be found here: https://cloud.google.com/compute/docs/reference/rest/v1/zoneOperations/list Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: "us-west3-b" instance_name: name of the virtual machine to look for. Returns: List of preemption operations in given zone. """ operation_client = compute_v1.ZoneOperationsClient() request = compute_v1.ListZoneOperationsRequest() request.project = project_id request.zone = zone request.filter = filter return operation_client.list(request)
def set_delete_protection( project_id: str, zone: str, instance_name: str, delete_protection: bool ): """ Updates the delete protection setting of given instance. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: “us-west3-b” instance_name: name of the virtual machine to update. delete_protection: boolean value indicating if the virtual machine should be protected against deletion or not. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() request = compute_v1.SetDeletionProtectionInstanceRequest() request.project = project_id request.zone = zone request.resource = instance_name request.deletion_protection = delete_protection operation = instance_client.set_deletion_protection_unary(request) operation_client.wait(project=project_id, zone=zone, operation=operation.name)
def create_instance( project_id: str, zone: str, instance_name: str, machine_type: str = "n1-standard-1", source_image: str = "projects/debian-cloud/global/images/family/debian-10", network_name: str = "global/networks/default", ) -> compute_v1.Instance: """ Sends an instance creation request to GCP and waits for it to complete. Args: project_id: ID or number of the project you want to use. zone: Name of the zone you want to use, for example: us-west3-b instance_name: Name of the new machine. machine_type: Machine type you want to create in following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" You can find the list of available machine types using: https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list source_image: Path the the disk image you want to use for your boot disk. This can be one of the public images (e.g. "projects/debian-cloud/global/images/family/debian-10") or a private image you have access to. You can check the list of available public images using: $ gcloud compute images list network_name: Name of the network you want the new instance to use. For example: global/networks/default - if you want to use the default network. Returns: Instance object. """ instance_client = compute_v1.InstancesClient() # Every machine requires at least one persistent disk disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( source_image # "projects/debian-cloud/global/images/family/debian-10" ) initialize_params.disk_size_gb = "10" disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True disk.type_ = compute_v1.AttachedDisk.Type.PERSISTENT # Every machine needs to be connected to a VPC network. # The 'default' network is created automatically in every project. network_interface = compute_v1.NetworkInterface() network_interface.name = network_name # Collecting all the information into the Instance object instance = compute_v1.Instance() instance.name = instance_name instance.disks = [disk] full_machine_type_name = f"zones/{zone}/machineTypes/{machine_type}" instance.machine_type = full_machine_type_name instance.network_interfaces = [network_interface] # Preparing the InsertInstanceRequest request = compute_v1.InsertInstanceRequest() request.zone = zone request.project = project_id request.instance_resource = instance print(f"Creating the {instance_name} instance in {zone}...") operation = instance_client.insert(request=request) if operation.status == compute_v1.Operation.Status.RUNNING: operation_client = compute_v1.ZoneOperationsClient() operation = operation_client.wait(operation=operation.name, zone=zone, project=project_id) if operation.error: print("Error during creation:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during creation:", operation.warnings, file=sys.stderr) print(f"Instance {instance_name} created.") return instance
def create_instance( project_id: str, zone: str, instance_name: str, machine_type: Union[str, "CustomMachineType"], ): """ Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. machine_type: machine type of the VM being created. This value uses the following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" OR It can be a CustomMachineType object, describing a custom type you want to use. Return: Instance object. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() # Describe the size and source image of the boot disk to attach to the instance. disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( "projects/debian-cloud/global/images/family/debian-10") initialize_params.disk_size_gb = 10 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True disk.type_ = compute_v1.AttachedDisk.Type.PERSISTENT.name # Use the network interface provided in the network_name argument. network_interface = compute_v1.NetworkInterface() network_interface.name = "global/networks/default" # Collect information into the Instance object. instance = compute_v1.Instance() instance.name = instance_name instance.disks = [disk] instance.machine_type = str(machine_type) instance.network_interfaces = [network_interface] # Prepare the request to insert an instance. request = compute_v1.InsertInstanceRequest() request.zone = zone request.project = project_id request.instance_resource = instance # Wait for the create operation to complete. print( f"Creating the {instance_name} instance of type {instance.machine_type} in {zone}..." ) operation = instance_client.insert_unary(request=request) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait(operation=operation.name, zone=zone, project=project_id) if operation.error: print("Error during creation:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during creation:", operation.warnings, file=sys.stderr) print(f"Instance {instance_name} created.") return instance_client.get(project=project_id, zone=zone, instance=instance.name)
def create_instance( project_id: str, zone: str, instance_name: str, machine_type: str = "n1-standard-1", source_image: str = "projects/debian-cloud/global/images/family/debian-10", network_name: str = "global/networks/default", ) -> compute_v1.Instance: """ Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: “us-west3-b” instance_name: name of the new virtual machine. machine_type: machine type of the VM being created. This value uses the following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" source_image: path to the operating system image to mount on your boot disk. This can be one of the public images (like "projects/debian-cloud/global/images/family/debian-10") or a private image you have access to. network_name: name of the network you want the new instance to use. For example: "global/networks/default" represents the `default` network interface, which is created automatically for each project. Returns: Instance object. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() # Describe the size and source image of the boot disk to attach to the instance. disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( source_image # "projects/debian-cloud/global/images/family/debian-10" ) initialize_params.disk_size_gb = 10 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True disk.type_ = compute_v1.AttachedDisk.Type.PERSISTENT # Use the network interface provided in the network_name argument. network_interface = compute_v1.NetworkInterface() network_interface.name = network_name # Collect information into the Instance object. instance = compute_v1.Instance() instance.name = instance_name instance.disks = [disk] full_machine_type_name = f"zones/{zone}/machineTypes/{machine_type}" instance.machine_type = full_machine_type_name instance.network_interfaces = [network_interface] # Prepare the request to insert an instance. request = compute_v1.InsertInstanceRequest() request.zone = zone request.project = project_id request.instance_resource = instance # Wait for the create operation to complete. print(f"Creating the {instance_name} instance in {zone}...") operation = instance_client.insert(request=request) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait(operation=operation.name, zone=zone, project=project_id) if operation.error: print("Error during creation:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during creation:", operation.warnings, file=sys.stderr) print(f"Instance {instance_name} created.") return instance
def create_with_disks( project_id: str, zone: str, instance_name: str, disks: List[compute_v1.AttachedDisk], machine_type: str = "n1-standard-1", network_link: str = "global/networks/default", subnetwork_link: str = None, ) -> compute_v1.Instance: """ Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone to create the instance in. For example: "us-west3-b" instance_name: name of the new virtual machine (VM) instance. machine_type: machine type of the VM being created. This value uses the following format: "zones/{zone}/machineTypes/{type_name}". For example: "zones/europe-west3-c/machineTypes/f1-micro" disks: a list of compute_v1.AttachedDisk objects describing the disks you want to attach to your new instance. network_link: name of the network you want the new instance to use. For example: "global/networks/default" represents the network named "default", which is created automatically for each project. subnetwork_link: name of the subnetwork you want the new instance to use. This value uses the following format: "regions/{region}/subnetworks/{subnetwork_name}" Returns: Instance object. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() # Use the network interface provided in the network_link argument. network_interface = compute_v1.NetworkInterface() network_interface.name = network_link if subnetwork_link: network_interface.subnetwork = subnetwork_link # Collect information into the Instance object. instance = compute_v1.Instance() instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): instance.machine_type = machine_type else: instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}" instance.network_interfaces = [network_interface] # Shielded Instance settings # Values presented here are the defaults. # instance.shielded_instance_config = compute_v1.ShieldedInstanceConfig() # instance.shielded_instance_config.enable_secure_boot = False # instance.shielded_instance_config.enable_vtpm = True # instance.shielded_instance_config.enable_integrity_monitoring = True # Prepare the request to insert an instance. request = compute_v1.InsertInstanceRequest() request.zone = zone request.project = project_id request.instance_resource = instance # Wait for the create operation to complete. print(f"Creating the {instance_name} instance in {zone}...") operation = instance_client.insert_unary(request=request) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait(operation=operation.name, zone=zone, project=project_id) if operation.error: print("Error during creation:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during creation:", operation.warnings, file=sys.stderr) print(f"Instance {instance_name} created.") return instance
def create_instance( project_id: str, zone: str, instance_name: str, delete_protection: bool, ) -> compute_v1.Instance: """ Send an instance creation request to the Compute Engine API and wait for it to complete. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone you want to use. For example: “us-west3-b” instance_name: name of the new virtual machine. delete_protection: boolean value indicating if the new virtual machine should be protected against deletion or not. Returns: Instance object. """ instance_client = compute_v1.InstancesClient() operation_client = compute_v1.ZoneOperationsClient() # Describe the size and source image of the boot disk to attach to the instance. disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( "projects/debian-cloud/global/images/family/debian-10" ) initialize_params.disk_size_gb = 10 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True disk.type_ = "PERSISTENT" # Use the default VPC network. network_interface = compute_v1.NetworkInterface() network_interface.name = "default" # Collect information into the Instance object. instance = compute_v1.Instance() instance.name = instance_name instance.disks = [disk] instance.machine_type = f"zones/{zone}/machineTypes/e2-small" instance.network_interfaces = [network_interface] # Set the delete protection bit instance.deletion_protection = delete_protection # Prepare the request to insert an instance. request = compute_v1.InsertInstanceRequest() request.zone = zone request.project = project_id request.instance_resource = instance # Wait for the create operation to complete. print(f"Creating the {instance_name} instance in {zone}...") operation = instance_client.insert_unary(request=request) while operation.status != compute_v1.Operation.Status.DONE: operation = operation_client.wait( operation=operation.name, zone=zone, project=project_id ) if operation.error: print("Error during creation:", operation.error, file=sys.stderr) if operation.warnings: print("Warning during creation:", operation.warnings, file=sys.stderr) print(f"Instance {instance_name} created.") return instance
def create_instance_from_template_with_overrides( project_id: str, zone: str, instance_name: str, instance_template_name: str, machine_type: str, new_disk_source_image: str, ) -> compute_v1.Instance: """ Creates a Compute Engine VM instance from an instance template, changing the machine type and adding a new disk created from a source image. Args: project_id: ID or number of the project you want to use. zone: Name of the zone you want to check, for example: us-west3-b instance_name: Name of the new instance. instance_template_name: Name of the instance template used for creating the new instance. machine_type: Machine type you want to set in following format: "zones/{zone}/machineTypes/{type_name}". For example: - "zones/europe-west3-c/machineTypes/f1-micro" - You can find the list of available machine types using: https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list newDiskSourceImage: Path the the disk image you want to use for your new disk. This can be one of the public images (like "projects/debian-cloud/global/images/family/debian-10") or a private image you have access to. For a list of available public images, see the documentation: http://cloud.google.com/compute/docs/images Returns: Instance object. """ operation_client = compute_v1.ZoneOperationsClient() instance_client = compute_v1.InstancesClient() instance_template_client = compute_v1.InstanceTemplatesClient() # Retrieve an instance template by name. instance_template = instance_template_client.get( project=project_id, instance_template=instance_template_name) # Adjust diskType field of the instance template to use the URL formatting required by instances.insert.diskType # For instance template, there is only a name, not URL. for disk in instance_template.properties.disks: if disk.initialize_params.disk_type: disk.initialize_params.disk_type = ( f"zones/{zone}/diskTypes/{disk.initialize_params.disk_type}") instance = compute_v1.Instance() instance.name = instance_name instance.machine_type = machine_type instance.disks = instance_template.properties.disks new_disk = compute_v1.AttachedDisk() new_disk.initialize_params.disk_size_gb = 50 new_disk.initialize_params.source_image = new_disk_source_image new_disk.auto_delete = True new_disk.boot = False new_disk.type_ = "PERSISTENT" instance.disks.append(new_disk) instance_insert_request = compute_v1.InsertInstanceRequest() instance_insert_request.project = project_id instance_insert_request.zone = zone instance_insert_request.instance_resource = instance instance_insert_request.source_instance_template = instance_template.self_link op = instance_client.insert_unary(instance_insert_request) operation_client.wait(project=project_id, zone=zone, operation=op.name) return instance_client.get(project=project_id, zone=zone, instance=instance_name)
import random import discord from discord.ext import commands from google.oauth2 import service_account from google.cloud import compute_v1 # GCP Authentication Setup json_acct_info = json.loads(os.getenv('GCP_CREDS_JSON')) credentials = service_account.Credentials.from_service_account_info( json_acct_info) scoped_credentials = credentials.with_scopes( ['https://www.googleapis.com/auth/cloud-platform']) # Objects to interface with GCE instances_client = compute_v1.InstancesClient(credentials=scoped_credentials) operations_client = compute_v1.ZoneOperationsClient( credentials=scoped_credentials) intents = discord.Intents(guilds=True, guild_messages=True, members=True) bot = commands.Bot(command_prefix="!", intents=intents) MAX_LENGTH = 60 # with open("config.yaml") as f: # """ # config.yaml # # bot_token: <bot-token> # guild_id: <guild-id> # """ # config = yaml.load(f, Loader=yaml.FullLoader) # BOT_TOKEN = config["bot_token"] # GUILD_ID = config["guild_id"]