Skip to content

Commit

Permalink
chore(samples): Adding samples for custom hostname (#221)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
m-strzelczyk authored Feb 23, 2022
1 parent 7446464 commit 3fa903e
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START compute_instances_create_custom_hostname]
import sys


# [START compute_instances_get_hostname]
from google.cloud import compute_v1

# [END compute_instances_get_hostname]
# [END compute_instances_create_custom_hostname]


# [START compute_instances_create_custom_hostname]
def create_instance(
project_id: str, zone: str, instance_name: str, hostname: str,
) -> compute_v1.Instance:
"""
Send an instance creation request to the Compute Engine API and wait for it to complete.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use. For example: “us-west3-b”
instance_name: name of the new virtual machine.
hostname: Custom hostname of the new VM instance.
Custom hostnames must conform to RFC 1035 requirements for valid hostnames.
Returns:
Instance object.
"""
instance_client = compute_v1.InstancesClient()
operation_client = compute_v1.ZoneOperationsClient()

# Describe 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-10"
)
initialize_params.disk_size_gb = 10
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
disk.type_ = "PERSISTENT"

# Use the default VPC network.
network_interface = compute_v1.NetworkInterface()
network_interface.name = "default"

# Collect information into the Instance object.
instance = compute_v1.Instance()
instance.name = instance_name
instance.disks = [disk]
instance.machine_type = f"zones/{zone}/machineTypes/e2-small"
instance.network_interfaces = [network_interface]

# Custom hostnames are not resolved by the automatically created records
# provided by Compute Engine internal DNS.
# You must manually configure the DNS record for your custom hostname.
instance.hostname = hostname

# Prepare the request to insert an instance.
request = compute_v1.InsertInstanceRequest()
request.zone = zone
request.project = project_id
request.instance_resource = instance

# Wait for the create operation to complete.
print(f"Creating the {instance_name} instance in {zone}...")
operation = instance_client.insert_unary(request=request)
while operation.status != compute_v1.Operation.Status.DONE:
operation = operation_client.wait(
operation=operation.name, zone=zone, project=project_id
)
if operation.error:
print("Error during creation:", operation.error, file=sys.stderr)
if operation.warnings:
print("Warning during creation:", operation.warnings, file=sys.stderr)
print(f"Instance {instance_name} created.")
return instance


# [END compute_instances_create_custom_hostname]


# [START compute_instances_get_hostname]
def get_instance_hostname(project_id: str, zone: str, instance_name: str) -> str:
"""
Get the hostname set for given instance.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use. For example: “us-west3-b”
instance_name: name of the virtual machine you want to check.
Returns:
The hostname of given instance.
"""
instance_client = compute_v1.InstancesClient()
instance = instance_client.get(
project=project_id, zone=zone, instance=instance_name
)
return instance.hostname


# [END compute_instances_get_hostname]
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import uuid

import google.auth
import pytest

from quickstart import delete_instance
from sample_custom_hostname import create_instance
from sample_custom_hostname import get_instance_hostname

PROJECT = google.auth.default()[1]
INSTANCE_ZONE = "europe-north1-c"


@pytest.fixture
def autodelete_instance_name():
instance_name = "test-host-instance-" + uuid.uuid4().hex[:10]

yield instance_name

delete_instance(PROJECT, INSTANCE_ZONE, instance_name)


@pytest.fixture
def random_hostname():
yield "instance.{}.hostname".format(random.randint(0, 2 ** 10))


def test_delete_protection(autodelete_instance_name, random_hostname):
instance = create_instance(
PROJECT, INSTANCE_ZONE, autodelete_instance_name, random_hostname
)
assert instance.name == autodelete_instance_name
assert instance.hostname == random_hostname
assert (
get_instance_hostname(PROJECT, INSTANCE_ZONE, autodelete_instance_name)
== random_hostname
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


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


@pytest.fixture
Expand All @@ -46,7 +46,7 @@ def instance_template():
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.machine_type = "n1-standard-4"
template.properties.network_interfaces = [network_interface]

template_client = compute_v1.InstanceTemplatesClient()
Expand Down

0 comments on commit 3fa903e

Please sign in to comment.