def disk_from_image(disk_type: str, disk_size_gb: int, boot: bool, source_image: str) -> compute_v1.AttachedDisk: """ Create an AttachedDisk object to be used in VM instance creation. Uses an image as the source for the new disk. Args: disk_type: the type of disk you want to create. This value uses the following format: "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". For example: "zones/us-west3-b/diskTypes/pd-ssd" disk_size_gb: size of the new disk in gigabytes boot: boolean flag indicating whether this disk should be used as a boot disk of an instance source_image: source image to use when creating this disk. You must have read access to this disk. This can be one of the publicly available images or an image from one of your projects. This value uses the following format: "projects/{project_name}/global/images/{image_name}" Returns: AttachedDisk object configured to be created using the specified image. """ boot_disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = source_image initialize_params.disk_size_gb = disk_size_gb initialize_params.disk_type = disk_type boot_disk.initialize_params = initialize_params # Remember to set auto_delete to True if you want the disk to be deleted when you delete # your VM instance. boot_disk.auto_delete = True boot_disk.boot = boot return boot_disk
def instance_template(): disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.source_image = ( "projects/debian-cloud/global/images/family/debian-11") initialize_params.disk_size_gb = 25 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True network_interface = compute_v1.NetworkInterface() network_interface.name = "global/networks/default" template = compute_v1.InstanceTemplate() template.name = "test-template-" + uuid.uuid4().hex[:10] template.properties.disks = [disk] template.properties.machine_type = "e2-standard-4" template.properties.network_interfaces = [network_interface] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=PROJECT, instance_template_resource=template) operation_client.wait(project=PROJECT, operation=op.name) template = template_client.get(project=PROJECT, instance_template=template.name) yield template op = template_client.delete_unary(project=PROJECT, instance_template=template.name) operation_client.wait(project=PROJECT, operation=op.name)
def create_template(project_id: str, template_name: str) -> compute_v1.InstanceTemplate: """ Create a new instance template with the provided name and a specific instance configuration. Args: project_id: project ID or project number of the Cloud project you use. template_name: name of the new template to create. Returns: InstanceTemplate object that represents the new instance template. """ # The template describes 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-11") initialize_params.disk_size_gb = 250 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True # The template connects the instance to the `default` network, # without specifying a subnetwork. network_interface = compute_v1.NetworkInterface() network_interface.name = "global/networks/default" # The template lets the instance use an external IP address. access_config = compute_v1.AccessConfig() access_config.name = "External NAT" access_config.type_ = "ONE_TO_ONE_NAT" access_config.network_tier = "PREMIUM" network_interface.access_configs = [access_config] template = compute_v1.InstanceTemplate() template.name = template_name template.properties.disks = [disk] template.properties.machine_type = "e2-standard-4" template.properties.network_interfaces = [network_interface] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=project_id, instance_template_resource=template) operation_client.wait(project=project_id, operation=op.name) return template_client.get(project=project_id, instance_template=template_name)
def create_template_with_subnet( project_id: str, network: str, subnetwork: str, template_name: str) -> compute_v1.InstanceTemplate: """ Create an instance template that uses a provided subnet. Args: project_id: project ID or project number of the Cloud project you use. network: the network to be used in the new template. This value uses the following format: "projects/{project}/global/networks/{network}" subnetwork: the subnetwork to be used in the new template. This value uses the following format: "projects/{project}/regions/{region}/subnetworks/{subnetwork}" template_name: name of the new template to create. Returns: InstanceTemplate object that represents the new instance template. """ # The template describes the size and source image of the book 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-11") initialize_params.disk_size_gb = 250 disk.initialize_params = initialize_params disk.auto_delete = True disk.boot = True template = compute_v1.InstanceTemplate() template.name = template_name template.properties = compute_v1.InstanceProperties() template.properties.disks = [disk] template.properties.machine_type = "e2-standard-4" # The template connects the instance to the specified network and subnetwork. network_interface = compute_v1.NetworkInterface() network_interface.network = network network_interface.subnetwork = subnetwork template.properties.network_interfaces = [network_interface] template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.insert_unary(project=project_id, instance_template_resource=template) operation_client.wait(project=project_id, operation=op.name) return template_client.get(project=project_id, instance_template=template_name)
def _make_disk(raw_key: bytes = None): 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 if raw_key: disk.disk_encryption_key = compute_v1.CustomerEncryptionKey() disk.disk_encryption_key.raw_key = raw_key return disk
def empty_disk(disk_type: str, disk_size_gb: int) -> compute_v1.AttachedDisk(): """ Create an AttachedDisk object to be used in VM instance creation. The created disk contains no data and requires formatting before it can be used. Args: disk_type: the type of disk you want to create. This value uses the following format: "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". For example: "zones/us-west3-b/diskTypes/pd-ssd" disk_size_gb: size of the new disk in gigabytes Returns: AttachedDisk object configured to be created as an empty disk. """ disk = compute_v1.AttachedDisk() initialize_params = compute_v1.AttachedDiskInitializeParams() initialize_params.disk_type = disk_type initialize_params.disk_size_gb = disk_size_gb disk.initialize_params = initialize_params # Remember to set auto_delete to True if you want the disk to be deleted when you delete # your VM instance. disk.auto_delete = True disk.boot = False return disk
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: 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_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( 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)