Skip to content

Commit 9253c48

Browse files
m-strzelczykparthea
authored andcommitted
docs(samples): adding sample for local SSD disk (#294)
* docs(samples): adding sample for local SSD disk * Updating region tag * Regenerating * Updating snippets Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent fda9de8 commit 9253c48

File tree

5 files changed

+441
-0
lines changed

5 files changed

+441
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2022 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+
# http://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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT local_ssd_disk>
23+
def local_ssd_disk(zone: str) -> compute_v1.AttachedDisk():
24+
"""
25+
Create an AttachedDisk object to be used in VM instance creation. The created disk contains
26+
no data and requires formatting before it can be used.
27+
28+
Args:
29+
zone: The zone in which the local SSD drive will be attached.
30+
31+
Returns:
32+
AttachedDisk object configured as a local SSD disk.
33+
"""
34+
disk = compute_v1.AttachedDisk()
35+
disk.type_ = compute_v1.AttachedDisk.Type.SCRATCH.name
36+
initialize_params = compute_v1.AttachedDiskInitializeParams()
37+
initialize_params.disk_type = f"zones/{zone}/diskTypes/local-ssd"
38+
disk.initialize_params = initialize_params
39+
disk.auto_delete = True
40+
return disk
41+
# </INGREDIENT>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2022 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+
# http://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+
# <INGREDIENT create_with_ssd>
23+
def create_with_ssd(project_id: str, zone: str, instance_name: str) -> compute_v1.Instance:
24+
"""
25+
Create a new VM instance with Debian 10 operating system and SSD local disk.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
zone: name of the zone to create the instance in. For example: "us-west3-b"
30+
instance_name: name of the new virtual machine (VM) instance.
31+
32+
Returns:
33+
Instance object.
34+
"""
35+
newest_debian = get_image_from_family(
36+
project="debian-cloud", family="debian-10"
37+
)
38+
disk_type = f"zones/{zone}/diskTypes/pd-standard"
39+
disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link, True),
40+
local_ssd_disk(zone)]
41+
instance = create_instance(project_id, zone, instance_name, disks)
42+
return instance
43+
# </INGREDIENT>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2022 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+
# http://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+
# flake8: noqa
15+
16+
# <REGION compute_instances_create_with_local_ssd>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT get_image_from_family />
20+
21+
# <INGREDIENT disk_from_image />
22+
23+
# <INGREDIENT local_ssd_disk />
24+
25+
# <INGREDIENT wait_for_extended_operation />
26+
27+
28+
# <INGREDIENT create_instance />
29+
30+
31+
# <INGREDIENT create_with_ssd />
32+
# </REGION compute_instances_create_with_local_ssd>

0 commit comments

Comments
 (0)