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 list_instance_templates( project_id: str) -> Iterable[compute_v1.InstanceTemplate]: """ Get a list of InstanceTemplate objects available in a project. Args: project_id: project ID or project number of the Cloud project you use. Returns: Iterable list of InstanceTemplate objects. """ template_client = compute_v1.InstanceTemplatesClient() return template_client.list(project=project_id)
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 delete_instance_template(project_id: str, template_name: str): """ Delete an instance template. Args: project_id: project ID or project number of the Cloud project you use. template_name: name of the template to delete. """ template_client = compute_v1.InstanceTemplatesClient() operation_client = compute_v1.GlobalOperationsClient() op = template_client.delete_unary(project=project_id, instance_template=template_name) operation_client.wait(project=project_id, operation=op.name) return
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 get_instance_template(project_id: str, template_name: str) -> compute_v1.InstanceTemplate: """ Retrieve an instance template, which you can use to create virtual machine (VM) instances and managed instance groups (MIGs). Args: project_id: project ID or project number of the Cloud project you use. template_name: name of the template to retrieve. Returns: InstanceTemplate object that represents the retrieved template. """ template_client = compute_v1.InstanceTemplatesClient() return template_client.get(project=project_id, instance_template=template_name)
def create_template_from_instance( project_id: str, instance: str, template_name: str) -> compute_v1.InstanceTemplate: """ Create a new instance template based on an existing instance. This new template specifies a different boot disk. Args: project_id: project ID or project number of the Cloud project you use. instance: the instance to base the new template on. This value uses the following format: "projects/{project}/zones/{zone}/instances/{instance_name}" template_name: name of the new template to create. Returns: InstanceTemplate object that represents the new instance template. """ disk = compute_v1.DiskInstantiationConfig() # Device name must match the name of a disk attached to the instance you are # basing your template on. disk.device_name = "disk-1" # Replace the original boot disk image used in your instance with a Rocky Linux image. disk.instantiate_from = "CUSTOM_IMAGE" disk.custom_image = "projects/rocky-linux-cloud/global/images/family/rocky-linux-8" # Override the auto_delete setting. disk.auto_delete = True template = compute_v1.InstanceTemplate() template.name = template_name template.source_instance = instance template.source_instance_params = compute_v1.SourceInstanceParams() template.source_instance_params.disk_configs = [disk] 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_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)