Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit ece0cb5

Browse files
authored
chore(samples): Adding samples for custom hostname (#221)
* chore(samples): Adding samples for custom hostname * chore(samples): Changing zone for tests, as europe-central2-c seems to have capcity issues with e2 instances.
1 parent 17b95c3 commit ece0cb5

File tree

3 files changed

+171
-2
lines changed

3 files changed

+171
-2
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
# [START compute_instances_create_custom_hostname]
16+
import sys
17+
18+
19+
# [START compute_instances_get_hostname]
20+
from google.cloud import compute_v1
21+
22+
# [END compute_instances_get_hostname]
23+
# [END compute_instances_create_custom_hostname]
24+
25+
26+
# [START compute_instances_create_custom_hostname]
27+
def create_instance(
28+
project_id: str, zone: str, instance_name: str, hostname: str,
29+
) -> compute_v1.Instance:
30+
"""
31+
Send an instance creation request to the Compute Engine API and wait for it to complete.
32+
33+
Args:
34+
project_id: project ID or project number of the Cloud project you want to use.
35+
zone: name of the zone you want to use. For example: “us-west3-b”
36+
instance_name: name of the new virtual machine.
37+
hostname: Custom hostname of the new VM instance.
38+
Custom hostnames must conform to RFC 1035 requirements for valid hostnames.
39+
40+
Returns:
41+
Instance object.
42+
"""
43+
instance_client = compute_v1.InstancesClient()
44+
operation_client = compute_v1.ZoneOperationsClient()
45+
46+
# Describe the size and source image of the boot disk to attach to the instance.
47+
disk = compute_v1.AttachedDisk()
48+
initialize_params = compute_v1.AttachedDiskInitializeParams()
49+
initialize_params.source_image = (
50+
"projects/debian-cloud/global/images/family/debian-10"
51+
)
52+
initialize_params.disk_size_gb = 10
53+
disk.initialize_params = initialize_params
54+
disk.auto_delete = True
55+
disk.boot = True
56+
disk.type_ = "PERSISTENT"
57+
58+
# Use the default VPC network.
59+
network_interface = compute_v1.NetworkInterface()
60+
network_interface.name = "default"
61+
62+
# Collect information into the Instance object.
63+
instance = compute_v1.Instance()
64+
instance.name = instance_name
65+
instance.disks = [disk]
66+
instance.machine_type = f"zones/{zone}/machineTypes/e2-small"
67+
instance.network_interfaces = [network_interface]
68+
69+
# Custom hostnames are not resolved by the automatically created records
70+
# provided by Compute Engine internal DNS.
71+
# You must manually configure the DNS record for your custom hostname.
72+
instance.hostname = hostname
73+
74+
# Prepare the request to insert an instance.
75+
request = compute_v1.InsertInstanceRequest()
76+
request.zone = zone
77+
request.project = project_id
78+
request.instance_resource = instance
79+
80+
# Wait for the create operation to complete.
81+
print(f"Creating the {instance_name} instance in {zone}...")
82+
operation = instance_client.insert_unary(request=request)
83+
while operation.status != compute_v1.Operation.Status.DONE:
84+
operation = operation_client.wait(
85+
operation=operation.name, zone=zone, project=project_id
86+
)
87+
if operation.error:
88+
print("Error during creation:", operation.error, file=sys.stderr)
89+
if operation.warnings:
90+
print("Warning during creation:", operation.warnings, file=sys.stderr)
91+
print(f"Instance {instance_name} created.")
92+
return instance
93+
94+
95+
# [END compute_instances_create_custom_hostname]
96+
97+
98+
# [START compute_instances_get_hostname]
99+
def get_instance_hostname(project_id: str, zone: str, instance_name: str) -> str:
100+
"""
101+
Get the hostname set for given instance.
102+
103+
Args:
104+
project_id: project ID or project number of the Cloud project you want to use.
105+
zone: name of the zone you want to use. For example: “us-west3-b”
106+
instance_name: name of the virtual machine you want to check.
107+
108+
Returns:
109+
The hostname of given instance.
110+
"""
111+
instance_client = compute_v1.InstancesClient()
112+
instance = instance_client.get(
113+
project=project_id, zone=zone, instance=instance_name
114+
)
115+
return instance.hostname
116+
117+
118+
# [END compute_instances_get_hostname]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import random
15+
import uuid
16+
17+
import google.auth
18+
import pytest
19+
20+
from quickstart import delete_instance
21+
from sample_custom_hostname import create_instance
22+
from sample_custom_hostname import get_instance_hostname
23+
24+
PROJECT = google.auth.default()[1]
25+
INSTANCE_ZONE = "europe-north1-c"
26+
27+
28+
@pytest.fixture
29+
def autodelete_instance_name():
30+
instance_name = "test-host-instance-" + uuid.uuid4().hex[:10]
31+
32+
yield instance_name
33+
34+
delete_instance(PROJECT, INSTANCE_ZONE, instance_name)
35+
36+
37+
@pytest.fixture
38+
def random_hostname():
39+
yield "instance.{}.hostname".format(random.randint(0, 2 ** 10))
40+
41+
42+
def test_delete_protection(autodelete_instance_name, random_hostname):
43+
instance = create_instance(
44+
PROJECT, INSTANCE_ZONE, autodelete_instance_name, random_hostname
45+
)
46+
assert instance.name == autodelete_instance_name
47+
assert instance.hostname == random_hostname
48+
assert (
49+
get_instance_hostname(PROJECT, INSTANCE_ZONE, autodelete_instance_name)
50+
== random_hostname
51+
)

samples/snippets/test_sample_instance_from_template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
PROJECT = google.auth.default()[1]
28-
INSTANCE_ZONE = "us-central1-b"
28+
INSTANCE_ZONE = "europe-north1-c"
2929

3030

3131
@pytest.fixture
@@ -46,7 +46,7 @@ def instance_template():
4646
template = compute_v1.InstanceTemplate()
4747
template.name = "test-template-" + uuid.uuid4().hex[:10]
4848
template.properties.disks = [disk]
49-
template.properties.machine_type = "e2-standard-4"
49+
template.properties.machine_type = "n1-standard-4"
5050
template.properties.network_interfaces = [network_interface]
5151

5252
template_client = compute_v1.InstanceTemplatesClient()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy