Я хочу создать шаблон экземпляра Google Cloud с последней версией ОС Ubuntu.
Я получаю следующую ошибку:< /p>
google.api_core.Exceptions.NotFound: 404 POST https://compute.googleapis.com/compute/ ... eTemplates: ресурс 'projects/debian-cloud/global/images/family/ubuntu-2204-lts' не найден
Что мне следует изменить в своем коде? использовать последнюю версию ОС Ubuntu? Я думаю, что-то с этим кортежем:
initialize_params.source_image = ("projects/debian-cloud/global/images/family/ubuntu-2204-lts")
create_template():
from __future__ import annotations
import sys
from typing import Any
import google
from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1
from src.routes.run_fw_engagements.c_instance_template.wait_for_extended_operation import wait_for_extended_operation
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/ubuntu-2204-lts"
)
initialize_params.disk_size_gb = 10
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-micro"
template.properties.network_interfaces = [network_interface]
template_client = compute_v1.InstanceTemplatesClient()
operation = template_client.insert(
project=project_id, instance_template_resource=template
)
wait_for_extended_operation(operation, "instance template creation")
response: google.cloud.compute_v1.types.compute.InstanceTemplate = template_client.get(project=project_id, instance_template=template_name)
# Map the response to variables
instance_template_id = response.id
instance_template_kind = response.kind
instance_template_name = response.name
creation_timestamp = response.creation_timestamp
description = response.description
self_link = response.self_link
machine_type = response.properties.machine_type
# Accessing network interfaces
network_interface = response.properties.network_interfaces[0]
network_name = network_interface.name
access_config = network_interface.access_configs[0]
access_config_name = access_config.name
access_config_type = access_config.type_
access_config_network_tier = access_config.network_tier
# Accessing disks
disk = response.properties.disks[0]
disk_device_name = disk.device_name
disk_boot = disk.boot
disk_type = disk.type_
disk_source_image = disk.initialize_params.source_image
disk_size_gb = disk.initialize_params.disk_size_gb
# Print the extracted variables
print("Instance Template ID:", instance_template_id)
print("Kind:", instance_template_kind)
print("Name:", instance_template_name)
print("Creation Timestamp:", creation_timestamp)
print("Description:", description)
print("Self Link:", self_link)
print("Machine Type:", machine_type)
print("Network Interface Name:", network_name)
print("Access Config Name:", access_config_name)
print("Access Config Type:", access_config_type)
print("Access Config Network Tier:", access_config_network_tier)
print("Disk Device Name:", disk_device_name)
print("Disk Boot:", disk_boot)
print("Disk Type:", disk_type)
print("Disk Source Image:", disk_source_image)
print("Disk Size (GB):", disk_size_gb)
# Return response
return response
if __name__ == '__main__':
create_template(project_id="xyz", template_name="template-test-6")
wait_for_extended_operation():
from __future__ import annotations
import sys
from typing import Any
from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1
def wait_for_extended_operation(
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
"""
Waits for the extended (long-running) operation to complete.
If the operation is successful, it will return its result.
If the operation ends with an error, an exception will be raised.
If there were any warnings during the execution of the operation
they will be printed to sys.stderr.
Args:
operation: a long-running operation you want to wait on.
verbose_name: (optional) a more verbose name of the operation,
used only during error and warning reporting.
timeout: how long (in seconds) to wait for operation to finish.
If None, wait indefinitely.
Returns:
Whatever the operation.result() returns.
Raises:
This method will raise the exception received from `operation.exception()`
or RuntimeError if there is no exception set, but there is an `error_code`
set for the `operation`.
In case of an operation taking longer than `timeout` seconds to complete,
a `concurrent.futures.TimeoutError` will be raised.
"""
result = operation.result(timeout=timeout)
if operation.error_code:
print(
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
file=sys.stderr,
flush=True,
)
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
raise operation.exception() or RuntimeError(operation.error_message)
if operation.warnings:
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
return result
Подробнее здесь: https://stackoverflow.com/questions/790 ... -ubuntu-os