Skip to content

Commit 0cf659a

Browse files
feat: New Compute Sample - Consume single project reservation (#12750)
* Created "Specific Single Project Reservation" Sample. * Added 2 more Samples for Consume Reservation. * Deleted useless parameter in create_compute_reservation.py. * Updated tests
1 parent 9d13413 commit 0cf659a

12 files changed

+1013
-2
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT consume_any_matching_reservation>
24+
def consume_any_project_reservation(
25+
project_id: str,
26+
zone: str,
27+
reservation_name: str,
28+
instance_name: str,
29+
machine_type: str = "n1-standard-1",
30+
min_cpu_platform: str = "Intel Ivy Bridge",
31+
) -> compute_v1.Instance:
32+
"""
33+
Creates a specific reservation in a single project and launches a VM
34+
that consumes the newly created reservation.
35+
Args:
36+
project_id (str): The ID of the Google Cloud project.
37+
zone (str): The zone to create the reservation.
38+
reservation_name (str): The name of the reservation to create.
39+
instance_name (str): The name of the instance to create.
40+
machine_type (str): The machine type for the instance.
41+
min_cpu_platform (str): The minimum CPU platform for the instance.
42+
"""
43+
instance_properties = (
44+
compute_v1.AllocationSpecificSKUAllocationReservedInstanceProperties(
45+
machine_type=machine_type,
46+
min_cpu_platform=min_cpu_platform,
47+
)
48+
)
49+
50+
reservation = compute_v1.Reservation(
51+
name=reservation_name,
52+
specific_reservation=compute_v1.AllocationSpecificSKUReservation(
53+
count=3,
54+
instance_properties=instance_properties,
55+
),
56+
)
57+
58+
# Create a reservation client
59+
client = compute_v1.ReservationsClient()
60+
operation = client.insert(
61+
project=project_id,
62+
zone=zone,
63+
reservation_resource=reservation,
64+
)
65+
wait_for_extended_operation(operation, "Reservation creation")
66+
67+
instance = compute_v1.Instance()
68+
instance.name = instance_name
69+
instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}"
70+
instance.min_cpu_platform = min_cpu_platform
71+
instance.zone = zone
72+
73+
# Set the reservation affinity to target any matching reservation
74+
instance.reservation_affinity = compute_v1.ReservationAffinity(
75+
consume_reservation_type="ANY_RESERVATION", # Type of reservation to consume
76+
)
77+
# Define the disks for the instance
78+
instance.disks = [
79+
compute_v1.AttachedDisk(
80+
boot=True, # Indicates that this is a boot disk
81+
auto_delete=True, # The disk will be deleted when the instance is deleted
82+
initialize_params=compute_v1.AttachedDiskInitializeParams(
83+
source_image="projects/debian-cloud/global/images/family/debian-11",
84+
disk_size_gb=10,
85+
),
86+
)
87+
]
88+
instance.network_interfaces = [
89+
compute_v1.NetworkInterface(
90+
network="global/networks/default", # The network to use
91+
access_configs=[
92+
compute_v1.AccessConfig(
93+
name="External NAT", # Name of the access configuration
94+
type="ONE_TO_ONE_NAT", # Type of access configuration
95+
)
96+
],
97+
)
98+
]
99+
# Create a request to insert the instance
100+
request = compute_v1.InsertInstanceRequest()
101+
request.zone = zone
102+
request.project = project_id
103+
request.instance_resource = instance
104+
105+
vm_client = compute_v1.InstancesClient()
106+
operation = vm_client.insert(request)
107+
wait_for_extended_operation(operation, "instance creation")
108+
print(f"Instance {instance_name} that targets any open reservation created.")
109+
110+
return vm_client.get(project=project_id, zone=zone, instance=instance_name)
111+
112+
113+
# </INGREDIENT>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT consume_single_project_reservation>
24+
def consume_specific_single_project_reservation(
25+
project_id: str,
26+
zone: str,
27+
reservation_name: str,
28+
instance_name: str,
29+
machine_type: str = "n1-standard-1",
30+
min_cpu_platform: str = "Intel Ivy Bridge",
31+
) -> compute_v1.Instance:
32+
"""
33+
Creates a specific reservation in a single project and launches a VM
34+
that consumes the newly created reservation.
35+
Args:
36+
project_id (str): The ID of the Google Cloud project.
37+
zone (str): The zone to create the reservation.
38+
reservation_name (str): The name of the reservation to create.
39+
instance_name (str): The name of the instance to create.
40+
machine_type (str): The machine type for the instance.
41+
min_cpu_platform (str): The minimum CPU platform for the instance.
42+
"""
43+
instance_properties = (
44+
compute_v1.AllocationSpecificSKUAllocationReservedInstanceProperties(
45+
machine_type=machine_type,
46+
min_cpu_platform=min_cpu_platform,
47+
)
48+
)
49+
50+
reservation = compute_v1.Reservation(
51+
name=reservation_name,
52+
specific_reservation=compute_v1.AllocationSpecificSKUReservation(
53+
count=3,
54+
instance_properties=instance_properties,
55+
),
56+
# Only VMs that target the reservation by name can consume from this reservation
57+
specific_reservation_required=True,
58+
)
59+
60+
# Create a reservation client
61+
client = compute_v1.ReservationsClient()
62+
operation = client.insert(
63+
project=project_id,
64+
zone=zone,
65+
reservation_resource=reservation,
66+
)
67+
wait_for_extended_operation(operation, "Reservation creation")
68+
69+
instance = compute_v1.Instance()
70+
instance.name = instance_name
71+
instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}"
72+
instance.min_cpu_platform = min_cpu_platform
73+
instance.zone = zone
74+
75+
# Set the reservation affinity to target the specific reservation
76+
instance.reservation_affinity = compute_v1.ReservationAffinity(
77+
consume_reservation_type="SPECIFIC_RESERVATION", # Type of reservation to consume
78+
key="compute.googleapis.com/reservation-name", # Key for the reservation
79+
values=[reservation_name], # Reservation name to consume
80+
)
81+
# Define the disks for the instance
82+
instance.disks = [
83+
compute_v1.AttachedDisk(
84+
boot=True, # Indicates that this is a boot disk
85+
auto_delete=True, # The disk will be deleted when the instance is deleted
86+
initialize_params=compute_v1.AttachedDiskInitializeParams(
87+
source_image="projects/debian-cloud/global/images/family/debian-11",
88+
disk_size_gb=10,
89+
),
90+
)
91+
]
92+
instance.network_interfaces = [
93+
compute_v1.NetworkInterface(
94+
network="global/networks/default", # The network to use
95+
access_configs=[
96+
compute_v1.AccessConfig(
97+
name="External NAT", # Name of the access configuration
98+
type="ONE_TO_ONE_NAT", # Type of access configuration
99+
)
100+
],
101+
)
102+
]
103+
# Create a request to insert the instance
104+
request = compute_v1.InsertInstanceRequest()
105+
request.zone = zone
106+
request.project = project_id
107+
request.instance_resource = instance
108+
109+
vm_client = compute_v1.InstancesClient()
110+
operation = vm_client.insert(request)
111+
wait_for_extended_operation(operation, "instance creation")
112+
print(f"Instance {instance_name} with specific reservation created successfully.")
113+
114+
return vm_client.get(project=project_id, zone=zone, instance=instance_name)
115+
116+
117+
# </INGREDIENT>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT consume_shared_project_reservation>
24+
def consume_specific_shared_project_reservation(
25+
owner_project_id: str,
26+
shared_project_id: str,
27+
zone: str,
28+
reservation_name: str,
29+
instance_name: str,
30+
machine_type: str = "n1-standard-1",
31+
min_cpu_platform: str = "Intel Ivy Bridge",
32+
) -> compute_v1.Instance:
33+
"""
34+
Creates a specific reservation in a single project and launches a VM
35+
that consumes the newly created reservation.
36+
Args:
37+
owner_project_id (str): The ID of the Google Cloud project.
38+
shared_project_id: The ID of the owner project of the reservation in the same zone.
39+
zone (str): The zone to create the reservation.
40+
reservation_name (str): The name of the reservation to create.
41+
instance_name (str): The name of the instance to create.
42+
machine_type (str): The machine type for the instance.
43+
min_cpu_platform (str): The minimum CPU platform for the instance.
44+
"""
45+
instance_properties = (
46+
compute_v1.AllocationSpecificSKUAllocationReservedInstanceProperties(
47+
machine_type=machine_type,
48+
min_cpu_platform=min_cpu_platform,
49+
)
50+
)
51+
52+
reservation = compute_v1.Reservation(
53+
name=reservation_name,
54+
specific_reservation=compute_v1.AllocationSpecificSKUReservation(
55+
count=3,
56+
instance_properties=instance_properties,
57+
),
58+
# Only VMs that target the reservation by name can consume from this reservation
59+
specific_reservation_required=True,
60+
share_settings=compute_v1.ShareSettings(
61+
share_type="SPECIFIC_PROJECTS",
62+
project_map={
63+
shared_project_id: compute_v1.ShareSettingsProjectConfig(
64+
project_id=shared_project_id
65+
)
66+
},
67+
),
68+
)
69+
70+
# Create a reservation client
71+
client = compute_v1.ReservationsClient()
72+
operation = client.insert(
73+
project=owner_project_id,
74+
zone=zone,
75+
reservation_resource=reservation,
76+
)
77+
wait_for_extended_operation(operation, "Reservation creation")
78+
79+
instance = compute_v1.Instance()
80+
instance.name = instance_name
81+
instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}"
82+
instance.min_cpu_platform = min_cpu_platform
83+
instance.zone = zone
84+
85+
# Set the reservation affinity to target the specific reservation
86+
instance.reservation_affinity = compute_v1.ReservationAffinity(
87+
consume_reservation_type="SPECIFIC_RESERVATION", # Type of reservation to consume
88+
key="compute.googleapis.com/reservation-name",
89+
# To consume this reservation from any consumer projects, specify the owner project of the reservation
90+
values=[f"projects/{owner_project_id}/reservations/{reservation_name}"],
91+
)
92+
# Define the disks for the instance
93+
instance.disks = [
94+
compute_v1.AttachedDisk(
95+
boot=True, # Indicates that this is a boot disk
96+
auto_delete=True, # The disk will be deleted when the instance is deleted
97+
initialize_params=compute_v1.AttachedDiskInitializeParams(
98+
source_image="projects/debian-cloud/global/images/family/debian-11",
99+
disk_size_gb=10,
100+
),
101+
)
102+
]
103+
instance.network_interfaces = [
104+
compute_v1.NetworkInterface(
105+
network="global/networks/default", # The network to use
106+
access_configs=[
107+
compute_v1.AccessConfig(
108+
name="External NAT", # Name of the access configuration
109+
type="ONE_TO_ONE_NAT", # Type of access configuration
110+
)
111+
],
112+
)
113+
]
114+
# Create a request to insert the instance
115+
request = compute_v1.InsertInstanceRequest()
116+
request.zone = zone
117+
# The instance will be created in the shared project
118+
request.project = shared_project_id
119+
request.instance_resource = instance
120+
121+
vm_client = compute_v1.InstancesClient()
122+
operation = vm_client.insert(request)
123+
wait_for_extended_operation(operation, "instance creation")
124+
print(f"Instance {instance_name} from project {owner_project_id} created.")
125+
# The instance is created in the shared project, so we return it from there.
126+
return vm_client.get(project=shared_project_id, zone=zone, instance=instance_name)
127+
128+
129+
# </INGREDIENT>

compute/client_library/ingredients/compute_reservation/create_compute_reservation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def create_compute_reservation(
6565
# source_instance_template="projects/[PROJECT_ID]/global/instanceTemplates/my-instance-template",
6666
instance_properties=instance_properties,
6767
),
68-
specific_reservation_required=True,
6968
)
7069

7170
# Create a client
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# <REGION compute_consume_any_matching_reservation>
16+
# <IMPORTS/>
17+
18+
# <INGREDIENT wait_for_extended_operation />
19+
20+
# <INGREDIENT consume_any_matching_reservation />
21+
# </REGION compute_consume_any_matching_reservation>

0 commit comments

Comments
 (0)